From dc25d1861ede79f3a0e3173bbca57255d7d9ed31 Mon Sep 17 00:00:00 2001 From: VanyaAlekseev Date: Sun, 24 Dec 2023 22:52:28 +0400 Subject: [PATCH] SemesterFirstLabThirdBomberHard --- .../src/ProjectBomber/DoubleParametrized.java | 41 +++++++ .../src/ProjectBomber/DrawingBomber.java | 4 +- .../src/ProjectBomber/DrawingPlane.java | 20 +++- .../src/ProjectBomber/FormBomber.form | 24 ++-- .../src/ProjectBomber/FormBomber.java | 17 ++- .../ProjectBomber/FormDoubleParametrized.form | 31 +++++ .../ProjectBomber/FormDoubleParametrized.java | 78 ++++++++++++ .../ProjectBomber/FormPlaneCollection.form | 82 +++++++++++++ .../ProjectBomber/FormPlaneCollection.java | 112 ++++++++++++++++++ .../src/ProjectBomber/FrameBomber.java | 17 +++ .../FrameDoubleParametrized.java | 17 +++ .../ProjectBomber/FramePlaneCollection.java | 17 +++ .../src/ProjectBomber/IDrawingEngines.java | 2 +- ProjectBomber/src/ProjectBomber/Main.java | 5 +- .../src/ProjectBomber/MainFrameBomber.java | 19 --- .../PlanesGenericCollection.java | 72 +++++++++++ .../src/ProjectBomber/SetGeneric.java | 65 ++++++++++ 17 files changed, 586 insertions(+), 37 deletions(-) create mode 100644 ProjectBomber/src/ProjectBomber/DoubleParametrized.java create mode 100644 ProjectBomber/src/ProjectBomber/FormDoubleParametrized.form create mode 100644 ProjectBomber/src/ProjectBomber/FormDoubleParametrized.java create mode 100644 ProjectBomber/src/ProjectBomber/FormPlaneCollection.form create mode 100644 ProjectBomber/src/ProjectBomber/FormPlaneCollection.java create mode 100644 ProjectBomber/src/ProjectBomber/FrameBomber.java create mode 100644 ProjectBomber/src/ProjectBomber/FrameDoubleParametrized.java create mode 100644 ProjectBomber/src/ProjectBomber/FramePlaneCollection.java delete mode 100644 ProjectBomber/src/ProjectBomber/MainFrameBomber.java create mode 100644 ProjectBomber/src/ProjectBomber/PlanesGenericCollection.java create mode 100644 ProjectBomber/src/ProjectBomber/SetGeneric.java diff --git a/ProjectBomber/src/ProjectBomber/DoubleParametrized.java b/ProjectBomber/src/ProjectBomber/DoubleParametrized.java new file mode 100644 index 0000000..3e8eee2 --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/DoubleParametrized.java @@ -0,0 +1,41 @@ +package ProjectBomber; + +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 EntityBomber) { + drawingPlane = new DrawingBomber((EntityBomber) entityPlane, drawingEngine, pictureWidth, pictureHeight); + } else { + drawingPlane = new DrawingPlane(entityPlane, drawingEngine, pictureWidth, pictureHeight); + } + return drawingPlane; + } +} diff --git a/ProjectBomber/src/ProjectBomber/DrawingBomber.java b/ProjectBomber/src/ProjectBomber/DrawingBomber.java index 87e99fe..f704e08 100644 --- a/ProjectBomber/src/ProjectBomber/DrawingBomber.java +++ b/ProjectBomber/src/ProjectBomber/DrawingBomber.java @@ -11,7 +11,9 @@ public class DrawingBomber extends DrawingPlane { EntityPlane = new EntityBomber(speed, weight, bodyColor, additionalColor, fuelTanks, bombs); } } - + public DrawingBomber(EntityBomber entityBomber, IDrawingEngines drawingEngines, int width, int height) { + super(entityBomber, drawingEngines, width, height, 140, 90); + } public void DrawTransport(Graphics g) { if (EntityPlane == null) { return; diff --git a/ProjectBomber/src/ProjectBomber/DrawingPlane.java b/ProjectBomber/src/ProjectBomber/DrawingPlane.java index 17d5114..9da3b53 100644 --- a/ProjectBomber/src/ProjectBomber/DrawingPlane.java +++ b/ProjectBomber/src/ProjectBomber/DrawingPlane.java @@ -52,6 +52,18 @@ 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); } @@ -119,7 +131,7 @@ public class DrawingPlane { return; } Graphics2D g2d = (Graphics2D) g; - + g2d.setStroke(new BasicStroke(2)); Color bodyColor = EntityPlane.BodyColor; Color blackColor = Color.BLACK; @@ -179,4 +191,10 @@ public class DrawingPlane { bodyHeight ); } + + public IMoveableObject GetMoveableObject() { + return new DrawingObjectPlane(this); + } + + ; } diff --git a/ProjectBomber/src/ProjectBomber/FormBomber.form b/ProjectBomber/src/ProjectBomber/FormBomber.form index 8046b3c..9fc064a 100644 --- a/ProjectBomber/src/ProjectBomber/FormBomber.form +++ b/ProjectBomber/src/ProjectBomber/FormBomber.form @@ -1,6 +1,6 @@
- + @@ -26,7 +26,7 @@ - + @@ -41,7 +41,7 @@ - + @@ -56,7 +56,7 @@ - + @@ -72,7 +72,7 @@ - + @@ -94,23 +94,31 @@ - + - + - + + + + + + + + + diff --git a/ProjectBomber/src/ProjectBomber/FormBomber.java b/ProjectBomber/src/ProjectBomber/FormBomber.java index 70f3ddd..24cccc8 100644 --- a/ProjectBomber/src/ProjectBomber/FormBomber.java +++ b/ProjectBomber/src/ProjectBomber/FormBomber.java @@ -4,9 +4,10 @@ import javax.swing.*; import java.awt.*; import java.util.Random; import java.awt.event.ActionListener; +import javax.swing.JColorChooser; -public class FormBomber { - DrawingPlane _drawingPlane; +public class FormBomber extends JDialog{ + public DrawingPlane _drawingPlane; AbstractStrategy _abstractStrategy; private JButton buttonCreateBomber; private JPanel pictureBox; @@ -17,6 +18,7 @@ public class FormBomber { private JButton buttonCreatePlane; private JComboBox comboBoxStrategy; private JButton buttonStep; + public JButton buttonSelectPlane; public JPanel getPictureBox() { return pictureBox; @@ -32,11 +34,14 @@ public class FormBomber { buttonCreateBomber.addActionListener(e -> { Random random = new Random(); + Color color = JColorChooser.showDialog(this.pictureBox, "Выберите цвет", Color.BLACK); + Color additionalColor = JColorChooser.showDialog(this.pictureBox, "Выберите дополнительный цвет", Color.BLACK); + _drawingPlane = new DrawingBomber( 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(), @@ -51,11 +56,11 @@ public class FormBomber { 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() ); diff --git a/ProjectBomber/src/ProjectBomber/FormDoubleParametrized.form b/ProjectBomber/src/ProjectBomber/FormDoubleParametrized.form new file mode 100644 index 0000000..9b8965c --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/FormDoubleParametrized.form @@ -0,0 +1,31 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectBomber/src/ProjectBomber/FormDoubleParametrized.java b/ProjectBomber/src/ProjectBomber/FormDoubleParametrized.java new file mode 100644 index 0000000..2e6f3e8 --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/FormDoubleParametrized.java @@ -0,0 +1,78 @@ +package ProjectBomber; + +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 EntityBomber( + _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/ProjectBomber/src/ProjectBomber/FormPlaneCollection.form b/ProjectBomber/src/ProjectBomber/FormPlaneCollection.form new file mode 100644 index 0000000..2eb6cdb --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/FormPlaneCollection.form @@ -0,0 +1,82 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/ProjectBomber/src/ProjectBomber/FormPlaneCollection.java b/ProjectBomber/src/ProjectBomber/FormPlaneCollection.java new file mode 100644 index 0000000..8635a9e --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/FormPlaneCollection.java @@ -0,0 +1,112 @@ +package ProjectBomber; + +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) { + FrameBomber frameBomber = new FrameBomber(); + frameBomber.setVisible(true); + frameBomber._formPlaneCollection.buttonSelectPlane.addActionListener(ev -> { + SelectedPlane = frameBomber._formPlaneCollection._drawingPlane; + frameBomber.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/ProjectBomber/src/ProjectBomber/FrameBomber.java b/ProjectBomber/src/ProjectBomber/FrameBomber.java new file mode 100644 index 0000000..a13e4dc --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/FrameBomber.java @@ -0,0 +1,17 @@ +package ProjectBomber; + +import javax.swing.*; + +public class FrameBomber extends JFrame { + public FormBomber _formPlaneCollection; + public FrameBomber() { + super(); + setTitle("Бомбардировщик"); + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + _formPlaneCollection = new FormBomber(); + setContentPane(_formPlaneCollection.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setLocation(300, 100); + pack(); + } +} diff --git a/ProjectBomber/src/ProjectBomber/FrameDoubleParametrized.java b/ProjectBomber/src/ProjectBomber/FrameDoubleParametrized.java new file mode 100644 index 0000000..66a7813 --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/FrameDoubleParametrized.java @@ -0,0 +1,17 @@ +package ProjectBomber; + +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/ProjectBomber/src/ProjectBomber/FramePlaneCollection.java b/ProjectBomber/src/ProjectBomber/FramePlaneCollection.java new file mode 100644 index 0000000..3c782bc --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/FramePlaneCollection.java @@ -0,0 +1,17 @@ +package ProjectBomber; + +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/ProjectBomber/src/ProjectBomber/IDrawingEngines.java b/ProjectBomber/src/ProjectBomber/IDrawingEngines.java index 79fdac2..6306f2d 100644 --- a/ProjectBomber/src/ProjectBomber/IDrawingEngines.java +++ b/ProjectBomber/src/ProjectBomber/IDrawingEngines.java @@ -15,7 +15,7 @@ public interface IDrawingEngines { return; } Graphics2D g2d = (Graphics2D) g; - + g2d.setStroke(new BasicStroke(2)); int engineWidth = 20; int engineHeight = 6; diff --git a/ProjectBomber/src/ProjectBomber/Main.java b/ProjectBomber/src/ProjectBomber/Main.java index b77ef84..321305f 100644 --- a/ProjectBomber/src/ProjectBomber/Main.java +++ b/ProjectBomber/src/ProjectBomber/Main.java @@ -1,5 +1,8 @@ package ProjectBomber; public class Main { - public static void main(String[] args) {MainFrameBomber mainFrameBomber = new MainFrameBomber();} + public static void main(String[] args) { + FramePlaneCollection framePlaneCollection = new FramePlaneCollection(); + framePlaneCollection.setVisible(true); + } } \ No newline at end of file diff --git a/ProjectBomber/src/ProjectBomber/MainFrameBomber.java b/ProjectBomber/src/ProjectBomber/MainFrameBomber.java deleted file mode 100644 index afe554b..0000000 --- a/ProjectBomber/src/ProjectBomber/MainFrameBomber.java +++ /dev/null @@ -1,19 +0,0 @@ -package ProjectBomber; - -import javax.swing.*; - -public class MainFrameBomber extends JFrame { - private FormBomber _formBomber; - - public MainFrameBomber() { - super(); - setTitle("Бомбардировщик"); - setDefaultCloseOperation(EXIT_ON_CLOSE); - _formBomber = new FormBomber(); - setContentPane(_formBomber.getPictureBox()); - setDefaultLookAndFeelDecorated(false); - setLocation(300, 100); - pack(); - setVisible(true); - } -} diff --git a/ProjectBomber/src/ProjectBomber/PlanesGenericCollection.java b/ProjectBomber/src/ProjectBomber/PlanesGenericCollection.java new file mode 100644 index 0000000..f48588a --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/PlanesGenericCollection.java @@ -0,0 +1,72 @@ +package ProjectBomber; + +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/ProjectBomber/src/ProjectBomber/SetGeneric.java b/ProjectBomber/src/ProjectBomber/SetGeneric.java new file mode 100644 index 0000000..af0fa13 --- /dev/null +++ b/ProjectBomber/src/ProjectBomber/SetGeneric.java @@ -0,0 +1,65 @@ +package ProjectBomber; + +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]; + } +}