diff --git a/AbstractMap.java b/AbstractMap.java index 8d39cc8..4d2ba50 100644 --- a/AbstractMap.java +++ b/AbstractMap.java @@ -27,13 +27,13 @@ public abstract class AbstractMap { return DrawMapWithObject(); } - private boolean CanMove(Direction direction, float[] position) //Проверка на возможность шага + 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; + if (_map[i][j] == _barrier) return false; } } return true; @@ -59,7 +59,7 @@ public abstract class AbstractMap { position[3] += _drawningObject.getStep(); } - if (CanMove(direction, position)) + if (CanMove(position)) { _drawningObject.MoveObject(direction); } @@ -91,22 +91,22 @@ public abstract class AbstractMap { { return bmp; } - Graphics gr = bmp.getGraphics(); + 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(gr, i, j); + DrawRoadPart(g, i, j); } else if (_map[i][j] == _barrier) { - DrawBarrierPart(gr, i, j); + DrawBarrierPart(g, i, j); } } } - _drawningObject.DrawningObject(gr); + _drawningObject.DrawningObject(g); return bmp; } protected abstract void GenerateMap(); 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..32d29c7 100644 --- a/DrawDeck.java +++ b/DrawDeck.java @@ -1,12 +1,14 @@ 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){ switch (deckCount) { 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 2e118eb..6b2bc10 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -13,14 +13,26 @@ public class DrawingShip extends JPanel { protected int _warmlyShipHeight = 50; //Высота отрисовки корабля private int deckCount = 1; - private DrawDeck dd = new DrawDeck(); + private IDrawningDeck idd; //Инициализация public DrawingShip(int speed, float weight, Color 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) @@ -88,7 +100,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()); } //Изменение границ отрисовки 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/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/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..8c18d44 100644 --- a/Main.java +++ b/Main.java @@ -3,7 +3,7 @@ import javax.swing.*; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("Hard №1"); - frame.setContentPane(new FormShip().Mainpanel); + 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)); + } + +}