From 0e17b7a7511c8ebbe2b8b44b2436f2d97af68729 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Wed, 18 Oct 2023 21:34:48 +0400 Subject: [PATCH 1/6] =?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=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20SetGeneri?= =?UTF-8?q?c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectStormtrooper/SetGeneric.java | 72 +++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 ProjectStormtrooper/SetGeneric.java diff --git a/ProjectStormtrooper/SetGeneric.java b/ProjectStormtrooper/SetGeneric.java new file mode 100644 index 0000000..cdebed4 --- /dev/null +++ b/ProjectStormtrooper/SetGeneric.java @@ -0,0 +1,72 @@ +package ProjectStormtrooper; + +public class SetGeneric { + private T[] _places; + public int Count() {return _places.length;} + public SetGeneric(int count) + { + _places = (T[]) new DrawingPlane[count]; + } + public int Insert(T plane) + { + return Insert(plane, 0); + } + public int Insert(T plane, int position) + { + // Проверка позиции + if (position < 0 || position >= Count()) + { + return -1; + } + // Проверка, что элемент массива по этой позиции пустой + if (_places[position] != null) + { + // Проверка, что после вставляемого элемента в массиве есть пустой элемент + int nullIndex = -1; + for (int i = position + 1; i < Count(); i++) + { + if (_places[i] == null) + { + nullIndex = i; + break; + } + } + // Если пустого элемента нет, то выходим + if (nullIndex < 0) + { + return -1; + } + // Сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + int j = nullIndex - 1; + while (j >= position) + { + _places[j + 1] = _places[j]; + j--; + } + } + // Вставка по позиции + _places[position] = plane; + return position; + } + public T Remove(int position) + { + // Проверка позиции + if (position < 0 || position >= Count()) + { + return null; + } + // Удаление объекта из массива, присвоив элементу массива значение null + T plane = _places[position]; + _places[position] = null; + return plane; + } + public T Get(int position) + { + // Проверка позиции + if (position < 0 || position >= Count()) + { + return null; + } + return _places[position]; + } +} -- 2.25.1 From 26b0c2c0a28322e6232bce4676583242c1d3a8d1 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Fri, 20 Oct 2023 12:17:34 +0400 Subject: [PATCH 2/6] =?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=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20PlanesGen?= =?UTF-8?q?ericCollection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectStormtrooper/DrawingPlane.java | 7 ++ .../PlanesGenericCollection.java | 74 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 ProjectStormtrooper/PlanesGenericCollection.java diff --git a/ProjectStormtrooper/DrawingPlane.java b/ProjectStormtrooper/DrawingPlane.java index 8b5e9f5..12aa27b 100644 --- a/ProjectStormtrooper/DrawingPlane.java +++ b/ProjectStormtrooper/DrawingPlane.java @@ -52,6 +52,7 @@ public class DrawingPlane { _planeWidth = planeWidth; _planeHeight = planeHeight; } + public void SetEnginesCount(int enginesCount) { _drawingEngines.SetEnumEnginesCount(enginesCount); } @@ -179,4 +180,10 @@ public class DrawingPlane { bodyHeight ); } + + public IMoveableObject GetMoveableObject() { + return new DrawingObjectPlane(this); + } + + ; } diff --git a/ProjectStormtrooper/PlanesGenericCollection.java b/ProjectStormtrooper/PlanesGenericCollection.java new file mode 100644 index 0000000..fbb0366 --- /dev/null +++ b/ProjectStormtrooper/PlanesGenericCollection.java @@ -0,0 +1,74 @@ +package ProjectStormtrooper; + +import java.awt.*; + +public class PlanesGenericCollection { + private int _pictureWidth; + private int _pictureHeight; + private final int _placeSizeWidth = 160; + private final int _placeSizeHeight = 120; + private SetGeneric _collection; + + public PlanesGenericCollection(int picWidth, int picHeight) { + int horizontalObjectsCount = picWidth / _placeSizeWidth; + int verticalObjectsCount = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(horizontalObjectsCount * verticalObjectsCount); + } + + public int Add(PlanesGenericCollection collect, T obj) { + if (obj == null) { + return -1; + } + if (collect != null) + return collect._collection.Insert(obj); + return -1; + } + + public T Sub(PlanesGenericCollection collect, int pos) { + return collect._collection.Remove(pos); + } + + public U GetU(int pos) { + if (_collection.Get(pos) != null) + return (U) _collection.Get(pos).GetMoveableObject(); + return null; + } + + public void ShowPlanes(Graphics g) { + DrawBackground(g); + DrawObjects(g); + } + + private void DrawBackground(Graphics g) { + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.BLACK); + g2d.setStroke(new BasicStroke(3)); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) { + // Линия разметки места + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + private void DrawObjects(Graphics g) { + int placesColumnCount = _pictureHeight / _placeSizeHeight; + int placesRowCount = _pictureWidth / _placeSizeWidth; + for (int i = 0; i < _collection.Count(); i++) { + // получение объекта + var obj = _collection.Get(i); + // установка позиции + if (obj == null) + continue; + obj.SetPosition( + (placesRowCount - 1) * _placeSizeWidth - (i % placesColumnCount * _placeSizeWidth) + (_placeSizeWidth - obj.GetWidth()) / 2, + i / placesColumnCount * _placeSizeHeight + (_placeSizeHeight - obj.GetHeight()) / 2 + ); + // прорисовка объекта + obj.DrawTransport(g); + } + } +} -- 2.25.1 From b04f8401c351110dd8d7e14849af8f9e32448ae7 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Fri, 20 Oct 2023 16:22:20 +0400 Subject: [PATCH 3/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BD=D0=BE=D0=B2=D0=B0=D1=8F=20=D1=84=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D0=B0,=20=D0=BD=D0=B5=D0=BE=D0=B1=D1=85=D0=BE?= =?UTF-8?q?=D0=B4=D0=B8=D0=BC=D0=BE=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA?= =?UTF-8?q?=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectStormtrooper/FormPlaneCollection.form | 74 ++++++++++++++++++++ ProjectStormtrooper/FormPlaneCollection.java | 34 +++++++++ 2 files changed, 108 insertions(+) create mode 100644 ProjectStormtrooper/FormPlaneCollection.form create mode 100644 ProjectStormtrooper/FormPlaneCollection.java diff --git a/ProjectStormtrooper/FormPlaneCollection.form b/ProjectStormtrooper/FormPlaneCollection.form new file mode 100644 index 0000000..b1858c7 --- /dev/null +++ b/ProjectStormtrooper/FormPlaneCollection.form @@ -0,0 +1,74 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectStormtrooper/FormPlaneCollection.java b/ProjectStormtrooper/FormPlaneCollection.java new file mode 100644 index 0000000..50df42b --- /dev/null +++ b/ProjectStormtrooper/FormPlaneCollection.java @@ -0,0 +1,34 @@ +package ProjectStormtrooper; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; + +public class FormPlaneCollection { + PlanesGenericCollection _planes; + private JPanel PanelWrapper; + private JPanel GroupBoxInstruments; + private JPanel PictureBoxCollection; + private JButton buttonAddPlane; + private JTextField textFieldNumber; + private JButton buttonRemovePlane; + private JButton buttonRefreshCollection; + + public FormPlaneCollection() { + _planes = new PlanesGenericCollection<>(PictureBoxCollection.getWidth(), PictureBoxCollection.getHeight()); + buttonAddPlane.addActionListener(this::buttonAddPlaneClicked); + buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked); + buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked); + } + + public void buttonAddPlaneClicked(ActionEvent e) { + //todo + } + public void buttonRemovePlaneClicked(ActionEvent e) { + //todo + } + public void buttonRefreshCollectionClicked(ActionEvent e) { + Graphics g = PictureBoxCollection.getGraphics(); + _planes.ShowPlanes(g); + } +} -- 2.25.1 From e89cbc85a23090a729650bca23b4c6e24cd6a201 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Fri, 20 Oct 2023 22:43:07 +0400 Subject: [PATCH 4/6] =?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=B1=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B0,=20=D0=BD=D0=B5=D0=BE=D0=B1?= =?UTF-8?q?=D1=85=D0=BE=D0=B4=D0=B8=D0=BC=D0=BE=20=D1=81=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D1=82=D1=8C=20=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectStormtrooper/DrawingPlane.java | 2 + ProjectStormtrooper/FormPlaneCollection.java | 72 ++++++++++++++++++- ProjectStormtrooper/FormStormtrooper.form | 24 ++++--- ProjectStormtrooper/FormStormtrooper.java | 19 +++-- ProjectStormtrooper/FramePlaneCollection.java | 17 +++++ ProjectStormtrooper/FrameStormtrooper.java | 17 +++++ ProjectStormtrooper/IDrawingEngines.java | 2 + ProjectStormtrooper/Main.java | 3 +- .../MainFrameStormtrooper.java | 19 ----- .../PlanesGenericCollection.java | 12 ++-- ProjectStormtrooper/SetGeneric.java | 47 ++++++------ 11 files changed, 164 insertions(+), 70 deletions(-) create mode 100644 ProjectStormtrooper/FramePlaneCollection.java create mode 100644 ProjectStormtrooper/FrameStormtrooper.java delete mode 100644 ProjectStormtrooper/MainFrameStormtrooper.java diff --git a/ProjectStormtrooper/DrawingPlane.java b/ProjectStormtrooper/DrawingPlane.java index 12aa27b..c47e99c 100644 --- a/ProjectStormtrooper/DrawingPlane.java +++ b/ProjectStormtrooper/DrawingPlane.java @@ -121,6 +121,8 @@ public class DrawingPlane { } Graphics2D g2d = (Graphics2D) g; + g2d.setStroke(new BasicStroke(2)); + Color bodyColor = EntityPlane.BodyColor; Color blackColor = Color.BLACK; diff --git a/ProjectStormtrooper/FormPlaneCollection.java b/ProjectStormtrooper/FormPlaneCollection.java index 50df42b..ef973f3 100644 --- a/ProjectStormtrooper/FormPlaneCollection.java +++ b/ProjectStormtrooper/FormPlaneCollection.java @@ -13,22 +13,88 @@ public class FormPlaneCollection { private JTextField textFieldNumber; private JButton buttonRemovePlane; private JButton buttonRefreshCollection; + public DrawingPlane SelectedPlane; + + + public JPanel getPanelWrapper() { + return PanelWrapper; + } public FormPlaneCollection() { - _planes = new PlanesGenericCollection<>(PictureBoxCollection.getWidth(), PictureBoxCollection.getHeight()); + PictureBoxCollection.setPreferredSize(new Dimension(600, 500)); + _planes = new PlanesGenericCollection<>(600, 500); buttonAddPlane.addActionListener(this::buttonAddPlaneClicked); buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked); buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked); } public void buttonAddPlaneClicked(ActionEvent e) { - //todo + FrameStormtrooper frameStormtrooper = new FrameStormtrooper(); + frameStormtrooper.setVisible(true); + frameStormtrooper._formPlaneCollection.buttonSelectPlane.addActionListener(ev -> { + SelectedPlane = frameStormtrooper._formPlaneCollection._drawingPlane; + frameStormtrooper.dispose(); + if (SelectedPlane != null) { + if (_planes.Add(SelectedPlane) > -1) { + refreshPictureBox(); + JOptionPane.showMessageDialog(this.getPanelWrapper(), + "Объект добавлен", + "Успех", + JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this.getPanelWrapper(), + "Не удалось добавить объект", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + } + } + }); } + public void buttonRemovePlaneClicked(ActionEvent e) { - //todo + Object[] options = {"Да", "Нет"}; + int n = JOptionPane.showOptionDialog(this.getPanelWrapper(), + "Удалить объект?", + "Все серьезно", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + options, + options[0] + ); + 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 (Exception ex) { + JOptionPane.showMessageDialog(this.getPanelWrapper(), + "Неверное значение", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + } + } + public void buttonRefreshCollectionClicked(ActionEvent e) { + refreshPictureBox(); + } + + public void refreshPictureBox() { Graphics g = PictureBoxCollection.getGraphics(); + PictureBoxCollection.paint(g); _planes.ShowPlanes(g); } } diff --git a/ProjectStormtrooper/FormStormtrooper.form b/ProjectStormtrooper/FormStormtrooper.form index 6f7d474..6993efe 100644 --- a/ProjectStormtrooper/FormStormtrooper.form +++ b/ProjectStormtrooper/FormStormtrooper.form @@ -1,6 +1,6 @@
- + @@ -21,7 +21,7 @@ - + @@ -31,7 +31,7 @@ - + @@ -46,7 +46,7 @@ - + @@ -61,7 +61,7 @@ - + @@ -77,7 +77,7 @@ - + @@ -99,18 +99,26 @@ - + - + + + + + + + + + diff --git a/ProjectStormtrooper/FormStormtrooper.java b/ProjectStormtrooper/FormStormtrooper.java index a5ecad3..04f0ead 100644 --- a/ProjectStormtrooper/FormStormtrooper.java +++ b/ProjectStormtrooper/FormStormtrooper.java @@ -2,11 +2,12 @@ package ProjectStormtrooper; import javax.swing.*; import java.awt.*; +import javax.swing.JColorChooser; import java.util.Random; import java.awt.event.ActionListener; -public class FormStormtrooper { - DrawingPlane _drawingPlane; +public class FormStormtrooper extends JDialog { + public DrawingPlane _drawingPlane; AbstractStrategy _abstractStrategy; private JButton buttonCreateStormtrooper; private JPanel pictureBox; @@ -17,6 +18,7 @@ public class FormStormtrooper { private JButton buttonCreatePlane; private JComboBox comboBoxStrategy; private JButton buttonStep; + public JButton buttonSelectPlane; public JPanel getPictureBox() { return pictureBox; @@ -32,11 +34,15 @@ public class FormStormtrooper { buttonCreateStormtrooper.addActionListener(e -> { Random random = new Random(); + Color color = JColorChooser.showDialog(this.pictureBox, "Выберите цвет", Color.BLACK); + Color additionalColor = JColorChooser.showDialog(this.pictureBox, "Выберите дополнительный цвет", Color.BLACK); + + _drawingPlane = new DrawingStormtrooper( 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)), + color, + additionalColor, random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), @@ -52,10 +58,12 @@ public class FormStormtrooper { buttonCreatePlane.addActionListener(e -> { Random random = new Random(); + Color color = JColorChooser.showDialog(this.pictureBox, "Выберите цвет", Color.BLACK); + _drawingPlane = new DrawingPlane( random.nextInt(100, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + color, pictureBox.getWidth(), pictureBox.getHeight() ); @@ -92,6 +100,7 @@ public class FormStormtrooper { } }); + ActionListener buttonMoveClickedListener = e -> { String buttonName = ((JButton) e.getSource()).getName(); diff --git a/ProjectStormtrooper/FramePlaneCollection.java b/ProjectStormtrooper/FramePlaneCollection.java new file mode 100644 index 0000000..3428147 --- /dev/null +++ b/ProjectStormtrooper/FramePlaneCollection.java @@ -0,0 +1,17 @@ +package ProjectStormtrooper; + +import javax.swing.*; + +public class FramePlaneCollection extends JFrame { + FormPlaneCollection _formPlaneCollection; + public FramePlaneCollection() { + super(); + setTitle("Набор самолетов"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + _formPlaneCollection = new FormPlaneCollection(); + setContentPane(_formPlaneCollection.getPanelWrapper()); + setDefaultLookAndFeelDecorated(false); + setLocation(300, 100); + pack(); + } +} diff --git a/ProjectStormtrooper/FrameStormtrooper.java b/ProjectStormtrooper/FrameStormtrooper.java new file mode 100644 index 0000000..c4dcebb --- /dev/null +++ b/ProjectStormtrooper/FrameStormtrooper.java @@ -0,0 +1,17 @@ +package ProjectStormtrooper; + +import javax.swing.*; + +public class FrameStormtrooper extends JFrame { + public FormStormtrooper _formPlaneCollection; + public FrameStormtrooper() { + super(); + setTitle("Штурмовик"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + _formPlaneCollection = new FormStormtrooper(); + setContentPane(_formPlaneCollection.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(300, 100); + pack(); + } +} diff --git a/ProjectStormtrooper/IDrawingEngines.java b/ProjectStormtrooper/IDrawingEngines.java index 628b705..3377b55 100644 --- a/ProjectStormtrooper/IDrawingEngines.java +++ b/ProjectStormtrooper/IDrawingEngines.java @@ -16,6 +16,8 @@ public interface IDrawingEngines { } Graphics2D g2d = (Graphics2D) g; + g2d.setStroke(new BasicStroke(2)); + int engineWidth = 20; int engineHeight = 6; diff --git a/ProjectStormtrooper/Main.java b/ProjectStormtrooper/Main.java index 3649ce1..bd4ad99 100644 --- a/ProjectStormtrooper/Main.java +++ b/ProjectStormtrooper/Main.java @@ -2,6 +2,7 @@ package ProjectStormtrooper; public class Main { public static void main(String[] args) { - MainFrameStormtrooper mainFrameStormtrooper = new MainFrameStormtrooper(); + FramePlaneCollection framePlaneCollection = new FramePlaneCollection(); + framePlaneCollection.setVisible(true); } } diff --git a/ProjectStormtrooper/MainFrameStormtrooper.java b/ProjectStormtrooper/MainFrameStormtrooper.java deleted file mode 100644 index 670d59e..0000000 --- a/ProjectStormtrooper/MainFrameStormtrooper.java +++ /dev/null @@ -1,19 +0,0 @@ -package ProjectStormtrooper; - -import javax.swing.*; - -public class MainFrameStormtrooper extends JFrame { - private FormStormtrooper _formStormtrooper; - - public MainFrameStormtrooper() { - super(); - setTitle("Штурмовик"); - setDefaultCloseOperation(EXIT_ON_CLOSE); - _formStormtrooper = new FormStormtrooper(); - setContentPane(_formStormtrooper.getPictureBox()); - setDefaultLookAndFeelDecorated(false); - setLocation(300, 100); - pack(); - setVisible(true); - } -} diff --git a/ProjectStormtrooper/PlanesGenericCollection.java b/ProjectStormtrooper/PlanesGenericCollection.java index fbb0366..9142cf1 100644 --- a/ProjectStormtrooper/PlanesGenericCollection.java +++ b/ProjectStormtrooper/PlanesGenericCollection.java @@ -17,17 +17,15 @@ public class PlanesGenericCollection(horizontalObjectsCount * verticalObjectsCount); } - public int Add(PlanesGenericCollection collect, T obj) { + public int Add(T obj) { if (obj == null) { return -1; } - if (collect != null) - return collect._collection.Insert(obj); - return -1; + return _collection.Insert(obj); } - public T Sub(PlanesGenericCollection collect, int pos) { - return collect._collection.Remove(pos); + public T Sub(int pos) { + return _collection.Remove(pos); } public U GetU(int pos) { @@ -57,7 +55,7 @@ public class PlanesGenericCollection { private T[] _places; - public int Count() {return _places.length;} - public SetGeneric(int count) - { + public int Count; + + public SetGeneric(int count) { _places = (T[]) new DrawingPlane[count]; + Count = count; } - public int Insert(T plane) - { + + public int Insert(T plane) { return Insert(plane, 0); } - public int Insert(T plane, int position) - { + + public int Insert(T plane, int position) { // Проверка позиции - if (position < 0 || position >= Count()) - { + if (position < 0 || position >= Count) { return -1; } // Проверка, что элемент массива по этой позиции пустой - if (_places[position] != null) - { + if (_places[position] != null) { // Проверка, что после вставляемого элемента в массиве есть пустой элемент int nullIndex = -1; - for (int i = position + 1; i < Count(); i++) - { - if (_places[i] == null) - { + for (int i = position + 1; i < Count; i++) { + if (_places[i] == null) { nullIndex = i; break; } } // Если пустого элемента нет, то выходим - if (nullIndex < 0) - { + if (nullIndex < 0) { return -1; } // Сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента int j = nullIndex - 1; - while (j >= position) - { + while (j >= position) { _places[j + 1] = _places[j]; j--; } @@ -48,11 +43,10 @@ public class SetGeneric { _places[position] = plane; return position; } - public T Remove(int position) - { + + public T Remove(int position) { // Проверка позиции - if (position < 0 || position >= Count()) - { + if (position < 0 || position >= Count) { return null; } // Удаление объекта из массива, присвоив элементу массива значение null @@ -60,11 +54,10 @@ public class SetGeneric { _places[position] = null; return plane; } - public T Get(int position) - { + + public T Get(int position) { // Проверка позиции - if (position < 0 || position >= Count()) - { + if (position < 0 || position >= Count) { return null; } return _places[position]; -- 2.25.1 From 68903479c1abaa8d06aa4e6bc7dfea8df271cec1 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Sun, 22 Oct 2023 10:55:44 +0400 Subject: [PATCH 5/6] =?UTF-8?q?=D0=A3=D1=82=D0=BE=D1=87=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D0=BE=D1=88=D0=B8=D0=B1?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectStormtrooper/FormPlaneCollection.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ProjectStormtrooper/FormPlaneCollection.java b/ProjectStormtrooper/FormPlaneCollection.java index ef973f3..88b021a 100644 --- a/ProjectStormtrooper/FormPlaneCollection.java +++ b/ProjectStormtrooper/FormPlaneCollection.java @@ -79,13 +79,12 @@ public class FormPlaneCollection { "Ошибка", JOptionPane.ERROR_MESSAGE); } - } catch (Exception ex) { + } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this.getPanelWrapper(), "Неверное значение", "Ошибка", JOptionPane.ERROR_MESSAGE); } - } public void buttonRefreshCollectionClicked(ActionEvent e) { -- 2.25.1 From 9f8be965e5714fc94651311269522eed3bc898c0 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Sun, 22 Oct 2023 13:01:12 +0400 Subject: [PATCH 6/6] =?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(); + } +} -- 2.25.1