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 8b5e9f5..80f8b25 100644 --- a/ProjectStormtrooper/DrawingPlane.java +++ b/ProjectStormtrooper/DrawingPlane.java @@ -52,6 +52,20 @@ public class DrawingPlane { _planeWidth = planeWidth; _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); } @@ -120,6 +134,8 @@ public class DrawingPlane { } Graphics2D g2d = (Graphics2D) g; + g2d.setStroke(new BasicStroke(2)); + Color bodyColor = EntityPlane.BodyColor; Color blackColor = Color.BLACK; @@ -179,4 +195,10 @@ public class DrawingPlane { bodyHeight ); } + + public IMoveableObject GetMoveableObject() { + return new DrawingObjectPlane(this); + } + + ; } 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 new file mode 100644 index 0000000..aba2572 --- /dev/null +++ b/ProjectStormtrooper/FormPlaneCollection.form @@ -0,0 +1,82 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectStormtrooper/FormPlaneCollection.java b/ProjectStormtrooper/FormPlaneCollection.java new file mode 100644 index 0000000..ce48a8a --- /dev/null +++ b/ProjectStormtrooper/FormPlaneCollection.java @@ -0,0 +1,112 @@ +package ProjectStormtrooper; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; + +public class FormPlaneCollection { + PlanesGenericCollection _planes; + FrameDoubleParametrized _frameDoubleParametrized; + private JPanel PanelWrapper; + private JPanel GroupBoxInstruments; + private JPanel PictureBoxCollection; + private JButton buttonAddPlane; + private JTextField textFieldNumber; + private JButton buttonRemovePlane; + private JButton buttonRefreshCollection; + private JButton buttonOpenGenerateWindow; + public DrawingPlane SelectedPlane; + + + public JPanel getPanelWrapper() { + return PanelWrapper; + } + + public FormPlaneCollection() { + PictureBoxCollection.setPreferredSize(new Dimension(600, 500)); + _planes = new PlanesGenericCollection<>(600, 500); + buttonAddPlane.addActionListener(this::buttonAddPlaneClicked); + buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked); + buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked); + buttonOpenGenerateWindow.addActionListener(this::buttonOpenGenerateWindowClicked); + } + + public void buttonAddPlaneClicked(ActionEvent e) { + 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) { + 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(), + "Удалить объект?", + "Все серьезно", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + options, + options[0] + ); + if (n == 1) { + return; + } + if (_planes.Sub(pos) != null) { + refreshPictureBox(); + JOptionPane.showMessageDialog(this.getPanelWrapper(), + "Объект удален", + "Успех", + JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this.getPanelWrapper(), + "Не удалось удалить объект", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + } + } + + public void buttonRefreshCollectionClicked(ActionEvent e) { + 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); + _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/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(); + } +} 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 new file mode 100644 index 0000000..9142cf1 --- /dev/null +++ b/ProjectStormtrooper/PlanesGenericCollection.java @@ -0,0 +1,72 @@ +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(T obj) { + if (obj == null) { + return -1; + } + return _collection.Insert(obj); + } + + public T Sub(int pos) { + return _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); + } + } +} diff --git a/ProjectStormtrooper/SetGeneric.java b/ProjectStormtrooper/SetGeneric.java new file mode 100644 index 0000000..e0d08eb --- /dev/null +++ b/ProjectStormtrooper/SetGeneric.java @@ -0,0 +1,65 @@ +package ProjectStormtrooper; + +public class SetGeneric { + private T[] _places; + public int Count; + + public SetGeneric(int count) { + _places = (T[]) new DrawingPlane[count]; + Count = 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]; + } +}