9 Commits

23 changed files with 1406 additions and 101 deletions

129
src/AbstractMap.java Normal file
View File

@@ -0,0 +1,129 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public abstract class AbstractMap {
private IDrawingObject _drawningObject = null;
protected int[][] _map = null;
protected int _width;
protected int _height;
protected float[] position;
protected float _size_x;
protected float _size_y;
protected Random _random = new Random();
protected int _freeRoad = 0;
protected int _barrier = 1;
public BufferedImage CreateMap(int width, int height, IDrawingObject drawningObject)
{
_width = width;
_height = height;
_drawningObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public BufferedImage MoveObject(Direction direction)
{
position = _drawningObject.GetCurrentPosition();
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[i].length; ++j)
{
if (_map[i][j] == _barrier)
{
switch (direction)
{
case Up:
if(_size_y * (j+1) >= position[0] - _drawningObject.Step() && _size_y * (j+1) < position[0] && _size_x * (i + 1) > position[3]
&& _size_x * (i + 1) <= position[1])
{
return DrawMapWithObject();
}
break;
case Down:
if (_size_y * j <= position[2] + _drawningObject.Step() && _size_y * j > position[2] && _size_x * (i+1) > position[3]
&& _size_x * (i+1) <= position[1])
{
return DrawMapWithObject();
}
break;
case Left:
if (_size_x * (i+1) >= position[3] - _drawningObject.Step() && _size_x * (i + 1) < position[3] && _size_y * (j + 1) < position[2]
&& _size_y * (j + 1) >= position[0])
{
return DrawMapWithObject();
}
break;
case Right:
if (_size_x * i <= position[1] + _drawningObject.Step() && _size_x * i > position[3] && _size_y * (j + 1) < position[2]
&& _size_y * (j + 1) >= position[0])
{
return DrawMapWithObject();
}
break;
}
}
}
}
_drawningObject.MoveObject(direction);
return DrawMapWithObject();
}
private boolean SetObjectOnMap()
{
if (_drawningObject == null || _map == null)
{
return false;
}
int x = _random.nextInt( 10);
int y = _random.nextInt( 10);
_drawningObject.SetObject(x, y, _width, _height);
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[0].length; ++j)
{
if (i * _size_x >= x && j * _size_y >= y &&
i * _size_x <= x + _drawningObject.GetCurrentPosition()[1] &&
j * _size_y <= j + _drawningObject.GetCurrentPosition()[2])
{
if (_map[i][j] == _barrier)
{
return false;
}
}
}
}
return true;
}
private BufferedImage DrawMapWithObject()
{
BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
if (_drawningObject == null || _map == null)
{
return bmp;
}
Graphics gr = bmp.getGraphics();
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[i].length; ++j)
{
if (_map[i][j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i][j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawningObject.DrawingObject(gr);
return bmp;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);
}

View File

@@ -1,40 +1,43 @@
import javax.swing.*;
import java.awt.*;
public class DrawingDeck extends JComponent {
public class DrawingDeck extends JComponent implements IAdditionalDrawingObject{
private Additional_Enum _decksEnum;
public void SetAddEnum(Additional_Enum addEnum) {
_decksEnum = addEnum;
@Override
public void SetAddEnum(int decksAmount) {
for(Additional_Enum item : _decksEnum.values())
{
if(item.GetAddEnum()==decksAmount)
{
_decksEnum=item;
return;
}
}
}
@Override
public void DrawDeck(Color colorDeck, Graphics g,float _startPosX,float _startPosY)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
switch (_decksEnum.GetAddEnum())
if(_decksEnum.GetAddEnum()>=1)
{
case 1:
g2d.setPaint(colorDeck);
g2d.fillRect((int)_startPosX+40, (int)_startPosY+15, 30*2, 5);
g2d.setPaint(Color.BLACK);
g2d.drawRect((int)_startPosX+40, (int)_startPosY+15, 30*2, 5);
break;
case 2:
g2d.setPaint(colorDeck);
g2d.fillRect((int)_startPosX+40, (int)_startPosY+15, 70, 5);
g2d.fillRect((int)_startPosX+50, (int)_startPosY+10, 60, 5);
g2d.setPaint(Color.BLACK);
g2d.drawRect((int)_startPosX+40, (int)_startPosY+15, 70, 5);
g2d.drawRect((int)_startPosX+50, (int)_startPosY+10, 60, 5);
break;
case 3:
g2d.setPaint(colorDeck);
g2d.fillRect((int)_startPosX+40, (int)_startPosY+15, 70, 5);
g2d.fillRect((int)_startPosX+50, (int)_startPosY+10, 60, 5);
g2d.fillRect((int)_startPosX+60, (int)_startPosY+5, 50, 5);
g2d.setPaint(Color.BLACK);
g2d.drawRect((int)_startPosX+40, (int)_startPosY+15, 70, 5);
g2d.drawRect((int)_startPosX+50, (int)_startPosY+10, 60, 5);
g2d.drawRect((int)_startPosX+60, (int)_startPosY+5, 50, 5);
break;
g2d.setPaint(colorDeck);
g2d.fillRect((int)_startPosX+40, (int)_startPosY+15, 70, 5);
g2d.setPaint(Color.BLACK);
g2d.drawRect((int)_startPosX+40, (int)_startPosY+15, 70, 5);
}
if(_decksEnum.GetAddEnum()>=2)
{
g2d.setPaint(colorDeck);
g2d.fillRect((int)_startPosX+50, (int)_startPosY+10, 60, 5);
g2d.setPaint(Color.BLACK);
g2d.drawRect((int)_startPosX+50, (int)_startPosY+10, 60, 5);
}
if(_decksEnum.GetAddEnum()>=3)
{
g2d.setPaint(colorDeck);
g2d.fillRect((int)_startPosX+60, (int)_startPosY+5, 50, 5);
g2d.setPaint(Color.BLACK);
g2d.drawRect((int)_startPosX+60, (int)_startPosY+5, 50, 5);
}
}
}

58
src/DrawingEntities.java Normal file
View File

@@ -0,0 +1,58 @@
import java.util.Random;
public class DrawingEntities <T extends EntityShip,U extends IAdditionalDrawingObject>{
public Object[] _entities;
public Object[] _decks;
int entitiesCount = 0;
int decksCount = 0;
String indx;
String indy;
public DrawingEntities(int countE,int countD)
{
_entities=new Object[countE];
_decks=new Object[countD];
}
public int Insert(T ship)
{
if(entitiesCount<_entities.length){
_entities[entitiesCount] = ship;
entitiesCount++;
return entitiesCount-1;
}
return -1;
}
public int Insert(U deck)
{
if(decksCount<_decks.length){
_decks[decksCount] = deck;
decksCount++;
return decksCount-1;
}
return -1;
}
public void SetIndexs(int ind1, int ind2)
{
indx=Integer.toString(ind1);
indy=Integer.toString(ind2);
}
public DrawingShip CreateShip()
{
Random rnd = new Random();
int indEnt=0;
int indDeck=0;
if(entitiesCount-1!=0 & decksCount-1!=0)
{
indDeck=rnd.nextInt(0,decksCount-1);
indEnt=rnd.nextInt(0,entitiesCount-1);
}
T ship=(T)_entities[indEnt];
U deck=(U)_decks[indDeck];
SetIndexs(indEnt,indDeck);
if(ship instanceof EntityLiner)
{
return new DrawingLiner(ship,deck);
}
return new DrawingShip(ship,deck);
}
}

47
src/DrawingLiner.java Normal file
View File

@@ -0,0 +1,47 @@
import java.awt.*;
public class DrawingLiner extends DrawingShip
{
public DrawingLiner(int speed, float weight, Color bodyColor,Color dopColor,boolean dopDeck,boolean pool)
{
super(speed,weight,bodyColor,130,45);
Ship = new EntityLiner(speed, weight, bodyColor, dopColor, dopDeck, pool);
}
protected DrawingLiner(EntityShip ship,IAdditionalDrawingObject deck)
{
super(ship,deck);
Ship=ship;
}
@Override
public void DrawTransport(Graphics gr) {
Graphics2D g2d = (Graphics2D) gr;
EntityLiner liner;
if (!(GetShip() instanceof EntityLiner))
{
return;
}
liner= (EntityLiner) Ship;
_startPosX+=10;
_startPosY+=5;
super.DrawTransport(gr);
_startPosY -= 5;
_startPosX -= 10;
if(liner.Pool)
{
g2d.setPaint(liner.DopColor);
g2d.fillRect((int)_startPosX + 15, (int)_startPosY + 25, 30, 3);
g2d.setPaint(Color.BLACK);
g2d.setPaint(Color.BLACK);
g2d.drawRect((int)_startPosX + 15, (int)_startPosY + 25, 30, 3);
gr.drawLine((int)_startPosX + 45, (int)_startPosY + 25, (int)_startPosX + 45, (int)_startPosY + 20);
gr.drawLine((int)_startPosX + 45, (int)_startPosY + 20, (int)_startPosX + 35, (int)_startPosY + 20);
}
if(liner.DopDeck)
{
g2d.setPaint(liner.DopColor);
g2d.fillRect((int)_startPosX + 50, (int)_startPosY + 15, 60, 5);
g2d.setPaint(Color.BLACK);
g2d.drawRect((int)_startPosX + 50, (int)_startPosY + 15, 60, 5);
}
}
}

View File

@@ -0,0 +1,35 @@
import java.awt.*;
public class DrawingObjectShip implements IDrawingObject {
private DrawingShip _ship = null;
public DrawingObjectShip(DrawingShip ship)
{
_ship=ship;
}
@Override
public float Step() {
if (_ship!=null && _ship.Ship != null) {
return _ship.Ship.Step();
}
return 0;
}
@Override
public void SetObject(int x, int y, int width, int height) {
_ship.SetPosition(x,y,width,height);
}
@Override
public void MoveObject(Direction direction) {
_ship.MoveTransport(direction);
}
@Override
public void DrawingObject(Graphics g) {
_ship.DrawTransport(g);
}
@Override
public float[] GetCurrentPosition() {
if(_ship!=null)
return _ship.GetCurrentPosition();
return null;
}
}

44
src/DrawingOvalDeck.java Normal file
View File

@@ -0,0 +1,44 @@
import javax.swing.*;
import java.awt.*;
public class DrawingOvalDeck extends JComponent implements IAdditionalDrawingObject{
private Additional_Enum _decksEnum;
@Override
public void SetAddEnum(int decksAmount) {
for(Additional_Enum item : _decksEnum.values())
{
if(item.GetAddEnum()==decksAmount)
{
_decksEnum=item;
return;
}
}
}
@Override
public void DrawDeck(Color colorDeck, Graphics g, float _startPosX, float _startPosY)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(_decksEnum.GetAddEnum()>=1)
{
g2d.setPaint(colorDeck);
g2d.fillOval((int)_startPosX+40, (int)_startPosY+15, 70, 5);
g2d.setPaint(Color.BLACK);
g2d.drawOval((int)_startPosX+40, (int)_startPosY+15, 70, 5);
}
if(_decksEnum.GetAddEnum()>=2)
{
g2d.setPaint(colorDeck);
g2d.fillOval((int)_startPosX+50, (int)_startPosY+10, 60, 5);
g2d.setPaint(Color.BLACK);
g2d.drawOval((int)_startPosX+50, (int)_startPosY+10, 60, 5);
}
if(_decksEnum.GetAddEnum()>=3)
{
g2d.setPaint(colorDeck);
g2d.fillOval((int)_startPosX+60, (int)_startPosY+5, 50, 5);
g2d.setPaint(Color.BLACK);
g2d.drawOval((int)_startPosX+60, (int)_startPosY+5, 50, 5);
}
}
}

View File

@@ -2,36 +2,52 @@ import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingShip extends JPanel {
private EntityShip Ship;
public DrawingDeck Deck;
protected EntityShip Ship;
public IAdditionalDrawingObject Deck;
public void SetEnum() {
Random r = new Random();
int numbEnum = r.nextInt(1, 4);
Deck.SetAddEnum(numbEnum);
}
public void SetFormEnum()
{
Random r = new Random();
int numbEnum = r.nextInt(1, 4);
if (numbEnum == 1) {
Deck.SetAddEnum(Additional_Enum.One);
Deck=new DrawingDeck();
}
if (numbEnum == 2) {
Deck.SetAddEnum(Additional_Enum.Two);
Deck = new DrawingOvalDeck();
}
if (numbEnum == 3) {
Deck.SetAddEnum(Additional_Enum.Three);
Deck=new DrawingTriangleDeck();
}
}
public EntityShip GetShip() {
return Ship;
}
private float _startPosX;
private float _startPosY;
protected float _startPosX;
protected float _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
protected final int _ShipWidth = 120;
protected final int _ShipHeight = 40;
public void Init(int speed, float weight, Color bodycolor) {
Ship = new EntityShip();
Ship.Init(speed, weight, bodycolor);
Deck = new DrawingDeck();
private int _ShipWidth = 120;
private int _ShipHeight = 40;
public DrawingShip(int speed, float weight, Color bodycolor) {
Ship = new EntityShip(speed, weight, bodycolor);
SetFormEnum();
SetEnum();
}
public DrawingShip(int speed,float weight,Color bodyColor,int shipWidth,int shipHeight)
{
this(speed,weight,bodyColor);
_ShipWidth = shipWidth;
_ShipHeight = shipHeight;
}
protected DrawingShip(EntityShip ship,IAdditionalDrawingObject deck)
{
Ship=ship;
Deck=deck;
}
public void SetPosition(int x, int y, int width, int height) {
if (width <= _ShipWidth + x || height <= _ShipHeight + y || x < 0 || y < 0) {
_pictureWidth = null;
@@ -70,29 +86,21 @@ public class DrawingShip extends JPanel {
break;
}
}
public void DrawTransport() {
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
repaint();
}
@Override
public void paintComponent(Graphics g) {
public void DrawTransport(Graphics gr) {
Graphics2D g2d = (Graphics2D) gr;
if (GetShip() == null) {
return;
}
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Ship.GetBodyColor());
int xValues[]={(int) _startPosX,(int) _startPosX + 60 * 2,(int) _startPosX + 100,(int) _startPosX + 20};
int yValues[]={(int) _startPosY + 20,(int) _startPosY + 20,(int) _startPosY + 20 * 2,(int) _startPosY + 40};
g2d.fillPolygon(xValues,yValues,4);
g2d.setPaint(Color.BLACK);
g2d.drawPolygon(xValues,yValues,4);
Deck.DrawDeck(Ship.GetBodyColor(), g, _startPosX, _startPosY);
Deck.DrawDeck(Ship.GetBodyColor(), gr, _startPosX, _startPosY);
}
public void ChangeBorders(int width, int height) {
_pictureWidth = width;
@@ -109,4 +117,8 @@ public class DrawingShip extends JPanel {
_startPosY = _pictureHeight - _ShipHeight;
}
}
public float[] GetCurrentPosition()
{
return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _ShipWidth, /*DOWN*/ _startPosY + _ShipHeight, /*LEFT*/ _startPosX};
}
}

View File

@@ -0,0 +1,49 @@
import javax.swing.*;
import java.awt.*;
public class DrawingTriangleDeck extends JComponent implements IAdditionalDrawingObject{
private Additional_Enum _decksEnum;
@Override
public void SetAddEnum(int decksAmount) {
for(Additional_Enum item : _decksEnum.values())
{
if(item.GetAddEnum()==decksAmount)
{
_decksEnum=item;
return;
}
}
}
@Override
public void DrawDeck(Color colorDeck, Graphics g,float _startPosX,float _startPosY)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(_decksEnum.GetAddEnum()>=1)
{
g2d.setPaint(colorDeck);
int[] xValues={(int)_startPosX+40,(int)_startPosX+40+15,(int)_startPosX+40+45,(int)_startPosX+40+60};
int[] yValues={(int)_startPosY+15,(int)_startPosY+20,(int)_startPosY+20,(int)_startPosY+15};
g2d.fillPolygon(xValues,yValues,4);
g2d.setPaint(Color.BLACK);
g2d.drawPolygon(xValues,yValues,4);
}
if(_decksEnum.GetAddEnum()>=2)
{
g2d.setPaint(colorDeck);
int[] xValues2={(int)_startPosX+50,(int)_startPosX+50+15,(int)_startPosX+50+45,(int)_startPosX+50+60};
int[] yValues2={(int)_startPosY+10,(int)_startPosY+15,(int)_startPosY+15,(int)_startPosY+10};
g2d.fillPolygon(xValues2,yValues2,4);
g2d.setPaint(Color.BLACK);
g2d.drawPolygon(xValues2,yValues2,4);
}
if(_decksEnum.GetAddEnum()>=3)
{
g2d.setPaint(colorDeck);
int[] xValues3_3={(int)_startPosX+50,(int)_startPosX+50+15,(int)_startPosX+50+45,(int)_startPosX+50+60};
int[] yValues3_3={(int)_startPosY+5,(int)_startPosY+10,(int)_startPosY+10,(int)_startPosY+5};
g2d.fillPolygon(xValues3_3,yValues3_3,4);
g2d.setPaint(Color.BLACK);
g2d.drawPolygon(xValues3_3,yValues3_3,4);
}
}
}

15
src/EntityLiner.java Normal file
View File

@@ -0,0 +1,15 @@
import java.awt.*;
public class EntityLiner extends EntityShip {
public Color DopColor;
public boolean DopDeck;
public boolean Pool;
public EntityLiner(int speed, float weight, Color bodyColor, Color dopColor, boolean dopDeck, boolean pool)
{
super(speed,weight,bodyColor);
DopColor = dopColor;
DopDeck = dopDeck;
Pool = pool;
}
}

View File

@@ -16,7 +16,7 @@ public class EntityShip {
{
return Speed*100/Weight;
}
public void Init(int speed,float weight, Color bodyColor)
public EntityShip(int speed,float weight, Color bodyColor)
{
Speed = speed;
Weight=weight;

View File

@@ -0,0 +1,135 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormMapWithSetShipsGeneric">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="649" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="9bb5a" binding="pictureBoxShip" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<hspacer id="b305e">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="4fa91" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="95ba3" class="javax.swing.JComboBox" binding="ComboBoxSelectorMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Карта море"/>
</model>
</properties>
</component>
<component id="cdb80" class="javax.swing.JButton" binding="ButtonAddShip">
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить корабль"/>
</properties>
</component>
<component id="e5494" class="javax.swing.JTextField" binding="maskedTextBoxPosition">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="8bb89" class="javax.swing.JButton" binding="ButtonDeleteShip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить корабль"/>
</properties>
</component>
<component id="aa482" class="javax.swing.JButton" binding="ButtonShowStorage">
<constraints>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Показать хранилище"/>
</properties>
</component>
<component id="9ebdc" class="javax.swing.JButton" binding="ButtonShowMap">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Показать карту"/>
</properties>
</component>
<component id="acbc" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="a584e" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="e9fe6" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="25a22" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="7" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@@ -0,0 +1,223 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
public class FormMapWithSetShipsGeneric extends JFrame{
public JPanel Mainpanel;
private JPanel pictureBoxShip;
private JPanel GroupBoxTools;
private JComboBox ComboBoxSelectorMap;
private JButton ButtonAddShip;
private JButton ButtonDeleteShip;
private JButton ButtonShowStorage;
private JButton ButtonShowMap;
private JButton ButtonLeft;
private JButton ButtonDown;
private JButton ButtonUp;
private JButton ButtonRight;
private JTextField maskedTextBoxPosition;
private MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap> _mapShipsCollectionGeneric;
public void UpdateWindow(BufferedImage bmp)
{
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(bmp));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
}
public FormMapWithSetShipsGeneric()
{
try {
Image img = ImageIO.read(FormShip.class.getResource("Images/4.png"));
ButtonUp.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/2.png"));
ButtonDown.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/1.png"));
ButtonLeft.setIcon(new ImageIcon(img));
img = ImageIO.read(FormShip.class.getResource("Images/3.png"));
ButtonRight.setIcon(new ImageIcon(img));
} catch (Exception ex) {
System.out.println(ex);
}
ComboBoxSelectorMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AbstractMap map = null;
ComboBoxSelectorMap = (JComboBox)e.getSource();
String item = (String)ComboBoxSelectorMap.getSelectedItem();
switch (item) {
case "Простая карта": {
map = new SimpleMap();
break;
}
case "Карта море": {
map = new SeaMap();
break;
}
}
if(map!=null)
{
_mapShipsCollectionGeneric = new MapWithSetShipsGeneric<DrawingObjectShip,AbstractMap>(pictureBoxShip.getWidth(),pictureBoxShip.getHeight(),map);
}
else
{
_mapShipsCollectionGeneric=null;
}
}
});
ButtonAddShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_mapShipsCollectionGeneric==null)
{
return;
}
FormShip form = new FormShip();
form.setSize(1000,700);
form.setVisible(true);
form.setModal(true);
DrawingObjectShip ship = new DrawingObjectShip(form.GetSelectedShip());
if(_mapShipsCollectionGeneric.Add(ship)!=-1)
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});
ButtonDeleteShip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (maskedTextBoxPosition.getText().isEmpty())
{
return;
}
int result = JOptionPane.showConfirmDialog(null,"Удалить объект?","Удаление",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
if(result==JOptionPane.NO_OPTION)
{
return;
}
int pos=Integer.parseInt(maskedTextBoxPosition.getText());
if(_mapShipsCollectionGeneric.Delete(pos)!=null)
{
JOptionPane.showMessageDialog(null, "Объект удален");
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
}
});
ButtonShowStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
UpdateWindow(_mapShipsCollectionGeneric.ShowSet());
}
});
ButtonShowMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
UpdateWindow(_mapShipsCollectionGeneric.ShowOnMap());
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Up)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Down)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Right)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapShipsCollectionGeneric == null)
{
return;
}
pictureBoxShip.removeAll();
JLabel imageWithMapAndObject = new JLabel();
imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize());
imageWithMapAndObject.setMinimumSize(new Dimension(1, 1));
imageWithMapAndObject.setIcon(new ImageIcon(_mapShipsCollectionGeneric.MoveObject(Direction.Left)));
pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER);
pictureBoxShip.revalidate();
pictureBoxShip.repaint();
}
});
maskedTextBoxPosition.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if ( ((c < '0') || (c > '9')) || maskedTextBoxPosition.getText().length() >= 2) {
e.consume();
}
}
});
}
}

