diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..c5ccc9c --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,115 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public abstract class AbstractMap { + private IDrawningObject _drawningObject = null; + + protected int[][] _map = null; + protected int _width; + protected int _height; + 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, IDrawningObject drawningObject) + { + _width = width; + _height = height; + _drawningObject = drawningObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + + private boolean CanMove(float[] position) //Проверка на возможность шага + { + for (int i = (int)(position[2] / _size_y); i <= (int)(position[3] / _size_y); ++i) + { + for (int j = (int)(position[0] / _size_x); j <= (int)(position[1] / _size_x); ++j) + { + if (i >= 0 && j >= 0 && i < _map.length && j < _map[0].length && _map[i][j] == _barrier) return false; + } + } + return true; + } + + public BufferedImage MoveObject(Direction direction) + { + float[] position = _drawningObject.GetCurrentPosition(); + if (direction == Direction.Left) + { + position[0] -= _drawningObject.getStep(); + } + else if (direction == Direction.Right) + { + position[1] += _drawningObject.getStep(); + } + else if (direction == Direction.Up) + { + position[2] -= _drawningObject.getStep(); + } + else if (direction == Direction.Down) + { + position[3] += _drawningObject.getStep(); + } + + if (CanMove(position)) + { + _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); + _drawningObject.SetObject(x, y, _width, _height); + for (int i = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y); i <= (int)(_drawningObject.GetCurrentPosition()[3] / _size_y); ++i) + { + for (int j = (int)(_drawningObject.GetCurrentPosition()[0] / _size_x); j <= (int)(_drawningObject.GetCurrentPosition()[1] / _size_x); ++j) + { + if (_map[i][j] == _barrier) _map[i][j] = _freeRoad; + } + } + return true; + } + private BufferedImage DrawMapWithObject() + { + BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); + if (_drawningObject == null || _map == null) + { + return bmp; + } + Graphics g = bmp.getGraphics(); + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + DrawRoadPart(g, i, j); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart(g, i, j); + } + } + } + _drawningObject.DrawningObject(g); + 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); +} diff --git a/Direction.java b/Direction.java index 8bbcfd5..108cc6d 100644 --- a/Direction.java +++ b/Direction.java @@ -1,4 +1,5 @@ public enum Direction { + None(0), //None Left(1), //Влево Up(2), //Вверх Right(3), //Вправо diff --git a/DrawDeck.java b/DrawDeck.java index 3ec0087..6cddc71 100644 --- a/DrawDeck.java +++ b/DrawDeck.java @@ -1,33 +1,32 @@ import java.awt.*; -public class DrawDeck { +public class DrawDeck implements IDrawningDeck { private Deck deckCount; + @Override public void SetDeckCount(int count){ deckCount = Deck.GetDeck(count); } + @Override public void DrawningDeck(float _startPosX, float _startPosY, int _warmlyShipWidth, Graphics2D g2d, Color bodyColor){ + int count = 1; switch (deckCount) { case One: break; - case Two: - g2d.setColor(bodyColor); - g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20, _warmlyShipWidth * 3 / 5, 20); - g2d.setColor(Color.BLACK); - g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20, _warmlyShipWidth * 3 / 5, 20); + count = 2; break; - case Three: - for (int i = 1; i < 3; ++i){ - g2d.setColor(bodyColor); - g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20); - g2d.setColor(Color.BLACK); - g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20); - } + count = 3; break; } + for (int i = 1; i < count; ++i){ + g2d.setColor(bodyColor); + g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20); + g2d.setColor(Color.BLACK); + g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20); + } } } diff --git a/DrawGuns.java b/DrawGuns.java new file mode 100644 index 0000000..f788766 --- /dev/null +++ b/DrawGuns.java @@ -0,0 +1,32 @@ +import java.awt.*; + +public class DrawGuns implements IDrawningDeck { + private Deck gunsCount; + + @Override + public void SetDeckCount(int count) { + gunsCount = Deck.GetDeck(count); + } + + @Override + public void DrawningDeck(float _startPosX, float _startPosY, int _warmlyShipWidth, Graphics2D g2d, Color bodyColor) { + int count = 1; + switch (gunsCount) + { + case One: + break; + case Two: + count = 2; + break; + case Three: + count = 3; + break; + } + g2d.setColor(Color.DARK_GRAY); + for (int i = 0; i < count; ++i) + { + g2d.fillPolygon(new Polygon(new int[]{(int)(_startPosX + _warmlyShipWidth / 5 * (i + 1)) + 10, (int)(_startPosX + _warmlyShipWidth / 5 * (i + 1)) + 25, (int)(_startPosX + _warmlyShipWidth / 5 * (i + 1)) + 40, (int)(_startPosX + _warmlyShipWidth / 5 * (i + 1)) + 25}, new int[]{(int)_startPosY - 20, (int)_startPosY - 10, (int)_startPosY - 30, (int)_startPosY - 40}, 4)); + g2d.fillOval((int)(_startPosX + _warmlyShipWidth / 5 * (i + 1)), (int)_startPosY - 20, 25, 20); + } + } +} diff --git a/DrawOvalDeck.java b/DrawOvalDeck.java new file mode 100644 index 0000000..1f1994e --- /dev/null +++ b/DrawOvalDeck.java @@ -0,0 +1,32 @@ +import java.awt.*; + +public class DrawOvalDeck implements IDrawningDeck{ + private Deck deckCount; + + @Override + public void SetDeckCount(int count) { + deckCount = Deck.GetDeck(count); + } + + @Override + public void DrawningDeck(float _startPosX, float _startPosY, int _warmlyShipWidth, Graphics2D g2d, Color bodyColor) { + int count = 1; + switch (deckCount) + { + case One: + break; + case Two: + count = 2; + break; + case Three: + count = 3; + break; + } + for (int i = 1; i < count; ++i){ + g2d.setColor(bodyColor); + g2d.fillOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20); + g2d.setColor(Color.BLACK); + g2d.drawOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY - 20 * i, _warmlyShipWidth * 3 / 5, 20); + } + } +} diff --git a/DrawingShip.java b/DrawingShip.java index b2c2188..8e3766c 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -3,25 +3,42 @@ import java.awt.*; import java.util.*; public class DrawingShip extends JPanel { - private EntityWarmlyShip warmlyShip; //Класс-сущность + public EntityWarmlyShip warmlyShip; //Класс-сущность public EntityWarmlyShip GetWarmlyShip(){return warmlyShip;} public float _startPosX; //Координаты отрисовки по оси x public float _startPosY; //Координаты отрисовки по оси y private Integer _pictureWidth = null; //Ширина окна private Integer _pictureHeight = null; //Высота окна - private final int _warmlyShipWidth = 125; //Ширина отрисовки корабля - private final int _warmlyShipHeight = 50; //Высота отрисовки корабля + protected int _warmlyShipWidth = 125; //Ширина отрисовки корабля + protected int _warmlyShipHeight = 50; //Высота отрисовки корабля - private int deckCount = 1; - private DrawDeck dd = new DrawDeck(); + private IDrawningDeck idd; //Инициализация - public void Init(int speed, float weight, Color bodyColor) + public DrawingShip(int speed, float weight, Color bodyColor) { - warmlyShip = new EntityWarmlyShip(); - warmlyShip.Init(speed, weight, bodyColor); + warmlyShip = new EntityWarmlyShip(speed, weight, bodyColor); Random random = new Random(); - dd.SetDeckCount(random.nextInt(1, 4)); + switch (random.nextInt(3)) + { + case 0: + idd = new DrawDeck(); + break; + case 1: + idd = new DrawGuns(); + break; + case 2: + idd = new DrawOvalDeck(); + break; + } + idd.SetDeckCount(random.nextInt(1, 4)); + } + + protected DrawingShip(int speed, float weight, Color bodyColor, int warmlyWidth, int warmlyHeight) + { + this(speed, weight, bodyColor); + _warmlyShipWidth = warmlyWidth; + _warmlyShipHeight = warmlyHeight; } //Начальные коордитанты @@ -57,24 +74,14 @@ public class DrawingShip extends JPanel { } //Отрисовка транспорта - 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 (GetWarmlyShip() == null) return; if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { return; } - super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(warmlyShip.GetBodyColor()); int[] xPol = new int[] {(int)(_startPosX), (int)(_startPosX + _warmlyShipWidth), (int)(_startPosX + _warmlyShipWidth - 25), (int)(_startPosX + 25)}; @@ -92,7 +99,7 @@ public class DrawingShip extends JPanel { g2d.drawOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20); g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20); g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20); - dd.DrawningDeck(_startPosX, _startPosY, _warmlyShipWidth, g2d, warmlyShip.GetBodyColor()); + idd.DrawningDeck(_startPosX, _startPosY, _warmlyShipWidth, g2d, warmlyShip.GetBodyColor()); } //Изменение границ отрисовки @@ -116,4 +123,8 @@ public class DrawingShip extends JPanel { } } + + public float[] GetCurrentPosition() { + return new float[]{_startPosX, _startPosX + _warmlyShipWidth, _startPosY, _startPosY + _warmlyShipHeight}; + } } diff --git a/DrawningMotorShip.java b/DrawningMotorShip.java new file mode 100644 index 0000000..f77ed25 --- /dev/null +++ b/DrawningMotorShip.java @@ -0,0 +1,45 @@ +import java.awt.*; + +public class DrawningMotorShip extends DrawingShip { + public DrawningMotorShip(int speed, float weight, Color bodyColor, Color dopColor, boolean tubes, boolean cistern) + { + super(speed, weight, bodyColor, 150, 75); + warmlyShip = new EntityMotorShip(speed, weight, bodyColor, dopColor, tubes , cistern); + } + + @Override + public void DrawTransport(Graphics g) + { + if (!(warmlyShip instanceof EntityMotorShip motorShip)) + { + return; + } + + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.BLACK); + g2d.setColor(motorShip.GetDopColor()); + if (motorShip.GetTubes()) + { + g2d.setColor(motorShip.GetDopColor()); + g2d.fillRect((int)_startPosX + _warmlyShipWidth / 5, (int)_startPosY, _warmlyShipWidth / 5, _warmlyShipHeight / 2); + g2d.setColor(Color.BLACK); + g2d.drawRect((int)_startPosX + _warmlyShipWidth / 5, (int)_startPosY, _warmlyShipWidth / 5, _warmlyShipHeight / 2); + g2d.setColor(motorShip.GetDopColor()); + g2d.fillRect((int)_startPosX + _warmlyShipWidth * 3 / 5, (int)_startPosY, _warmlyShipWidth / 5, _warmlyShipHeight / 2); + g2d.setColor(Color.BLACK); + g2d.drawRect((int)_startPosX + _warmlyShipWidth * 3 / 5, (int)_startPosY, _warmlyShipWidth / 5, _warmlyShipHeight / 2); + } + + _startPosY += 25; + super.DrawTransport(g); + _startPosY -= 25; + + if (motorShip.GetCistern()) + { + g2d.setColor(motorShip.GetDopColor()); + g2d.fillOval((int)_startPosX, (int)_startPosY + 25, 25, 20); + g2d.setColor(Color.BLACK); + g2d.drawOval((int)_startPosX, (int)_startPosY + 25, 25, 20); + } + } +} diff --git a/DrawningObjectShip.java b/DrawningObjectShip.java new file mode 100644 index 0000000..7f22022 --- /dev/null +++ b/DrawningObjectShip.java @@ -0,0 +1,38 @@ +import java.awt.*; + +public class DrawningObjectShip implements IDrawningObject { + + private DrawingShip _warmlyShip = null; + + public DrawningObjectShip(DrawingShip warmlyShip) + { + _warmlyShip = warmlyShip; + } + + @Override + public float getStep() { + if (_warmlyShip == null || _warmlyShip.warmlyShip == null) return 0; + return _warmlyShip.warmlyShip.GetStep(); + } + + @Override + public void SetObject(int x, int y, int width, int height) { + _warmlyShip.SetPosition(x, y, width, height); + } + + @Override + public void MoveObject(Direction direction) { + _warmlyShip.MoveTransport(direction); + } + + @Override + public void DrawningObject(Graphics g) { + _warmlyShip.DrawTransport(g); + } + + @Override + public float[] GetCurrentPosition() { + if (_warmlyShip == null || _warmlyShip.warmlyShip == null) return null; + return _warmlyShip.GetCurrentPosition(); + } +} diff --git a/EntityMotorShip.java b/EntityMotorShip.java new file mode 100644 index 0000000..b8e16c2 --- /dev/null +++ b/EntityMotorShip.java @@ -0,0 +1,21 @@ +import java.awt.*; + +public class EntityMotorShip extends EntityWarmlyShip { + private Color DopColor; + private boolean Tubes; + private boolean Cistern; + + public EntityMotorShip(int speed, float weight, Color bodyColor, Color dopColor, boolean tubes, boolean cistern) + { + super(speed, weight, bodyColor); + DopColor = dopColor; + Tubes = tubes; + Cistern = cistern; + } + + public Color GetDopColor(){return DopColor;} + + public boolean GetTubes(){return Tubes;} + + public boolean GetCistern(){return Cistern;} +} diff --git a/EntityWarmlyShip.java b/EntityWarmlyShip.java index 81136c8..073ec41 100644 --- a/EntityWarmlyShip.java +++ b/EntityWarmlyShip.java @@ -8,7 +8,7 @@ public class EntityWarmlyShip { private float Step; //Шаг при перемещении //Инициализация - public void Init(int speed, float weight, Color bodyColor) + public EntityWarmlyShip(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..707b92d --- /dev/null +++ b/FormMap.form @@ -0,0 +1,133 @@ + +
diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..3c28ef6 --- /dev/null +++ b/FormMap.java @@ -0,0 +1,159 @@ +import javax.imageio.ImageIO; +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.Random; + +public class FormMap extends JFrame { + + private AbstractMap _abstractMap; + + private JPanel JPanel; + private JButton buttonDown; + private JButton buttonRight; + private JButton buttonUp; + private JToolBar statusStrip; + private JButton buttonCreate; + private JButton buttonLeft; + private JPanel GraphicsOutput; + private JButton buttonCreateModif; + public JPanel Mainpanel; + private JComboBox comboBoxSelectorMap; + private JLabel JLabelSpeed = new JLabel(); + private JLabel JLabelWeight = new JLabel(); + private JLabel JLabelColor = new JLabel(); + + private void SetData(DrawingShip ship) + { + Random random = new Random(); + GraphicsOutput.removeAll(); + ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), GraphicsOutput.getWidth(), GraphicsOutput.getHeight()); + JLabelSpeed.setText("Cкорость: " + ship.GetWarmlyShip().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + ship.GetWarmlyShip().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + ship.GetWarmlyShip().GetBodyColor() + " ")); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(GraphicsOutput.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_abstractMap.CreateMap(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(), new DrawningObjectShip(ship)))); + GraphicsOutput.add(imageOfShip,BorderLayout.CENTER); + GraphicsOutput.revalidate(); + GraphicsOutput.repaint(); + } + + private void ButtonMove_Click(String name) + { + if (_abstractMap == null) return; + Direction direction = Direction.None; + switch (name) + { + case "buttonLeft": + direction = Direction.Left; + break; + case "buttonUp": + direction = Direction.Up; + break; + case "buttonRight": + direction = Direction.Right; + break; + case "buttonDown": + direction = Direction.Down; + break; + } + GraphicsOutput.removeAll(); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(GraphicsOutput.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(_abstractMap.MoveObject(direction))); + GraphicsOutput.add(imageOfShip,BorderLayout.CENTER); + GraphicsOutput.revalidate(); + GraphicsOutput.repaint(); + } + + public FormMap() { + Box LabelBox = Box.createHorizontalBox(); + LabelBox.setMinimumSize(new Dimension(1, 20)); + LabelBox.add(JLabelSpeed); + LabelBox.add(JLabelWeight); + LabelBox.add(JLabelColor); + statusStrip.add(LabelBox); + comboBoxSelectorMap.addItem("Простая карта"); + comboBoxSelectorMap.addItem("Вторая карта"); + comboBoxSelectorMap.addItem("Последняя карта"); + + _abstractMap = new SimpleMap(); + + try { + Image img = ImageIO.read(FormShip.class.getResource("/Images/totop.png")); + buttonUp.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormShip.class.getResource("/Images/toleft.png")); + buttonLeft.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormShip.class.getResource("/Images/todown.png")); + buttonDown.setIcon(new ImageIcon(img)); + img = ImageIO.read(FormShip.class.getResource("/Images/toright.png")); + buttonRight.setIcon(new ImageIcon(img)); + } catch (Exception ex) { + System.out.println(ex); + } + + buttonCreate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + var ship = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); + SetData(ship); + } + }); + buttonUp.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonUp"); + } + }); + buttonLeft.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonLeft"); + } + }); + buttonDown.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonDown"); + } + }); + buttonRight.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ButtonMove_Click("buttonRight"); + } + }); + buttonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + var ship = new DrawningMotorShip(random.nextInt(100, 300), random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + random.nextBoolean(), random.nextBoolean()); + SetData(ship); + } + }); + comboBoxSelectorMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + switch (comboBoxSelectorMap.getSelectedItem().toString()) + { + case "Простая карта": + _abstractMap = new SimpleMap(); + break; + case "Вторая карта": + _abstractMap = new SecondMap(); + break; + case "Последняя карта": + _abstractMap = new LastMap(); + break; + } + } + }); + } +} diff --git a/FormShip.form b/FormShip.form index ee8a98c..d356abf 100644 --- a/FormShip.form +++ b/FormShip.form @@ -1,6 +1,6 @@ diff --git a/FormShip.java b/FormShip.java index 5967181..707cf9c 100644 --- a/FormShip.java +++ b/FormShip.java @@ -5,43 +5,66 @@ import java.awt.event.*; import java.awt.image.*; import java.util.*; -public class FormShip { +public class FormShip extends JFrame{ private JToolBar statusStrip; public JPanel Mainpanel; - private DrawingShip pictureShip; + private DrawingShip ship; private JButton buttonRight; private JButton buttonCreate; private JButton buttonLeft; private JButton buttonUp; private JButton buttonDown; + private JPanel GraphicsOutput; + private JButton buttonCreateModif; private JLabel JLabelSpeed = new JLabel(); private JLabel JLabelWeight = new JLabel(); private JLabel JLabelColor = new JLabel(); private void Draw() { - if (pictureShip.GetWarmlyShip() == null) return; - pictureShip.DrawTransport(); + if (ship == null || ship.GetWarmlyShip() == null) return; + GraphicsOutput.removeAll(); + BufferedImage bmp = new BufferedImage(GraphicsOutput.getWidth(), GraphicsOutput.getHeight(),BufferedImage.TYPE_INT_RGB); + Graphics g = bmp.getGraphics(); + g.setColor(new Color(238, 238, 238)); + g.fillRect(0, 0, GraphicsOutput.getWidth(), GraphicsOutput.getHeight()); + ship.DrawTransport(g); + JLabel imageOfShip = new JLabel(); + imageOfShip.setPreferredSize(GraphicsOutput.getSize()); + imageOfShip.setMinimumSize(new Dimension(1, 1)); + imageOfShip.setIcon(new ImageIcon(bmp)); + GraphicsOutput.add(imageOfShip,BorderLayout.CENTER); + validate(); + } + + private void SetData() + { + Random random = new Random(); + ship.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), GraphicsOutput.getWidth(), GraphicsOutput.getHeight()); + JLabelSpeed.setText("Cкорость: " + ship.GetWarmlyShip().GetSpeed() + " "); + JLabelWeight.setText("Вес: " + ship.GetWarmlyShip().GetWeight() + " "); + JLabelColor.setText(("Цвет: " + ship.GetWarmlyShip().GetBodyColor() + " ")); } private void ButtonMove_Click(String name) { - if (pictureShip == null) return; + if (ship == null) return; switch (name) { case "buttonLeft": - pictureShip.MoveTransport(Direction.Left); + ship.MoveTransport(Direction.Left); break; case "buttonUp": - pictureShip.MoveTransport(Direction.Up); + ship.MoveTransport(Direction.Up); break; case "buttonRight": - pictureShip.MoveTransport(Direction.Right); + ship.MoveTransport(Direction.Right); break; case "buttonDown": - pictureShip.MoveTransport(Direction.Down); + ship.MoveTransport(Direction.Down); break; } + GraphicsOutput.revalidate(); Draw(); } @@ -70,11 +93,8 @@ public class FormShip { @Override public void actionPerformed(ActionEvent e) { Random random = new Random(); - pictureShip.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); - pictureShip.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100), pictureShip.getWidth(), pictureShip.getHeight()); - JLabelSpeed.setText("Cкорость: " + pictureShip.GetWarmlyShip().GetSpeed() + " "); - JLabelWeight.setText("Вес: " + pictureShip.GetWarmlyShip().GetWeight() + " "); - JLabelColor.setText(("Цвет: " + pictureShip.GetWarmlyShip().GetBodyColor() + " ")); + ship = new DrawingShip(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); + SetData(); Draw(); } }); @@ -102,11 +122,25 @@ public class FormShip { ButtonMove_Click("buttonRight"); } }); - pictureShip.addComponentListener(new ComponentAdapter() { + GraphicsOutput.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { super.componentResized(e); - pictureShip.ChangeBorders(pictureShip.getWidth(), pictureShip.getHeight()); + if (ship == null || ship.GetWarmlyShip() == null) return; + ship.ChangeBorders(GraphicsOutput.getWidth(), GraphicsOutput.getHeight()); + GraphicsOutput.revalidate(); + Draw(); + } + }); + buttonCreateModif.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Random random = new Random(); + ship = new DrawningMotorShip(random.nextInt(100, 300), random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), + random.nextBoolean(), random.nextBoolean()); + SetData(); Draw(); } }); diff --git a/IDrawningDeck.java b/IDrawningDeck.java new file mode 100644 index 0000000..a61cb86 --- /dev/null +++ b/IDrawningDeck.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawningDeck { + void SetDeckCount(int count); + void DrawningDeck(float _startPosX, float _startPosY, int _warmlyShipWidth, Graphics2D g2d, Color bodyColor); +} diff --git a/IDrawningObject.java b/IDrawningObject.java new file mode 100644 index 0000000..af06c5b --- /dev/null +++ b/IDrawningObject.java @@ -0,0 +1,9 @@ +import java.awt.*; + +public interface IDrawningObject { + float getStep(); + void SetObject(int x, int y, int width, int height); + void MoveObject(Direction direction); + void DrawningObject(Graphics g); + float[] GetCurrentPosition(); +} diff --git a/LastMap.java b/LastMap.java new file mode 100644 index 0000000..9602d73 --- /dev/null +++ b/LastMap.java @@ -0,0 +1,56 @@ +import java.awt.*; + +public class LastMap extends AbstractMap{ + + private Color barrierColor = Color.RED; + private Color roadColor = Color.GREEN; + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setPaint(barrierColor); + g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setPaint(roadColor); + g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + + @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 < 45) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + if (x > 0 && y > 0 && x < _map.length - 1 && y < _map[0].length - 1) + { + _map[x - 1][y] = _barrier; + _map[x + 1][y] = _barrier; + _map[x][y - 1] = _barrier; + _map[x][y + 1] = _barrier; + } + counter++; + } + } + } +} diff --git a/Main.java b/Main.java index 56bf4cb..5a80f25 100644 --- a/Main.java +++ b/Main.java @@ -2,8 +2,8 @@ import javax.swing.*; public class Main { public static void main(String[] args) { - JFrame frame = new JFrame("Hard №1"); - frame.setContentPane(new FormShip().Mainpanel); + JFrame frame = new JFrame("Hard №2"); + frame.setContentPane(new FormMap().Mainpanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(500, 200); frame.pack(); diff --git a/SecondMap.java b/SecondMap.java new file mode 100644 index 0000000..1725dbf --- /dev/null +++ b/SecondMap.java @@ -0,0 +1,58 @@ +import java.awt.*; + +public class SecondMap extends AbstractMap{ + + private Color barrierColor = Color.ORANGE; + private Color roadColor = Color.BLACK; + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setPaint(barrierColor); + g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setPaint(roadColor); + g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + + @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; + } + } + for (int i = 0; i < _map.length; ++i) + { + _map[i][_map[0].length / 2] = _barrier; + _map[i][_map[0].length - 1] = _barrier; + } + for (int j = 0; j < _map[0].length; ++j) + { + _map[_map.length - 1][j] = _barrier; + } + while (counter < 45) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } +} diff --git a/SimpleMap.java b/SimpleMap.java new file mode 100644 index 0000000..4fc57c0 --- /dev/null +++ b/SimpleMap.java @@ -0,0 +1,49 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap{ + + private Color barrierColor = Color.BLACK; + private Color roadColor = Color.GRAY; + + @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++; + } + } + } + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setPaint(barrierColor); + g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setPaint(roadColor); + g2d.fillRect((int)Math.floor(j * _size_x), (int)Math.floor(i * _size_y),(int)Math.ceil(_size_x), (int)Math.ceil(_size_y)); + } + +}