diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..3e4f10e --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,214 @@ +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 _water = 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) + { + boolean moveAccept = true; + + float[] position = _drawningObject.GetCurrentPosition(); + int xObjWidth = (int)Math.ceil((position[2] - position[0]) / _size_x); + int yObjHeight = (int)Math.ceil((position[3] - position[1]) / _size_y); + int vertStep = (int)Math.ceil(_drawningObject.Step() / _size_y); + int horizStep = (int)Math.ceil(_drawningObject.Step() / _size_x); + int xObjLeftBorder = (int)Math.floor(position[0] / _size_x); + int xObjRightBorder = (int)Math.ceil(position[2] / _size_x); + int yObjTopBorder = (int)Math.floor(position[1] / _size_y); + int yObjBottomBorder = (int)Math.ceil(position[3] / _size_y); + + switch (direction) + { + case Up: + for (int i = 0; i < vertStep; i++) + { + if (!moveAccept) + { + break; + } + for (int j = 0; j < xObjWidth; j++) + { + if (yObjTopBorder - i < 0 || xObjLeftBorder + j >= _map[0].length) + { + break; + } + if (_map[xObjLeftBorder + j][yObjTopBorder - i] == _barrier) + { + moveAccept = false; + break; + } + } + } + break; + + case Down: + for (int i = 0; i < vertStep; i++) + { + if (!moveAccept) + { + break; + } + for (int j = 0; j < xObjWidth; j++) + { + if (yObjBottomBorder + i >= _map.length || xObjLeftBorder + j >= _map[0].length) + { + break; + } + if (_map[xObjLeftBorder + j][yObjBottomBorder + i] == _barrier) + { + moveAccept = false; + break; + } + } + } + break; + + case Left: + for (int i = 0; i < yObjHeight; i++) + { + if (!moveAccept) + { + break; + } + for (int j = 0; j < horizStep; j++) + { + if (yObjTopBorder + i >= _map.length || xObjLeftBorder - j < 0) + { + break; + } + if (_map[xObjLeftBorder - j][yObjTopBorder + i] == _barrier) + { + moveAccept = false; + break; + } + } + } + break; + + case Right: + for (int i = 0; i < yObjHeight; i++) + { + if (!moveAccept) + { + break; + } + for (int j = 0; j < horizStep; j++) + { + if (yObjTopBorder + i >= _map.length || xObjRightBorder + j >= _map[0].length) + { + break; + } + if (_map[xObjRightBorder + j][yObjTopBorder + i] == _barrier) + { + moveAccept = false; + break; + } + } + } + break; + } + if (moveAccept) + { + _drawningObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + private boolean SetObjectOnMap() + { + if (_drawningObject == null || _map == null) + { + return false; + } + int x = _random.nextInt(0, 10); + int y = _random.nextInt(0, 10); + + float[] position = _drawningObject.GetCurrentPosition(); + int xObjWidth = (int)Math.ceil((position[2] - position[0]) / _size_x); + int yObjHeight = (int)Math.ceil((position[3] - position[1]) / _size_y); + int xObjLeftBorder = (int)Math.floor(position[0] / _size_x); + int yObjTopBorder = (int)Math.floor(position[1] / _size_y); + + while (y < _height - (position[3] - position[1])) + { + while (x < _width - (position[2] - position[0])) + { + if (CheckSpawnArea(xObjWidth, yObjHeight, xObjLeftBorder, yObjTopBorder)) + { + _drawningObject.SetObject(x, y, _width, _height); + return true; + } + x += (int)_size_x; + xObjLeftBorder = (int)(x / _size_x); + } + x = 0; + y += (int)_size_y; + yObjTopBorder = (int)(y / _size_y); + } + + return false; + } + private boolean CheckSpawnArea(int xObjWidth, int yObjHeight, int xObjLeftBorder, int yObjTopBorder) + { + for (int i = 0; i <= yObjHeight; i++) + { + for (int j = 0; j <= xObjWidth; j++) + { + if (yObjTopBorder + i >= _map.length || xObjLeftBorder + j >= _map[0].length || _map[xObjLeftBorder + j][yObjTopBorder + i] == _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] == _water) + { + 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); +} \ No newline at end of file diff --git a/DrawingContainerShip.java b/DrawingContainerShip.java new file mode 100644 index 0000000..f15db2f --- /dev/null +++ b/DrawingContainerShip.java @@ -0,0 +1,71 @@ +import java.awt.*; + +public class DrawingContainerShip extends DrawingShip +{ + public DrawingContainerShip(int speed, float weight, Color bodyColor, Color dopColor, boolean dopDeck, boolean pool) + { + super(speed,weight,bodyColor,130,45); + Ship = new EntityContainerShip(speed, weight, bodyColor, dopColor, dopDeck, pool); + } + @Override + public void DrawTransport(Graphics g) { + if (!(GetShip() instanceof EntityContainerShip containerShip)) + { + return; + } + super.DrawTransport(g); + Graphics2D g2d = (Graphics2D) g; + Deck.DrawDeck(Ship.GetBodyColor(), g, _startPosX, _startPosY); + if (containerShip.GetContainers()) + { + g2d.setPaint(containerShip.GetDopColor()); + //Заливка контейнеров + int[] xValuesFirstContainer = {(int) _startPosX + 16, (int) _startPosX + 16 + 29, (int) _startPosX + 16 + 29, (int) _startPosX + 16}; + int[] yValuesFirstContainer = {(int) _startPosY + 16, (int) _startPosY + 16, (int) _startPosY + 16 + 14, (int) _startPosY + 16 + 14}; + g2d.fillPolygon(xValuesFirstContainer, yValuesFirstContainer, 4); + int[] xValuesSecondContainer = {(int) _startPosX + 56, (int) _startPosX + 56 + 29, (int) _startPosX + 56 + 29, (int) _startPosX + 56}; + int[] yValuesSecondContainer = {(int) _startPosY + 16, (int) _startPosY + 16, (int) _startPosY + 16 + 14, (int) _startPosY + 16 + 14}; + g2d.fillPolygon(xValuesSecondContainer, yValuesSecondContainer, 4); + //Заливка центральных полос на контейнерах + g2d.setPaint(Color.ORANGE); + int[] xValuesFirstContainerLine = {(int) _startPosX + 16, (int) _startPosX + 16 + 29, (int) _startPosX + 16 + 29, (int) _startPosX + 16}; + int[] yValuesFirstContainerLine = {(int) _startPosY + 20, (int) _startPosY + 20, (int) _startPosY + 25, (int) _startPosY + 25}; + g2d.fillPolygon(xValuesFirstContainerLine, yValuesFirstContainerLine, 4); + + int[] xValuesSecondContainerLine = {(int) _startPosX + 56, (int) _startPosX + 56 + 29, (int) _startPosX + 56 + 29, (int) _startPosX + 56}; + int[] yValuesSecondContainerLine = {(int) _startPosY + 20, (int) _startPosY + 20, (int) _startPosY + 25, (int) _startPosY + 25}; + g2d.fillPolygon(xValuesSecondContainerLine, yValuesSecondContainerLine, 4); + //Границы контейнеров + g2d.setPaint(Color.BLACK); + g2d.drawPolygon(xValuesFirstContainer, yValuesFirstContainer, 4); + g2d.drawPolygon(xValuesSecondContainer, yValuesSecondContainer, 4); + } + + if (containerShip.GetCrane()) + { + int[] xValuesBorderTown = {(int) _startPosX + 45, (int) _startPosX + 55, (int) _startPosX + 55, (int) _startPosX + 45}; + int[] yValuesBorderTown = {(int) _startPosY, (int) _startPosY, (int) _startPosY + 30, (int) _startPosY + 30}; + g.drawPolygon(xValuesBorderTown, yValuesBorderTown, 4); + g.fillPolygon(xValuesBorderTown, yValuesBorderTown, 4); + //Граница заливки стрелы крана + int[] xValuesBorderCrane = {(int) _startPosX + 50, (int) _startPosX + 90, (int) _startPosX + 50}; + int[] yValuesBorderCrane = {(int) _startPosY + 10, (int) _startPosY + 13, (int) _startPosY + 16}; + g2d.fillPolygon(xValuesBorderCrane,yValuesBorderCrane,3); + + //Трос и крепление + g2d.setColor(Color.BLACK); + g2d.drawLine((int)_startPosX + 90, (int)_startPosY + 14, (int)_startPosX + 90, (int)_startPosY + 40); + g2d.drawLine((int)_startPosX + 90, (int)_startPosY + 40, (int)_startPosX + 85, (int)_startPosY + 43); + g2d.drawLine((int)_startPosX + 90, (int)_startPosY + 40, (int)_startPosX + 90, (int)_startPosY + 45); + g2d.drawLine((int)_startPosX + 90, (int)_startPosY + 40, (int)_startPosX + 95, (int)_startPosY + 43); + //Граница стрелы крана + g2d.drawPolygon(xValuesBorderCrane, yValuesBorderCrane, 3); + } + g2d.setPaint(Ship.GetBodyColor()); + int xValues[]={(int) _startPosX,(int) _startPosX + 100,(int) _startPosX + 80,(int) _startPosX + 20}; + int yValues[]={(int) _startPosY + 30,(int) _startPosY + 30,(int) _startPosY + 60,(int) _startPosY + 60}; + g2d.fillPolygon(xValues,yValues,4); + g2d.setPaint(Color.BLACK); + g2d.drawPolygon(xValues,yValues,4); + } +} diff --git a/DrawingDeck.java b/DrawingDeck.java index 94e826d..3063ad3 100644 --- a/DrawingDeck.java +++ b/DrawingDeck.java @@ -1,10 +1,12 @@ import javax.swing.*; import java.awt.*; -public class DrawingDeck extends JComponent { +public class DrawingDeck extends JComponent implements IAdditionalDrawingObject{ private AdditionalEnum _decksEnum; + @Override public void SetAddEnum(int decksAmount) { _decksEnum = AdditionalEnum.FromInteger(decksAmount); } + @Override public void DrawDeck(Color colorDeck, Graphics g,float _startPosX,float _startPosY) { super.paintComponent(g); diff --git a/DrawingObjectShip.java b/DrawingObjectShip.java new file mode 100644 index 0000000..3437abd --- /dev/null +++ b/DrawingObjectShip.java @@ -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.GetStep(); + } + 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; + } + +} diff --git a/DrawingShip.java b/DrawingShip.java index bcd6e15..e3205b3 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -2,29 +2,47 @@ import javax.swing.*; import java.awt.*; import java.util.Random; public class DrawingShip extends JPanel { - private EntityShip Ship; - public DrawingDeck Deck; - private float _startPosX; - private float _startPosY; - private Integer _pictureWidth = null; - private Integer _pictureHeight = null; - protected final int _shipWidth = 120; - protected final int _shipHeight = 40; + protected EntityShip Ship; + public IAdditionalDrawingObject Deck; public void SetEnum() { Random r = new Random(); - int numbDecks = r.nextInt(1, 4); - Deck.SetAddEnum(numbDecks); + 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 = new DrawingDeck(); + } + if (numbEnum == 2) { + Deck = new DrawingTrapezoidDeck(); + } + if (numbEnum == 3) { + Deck=new DrawingTriangleDeck(); + } } public EntityShip GetShip() { return Ship; } - - public void Init(int speed, float weight, Color bodycolor) { - Ship = new EntityShip(); - Ship.Init(speed, weight, bodycolor); - Deck = new DrawingDeck(); + protected float _startPosX; + protected float _startPosY; + private Integer _pictureWidth = null; + private Integer _pictureHeight = null; + private int _shipWidth = 100; + private int _shipHeight = 60; + 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; + } public void SetPosition(int x, int y, int width, int height) { if (x < 0 || y < 0) { @@ -75,21 +93,13 @@ public class DrawingShip extends JPanel { break; } } - public void DrawTransport() { + public void DrawTransport(Graphics g) { if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { return; } - repaint(); - } - @Override - public void paintComponent(Graphics g) { 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 + 100,(int) _startPosX + 80,(int) _startPosX + 20}; @@ -114,4 +124,13 @@ public class DrawingShip extends JPanel { _startPosY = _pictureHeight - _shipHeight; } } + public float[] GetCurrentPosition() + { + float[] position = new float[4]; + position[0] = _startPosX; + position[1] = _startPosY; + position[2] = _startPosX + _shipWidth; + position[3] = _startPosY + _shipHeight; + return position; + } } \ No newline at end of file diff --git a/DrawingTrapezoidDeck.java b/DrawingTrapezoidDeck.java new file mode 100644 index 0000000..753aa50 --- /dev/null +++ b/DrawingTrapezoidDeck.java @@ -0,0 +1,39 @@ +import javax.swing.*; +import java.awt.*; + +public class DrawingTrapezoidDeck extends JComponent implements IAdditionalDrawingObject{ + private AdditionalEnum _decksEnum; + @Override + public void SetAddEnum(int decksAmount) { + _decksEnum = AdditionalEnum.FromInteger(decksAmount); + } + @Override + public void DrawDeck(Color colorDeck, Graphics g, float _startPosX, float _startPosY) + { + if (_decksEnum == null) + { + return; + } + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + int numOfDecks = 0; + switch (_decksEnum) + { + case One: + numOfDecks = 1; + break; + case Two: + numOfDecks = 2; + break; + case Three: + numOfDecks = 3; + break; + } + for(int i = 0; i < numOfDecks; ++i){ + g2d.setPaint(colorDeck); + g2d.fillOval((int)_startPosX+30 + 5*i, (int)_startPosY+25 - 5*i, 30*2 - 5*i, 5); + g2d.setPaint(Color.BLACK); + g2d.drawOval((int)_startPosX+30 + 5*i, (int)_startPosY+25 - 5*i, 30*2 - 5*i, 5); + } + } +} diff --git a/DrawingTriangleDeck.java b/DrawingTriangleDeck.java new file mode 100644 index 0000000..0956bea --- /dev/null +++ b/DrawingTriangleDeck.java @@ -0,0 +1,41 @@ +import javax.swing.*; +import java.awt.*; +public class DrawingTriangleDeck extends JComponent implements IAdditionalDrawingObject{ + private AdditionalEnum _decksEnum; + @Override + public void SetAddEnum(int decksAmount) { + _decksEnum = AdditionalEnum.FromInteger(decksAmount); + } + @Override + public void DrawDeck(Color colorDeck, Graphics g,float _startPosX,float _startPosY) + { + if (_decksEnum == null) + { + return; + } + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + int numOfDecks = 0; + switch (_decksEnum) + { + case One: + numOfDecks = 1; + break; + case Two: + numOfDecks = 2; + break; + case Three: + numOfDecks = 3; + break; + } + + for(int i = 0; i < numOfDecks; ++i){ + g2d.setPaint(colorDeck); + int[] xValues={(int)_startPosX+40 + 5*i,(int)_startPosX+40+15 + 5*i,(int)_startPosX+40+45,(int)_startPosX+40+60}; + int[] yValues={(int)_startPosY+25 -5*i,(int)_startPosY+30 - 5*i,(int)_startPosY+30 - 5*i,(int)_startPosY+25 - 5*i}; + g2d.fillPolygon(xValues,yValues,4); + g2d.setPaint(Color.BLACK); + g2d.drawPolygon(xValues,yValues,4); + } + } +} diff --git a/EntityContainerShip.java b/EntityContainerShip.java new file mode 100644 index 0000000..9602b05 --- /dev/null +++ b/EntityContainerShip.java @@ -0,0 +1,30 @@ +import java.awt.*; + +public class EntityContainerShip extends EntityShip { + private Color _DopColor; + private boolean _Crane; + private boolean _Containers; + + public Color GetDopColor() + { + return _DopColor; + } + + public boolean GetCrane() + { + return _Crane; + } + + public boolean GetContainers() + { + return _Containers; + } + public EntityContainerShip(int speed, float weight, Color bodyColor, Color dopColor, boolean crane, boolean containers) + { + super(speed,weight,bodyColor); + _DopColor = dopColor; + _Crane = crane; + _Containers = containers; + } + +} diff --git a/EntityShip.java b/EntityShip.java index 88b332d..8a40f07 100644 --- a/EntityShip.java +++ b/EntityShip.java @@ -16,9 +16,9 @@ public class EntityShip { } public float GetStep() { - return Speed*100/Weight; + return Speed*100/Weight; } - public void Init(int speed,float weight, Color bodyColor) + public EntityShip(int speed,float weight, Color bodyColor) { Random random = new Random(); Speed = speed <= 0 ? random.nextInt(50, 150) : speed; diff --git a/FormMap.form b/FormMap.form new file mode 100644 index 0000000..9042995 --- /dev/null +++ b/FormMap.form @@ -0,0 +1,119 @@ + +
diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..15f8768 --- /dev/null +++ b/FormMap.java @@ -0,0 +1,173 @@ +import javax.imageio.ImageIO; +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; + +public class FormMap extends JFrame{ + private JButton ButtonCreateModif; + private JButton ButtonLeft; + private JButton ButtonDown; + private JButton ButtonRight; + private JButton ButtonUp; + private JButton ButtonCreate; + private JComboBox ComboBoxSelectorMap; + private AbstractMap _abstractMap; + private JPanel pictureBoxShip; + private JToolBar StatusStrip; + public JPanel MainPanel; + private Random random = new Random(); + protected DrawingShip _ship; + + private BufferedImage bufferImg = null; + private JLabel JLabelSpeed = new JLabel(); + private JLabel JLabelWeight = new JLabel(); + private JLabel JLabelColor = new JLabel(); + + public void Draw() { + 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 void SetData(DrawingShip _ship) + { + pictureBoxShip.removeAll(); + JLabelSpeed.setText("Cкорость: " + _ship.GetShip().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + _ship.GetShip().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + _ship.GetShip().GetBodyColor() + " ")); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.CreateMap(pictureBoxShip.getWidth(),pictureBoxShip.getHeight(), new DrawingObjectShip(_ship)))); + pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); + pictureBoxShip.revalidate(); + } + @Override + public void paint(Graphics g) { + super.paint(g); + Draw(); + } + public FormMap() { + _abstractMap = new SimpleMap(); + Box LabelBox = Box.createHorizontalBox(); + LabelBox.setMinimumSize(new Dimension(1, 20)); + LabelBox.add(JLabelSpeed); + LabelBox.add(JLabelWeight); + LabelBox.add(JLabelColor); + StatusStrip.add(LabelBox); + 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); + } + _ship=new DrawingShip (random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + ButtonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + var _ship=new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + _ship.SetPosition(random.nextInt(100, 500), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight()); + SetData(_ship); + } + }); + ButtonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + pictureBoxShip.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(Direction.Up))); + pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); + pictureBoxShip.revalidate(); + pictureBoxShip.repaint(); + } + }); + ButtonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + pictureBoxShip.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(Direction.Down))); + pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); + pictureBoxShip.revalidate(); + pictureBoxShip.repaint(); + } + }); + ButtonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + pictureBoxShip.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(Direction.Right))); + pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); + pictureBoxShip.revalidate(); + pictureBoxShip.repaint(); + } + }); + ButtonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + pictureBoxShip.removeAll(); + JLabel imageWithMapAndObject = new JLabel(); + imageWithMapAndObject.setPreferredSize(pictureBoxShip.getSize()); + imageWithMapAndObject.setMinimumSize(new Dimension(1, 1)); + imageWithMapAndObject.setIcon(new ImageIcon(_abstractMap.MoveObject(Direction.Left))); + pictureBoxShip.add(imageWithMapAndObject, BorderLayout.CENTER); + pictureBoxShip.revalidate(); + pictureBoxShip.repaint(); + + } + }); + ButtonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + _ship=new DrawingContainerShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),random.nextBoolean(),random.nextBoolean()); + _ship.SetPosition(random.nextInt(100, 500), random.nextInt(10, 100), pictureBoxShip.getWidth(), pictureBoxShip.getHeight()); + SetData(_ship); + } + }); + ComboBoxSelectorMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ComboBoxSelectorMap = (JComboBox)e.getSource(); + String item = (String)ComboBoxSelectorMap.getSelectedItem(); + switch (item) { + case "Простая карта": + _abstractMap = new SimpleMap(); + break; + case "Острова": + _abstractMap = new IslandsMap(); + break; + case "Скалы": + _abstractMap = new RocksMap(); + break; + } + } + }); + } +} + diff --git a/FormShip.form b/FormShip.form index c061738..a3ef324 100644 --- a/FormShip.form +++ b/FormShip.form @@ -1,21 +1,30 @@