62
src/FormParam.form Normal file
View File

@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormParam">
<grid id="27dc6" binding="MainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="7700e" binding="pictureBoxShip" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="b960b" class="javax.swing.JButton" binding="ButtonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Создать"/>
</properties>
</component>
<component id="a44b0" class="javax.swing.JButton" binding="ButtonCreateModif">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
<hspacer id="56304">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="31e01" class="javax.swing.JLabel" binding="LabelInfo">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<toolbar id="7a0ba" binding="StatusStrip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="20"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
<border type="none"/>
<children/>
</toolbar>
</children>
</grid>
</form>

113
src/FormParam.java Normal file
View File

@@ -0,0 +1,113 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Set;
public class FormParam extends JFrame{
public JPanel MainPanel;
private JPanel pictureBoxShip;
private JButton ButtonCreate;
private JButton ButtonCreateModif;
private JToolBar StatusStrip;
private JLabel LabelInfo;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
private DrawingEntities<EntityShip,IAdditionalDrawingObject> _drawingEntities;
private IAdditionalDrawingObject SetData()
{
Random random=new Random();
int r = random.nextInt(3);
if(r==0)
{
return new DrawingTriangleDeck();
}
if(r==1)
{
return new DrawingDeck();
}
else
{
return new DrawingOvalDeck();
}
}
private void Draw(DrawingShip _ship) {
pictureBoxShip.removeAll();
Random random = new Random();
BufferedImage bmp = new BufferedImage(pictureBoxShip.getWidth(), pictureBoxShip.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
if (_ship != null) {
_ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100),
pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
_ship.DrawTransport(gr);
JLabelSpeed.setText("орость: " + _ship.GetShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
JLabel imageOfLoco = new JLabel();
imageOfLoco.setPreferredSize(pictureBoxShip.getSize());
imageOfLoco.setMinimumSize(new Dimension(1, 1));
imageOfLoco.setIcon(new ImageIcon(bmp));
pictureBoxShip.add(imageOfLoco,BorderLayout.CENTER);
}
validate();
}
public FormParam()
{
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
LabelBox.add(JLabelWeight);
LabelBox.add(JLabelColor);
StatusStrip.add(LabelBox);
_drawingEntities=new DrawingEntities<>(10,10);
ButtonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
EntityShip ship=new EntityShip(random.nextInt(100,300), random.nextInt(1000,2000),colorFirst);
IAdditionalDrawingObject deck = SetData();
int DecksCount=random.nextInt(1,4);
deck.SetAddEnum(DecksCount);
if((_drawingEntities.Insert(ship)!=-1) & (_drawingEntities.Insert(deck)!=-1))
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
Draw(_drawingEntities.CreateShip());
LabelInfo.setText(_drawingEntities.indx+ " " + _drawingEntities.indy);
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});
ButtonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
Color colorSecond = JColorChooser.showDialog(null, "Цвет", null);
EntityLiner _ship=new EntityLiner(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst,colorSecond,random.nextBoolean(),random.nextBoolean());
IAdditionalDrawingObject deck = SetData();
int DecksCount=random.nextInt(1,4);
deck.SetAddEnum(DecksCount);
if((_drawingEntities.Insert(_ship)!=-1) & (_drawingEntities.Insert(deck)!=-1))
{
JOptionPane.showMessageDialog(null,"Объект добавлен");
Draw(_drawingEntities.CreateShip());
LabelInfo.setText(_drawingEntities.indx+ " " + _drawingEntities.indy);
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
});
}
}

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="FormShip">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="Mainpanel" layout-manager="GridLayoutManager" row-count="4" column-count="9" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
@@ -10,7 +10,7 @@
<children>
<component id="2ea16" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="2" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@@ -22,7 +22,7 @@
</component>
<component id="4f60a" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="2" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@@ -34,7 +34,7 @@
</component>
<component id="4eb88" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="2" column="8" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@@ -45,15 +45,9 @@
<text value=""/>
</properties>
</component>
<component id="9f55" class="DrawingShip" binding="PictureBoxShip">
<constraints>
<grid row="0" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="fb937" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="1" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@@ -65,7 +59,7 @@
</component>
<toolbar id="e747d" binding="StatusStrip">
<constraints>
<grid row="3" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="3" column="0" row-span="1" col-span="9" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="-1" height="20"/>
<preferred-size width="-1" height="20"/>
<maximum-size width="-1" height="20"/>
@@ -77,11 +71,19 @@
<border type="none"/>
<children/>
</toolbar>
<hspacer id="d6bea">
<hspacer id="d86ff">
<constraints>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="5" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="6b0b8" binding="pictureBoxShip" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
<constraints>
<grid row="0" column="0" row-span="1" col-span="9" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="9e2ce" class="javax.swing.JButton" binding="ButtonCreate">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
@@ -90,11 +92,27 @@
<text value="Создать"/>
</properties>
</component>
<hspacer id="d86ff">
<component id="86a62" class="javax.swing.JButton" binding="ButtonCreateModif">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Модификация"/>
</properties>
</component>
<hspacer id="9abb1">
<constraints>
<grid row="1" column="1" row-span="1" col-span="4" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="8005c" class="javax.swing.JButton" binding="ButtonSelect">
<constraints>
<grid row="1" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Выбрать"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@@ -1,30 +1,55 @@
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class FormShip {
public class FormShip extends JDialog{
public JPanel Mainpanel;
private Random random = new Random();
private JButton ButtonCreate;
private JButton ButtonLeft;
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonRight;
protected DrawingShip PictureBoxShip;
private JButton ButtonCreateModif;
protected DrawingShip _ship;
private JPanel pictureBoxShip;
private JToolBar StatusStrip;
private JButton ButtonSelect;
private JLabel JLabelSpeed = new JLabel();
private JLabel JLabelWeight = new JLabel();
private JLabel JLabelColor = new JLabel();
public void Draw() {
if (PictureBoxShip.GetShip() == null) {
return;
}
PictureBoxShip.DrawTransport();
protected DrawingShip SelectedShip;
public DrawingShip GetSelectedShip()
{
return SelectedShip;
}
public FormShip() {
public void Draw(DrawingShip _ship) {
pictureBoxShip.removeAll();
BufferedImage bmp = new BufferedImage(pictureBoxShip.getWidth(), pictureBoxShip.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
gr.setColor(new Color(238, 238, 238));
gr.fillRect(0, 0, pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
if (_ship != null) {
_ship.DrawTransport(gr);
JLabel imageOfLoco = new JLabel();
imageOfLoco.setPreferredSize(pictureBoxShip.getSize());
imageOfLoco.setMinimumSize(new Dimension(1, 1));
imageOfLoco.setIcon(new ImageIcon(bmp));
pictureBoxShip.add(imageOfLoco,BorderLayout.CENTER);
}
validate();
}
public FormShip()
{
super(new Frame("Лайнер"));
CreateWindow();
setModal(true);
getContentPane().add(Mainpanel);
}
public void CreateWindow() {
setModal(true);
Box LabelBox = Box.createHorizontalBox();
LabelBox.setMinimumSize(new Dimension(1, 20));
LabelBox.add(JLabelSpeed);
@@ -43,52 +68,85 @@ public class FormShip {
} catch (Exception ex) {
System.out.println(ex);
}
SelectedShip=_ship;
ButtonCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e){
Random random = new Random();
PictureBoxShip.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
PictureBoxShip.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), PictureBoxShip.getWidth(), PictureBoxShip.getHeight());
JLabelSpeed.setText("орость: " + PictureBoxShip.GetShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + PictureBoxShip.GetShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + PictureBoxShip.GetShip().GetBodyColor() + " "));
Draw();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
_ship=new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst);
_ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
JLabelSpeed.setText("орость: " + _ship.GetShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
Draw(_ship);
}
});
PictureBoxShip.addComponentListener(new ComponentAdapter() {
pictureBoxShip.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
PictureBoxShip.ChangeBorders(PictureBoxShip.getWidth(), PictureBoxShip.getHeight());
Draw();
if(_ship!=null)
{
_ship.ChangeBorders(pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
pictureBoxShip.revalidate();
Draw(_ship);
}
}
});
ButtonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxShip.MoveTransport(Direction.Up);
Draw();
_ship.MoveTransport(Direction.Up);
pictureBoxShip.revalidate();
Draw(_ship);
}
});
ButtonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxShip.MoveTransport(Direction.Down);
Draw();
_ship.MoveTransport(Direction.Down);
pictureBoxShip.revalidate();
Draw(_ship);
}
});
ButtonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxShip.MoveTransport(Direction.Right);
Draw();
_ship.MoveTransport(Direction.Right);
pictureBoxShip.revalidate();
Draw(_ship);
}
});
ButtonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PictureBoxShip.MoveTransport(Direction.Left);
Draw();
_ship.MoveTransport(Direction.Left);
pictureBoxShip.revalidate();
Draw(_ship);
}
});
ButtonCreateModif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", null);
Color colorSecond = JColorChooser.showDialog(null, "Цвет", null);
_ship=new DrawingLiner(random.nextInt(100, 300), random.nextInt(1000, 2000), colorFirst,colorSecond,random.nextBoolean(),random.nextBoolean());
_ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight());
JLabelSpeed.setText("орость: " + _ship.GetShip().GetSpeed() + " ");
JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " ");
JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " "));
Draw(_ship);
}
});
ButtonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SelectedShip=_ship;
dispose();
}
});
}

