diff --git a/.idea/misc.xml b/.idea/misc.xml index 862d09b..9a35717 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/AccordionBus/AccordionBusForm.form b/AccordionBus/AccordionBusForm.form index 4cbce88..7267cd2 100644 --- a/AccordionBus/AccordionBusForm.form +++ b/AccordionBus/AccordionBusForm.form @@ -3,12 +3,12 @@ - + - + @@ -32,6 +32,14 @@ + + + + + + + + diff --git a/AccordionBus/AccordionBusForm.java b/AccordionBus/AccordionBusForm.java index 60d8f66..576ca74 100644 --- a/AccordionBus/AccordionBusForm.java +++ b/AccordionBus/AccordionBusForm.java @@ -6,7 +6,7 @@ import java.awt.event.ActionListener; import java.util.Random; public class AccordionBusForm { - private DrawingBus drawingBus; + public DrawingBus drawingBus; private AbstractStrategy abstractStrategy; private JPanel pictureBox; @@ -25,6 +25,7 @@ public class AccordionBusForm { private JButton buttonCreateBus; private JComboBox comboBoxStrategy; private JButton buttonStep; + public JButton buttonSelectBus; public AccordionBusForm() { buttonUp.setName("buttonUp"); @@ -37,10 +38,12 @@ public class AccordionBusForm { buttonCreateAccordionBus.addActionListener(e -> { Random random = new Random(); + Color bodyColor = JColorChooser.showDialog(this.pictureBox, "Выберите цвет", Color.BLACK); + Color additionalColor = JColorChooser.showDialog(this.pictureBox, "Выберите дополнительный цвет", Color.BLACK); drawingBus = new DrawingAccordionBus(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)), + bodyColor, + additionalColor, random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), @@ -52,9 +55,10 @@ public class AccordionBusForm { buttonCreateBus.addActionListener(e -> { Random random = new Random(); + Color bodyColor = JColorChooser.showDialog(this.pictureBox, "Выберите цвет", Color.BLACK); drawingBus = new DrawingBus(random.nextInt(100, 300), random.nextInt(1000, 3000), - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + bodyColor, pictureBox.getWidth(), pictureBox.getHeight()); drawingBus.SetPosition(random.nextInt(10, 100), diff --git a/AccordionBus/BusCollectionForm.form b/AccordionBus/BusCollectionForm.form new file mode 100644 index 0000000..046e602 --- /dev/null +++ b/AccordionBus/BusCollectionForm.form @@ -0,0 +1,80 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/AccordionBus/BusCollectionForm.java b/AccordionBus/BusCollectionForm.java new file mode 100644 index 0000000..52c26f2 --- /dev/null +++ b/AccordionBus/BusCollectionForm.java @@ -0,0 +1,92 @@ +package AccordionBus; + +import javax.swing.*; +import java.awt.*; + +// Форма для работы с набором объектов класса DrawingBus +public class BusCollectionForm extends JFrame { + // Набор объектов + private final BusGenericCollection _buses; + // Выбранный автобус + public DrawingBus SelectedBus; + private FrameBusGeneric frameBusGeneric; + + private JPanel mainPanel; + public JPanel getMainPanel() { + return mainPanel; + } + private JPanel pictureBoxCollection; + private JButton buttonAddBus; + private JTextField textField; + private JButton buttonRemoveBus; + private JButton buttonRefreshCollection; + private JPanel toolsPanel; + private JLabel toolsLabel; + private JButton buttonGenerateBus; + + // Конструктор + public BusCollectionForm() { + pictureBoxCollection.setSize(new Dimension(700, 450)); + _buses = new BusGenericCollection<>(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight()); + + buttonAddBus.addActionListener(e -> { + FrameAccordionBus frameAccordionBus = new FrameAccordionBus(); + frameAccordionBus.setVisible(true); + frameAccordionBus.accordionBusForm.buttonSelectBus.addActionListener(ev -> { + SelectedBus = frameAccordionBus.accordionBusForm.drawingBus; + frameAccordionBus.dispose(); + if (SelectedBus != null) { + if (_buses.plus(SelectedBus) > -2) { + Refresh(); + JOptionPane.showMessageDialog(this.getMainPanel(), "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); + } + else { + JOptionPane.showMessageDialog(this.getMainPanel(), " Объект не добавлен", "Ошибка", JOptionPane.INFORMATION_MESSAGE); + } + } + }); + }); + + buttonRemoveBus.addActionListener(e -> { + Object[] options = {"Да", "Нет"}; + int n = JOptionPane.showOptionDialog(this.getMainPanel(), "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); + if (n == 1) { + return; + } + + try { + int pos = Integer.parseInt(textField.getText()); + if (_buses.minus(pos) != null) { + Refresh(); + JOptionPane.showMessageDialog(this.getMainPanel(), "Объект удален", "Успех", JOptionPane.INFORMATION_MESSAGE); + } + else { + JOptionPane.showMessageDialog(this.getMainPanel(), "Не удалось удалить объект", "Ошибка", JOptionPane.ERROR_MESSAGE); + } + } + catch (Exception ex) { + JOptionPane.showMessageDialog(this.getMainPanel(), "Неверное значение", "Ошибка", JOptionPane.ERROR_MESSAGE); + } + }); + + buttonRefreshCollection.addActionListener(e -> { + Refresh(); + }); + + buttonGenerateBus.addActionListener(e -> { + if (frameBusGeneric != null) { + frameBusGeneric.dispose(); + } + + frameBusGeneric = new FrameBusGeneric(); + frameBusGeneric.setVisible(true); + }); + } + + // Обновить картинку + public void Refresh() { + Graphics g = mainPanel.getGraphics(); + mainPanel.paint(g); + _buses.ShowBuses(g); + } +} diff --git a/AccordionBus/BusGenericCollection.java b/AccordionBus/BusGenericCollection.java new file mode 100644 index 0000000..f36dc77 --- /dev/null +++ b/AccordionBus/BusGenericCollection.java @@ -0,0 +1,96 @@ +package AccordionBus; + +import java.awt.*; + +// Параметризованный класс для набора объектов DrawingBus +public class BusGenericCollection { + // Ширина окна прорисовки + private final int _pictureWidth; + + // Высота окна прорисовки + private final int _pictureHeight; + + // Размер занимаемого объектом места (ширина) + private final int _placeSizeWidth = 215; + + // Размер занимаемого объектом места (высота) + private final int _placeSizeHeight = 50; + + // Набор объектов + private final SetGeneric _collection; + + // Конструктор + public BusGenericCollection(int picWidth, int picHeight) { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + + _collection = new SetGeneric(width * height); + } + + // Перегрузка оператора сложения + public int plus(T obj) { + if (obj == null) { + return -1; + } + + return _collection.Insert(obj); + } + + // Перегрузка оператора вычитания + public T minus(int pos) { + T obj = _collection.Get(pos); + + if (obj != null) + { + _collection.Remove(pos); + } + + return obj; + } + + // Получение объекта IMoveableObject + public U GetU(int pos) + { + return (U)_collection.Get(pos).GetMoveableObject(); + } + + // Вывод всего набора объектов + public void ShowBuses(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++) { + // Линия разметки места + g2d.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g2d.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + + private void DrawObjects(Graphics g) { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _collection.Count; i++) { + // Получение объекта + var obj = _collection.Get(i); + if (obj != null) { + // Установка позиции + obj.SetPosition( + (int)((width - 1) * _placeSizeWidth - (i % width * _placeSizeWidth)), + (int)((height - 1) * _placeSizeHeight - (i / width * _placeSizeHeight)) + ); + // Прорисовка объекта + obj.DrawTransport(g); + } + } + } +} diff --git a/AccordionBus/BusGenericForm.form b/AccordionBus/BusGenericForm.form new file mode 100644 index 0000000..60758ec --- /dev/null +++ b/AccordionBus/BusGenericForm.form @@ -0,0 +1,31 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/AccordionBus/BusGenericForm.java b/AccordionBus/BusGenericForm.java new file mode 100644 index 0000000..adfd1d5 --- /dev/null +++ b/AccordionBus/BusGenericForm.java @@ -0,0 +1,86 @@ +package AccordionBus; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class BusGenericForm { + private DrawingBus drawingBus; + private SetBusGeneric _busGeneric; + + private JPanel pictureBox; + public JPanel getPictureBox() { + return pictureBox; + } + private JButton buttonGenerateBus; + + // Конструктор + public BusGenericForm() { + _busGeneric = new SetBusGeneric<>(15, 15); + + buttonGenerateBus.addActionListener(e -> { + addEntityBus(); + addDrawingDoors(); + + drawingBus = _busGeneric.NewBus(pictureBox.getWidth(), pictureBox.getHeight()); + drawingBus.SetPosition((pictureBox.getWidth() - drawingBus.GetWidth()) / 2, (pictureBox.getHeight() - drawingBus.GetHeigth()) / 2); + Draw(); + }); + } + + // Добавить объект-сущность в набор + private void addEntityBus() { + Random random = new Random(); + EntityBus entityBus; + + if (random.nextBoolean()) { + entityBus = new EntityBus(random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + } + else { + entityBus = new EntityAccordionBus(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()); + } + + _busGeneric.Insert(entityBus); + } + + // Добавить тип отрисовки дверей в набор + public void addDrawingDoors() { + Random random = new Random(); + IDrawingDoors drawingDoors; + + switch (random.nextInt(0, 3)) { + case 0: + drawingDoors = new DrawingDoorsTypeOne(); + break; + case 1: + drawingDoors = new DrawingDoorsTypeTwo(); + break; + case 2: + drawingDoors = new DrawingDoorsTypeThree(); + break; + default: + drawingDoors = new DrawingDoorsTypeOne(); + break; + } + drawingDoors.setNumDoors(random.nextInt(3, 6)); + + _busGeneric.Insert(drawingDoors); + } + + public void Draw() { + if (drawingBus == null) { + return; + } + + Graphics g = pictureBox.getGraphics(); + pictureBox.paint(g); + drawingBus.DrawTransport(g); + } +} diff --git a/AccordionBus/DrawingAccordionBus.java b/AccordionBus/DrawingAccordionBus.java index 98cbea8..0ee5cd8 100644 --- a/AccordionBus/DrawingAccordionBus.java +++ b/AccordionBus/DrawingAccordionBus.java @@ -1,9 +1,6 @@ package AccordionBus; import java.awt.*; -import java.awt.event.*; -import javax.swing.*; -import java.util.Random; // Класс, отвечающий за прорисовку и перемещение объекта-сущности public class DrawingAccordionBus extends DrawingBus { @@ -15,6 +12,12 @@ public class DrawingAccordionBus extends DrawingBus { } } + // Конструктор + public DrawingAccordionBus(EntityBus bus, IDrawingDoors door, int width, int height) { + super(bus, door, width, height); + drawingDoors = door; + } + // Прорисовка объекта public void DrawTransport(Graphics g) { if (EntityBus == null) { diff --git a/AccordionBus/DrawingBus.java b/AccordionBus/DrawingBus.java index 9f4809e..8e13b41 100644 --- a/AccordionBus/DrawingBus.java +++ b/AccordionBus/DrawingBus.java @@ -1,9 +1,7 @@ package AccordionBus; import java.awt.*; -import java.awt.event.*; import java.util.Random; -import javax.swing.*; // Класс, отвечающий за прорисовку и перемещение объекта-сущности public class DrawingBus { @@ -43,6 +41,11 @@ public class DrawingBus { // Высота объекта public int GetHeigth() { return _busHeight; } + // Получение объекта IMoveableObject из объекта DrawingBus + public IMoveableObject GetMoveableObject() { + return new DrawingObjectBus(this); + } + // Проверка, что объект может перемещаться по указанному направлению public boolean CanMove(DirectionType direction) { if (EntityBus == null) { @@ -127,6 +130,14 @@ public class DrawingBus { drawingDoors.setNumDoors(random.nextInt(3, 6)); } + // Конструктор + public DrawingBus(EntityBus bus, IDrawingDoors door, int width, int height) { + EntityBus = bus; + drawingDoors = door; + _pictureWidth = width; + _pictureHeight = height; + } + // Установка позиции public void SetPosition(int x, int y) { if (x < 0 || y < 0) { diff --git a/AccordionBus/FrameAccordionBus.java b/AccordionBus/FrameAccordionBus.java index 4e5e3a8..03b8ad0 100644 --- a/AccordionBus/FrameAccordionBus.java +++ b/AccordionBus/FrameAccordionBus.java @@ -4,7 +4,7 @@ import javax.swing.*; import java.awt.*; public class FrameAccordionBus extends JFrame { - private AccordionBusForm accordionBusForm; + public AccordionBusForm accordionBusForm; public FrameAccordionBus() { super(); diff --git a/AccordionBus/FrameBusCollection.java b/AccordionBus/FrameBusCollection.java new file mode 100644 index 0000000..dad73ad --- /dev/null +++ b/AccordionBus/FrameBusCollection.java @@ -0,0 +1,21 @@ +package AccordionBus; + +import javax.swing.*; +import java.awt.*; + +public class FrameBusCollection extends JFrame { + public BusCollectionForm busCollectionForm; + + public FrameBusCollection() { + super(); + setTitle("Набор автобусов"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + busCollectionForm = new BusCollectionForm(); + setContentPane(busCollectionForm.getMainPanel()); + setDefaultLookAndFeelDecorated(false); + setPreferredSize(new Dimension(900, 500)); + setLocation(500, 500); + pack(); + setVisible(true); + } +} diff --git a/AccordionBus/FrameBusGeneric.java b/AccordionBus/FrameBusGeneric.java new file mode 100644 index 0000000..f5a9890 --- /dev/null +++ b/AccordionBus/FrameBusGeneric.java @@ -0,0 +1,21 @@ +package AccordionBus; + +import javax.swing.*; +import java.awt.*; + +public class FrameBusGeneric extends JFrame { + public BusGenericForm busGenericForm; + + public FrameBusGeneric() { + super(); + setTitle("Генерация автобуса"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + busGenericForm = new BusGenericForm(); + setContentPane(busGenericForm.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setPreferredSize(new Dimension(900, 500)); + setLocation(500, 500); + pack(); + setVisible(true); + } +} diff --git a/AccordionBus/Main.java b/AccordionBus/Main.java index ab56656..29dfb70 100644 --- a/AccordionBus/Main.java +++ b/AccordionBus/Main.java @@ -2,6 +2,6 @@ package AccordionBus; public class Main { public static void main(String[] args) { - new MainFrameAccordionBus(); + new FrameBusCollection(); } } \ No newline at end of file diff --git a/AccordionBus/SetBusGeneric.java b/AccordionBus/SetBusGeneric.java new file mode 100644 index 0000000..7d281b7 --- /dev/null +++ b/AccordionBus/SetBusGeneric.java @@ -0,0 +1,54 @@ +package AccordionBus; + +import java.util.Random; + +// Дополнительный параметризованный набор объектов +public class SetBusGeneric { + // Набор объектов от первого параметра + private final T[] _buses; + public int BusesCount; + + // Набор объектов от второго параметра + private final U[] _doors; + public int DoorsCount; + + // Конструктор + public SetBusGeneric(int busesCount, int doorsCount) { + _buses = (T[]) new EntityBus[busesCount]; + _doors = (U[]) new IDrawingDoors[doorsCount]; + } + + // Добавление объекта в первый набор + public int Insert(T bus) { + // Проверка на вместимость + if (BusesCount >= _buses.length) { + return -1; + } + + _buses[BusesCount++] = bus; + return BusesCount; + } + + // Добавление объекта во второй набор + public int Insert(U door) { + // Проверка на вместимость + if (DoorsCount >= _doors.length) { + return -1; + } + + _doors[DoorsCount++] = door; + return DoorsCount; + } + + // Создание объекта класса прорисовки + public DrawingBus NewBus(int pictureWidth, int pictureHeight) { + Random random = new Random(); + T bus = _buses[random.nextInt(BusesCount)]; + U door = _doors[random.nextInt(DoorsCount)]; + + if (bus instanceof EntityAccordionBus) { + return new DrawingAccordionBus(bus, door, pictureWidth, pictureHeight); + } + return new DrawingBus(bus, door, pictureWidth, pictureHeight); + } +} diff --git a/AccordionBus/SetGeneric.java b/AccordionBus/SetGeneric.java new file mode 100644 index 0000000..bbbbc58 --- /dev/null +++ b/AccordionBus/SetGeneric.java @@ -0,0 +1,79 @@ +package AccordionBus; + +// Параметризованный набор рбъектов +public class SetGeneric { + // Массив объектов, которые храним + private final Object[] _places; + + // Количество объектов в массиве + public int Count; + + // Конструктор + public SetGeneric(int count) { + _places = new Object[count]; + Count = count; + } + + // Добавление объекта в набор + public int Insert(T bus) { + return Insert(bus, 0); + } + + // Добавление объекта в набор на конкретную позицию + public int Insert(T bus, int position) { + // Проверка позиции + if (position < 0 || position >= Count) { + return -1; + } + + // Проверка, что элемент массива по этой позиции пустой + if (_places[position] != null) { + // Проверка, что после вставляемого элемента в массиве есть пустой элемент + int index = -1; + for (int i = position + 1; i < Count; i++) { + if (_places[i] == null) { + index = i; + break; + } + } + + // Проверка, если пустого элемента нет + if (index == -1) { + return -1; + } + + // Сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + int j = index - 1; + while (j >= position) { + _places[j + 1] = _places[j]; + j--; + } + } + + // Вставка по позиции + _places[position] = bus; + return position; + } + + // Удаление объекта из набора с конкретной позиции + public boolean Remove(int position) { + // Проверка позиции + if (position < 0 || position >= Count) { + return false; + } + + // Удаление объекта из массива, присвоив элементу массива значение null + _places[position] = null; + return true; + } + + // Получение объекта из набора по позиции + public T Get(int position) { + // Проверка позиции + if (position < 0 || position >= Count) { + return null; + } + + return (T)_places[position]; + } +}