From facad042bed4bac47130e98ed3c6f1077129a1c7 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Mon, 27 Nov 2023 23:23:06 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20?= =?UTF-8?q?=D1=81=20=D1=84=D0=BE=D1=80=D0=BC=D0=BE=D0=B9....?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FormAirbusCollection.java | 164 ++++++++++++++++++---- src/Generics/AirbusGenericCollection.java | 20 +-- src/Generics/AirbusGenericStorage.java | 52 +++++++ src/Generics/SetGeneric.java | 46 +++--- 4 files changed, 215 insertions(+), 67 deletions(-) create mode 100644 src/Generics/AirbusGenericStorage.java diff --git a/src/FormAirbusCollection.java b/src/FormAirbusCollection.java index da76684..82417ab 100644 --- a/src/FormAirbusCollection.java +++ b/src/FormAirbusCollection.java @@ -1,37 +1,54 @@ import Drawnings.DrawningAirbus; import Generics.AirbusGenericCollection; +import Generics.AirbusGenericStorage; import MovementStrategy.DrawningObjectAirbus; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.LinkedList; import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; public class FormAirbusCollection extends JFrame { - Canvas canvas; + private Canvas canvas; + private AirbusGenericStorage _storage; static int pictureBoxWidth = 650; static int pictureBoxHeight = 460; - JButton buttonAddAirbus; - JButton buttonDeleteAirbus; - JButton buttonUpdate; - // открыть форму генерации private JButton buttonGenerateAirbus; + private JButton buttonUpdate; + private JButton buttonAddAirbus; + private JButton buttonDeleteAirbus; + + // работа с наборами + private JButton buttonAddStorage; + private JButton buttonDeleteStorage; + private JTextField textFieldStorage; + private JList listStorage; + private DefaultListModel listModel; + + private JTextField textFieldNumber; - JTextField textFieldNumber = new JTextField(); private AirbusGenericCollection _airbus; FormAirbusCollection() { + listModel = new DefaultListModel(); + listStorage = new JList(listModel); + //_airbus = new AirbusGenericCollection (pictureBoxWidth, pictureBoxHeight); + _storage = new AirbusGenericStorage(pictureBoxWidth, pictureBoxHeight); canvas = new Canvas(); JFrame frame = new JFrame("Коллекция аэробусов"); - _airbus = new AirbusGenericCollection (pictureBoxWidth, pictureBoxHeight); - canvas._airbus = _airbus; + buttonAddStorage = new JButton("Добавить набор"); + buttonAddStorage.setMargin(new Insets(0, 0, 0, 0)); + buttonDeleteStorage = new JButton("Удалить набор"); + buttonDeleteStorage.setMargin(new Insets(0, 0, 0, 0)); buttonAddAirbus = new JButton("Добавить аэробус"); buttonAddAirbus.setMargin(new Insets(0, 0, 0, 0)); - textFieldNumber = new JTextField(); buttonDeleteAirbus = new JButton("Удалить аэробус"); buttonDeleteAirbus.setMargin(new Insets(0, 0, 0, 0)); buttonUpdate = new JButton("Обновить"); @@ -39,11 +56,18 @@ public class FormAirbusCollection extends JFrame { buttonGenerateAirbus = new JButton("Форма генерации"); buttonGenerateAirbus.setMargin(new Insets(0,0,0,0)); + textFieldStorage = new JTextField(); + textFieldNumber = new JTextField(); + setSize(800,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); - buttonAddAirbus.setBounds(pictureBoxWidth, 20, 120, 25); + textFieldStorage.setBounds(pictureBoxWidth, 20, 120, 25); + buttonAddStorage.setBounds(pictureBoxWidth, textFieldStorage.getY()+30, 120, 25); + listStorage.setBounds(pictureBoxWidth, buttonAddStorage.getY()+30, 120, 120); + buttonDeleteStorage.setBounds(pictureBoxWidth, listStorage.getY()+125, 120, 25); + buttonAddAirbus.setBounds(pictureBoxWidth, buttonDeleteStorage.getY()+30, 120, 25); textFieldNumber.setBounds(pictureBoxWidth, buttonAddAirbus.getY()+30, 120, 25); buttonDeleteAirbus.setBounds(pictureBoxWidth, textFieldNumber.getY()+30, 120, 25); buttonUpdate.setBounds(pictureBoxWidth, buttonDeleteAirbus.getY()+30, 120, 25); @@ -51,6 +75,10 @@ public class FormAirbusCollection extends JFrame { canvas.setBounds(0,0,pictureBoxWidth, pictureBoxHeight); add(canvas); + add(buttonAddStorage); + add(listStorage); + add(textFieldStorage); + add(buttonDeleteStorage); add(buttonAddAirbus); add(buttonDeleteAirbus); add(buttonUpdate); @@ -59,12 +87,77 @@ public class FormAirbusCollection extends JFrame { setVisible(true); // логика формы + buttonAddStorage.addActionListener(AddStorageListener); + buttonDeleteStorage.addActionListener(DeleteStorageListener); + listStorage.addListSelectionListener(listSelectionListener); + buttonAddAirbus.addActionListener(AddAirbusListener); buttonDeleteAirbus.addActionListener(DeleteAirbusListener); buttonUpdate.addActionListener(UpdateAirbusListener); buttonGenerateAirbus.addActionListener(OpenGenerationForm); } + // секция работы с наборами + private void ReloadObject() + { + int index = listStorage.getSelectedIndex(); + listModel.clear(); + for (int i = 0; i < _storage.Keys().size(); i++) + { + listModel.addElement(_storage.Keys().get(i)); + } + for (String key : _storage.Keys()) { + listModel.addElement(key); + } + if (listModel.size() > 0 && (index == -1 || index >= listModel.size())) + { + listStorage.setSelectedIndex(0); + } + else if (listModel.size() > 0 && index > -1 && index < listModel.size()) + { + listStorage.setSelectedIndex(index); + } + } + + // добавить набор + ActionListener AddStorageListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (textFieldStorage.getText().length() == 0) + { + JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + _storage.AddSet(textFieldStorage.getText()); + ReloadObject(); + } + }; + + // выбрать + ListSelectionListener listSelectionListener = new ListSelectionListener() { + // public т.к. в интерфейсе метод тоже public + public void valueChanged(ListSelectionEvent e) { + canvas.repaint(); + } + }; + + // удалить набор + ActionListener DeleteStorageListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (listStorage.getSelectedIndex() == -1) + return; + + if (JOptionPane.showOptionDialog(null, "Удалить " + listStorage.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Да", "Нет"}, "Да") + == JOptionPane.NO_OPTION) + return; + + _storage.DelSet(listStorage.getSelectedValue()); + ReloadObject(); + } + }; + // конец секции работы с наборами + ActionListener OpenGenerationForm = new ActionListener() { @Override @@ -74,6 +167,7 @@ public class FormAirbusCollection extends JFrame { } }; + // добавить аэробус в набор ActionListener AddAirbusListener = new ActionListener() { @Override @@ -106,26 +200,30 @@ public class FormAirbusCollection extends JFrame { { @Override public void actionPerformed(ActionEvent e) { - int pos; - try { - pos = Integer.parseInt(textFieldNumber.getText()); - } catch (Exception ex) { - JOptionPane.showMessageDialog(null, "Ошибка ввода", "Ошибка", JOptionPane.ERROR_MESSAGE); + + if (listStorage.getSelectedIndex() == -1) return; - } + + var obj = _storage.get(listStorage.getSelectedValue()); + if (obj == null) + return; + Object[] options= {"да", "нет"}; if (JOptionPane.showOptionDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Да", "Нет"}, "Да") == JOptionPane.NO_OPTION) - { return; - } - if (_airbus.Remove(pos) != false) - { - _airbus.ShowAirbus(); + + try { + int pos = Integer.parseInt(textFieldStorage.getText()); + var deleted = obj.Remove(pos); + // добавить в список удалёнок + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); canvas.repaint(); - JOptionPane.showMessageDialog(null, "Объект удален"); - } else { - JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Ошибка", JOptionPane.ERROR_MESSAGE); + } + catch (Exception ex) + { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; } } }; @@ -140,16 +238,26 @@ public class FormAirbusCollection extends JFrame { }; class Canvas extends JComponent { - public AirbusGenericCollection _airbus; + public AirbusGenericCollection airbus; public Canvas() { + airbus = _airbus; } public void paintComponent(Graphics g) { super.paintComponents(g); - if (_airbus.ShowAirbus() != null) { - g.drawImage(_airbus.ShowAirbus(), 0, 0, this); - } + + if (listStorage.getSelectedIndex() == -1) + return; + + var obj = _storage.get(listStorage.getSelectedValue()); + + if (obj == null) + return; + + if (airbus.ShowAirbus() != null) + g.drawImage(airbus.ShowAirbus(), 0, 0, this); + super.repaint(); } } diff --git a/src/Generics/AirbusGenericCollection.java b/src/Generics/AirbusGenericCollection.java index 285399f..bd99c2b 100644 --- a/src/Generics/AirbusGenericCollection.java +++ b/src/Generics/AirbusGenericCollection.java @@ -77,27 +77,17 @@ public class AirbusGenericCollection> _airbusStorages; + public List Keys() { + return _airbusStorages.keySet().stream().toList(); + } + private int _pictureWidth; + private int _pictureHeight; + + public AirbusGenericStorage(int pictureWidth, int pictureHeight) + { + _airbusStorages = new HashMap> (); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(String name) + { + if (_airbusStorages.containsKey(name)) + // уже занят + return; + _airbusStorages.put(name, new AirbusGenericCollection(_pictureWidth, _pictureHeight)); + } + + public void DelSet(String name) + { + if (!_airbusStorages.containsKey(name)) + return; + _airbusStorages.remove(name); + } + + // Доступ к набору + public AirbusGenericCollection get(String ind) + { + if (_airbusStorages.containsKey(ind)) + return _airbusStorages.get(ind); + return null; + } + + // вызов удалёнок + public DrawningObjectAirbus get(String iStorage, int iAirbus){ + if (!_airbusStorages.containsKey(iStorage)) + return null; + return _airbusStorages.get(iStorage).GetU(iAirbus); + } +} \ No newline at end of file diff --git a/src/Generics/SetGeneric.java b/src/Generics/SetGeneric.java index 05dd4d0..0b582e7 100644 --- a/src/Generics/SetGeneric.java +++ b/src/Generics/SetGeneric.java @@ -1,16 +1,23 @@ package Generics; +import java.util.ArrayList; +import java.util.Iterator; + public class SetGeneric { // список объектов - private Object[] _places; + private ArrayList _places; // кол-во объектов - public int Count; + private int maxCount; + public int Count() + { + return _places.size(); + }; public SetGeneric(int count) { - _places = new Object[count]; - Count = _places.length; + _places = new ArrayList<>(count); + maxCount = count; } // Добавление объекта в набор @@ -22,46 +29,37 @@ public class SetGeneric { // Добавление объекта в набор на конкретную позицию public int Insert(T airbus, int position) { - if (position < 0 || position >= Count) + if (position < 0 || position >= maxCount || Count() >= maxCount) { return -1; } - int pos = position; - if (_places[position] != null) { - for (int i = position + 1; i < Count; ++i) { - if (_places[i] == null) { - pos = i; - break; - } - } - if (pos == Count-1) - return -1; - for (int i = pos; i > position; --i) { - _places[i] = _places[i - 1]; - } - } - _places[position] = airbus; + _places.add(position, airbus); return position; } // Удаление объекта из набора с конкретной позиции public boolean Remove(int position) { - if (position < 0 || position >= Count) + if (position < 0 || position >= Count()) { return false; } - _places[position] = null; + _places.remove(position); return true; } // Получение объекта из набора по позиции public T Get(int position) { - if (position < 0 || position >= Count) + if (position < 0 || position >= Count()) { return null; } - return (T)_places[position]; + return _places.get(position); + } + + public ArrayList GetAirbus() + { + return _places; } } -- 2.25.1 From bcc4a0d32f62a7589281dca7426d836617014b5c Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 28 Nov 2023 12:02:52 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FormAirbus.java | 1 - src/FormAirbusCollection.java | 75 +++++++++++++++++------ src/Generics/AirbusGenericCollection.java | 8 +++ 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/src/FormAirbus.java b/src/FormAirbus.java index 2539d48..f91c96c 100644 --- a/src/FormAirbus.java +++ b/src/FormAirbus.java @@ -74,7 +74,6 @@ public class FormAirbus extends JFrame { buttonDown.setIcon(new ImageIcon("images\\KeyDown.png")); setSize(800,500); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); buttonCreateAirbus.setBounds(12, 355, 130, 25); diff --git a/src/FormAirbusCollection.java b/src/FormAirbusCollection.java index 82417ab..f24d281 100644 --- a/src/FormAirbusCollection.java +++ b/src/FormAirbusCollection.java @@ -26,9 +26,11 @@ public class FormAirbusCollection extends JFrame { // работа с наборами private JButton buttonAddStorage; private JButton buttonDeleteStorage; + private JButton buttonGetDeleted; private JTextField textFieldStorage; private JList listStorage; private DefaultListModel listModel; + LinkedList linkedListDeleted; private JTextField textFieldNumber; @@ -37,8 +39,9 @@ public class FormAirbusCollection extends JFrame { FormAirbusCollection() { listModel = new DefaultListModel(); listStorage = new JList(listModel); - //_airbus = new AirbusGenericCollection (pictureBoxWidth, pictureBoxHeight); + _airbus = new AirbusGenericCollection (pictureBoxWidth, pictureBoxHeight); _storage = new AirbusGenericStorage(pictureBoxWidth, pictureBoxHeight); + linkedListDeleted = new LinkedList(); canvas = new Canvas(); JFrame frame = new JFrame("Коллекция аэробусов"); @@ -53,6 +56,8 @@ public class FormAirbusCollection extends JFrame { buttonDeleteAirbus.setMargin(new Insets(0, 0, 0, 0)); buttonUpdate = new JButton("Обновить"); buttonUpdate.setMargin(new Insets(0, 0, 0, 0)); + buttonGetDeleted = new JButton("Загрузить удалёнки"); + buttonGetDeleted.setMargin(new Insets(0, 0, 0, 0)); buttonGenerateAirbus = new JButton("Форма генерации"); buttonGenerateAirbus.setMargin(new Insets(0,0,0,0)); @@ -71,7 +76,8 @@ public class FormAirbusCollection extends JFrame { textFieldNumber.setBounds(pictureBoxWidth, buttonAddAirbus.getY()+30, 120, 25); buttonDeleteAirbus.setBounds(pictureBoxWidth, textFieldNumber.getY()+30, 120, 25); buttonUpdate.setBounds(pictureBoxWidth, buttonDeleteAirbus.getY()+30, 120, 25); - buttonGenerateAirbus.setBounds(pictureBoxWidth, buttonUpdate.getY()+30, 120, 25); + buttonGetDeleted.setBounds(pictureBoxWidth, buttonUpdate.getY()+30, 120, 25); + buttonGenerateAirbus.setBounds(pictureBoxWidth, buttonGetDeleted.getY()+30, 120, 25); canvas.setBounds(0,0,pictureBoxWidth, pictureBoxHeight); add(canvas); @@ -83,12 +89,14 @@ public class FormAirbusCollection extends JFrame { add(buttonDeleteAirbus); add(buttonUpdate); add(buttonGenerateAirbus); + add(buttonGetDeleted); add(textFieldNumber); setVisible(true); // логика формы buttonAddStorage.addActionListener(AddStorageListener); buttonDeleteStorage.addActionListener(DeleteStorageListener); + buttonGetDeleted.addActionListener(GetDeletedListener); listStorage.addListSelectionListener(listSelectionListener); buttonAddAirbus.addActionListener(AddAirbusListener); @@ -106,9 +114,6 @@ public class FormAirbusCollection extends JFrame { { listModel.addElement(_storage.Keys().get(i)); } - for (String key : _storage.Keys()) { - listModel.addElement(key); - } if (listModel.size() > 0 && (index == -1 || index >= listModel.size())) { listStorage.setSelectedIndex(0); @@ -156,6 +161,23 @@ public class FormAirbusCollection extends JFrame { ReloadObject(); } }; + + // загрузить удалёнки + ActionListener GetDeletedListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (linkedListDeleted.size() == 0) + { + JOptionPane.showMessageDialog(null, "Нет удалённых аэробусов", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + + FormAirbus form = new FormAirbus(); + form._drawningAirbus = linkedListDeleted.getLast(); + linkedListDeleted.removeLast(); + canvas.repaint(); + } + }; // конец секции работы с наборами ActionListener OpenGenerationForm = new ActionListener() @@ -172,18 +194,26 @@ public class FormAirbusCollection extends JFrame { { @Override public void actionPerformed(ActionEvent e) { - FormAirbus form = new FormAirbus(); + + if (listStorage.getSelectedIndex() == -1) + return; + + var obj = _storage.get(listStorage.getSelectedValue()); + if (obj == null) + return; + // выбор аэробуса на второй форме + FormAirbus form = new FormAirbus(); form.buttonSelectAirbus.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + DrawningAirbus selectedAirbus = form._drawningAirbus; form.dispose(); if (selectedAirbus != null) { - if (_airbus.Add(selectedAirbus) != -1) { + if (obj.Add(selectedAirbus) != -1) { JOptionPane.showMessageDialog(null, "Объект добавлен"); - _airbus.ShowAirbus(); canvas.repaint(); } else { JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Ошибка", JOptionPane.ERROR_MESSAGE); @@ -196,6 +226,7 @@ public class FormAirbusCollection extends JFrame { } }; + // удалить аэробус ActionListener DeleteAirbusListener = new ActionListener() { @Override @@ -214,11 +245,19 @@ public class FormAirbusCollection extends JFrame { return; try { - int pos = Integer.parseInt(textFieldStorage.getText()); - var deleted = obj.Remove(pos); + int pos = Integer.parseInt(textFieldNumber.getText()); + // запоминаем удалёнку + DrawningAirbus deleted = obj.GetT(pos); + if (obj.Remove(pos)) { // добавить в список удалёнок - JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); - canvas.repaint(); + linkedListDeleted.add(deleted); + JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + canvas.repaint(); + } + else + { + JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + } } catch (Exception ex) { @@ -228,21 +267,17 @@ public class FormAirbusCollection extends JFrame { } }; + // обновить ActionListener UpdateAirbusListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - _airbus.ShowAirbus(); canvas.repaint(); } }; class Canvas extends JComponent { - public AirbusGenericCollection airbus; - - public Canvas() { - airbus = _airbus; - } + public Canvas() {} public void paintComponent(Graphics g) { super.paintComponents(g); @@ -255,8 +290,8 @@ public class FormAirbusCollection extends JFrame { if (obj == null) return; - if (airbus.ShowAirbus() != null) - g.drawImage(airbus.ShowAirbus(), 0, 0, this); + if (obj.ShowAirbus() != null) + g.drawImage(obj.ShowAirbus(), 0, 0, this); super.repaint(); } diff --git a/src/Generics/AirbusGenericCollection.java b/src/Generics/AirbusGenericCollection.java index bd99c2b..c5eee83 100644 --- a/src/Generics/AirbusGenericCollection.java +++ b/src/Generics/AirbusGenericCollection.java @@ -45,6 +45,14 @@ public class AirbusGenericCollection Date: Sun, 3 Dec 2023 10:15:33 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B8=D0=B7=203=D0=B5=D0=B9=20=D0=BB=D0=B0=D0=B1=D1=8B=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=BB=D0=B8=D0=BC=D0=BE=D1=80=D1=84?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FormGenerationAirbus.java | 4 ++-- src/Generics/GenericParametrized.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/FormGenerationAirbus.java b/src/FormGenerationAirbus.java index 03117cf..39f09ae 100644 --- a/src/FormGenerationAirbus.java +++ b/src/FormGenerationAirbus.java @@ -58,7 +58,7 @@ public class FormGenerationAirbus extends JFrame { new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256)), rand.nextBoolean(), rand.nextBoolean()); } - generic.addAirbus(entity); + generic.add(entity); // генерация иллюминаторов IDrawningPortholes potholes; int n = rand.nextInt(3); @@ -69,7 +69,7 @@ public class FormGenerationAirbus extends JFrame { else potholes = new DrawningPortholesSquare(); potholes.SetCount(rand.nextInt(1,4)*10); - generic.addPortholes(potholes); + generic.add(potholes); // отрисовка _airbus = generic.randomDrawingObject(); diff --git a/src/Generics/GenericParametrized.java b/src/Generics/GenericParametrized.java index 0767a63..0512b93 100644 --- a/src/Generics/GenericParametrized.java +++ b/src/Generics/GenericParametrized.java @@ -28,7 +28,7 @@ public class GenericParametrized(); } - public boolean addAirbus(T airbus) + public boolean add(T airbus) { if (airbus == null || CountAirbus >= MaxCountAirbus) return false; @@ -37,7 +37,7 @@ public class GenericParametrized= MaxCountPortoles) return false; -- 2.25.1