View File

@@ -0,0 +1,6 @@
import java.awt.*;
public interface IAdditionalDrawingObject {
void SetAddEnum(int decksAmount);
void DrawDeck(Color colorDeck, Graphics g, float _startPosX, float _startPosY);
}

9
src/IDrawingObject.java Normal file
View File

@@ -0,0 +1,9 @@
import java.awt.*;
public interface IDrawingObject {
public float Step();
void SetObject(int x, int y,int width,int height);
void MoveObject(Direction direction);
void DrawingObject(Graphics g);
float[] GetCurrentPosition();
}

View File

@@ -3,11 +3,11 @@ import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Лайнер");
frame.setContentPane(new FormShip().Mainpanel);
frame.setContentPane(new FormParam().MainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(500, 200);
frame.pack();
frame.setSize(500, 500);
frame.setSize(800, 500);
frame.setVisible(true);
}
}

View File

@@ -0,0 +1,130 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends AbstractMap> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 170;
private final int _placeSizeHeight = 90;
private final SetShipsGeneric<T> _setShips;
private final U _map;
public MapWithSetShipsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setShips = new SetShipsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int Add(T ship)
{
return _setShips.Insert(ship);
}
public T Delete(int position)
{
return _setShips.Remove(position);
}
public BufferedImage ShowSet()
{
BufferedImage bmp = new BufferedImage(_pictureWidth,_pictureHeight,BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
DrawBackground(gr);
DrawShips(gr);
return bmp;
}
public BufferedImage ShowOnMap()
{
Shaking();
for (int i = 0; i < _setShips.Count(); i++)
{
var ship = _setShips.Get(i);
if (ship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
}
public BufferedImage MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
}
private void Shaking()
{
int j = _setShips.Count() - 1;
for (int i = 0; i < _setShips.Count(); i++)
{
if (_setShips.Get(i) == null)
{
for (; j > i; j--)
{
var ship = _setShips.Get(j);
if (ship != null)
{
_setShips.Insert(ship, i);
_setShips.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics gr)
{
Graphics2D g = (Graphics2D) gr;
g.setColor(Color.BLUE);
g.fillRect( 0, 0, _pictureWidth, _pictureHeight);
Color c = new Color(89,51,5);
g.setStroke(new BasicStroke(2));
g.setColor(c);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 40, j * _placeSizeHeight);
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
g.drawLine(i * _placeSizeWidth - 4, 0, i * _placeSizeWidth - 4, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawShips(Graphics g)
{
int widthEl = _pictureWidth / _placeSizeWidth;
int heightEl = _pictureHeight / _placeSizeHeight;
int curWidth = 1;
int curHeight = 0;
for (int i = 0; i < _setShips.Count(); i++)
{
if(_setShips.Get(i)==null)
{
continue;
}
_setShips.Get(i).SetObject(_pictureWidth - _placeSizeWidth * curWidth - (_pictureWidth / 7),
curHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
_setShips.Get(i).DrawingObject(g);
if (curWidth < widthEl)
curWidth++;
else
{
curWidth = 1;
curHeight++;
}
if (curHeight > heightEl)
{
return;
}
}
}
}

49
src/SeaMap.java Normal file
View File

@@ -0,0 +1,49 @@
import java.awt.*;
public class SeaMap extends AbstractMap{
private final Color barrierColor = Color.WHITE;
private final Color roadColor = Color.CYAN;
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(barrierColor);
g2d.fillRect((int)(i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(roadColor);
g2d.fillRect((int)(i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
}
@Override
protected void GenerateMap()
{
_map = new int[100][100];
_size_x = (float)_width / _map.length;
_size_y = (float)_height / _map[0].length;
int counter = 0;
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[0].length; ++j)
{
_map[i][j] = _freeRoad;
}
}
while (counter < 25)
{
int x = _random.nextInt(0, 97);
int y = _random.nextInt(0, 97);
if (_map[x][y] == _freeRoad)
{
_map[x][y + 1] = _barrier;
_map[x + 1][y + 1] = _barrier;
_map[x + 2][y + 1] = _barrier;
_map[x + 1][y] = _barrier;
counter++;
}
}
}
}

65
src/SetShipsGeneric.java Normal file
View File

@@ -0,0 +1,65 @@
public class SetShipsGeneric<T extends Object> {
private final Object[] _places;
public int Count()
{
return _places.length;
}
public SetShipsGeneric(int count)
{
_places = new Object[count];
}
public int Insert(T ship)
{
return Insert(ship, 0);
}
public int Insert(T ship, int position)
{
if (position >= _places.length || position < 0)
{
return -1;
}
if (_places[position] == null)
{
_places[position] = ship;
return position;
}
int emptyIndex = -1;
for (int i = position + 1; i < Count(); i++)
{
if (_places[i] == null)
{
emptyIndex = i;
break;
}
}
if (emptyIndex < 0)
{
return -1;
}
for (int i = emptyIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = ship;
return position;
}
public T Remove(int position)
{
if (position >= _places.length || position < 0)
{
return null;
}
T ship = (T) _places[position];
_places[position] = null;
return ship;
}
public T Get(int position)
{
if (position >= _places.length || position < 0)
{
return null;
}
return (T)_places[position];
}
}

45
src/SimpleMap.java Normal file
View File

@@ -0,0 +1,45 @@
import java.awt.*;
public class SimpleMap extends AbstractMap{
private final Color barrierColor = Color.black;
private final Color roadColor = Color.gray;
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(barrierColor);
g2d.fillRect((int)(i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(roadColor);
g2d.fillRect((int)(i * _size_x), (int) (j * _size_y), (int)(i * (_size_x + 1)), (int)(j * (_size_y + 1)));
}
@Override
protected void GenerateMap()
{
_map = new int[100][100];
_size_x = (float)_width / _map.length;
_size_y = (float)_height / _map[0].length;
int counter = 0;
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[0].length; ++j)
{
_map[i][j] = _freeRoad;
}
}
while (counter < 50)
{
int x = _random.nextInt(0, 100);
int y = _random.nextInt(0, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
}
}
}