From 759245f4f26200c62063c75135255162a9b99f9f Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Mon, 9 Oct 2023 20:51:56 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BE=D1=81=D0=BD=D0=BE=D0=B2=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=87=D0=B0=D1=81=D1=82=D1=8C=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectStormtrooper/DrawingPlane.java | 2 + ProjectStormtrooper/FormStormtrooper.form | 42 +++++++++--- ProjectStormtrooper/FormStormtrooper.java | 81 +++++++++++++++++++---- 3 files changed, 102 insertions(+), 23 deletions(-) diff --git a/ProjectStormtrooper/DrawingPlane.java b/ProjectStormtrooper/DrawingPlane.java index 7d713d7..14fe542 100644 --- a/ProjectStormtrooper/DrawingPlane.java +++ b/ProjectStormtrooper/DrawingPlane.java @@ -35,6 +35,7 @@ public class DrawingPlane { _pictureWidth = width; _pictureHeight = height; EntityPlane = new EntityPlane(speed, weight, bodyColor); + _drawingEngines = new DrawingEngines(); } protected DrawingPlane(int speed, double weight, Color bodyColor, @@ -47,6 +48,7 @@ public class DrawingPlane { _planeWidth = planeWidth; _planeHeight = planeHeight; EntityPlane = new EntityPlane(speed, weight, bodyColor); + _drawingEngines = new DrawingEngines(); } public void SetEnginesCount(int enginesCount) { _drawingEngines.SetEnumEnginesCount(enginesCount); diff --git a/ProjectStormtrooper/FormStormtrooper.form b/ProjectStormtrooper/FormStormtrooper.form index 5aa85d1..6f7d474 100644 --- a/ProjectStormtrooper/FormStormtrooper.form +++ b/ProjectStormtrooper/FormStormtrooper.form @@ -1,6 +1,6 @@
- + @@ -11,27 +11,27 @@ - + - + - + - + - + - + @@ -46,7 +46,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -77,7 +77,7 @@ - + @@ -89,6 +89,28 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/ProjectStormtrooper/FormStormtrooper.java b/ProjectStormtrooper/FormStormtrooper.java index bc8ab6b..a5ecad3 100644 --- a/ProjectStormtrooper/FormStormtrooper.java +++ b/ProjectStormtrooper/FormStormtrooper.java @@ -6,13 +6,17 @@ import java.util.Random; import java.awt.event.ActionListener; public class FormStormtrooper { - DrawingStormtrooper _drawingStormtrooper = new DrawingStormtrooper(); - private JButton buttonCreate; + DrawingPlane _drawingPlane; + AbstractStrategy _abstractStrategy; + private JButton buttonCreateStormtrooper; private JPanel pictureBox; private JButton buttonDown; private JButton buttonUp; private JButton buttonLeft; private JButton buttonRight; + private JButton buttonCreatePlane; + private JComboBox comboBoxStrategy; + private JButton buttonStep; public JPanel getPictureBox() { return pictureBox; @@ -24,11 +28,11 @@ public class FormStormtrooper { buttonLeft.setName("buttonLeft"); buttonRight.setName("buttonRight"); - buttonCreate.addActionListener(e -> { - _drawingStormtrooper = new DrawingStormtrooper(); + + buttonCreateStormtrooper.addActionListener(e -> { Random random = new Random(); - _drawingStormtrooper.Init( + _drawingPlane = new DrawingStormtrooper( random.nextInt(100, 300), random.nextInt(1000, 3000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), @@ -39,27 +43,70 @@ public class FormStormtrooper { pictureBox.getHeight() ); - _drawingStormtrooper.SetEnginesCount(random.nextInt(2, 7)); - _drawingStormtrooper.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + _drawingPlane.SetEnginesCount(random.nextInt(2, 7)); + _drawingPlane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); Draw(); }); + buttonCreatePlane.addActionListener(e -> { + Random random = new Random(); + + _drawingPlane = new DrawingPlane( + random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + pictureBox.getWidth(), + pictureBox.getHeight() + ); + + _drawingPlane.SetEnginesCount(random.nextInt(2, 7)); + _drawingPlane.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + + Draw(); + }); + + buttonStep.addActionListener(e -> { + if (_drawingPlane == null) + return; + if (comboBoxStrategy.isEnabled()) { + _abstractStrategy = null; + int comboBoxStrategySelectedIndex = comboBoxStrategy.getSelectedIndex(); + if (comboBoxStrategySelectedIndex == 0) { + _abstractStrategy = new MoveToCenter(); + } else if (comboBoxStrategySelectedIndex == 1) { + _abstractStrategy = new MoveToRightBottom(); + } + if (_abstractStrategy == null) + return; + _abstractStrategy.SetData(new DrawingObjectPlane(_drawingPlane), pictureBox.getWidth(), pictureBox.getHeight()); + comboBoxStrategy.setEnabled(false); + } + if (_abstractStrategy == null) + return; + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) { + comboBoxStrategy.setEnabled(true); + _abstractStrategy = null; + } + }); + ActionListener buttonMoveClickedListener = e -> { String buttonName = ((JButton) e.getSource()).getName(); switch (buttonName) { case ("buttonUp") -> { - _drawingStormtrooper.MoveTransport(EnumDirectionType.Up); + _drawingPlane.MoveTransport(EnumDirectionType.Up); } case ("buttonDown") -> { - _drawingStormtrooper.MoveTransport(EnumDirectionType.Down); + _drawingPlane.MoveTransport(EnumDirectionType.Down); } case ("buttonLeft") -> { - _drawingStormtrooper.MoveTransport(EnumDirectionType.Left); + _drawingPlane.MoveTransport(EnumDirectionType.Left); } case ("buttonRight") -> { - _drawingStormtrooper.MoveTransport(EnumDirectionType.Right); + _drawingPlane.MoveTransport(EnumDirectionType.Right); } } @@ -73,12 +120,20 @@ public class FormStormtrooper { } public void Draw() { - if (_drawingStormtrooper.EntityStormtrooper == null) { + if (_drawingPlane == null) { return; } Graphics g = pictureBox.getGraphics(); pictureBox.paint(g); - _drawingStormtrooper.DrawTransport(g); + _drawingPlane.DrawTransport(g); + } + + private void createUIComponents() { + String[] strategiesList = { + "MoveToCenter", + "MoveToRightBottom" + }; + comboBoxStrategy = new JComboBox(strategiesList); } }