From 60e488021c415c5fa4711830ba129f08fac97aca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Mon, 28 Nov 2022 23:18:52 +0400 Subject: [PATCH 01/10] rework Jpanel --- DrawingShip.java | 27 +++++++++++++++++++++++---- FormShip.form | 18 +++++++++--------- FormShip.java | 48 ++++++++++++++++++++++++++++++++---------------- 3 files changed, 64 insertions(+), 29 deletions(-) diff --git a/DrawingShip.java b/DrawingShip.java index b2c2188..24462b0 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -57,16 +57,35 @@ public class DrawingShip extends JPanel { } //Отрисовка транспорта - public void DrawTransport() + public void DrawTransport(Graphics g) { + if (GetWarmlyShip() == null) return; + if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { return; } - repaint(); + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(warmlyShip.GetBodyColor()); + int[] xPol = new int[] {(int)(_startPosX), (int)(_startPosX + _warmlyShipWidth), (int)(_startPosX + _warmlyShipWidth - 25), (int)(_startPosX + 25)}; + int[] yPol = new int[] {(int)(_startPosY + 20), (int)(_startPosY + 20), (int)(_startPosY + _warmlyShipHeight), (int)(_startPosY + _warmlyShipHeight)}; + g2d.fillPolygon(new Polygon(xPol, yPol, xPol.length)); + g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY, _warmlyShipWidth * 3 / 5, 20); + g2d.setColor(Color.CYAN); + g2d.fillOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20); + g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20); + g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(new Polygon(xPol, yPol, xPol.length)); + g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)(_startPosY), _warmlyShipWidth * 3 / 5, 20); + g2d.setColor(Color.BLUE); + 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()); } - @Override + /*@Override public void paintComponent(Graphics g){ if (GetWarmlyShip() == null) return; @@ -93,7 +112,7 @@ public class DrawingShip extends JPanel { 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()); - } + }*/ //Изменение границ отрисовки public void ChangeBorders(int width, int height) diff --git a/FormShip.form b/FormShip.form index ee8a98c..42e407f 100644 --- a/FormShip.form +++ b/FormShip.form @@ -36,14 +36,6 @@ - - - - - - - - @@ -83,7 +75,7 @@ - + @@ -102,6 +94,14 @@ + + + + + + + + diff --git a/FormShip.java b/FormShip.java index 5967181..2548441 100644 --- a/FormShip.java +++ b/FormShip.java @@ -5,43 +5,56 @@ 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 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 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 +83,12 @@ 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(); + ship.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); + 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() + " ")); Draw(); } }); @@ -102,11 +116,13 @@ 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(); } }); -- 2.25.1 From 1c30871417de9791b3b94571b81d055ad1c8e0d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Mon, 28 Nov 2022 23:26:56 +0400 Subject: [PATCH 02/10] rework construction --- DrawingShip.java | 5 ++--- EntityWarmlyShip.java | 2 +- FormShip.java | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/DrawingShip.java b/DrawingShip.java index 24462b0..f504d6e 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -16,10 +16,9 @@ public class DrawingShip extends JPanel { private DrawDeck dd = new DrawDeck(); //Инициализация - 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)); } 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/FormShip.java b/FormShip.java index 2548441..7fa214a 100644 --- a/FormShip.java +++ b/FormShip.java @@ -83,8 +83,7 @@ public class FormShip extends JFrame{ @Override public void actionPerformed(ActionEvent e) { Random random = new Random(); - ship = new DrawingShip(); - ship.Init(random.nextInt(100, 300), random.nextInt(1000, 2000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256))); + 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))); 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() + " "); -- 2.25.1 From e5a1bec005a24af42c18740d1c58411af5427b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Tue, 29 Nov 2022 00:17:16 +0400 Subject: [PATCH 03/10] =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D0=B8=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawingShip.java | 13 ++++++++++--- FormShip.form | 44 +++++++++++++++++++++++++++----------------- FormShip.java | 27 +++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/DrawingShip.java b/DrawingShip.java index f504d6e..f39d47d 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -3,14 +3,14 @@ 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(); @@ -23,6 +23,13 @@ public class DrawingShip extends JPanel { dd.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; + } + //Начальные коордитанты public void SetPosition(int x, int y, int width, int height) { diff --git a/FormShip.form b/FormShip.form index 42e407f..d356abf 100644 --- a/FormShip.form +++ b/FormShip.form @@ -1,6 +1,6 @@
- + @@ -13,7 +13,7 @@ - + @@ -25,7 +25,7 @@ - + @@ -38,7 +38,7 @@ - + @@ -50,7 +50,7 @@ - + @@ -62,16 +62,6 @@ - - - - - - - - - - @@ -84,7 +74,7 @@ - + @@ -96,12 +86,32 @@ - + + + + + + + + + + + + + + + + + + + + + diff --git a/FormShip.java b/FormShip.java index 7fa214a..707cf9c 100644 --- a/FormShip.java +++ b/FormShip.java @@ -15,6 +15,7 @@ public class FormShip extends JFrame{ 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(); @@ -36,6 +37,15 @@ public class FormShip extends JFrame{ 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 (ship == null) return; @@ -84,10 +94,7 @@ public class FormShip extends JFrame{ public void actionPerformed(ActionEvent e) { Random random = new Random(); 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))); - 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() + " ")); + SetData(); Draw(); } }); @@ -125,5 +132,17 @@ public class FormShip extends JFrame{ 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(); + } + }); } } -- 2.25.1 From 82055e73fed01ff299363cbb532f8baa7a3c7ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Tue, 29 Nov 2022 00:27:51 +0400 Subject: [PATCH 04/10] =?UTF-8?q?=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84?= =?UTF-8?q?=D0=B5=D0=B9=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningMotorShip.java | 45 ++++++++++++++++++++++++++++++++++++++++++ EntityMotorShip.java | 21 ++++++++++++++++++++ IDrawningObject.java | 9 +++++++++ 3 files changed, 75 insertions(+) create mode 100644 DrawningMotorShip.java create mode 100644 EntityMotorShip.java create mode 100644 IDrawningObject.java 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/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/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(); +} -- 2.25.1 From ce917ced054f33b8df6d4057a053855b7b98330f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Tue, 29 Nov 2022 18:58:13 +0400 Subject: [PATCH 05/10] =?UTF-8?q?=D0=B0=D1=81=D1=82=D1=80=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=20=D0=BC=D0=B0=D0=BF=D1=83=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawingShip.java | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/DrawingShip.java b/DrawingShip.java index f39d47d..2e118eb 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -91,35 +91,6 @@ public class DrawingShip extends JPanel { dd.DrawningDeck(_startPosX, _startPosY, _warmlyShipWidth, g2d, warmlyShip.GetBodyColor()); } - /*@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)}; - int[] yPol = new int[] {(int)(_startPosY + 20), (int)(_startPosY + 20), (int)(_startPosY + _warmlyShipHeight), (int)(_startPosY + _warmlyShipHeight)}; - g2d.fillPolygon(new Polygon(xPol, yPol, xPol.length)); - g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY, _warmlyShipWidth * 3 / 5, 20); - g2d.setColor(Color.CYAN); - g2d.fillOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20); - g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20); - g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20); - g2d.setColor(Color.BLACK); - g2d.drawPolygon(new Polygon(xPol, yPol, xPol.length)); - g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)(_startPosY), _warmlyShipWidth * 3 / 5, 20); - g2d.setColor(Color.BLUE); - 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()); - }*/ - //Изменение границ отрисовки public void ChangeBorders(int width, int height) { @@ -141,4 +112,8 @@ public class DrawingShip extends JPanel { } } + + public float[] GetCurrentPosition() { + return new float[]{_startPosX, _startPosY, _startPosX + _warmlyShipWidth, _startPosY + _warmlyShipHeight}; + } } -- 2.25.1 From 6c8db7840d3dc6ccc47279b2ee675b433d354f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Tue, 29 Nov 2022 19:01:22 +0400 Subject: [PATCH 06/10] =?UTF-8?q?=D0=B0=D1=81=D1=82=D1=80=D0=B0=D0=BA?= =?UTF-8?q?=D1=82=20=D0=BC=D0=B0=D0=BF=D1=83=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AbstractMap.java | 115 ++++++++++++++++++++++++++++++++++++++++ DrawningObjectShip.java | 38 +++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 AbstractMap.java create mode 100644 DrawningObjectShip.java diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..8d39cc8 --- /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(Direction direction, 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(direction, 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 gr = 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); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + _drawningObject.DrawningObject(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); +} 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(); + } +} -- 2.25.1 From 1703457b157c538005e8064d7e5fbace1613e668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Tue, 29 Nov 2022 22:18:43 +0400 Subject: [PATCH 07/10] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AbstractMap.java | 14 ++-- Direction.java | 1 + DrawDeck.java | 4 +- DrawGuns.java | 32 +++++++++ DrawOvalDeck.java | 32 +++++++++ DrawingShip.java | 18 ++++- FormMap.form | 133 +++++++++++++++++++++++++++++++++++++ FormMap.java | 159 +++++++++++++++++++++++++++++++++++++++++++++ IDrawningDeck.java | 6 ++ LastMap.java | 56 ++++++++++++++++ Main.java | 2 +- SecondMap.java | 58 +++++++++++++++++ SimpleMap.java | 49 ++++++++++++++ 13 files changed, 552 insertions(+), 12 deletions(-) create mode 100644 DrawGuns.java create mode 100644 DrawOvalDeck.java create mode 100644 FormMap.form create mode 100644 FormMap.java create mode 100644 IDrawningDeck.java create mode 100644 LastMap.java create mode 100644 SecondMap.java create mode 100644 SimpleMap.java 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)); + } + +} -- 2.25.1 From e822561eec5d0b24245829a5ff267c6002484aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Tue, 29 Nov 2022 22:21:03 +0400 Subject: [PATCH 08/10] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawDeck.java | 21 +++++++++------------ DrawingShip.java | 1 - 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/DrawDeck.java b/DrawDeck.java index 32d29c7..6cddc71 100644 --- a/DrawDeck.java +++ b/DrawDeck.java @@ -10,26 +10,23 @@ public class DrawDeck implements IDrawningDeck { @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/DrawingShip.java b/DrawingShip.java index 6b2bc10..442df41 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -12,7 +12,6 @@ public class DrawingShip extends JPanel { protected int _warmlyShipWidth = 125; //Ширина отрисовки корабля protected int _warmlyShipHeight = 50; //Высота отрисовки корабля - private int deckCount = 1; private IDrawningDeck idd; //Инициализация -- 2.25.1 From c7d237696243ab3960baacd7b233d710d6cef938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Tue, 29 Nov 2022 22:32:20 +0400 Subject: [PATCH 09/10] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D1=89=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AbstractMap.java | 2 +- DrawingShip.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/AbstractMap.java b/AbstractMap.java index 4d2ba50..c5ccc9c 100644 --- a/AbstractMap.java +++ b/AbstractMap.java @@ -33,7 +33,7 @@ public abstract class AbstractMap { { for (int j = (int)(position[0] / _size_x); j <= (int)(position[1] / _size_x); ++j) { - if (_map[i][j] == _barrier) return false; + if (i >= 0 && j >= 0 && i < _map.length && j < _map[0].length && _map[i][j] == _barrier) return false; } } return true; diff --git a/DrawingShip.java b/DrawingShip.java index 442df41..8e3766c 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -125,6 +125,6 @@ public class DrawingShip extends JPanel { } public float[] GetCurrentPosition() { - return new float[]{_startPosX, _startPosY, _startPosX + _warmlyShipWidth, _startPosY + _warmlyShipHeight}; + return new float[]{_startPosX, _startPosX + _warmlyShipWidth, _startPosY, _startPosY + _warmlyShipHeight}; } } -- 2.25.1 From 1dc780afc94b313a654a26d42b6e2d947c159c5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=20=D0=91=D0=BE=D0=BD=D0=B4=D0=B0?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 30 Nov 2022 10:32:51 +0400 Subject: [PATCH 10/10] =?UTF-8?q?=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Main.java b/Main.java index 8c18d44..5a80f25 100644 --- a/Main.java +++ b/Main.java @@ -2,7 +2,7 @@ import javax.swing.*; public class Main { public static void main(String[] args) { - JFrame frame = new JFrame("Hard №1"); + JFrame frame = new JFrame("Hard №2"); frame.setContentPane(new FormMap().Mainpanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(500, 200); -- 2.25.1