From 9f8be965e5714fc94651311269522eed3bc898c0 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Sun, 22 Oct 2023 13:01:12 +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=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=87=D0=B0=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectStormtrooper/DoubleParametrized.java | 41 ++++++++++ ProjectStormtrooper/DrawingPlane.java | 13 ++++ ProjectStormtrooper/DrawingStormtrooper.java | 4 + .../FormDoubleParametrized.form | 31 ++++++++ .../FormDoubleParametrized.java | 78 +++++++++++++++++++ ProjectStormtrooper/FormPlaneCollection.form | 10 ++- ProjectStormtrooper/FormPlaneCollection.java | 45 +++++++---- .../FrameDoubleParametrized.java | 17 ++++ 8 files changed, 222 insertions(+), 17 deletions(-) create mode 100644 ProjectStormtrooper/DoubleParametrized.java create mode 100644 ProjectStormtrooper/FormDoubleParametrized.form create mode 100644 ProjectStormtrooper/FormDoubleParametrized.java create mode 100644 ProjectStormtrooper/FrameDoubleParametrized.java diff --git a/ProjectStormtrooper/DoubleParametrized.java b/ProjectStormtrooper/DoubleParametrized.java new file mode 100644 index 0000000..8e01ee7 --- /dev/null +++ b/ProjectStormtrooper/DoubleParametrized.java @@ -0,0 +1,41 @@ +package ProjectStormtrooper; + +import java.util.ArrayList; +import java.util.Random; + +public class DoubleParametrized { + ArrayList _entityPlanes; + ArrayList _drawingEngines; + + public DoubleParametrized() { + _entityPlanes = new ArrayList<>(); + _drawingEngines = new ArrayList<>(); + } + + public void Add(T entityPlaneObject) { + _entityPlanes.add(entityPlaneObject); + } + + public void Add(U drawingEnginesObject) { + _drawingEngines.add(drawingEnginesObject); + } + + public DrawingPlane GeneratePlane(int pictureWidth, int pictureHeight) { + Random random = new Random(); + if (_entityPlanes.isEmpty()) { + return null; + } + T entityPlane = _entityPlanes.get(random.nextInt(_entityPlanes.size())); + U drawingEngine = null; + if (!_drawingEngines.isEmpty()) { + drawingEngine = _drawingEngines.get(random.nextInt(_drawingEngines.size())); + } + DrawingPlane drawingPlane; + if (entityPlane instanceof EntityStormtrooper) { + drawingPlane = new DrawingStormtrooper((EntityStormtrooper) entityPlane, drawingEngine, pictureWidth, pictureHeight); + } else { + drawingPlane = new DrawingPlane(entityPlane, drawingEngine, pictureWidth, pictureHeight); + } + return drawingPlane; + } +} diff --git a/ProjectStormtrooper/DrawingPlane.java b/ProjectStormtrooper/DrawingPlane.java index c47e99c..80f8b25 100644 --- a/ProjectStormtrooper/DrawingPlane.java +++ b/ProjectStormtrooper/DrawingPlane.java @@ -53,6 +53,19 @@ public class DrawingPlane { _planeHeight = planeHeight; } + protected DrawingPlane(EntityPlane entityPlane, IDrawingEngines drawingEngines, int width, int height) { + EntityPlane = entityPlane; + _drawingEngines = drawingEngines; + _pictureWidth = width; + _pictureHeight = height; + } + + protected DrawingPlane(EntityPlane entityPlane, IDrawingEngines drawingEngines, int width, int height, int planeWidth, int planeHeight) { + this(entityPlane, drawingEngines, width, height); + _planeWidth = planeWidth; + _planeHeight = planeHeight; + } + public void SetEnginesCount(int enginesCount) { _drawingEngines.SetEnumEnginesCount(enginesCount); } diff --git a/ProjectStormtrooper/DrawingStormtrooper.java b/ProjectStormtrooper/DrawingStormtrooper.java index 0a1e0a8..54b5709 100644 --- a/ProjectStormtrooper/DrawingStormtrooper.java +++ b/ProjectStormtrooper/DrawingStormtrooper.java @@ -12,6 +12,10 @@ public class DrawingStormtrooper extends DrawingPlane { } } + public DrawingStormtrooper(EntityStormtrooper entityStormtrooper, IDrawingEngines drawingEngines, int width, int height) { + super(entityStormtrooper, drawingEngines, width, height, 140, 90); + } + public void DrawTransport(Graphics g) { if (EntityPlane == null) { return; diff --git a/ProjectStormtrooper/FormDoubleParametrized.form b/ProjectStormtrooper/FormDoubleParametrized.form new file mode 100644 index 0000000..af45cfc --- /dev/null +++ b/ProjectStormtrooper/FormDoubleParametrized.form @@ -0,0 +1,31 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectStormtrooper/FormDoubleParametrized.java b/ProjectStormtrooper/FormDoubleParametrized.java new file mode 100644 index 0000000..18c42c3 --- /dev/null +++ b/ProjectStormtrooper/FormDoubleParametrized.java @@ -0,0 +1,78 @@ +package ProjectStormtrooper; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.util.Random; + +public class FormDoubleParametrized { + private JPanel PictureBox; + private JButton buttonGeneratePlane; + Random _random; + DrawingPlane _drawingPlane; + DoubleParametrized _doubleParametrized; + + public JPanel getPictureBox() { + return PictureBox; + } + + public FormDoubleParametrized() { + _random = new Random(); + _doubleParametrized = new DoubleParametrized<>(); + PictureBox.setPreferredSize(new Dimension(400, 400)); + buttonGeneratePlane.addActionListener(this::buttonGeneratePlaneClicked); + } + + private void addRandomEntityPlane() { + EntityPlane entityPlane; + if (_random.nextBoolean()) { + entityPlane = new EntityPlane( + _random.nextInt(100, 300), + _random.nextInt(1000, 3000), + new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)) + ); + } else { + entityPlane = new EntityStormtrooper( + _random.nextInt(100, 300), + _random.nextInt(1000, 3000), + new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)), + new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)), + _random.nextBoolean(), + _random.nextBoolean() + ); + } + + _doubleParametrized.Add(entityPlane); + } + + private void addRandomDrawingEngine() { + int choice = _random.nextInt(3); + IDrawingEngines drawingEngines; + if (choice == 0) { + drawingEngines = new DrawingEnginesSimple(); + } else if (choice == 1) { + drawingEngines = new DrawingEnginesEllipse(); + } else { + drawingEngines = new DrawingEnginesPyramid(); + } + drawingEngines.SetEnumEnginesCount(_random.nextInt(2, 7)); + _doubleParametrized.Add(drawingEngines); + } + + public void buttonGeneratePlaneClicked(ActionEvent e) { + addRandomEntityPlane(); + addRandomDrawingEngine(); + _drawingPlane = _doubleParametrized.GeneratePlane(PictureBox.getWidth(), PictureBox.getHeight()); + _drawingPlane.SetPosition((PictureBox.getWidth() - _drawingPlane._planeWidth) / 2, (PictureBox.getHeight() - _drawingPlane._planeHeight) / 2); + Draw(); + } + + public void Draw() { + if (_drawingPlane == null) { + return; + } + Graphics g = PictureBox.getGraphics(); + PictureBox.paint(g); + _drawingPlane.DrawTransport(g); + } +} diff --git a/ProjectStormtrooper/FormPlaneCollection.form b/ProjectStormtrooper/FormPlaneCollection.form index b1858c7..aba2572 100644 --- a/ProjectStormtrooper/FormPlaneCollection.form +++ b/ProjectStormtrooper/FormPlaneCollection.form @@ -14,7 +14,7 @@ - + @@ -67,6 +67,14 @@ + + + + + + + + diff --git a/ProjectStormtrooper/FormPlaneCollection.java b/ProjectStormtrooper/FormPlaneCollection.java index 88b021a..ce48a8a 100644 --- a/ProjectStormtrooper/FormPlaneCollection.java +++ b/ProjectStormtrooper/FormPlaneCollection.java @@ -6,6 +6,7 @@ import java.awt.event.ActionEvent; public class FormPlaneCollection { PlanesGenericCollection _planes; + FrameDoubleParametrized _frameDoubleParametrized; private JPanel PanelWrapper; private JPanel GroupBoxInstruments; private JPanel PictureBoxCollection; @@ -13,6 +14,7 @@ public class FormPlaneCollection { private JTextField textFieldNumber; private JButton buttonRemovePlane; private JButton buttonRefreshCollection; + private JButton buttonOpenGenerateWindow; public DrawingPlane SelectedPlane; @@ -26,6 +28,7 @@ public class FormPlaneCollection { buttonAddPlane.addActionListener(this::buttonAddPlaneClicked); buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked); buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked); + buttonOpenGenerateWindow.addActionListener(this::buttonOpenGenerateWindowClicked); } public void buttonAddPlaneClicked(ActionEvent e) { @@ -52,6 +55,16 @@ public class FormPlaneCollection { } public void buttonRemovePlaneClicked(ActionEvent e) { + int pos; + try { + pos = Integer.parseInt(textFieldNumber.getText()); + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(this.getPanelWrapper(), + "Неверное значение", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + return; + } Object[] options = {"Да", "Нет"}; int n = JOptionPane.showOptionDialog(this.getPanelWrapper(), "Удалить объект?", @@ -65,23 +78,15 @@ public class FormPlaneCollection { if (n == 1) { return; } - try { - int pos = Integer.parseInt(textFieldNumber.getText()); - if (_planes.Sub(pos) != null) { - refreshPictureBox(); - JOptionPane.showMessageDialog(this.getPanelWrapper(), - "Объект удален", - "Успех", - JOptionPane.INFORMATION_MESSAGE); - } else { - JOptionPane.showMessageDialog(this.getPanelWrapper(), - "Не удалось удалить объект", - "Ошибка", - JOptionPane.ERROR_MESSAGE); - } - } catch (NumberFormatException ex) { + if (_planes.Sub(pos) != null) { + refreshPictureBox(); JOptionPane.showMessageDialog(this.getPanelWrapper(), - "Неверное значение", + "Объект удален", + "Успех", + JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this.getPanelWrapper(), + "Не удалось удалить объект", "Ошибка", JOptionPane.ERROR_MESSAGE); } @@ -91,6 +96,14 @@ public class FormPlaneCollection { refreshPictureBox(); } + public void buttonOpenGenerateWindowClicked(ActionEvent e) { + if (_frameDoubleParametrized != null) { + _frameDoubleParametrized.dispose(); + } + _frameDoubleParametrized = new FrameDoubleParametrized(); + _frameDoubleParametrized.setVisible(true); + } + public void refreshPictureBox() { Graphics g = PictureBoxCollection.getGraphics(); PictureBoxCollection.paint(g); diff --git a/ProjectStormtrooper/FrameDoubleParametrized.java b/ProjectStormtrooper/FrameDoubleParametrized.java new file mode 100644 index 0000000..dc1ed07 --- /dev/null +++ b/ProjectStormtrooper/FrameDoubleParametrized.java @@ -0,0 +1,17 @@ +package ProjectStormtrooper; + +import javax.swing.*; + +public class FrameDoubleParametrized extends JFrame { + public FormDoubleParametrized _formDoubleParametrized; + public FrameDoubleParametrized() { + super(); + setTitle("Генерация самолетов"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + _formDoubleParametrized = new FormDoubleParametrized(); + setContentPane(_formDoubleParametrized.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(300, 100); + pack(); + } +}