From 230f260f8b50fb3d8d7b6fe2f4da2238a23553d6 Mon Sep 17 00:00:00 2001 From: russell Date: Fri, 1 Dec 2023 18:07:18 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=204?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DumpTruck/FrameDumpTruck.java | 6 +- src/DumpTruck/Generics/SetGeneric.java | 74 ++++++---- .../Generics/TrucksGenericCollection.java | 16 +-- .../Generics/TrucksGenericStorage.java | 48 +++++++ src/DumpTruck/PictureBoxCollection.java | 136 +++++++++++++++--- src/DumpTruck/PictureBoxDumpTruck.java | 10 +- 6 files changed, 232 insertions(+), 58 deletions(-) create mode 100644 src/DumpTruck/Generics/TrucksGenericStorage.java diff --git a/src/DumpTruck/FrameDumpTruck.java b/src/DumpTruck/FrameDumpTruck.java index ba61745..b4961a7 100644 --- a/src/DumpTruck/FrameDumpTruck.java +++ b/src/DumpTruck/FrameDumpTruck.java @@ -1,14 +1,16 @@ package DumpTruck; +import DumpTruck.DrawingObjects.DrawingTruck; + import javax.swing.*; public class FrameDumpTruck extends JFrame { public PictureBoxDumpTruck pictureBoxDumpTruck; - public FrameDumpTruck() { + public FrameDumpTruck(DrawingTruck truck) { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - pictureBoxDumpTruck = new PictureBoxDumpTruck(); + pictureBoxDumpTruck = new PictureBoxDumpTruck(truck); add(pictureBoxDumpTruck); pack(); setLocationRelativeTo(null); diff --git a/src/DumpTruck/Generics/SetGeneric.java b/src/DumpTruck/Generics/SetGeneric.java index aeeaeb2..6d1bcaf 100644 --- a/src/DumpTruck/Generics/SetGeneric.java +++ b/src/DumpTruck/Generics/SetGeneric.java @@ -1,12 +1,16 @@ package DumpTruck.Generics; -public class SetGeneric { - private Object[] _places; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.NoSuchElementException; - public int Count() {return _places.length;} +public class SetGeneric { + public final ArrayList _places; + private final int _maxCount; public SetGeneric(int count) { - _places = new Object[count]; + _maxCount = count; + _places = new ArrayList<>(); } public int Insert(T truck) { @@ -14,41 +18,53 @@ public class SetGeneric { } public int Insert(T truck, int position) { - if (position < 0 || position >= Count()) + if (position < 0 || position >= _maxCount) return -1; - if (_places[position] == null) - { - _places[position] = truck; - return position; - } - int index = -1; - for (int i = position; i < Count(); i++) - { - if (_places[i] == null) - { - index = i; - break; - } - } - if (index < 0) return -1; - for (int i = index; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = truck; + _places.add(position, truck); return position; } public boolean Remove(int position) { - if (position < 0 || position >= Count()) + if (position < 0 || position >= _places.size()) return false; - _places[position] = null; + _places.remove(position); return true; } public T Get(int position) { - if (position < 0 || position >= Count()) + if (position < 0 || position >= _places.size()) return null; - return (T) _places[position]; + return _places.get(position); + } + + public Iterable GetTrucks(final Integer maxTrucks) { + return new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + private int currentIndex = 0; + private int count = 0; + + @Override + public boolean hasNext() { + return currentIndex < _places.size() && (maxTrucks == null || count < maxTrucks); + } + + @Override + public T next() { + if (hasNext()) { + count++; + return _places.get(currentIndex++); + } + throw new NoSuchElementException(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + }; } } \ No newline at end of file diff --git a/src/DumpTruck/Generics/TrucksGenericCollection.java b/src/DumpTruck/Generics/TrucksGenericCollection.java index 228fbec..a49c137 100644 --- a/src/DumpTruck/Generics/TrucksGenericCollection.java +++ b/src/DumpTruck/Generics/TrucksGenericCollection.java @@ -27,12 +27,12 @@ public class TrucksGenericCollection> _truckStorages; + public ArrayList Keys() + { + return new ArrayList<>(_truckStorages.keySet()); + } + private final int _pictureWidth; + private final int _pictureHeight; + public TrucksGenericStorage(int pictureWidth, int pictureHeight) + { + _pictureHeight = pictureHeight; + _pictureWidth = pictureWidth; + _truckStorages = new HashMap<>(); + } + public void AddSet(String name) + { + if (Keys().contains(name)) + return; + _truckStorages.put(name, new TrucksGenericCollection<>(_pictureWidth, _pictureHeight)); + } + public void DelSet(String name) + { + if (!Keys().contains(name)) + return; + _truckStorages.remove(name); + } + + public TrucksGenericCollection get(String ind) + { + if (Keys().contains(ind)) + return _truckStorages.get(ind); + return null; + } + public DrawingObjectTruck get(String name, int ind) + { + if (!Keys().contains(name)) + return null; + return _truckStorages.get(name).GetU(ind); + } +} diff --git a/src/DumpTruck/PictureBoxCollection.java b/src/DumpTruck/PictureBoxCollection.java index f7b7251..fb81e5e 100644 --- a/src/DumpTruck/PictureBoxCollection.java +++ b/src/DumpTruck/PictureBoxCollection.java @@ -4,30 +4,104 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; +import java.util.LinkedList; import java.util.Random; import DumpTruck.DrawingObjects.*; import DumpTruck.Generics.*; import DumpTruck.MovementStrategy.*; public class PictureBoxCollection extends JPanel { - public TrucksGenericCollection _trucks; + private TrucksGenericStorage _storage; + private LinkedList removedTrucks; private JLabel labelTools; - private JButton buttonAddTruck, buttonDeleteTruck, buttonRefreshCollection, buttonShowDop; - private JTextField textFieldNumber; + private JButton buttonAddTruck, buttonDeleteTruck, buttonRefreshCollection, buttonShowDop, buttonAddCollection, buttonDeleteCollection, buttonShowDeleted; + private JTextField textFieldNumber, textFieldCollectionNumber; + private JList listStorage; + private DefaultListModel listModel; + protected void ReloadCollections() + { + int index = listStorage.getSelectedIndex(); + listModel.clear(); + for (String key : _storage.Keys()) + { + listModel.addElement(key); + } + if (!listModel.isEmpty() && (index == -1 || index >= listModel.size())) + { + listStorage.setSelectedIndex(0); + } + else if (!listModel.isEmpty() && index > -1 && index < listModel.size()) + { + listStorage.setSelectedIndex(index); + } + + } public PictureBoxCollection() { + removedTrucks = new LinkedList<>(); setLayout(null); - setBounds(0, 0, 800, 450); - _trucks = new TrucksGenericCollection<>(this.getWidth() - 200, this.getHeight()); - labelTools = new JLabel("Инструменты"); + setBounds(0, 0, 800, 750); + labelTools = new JLabel("Наборы"); labelTools.setBounds(660, 10, 150, 30); add(labelTools); + + textFieldCollectionNumber = new JTextField(); + textFieldCollectionNumber.setBounds(620, 50, 150, 30); + add(textFieldCollectionNumber); + + buttonAddCollection = new JButton("Добавить набор"); + buttonAddCollection.setFocusable(false); + buttonAddCollection.setBounds(620, 100, 150, 30); + buttonAddCollection.addActionListener(e -> { + String name = textFieldCollectionNumber.getText(); + if (name.length() == 0) + return; + _storage.AddSet(name); + ReloadCollections(); + }); + add(buttonAddCollection); + + listModel = new DefaultListModel<>(); + listStorage = new JList(listModel); + listStorage.setLayout(null); + listStorage.setBounds(620, 150, 150, 100); + add(listStorage); + listStorage.addListSelectionListener(e -> { + repaint(); + }); + + buttonDeleteCollection = new JButton("Удалить набор"); + buttonDeleteCollection.setFocusable(false); + buttonDeleteCollection.setBounds(620, 270, 150, 30); + buttonDeleteCollection.addActionListener(e -> { + if (listStorage.getSelectedIndex() == -1) + { + return; + } + if (JOptionPane.showConfirmDialog(null, "Delete object " + listStorage.getSelectedValue() + "?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) + { + return; + } + _storage.DelSet(listStorage.getSelectedValue()); + ReloadCollections(); + }); + add(buttonDeleteCollection); + + _storage = new TrucksGenericStorage(this.getWidth() - 200, this.getHeight()); + labelTools = new JLabel("Инструменты"); + labelTools.setBounds(660, 300, 150, 30); + add(labelTools); buttonAddTruck = new JButton("Добавить грузовик"); buttonAddTruck.setFocusable(false); - buttonAddTruck.setBounds(620, 50, 150, 30); + buttonAddTruck.setBounds(620, 350, 150, 30); buttonAddTruck.addActionListener(e -> { - FrameDumpTruck frameDumpTruck = new FrameDumpTruck(); + if (listStorage.getSelectedIndex() == -1) + return; + var obj = _storage.get(listStorage.getSelectedValue()); + if (obj == null) + return; + FrameDumpTruck frameDumpTruck = new FrameDumpTruck(null); frameDumpTruck.pictureBoxDumpTruck.buttonSelectTruck.addActionListener(e1 -> { - if (_trucks.Add(frameDumpTruck.pictureBoxDumpTruck.drawingTruck) != -1) { + if (obj.Add(frameDumpTruck.pictureBoxDumpTruck.drawingTruck) != -1) { JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); @@ -40,13 +114,18 @@ public class PictureBoxCollection extends JPanel { add(buttonAddTruck); textFieldNumber = new JTextField(); - textFieldNumber.setBounds(620, 100, 150, 30); + textFieldNumber.setBounds(620, 400, 150, 30); add(textFieldNumber); buttonDeleteTruck = new JButton("Удалить грузовик"); buttonDeleteTruck.setFocusable(false); - buttonDeleteTruck.setBounds(620, 150, 150, 30); + buttonDeleteTruck.setBounds(620, 450, 150, 30); buttonDeleteTruck.addActionListener(e -> { + if (listStorage.getSelectedIndex() == -1) + return; + var obj = _storage.get(listStorage.getSelectedValue()); + if (obj == null) + return; if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { return; } @@ -61,8 +140,11 @@ public class PictureBoxCollection extends JPanel { } int pos = Integer.parseInt(textFieldNumber.getText()); - if (_trucks.remove(pos)) { + DrawingTruck removed = null; + removed = obj.remove(pos); + if (removed != null) { JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + removedTrucks.add(removed); } else { JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); } @@ -72,23 +154,45 @@ public class PictureBoxCollection extends JPanel { buttonRefreshCollection = new JButton("Обновить коллекцию"); buttonRefreshCollection.setFocusable(false); - buttonRefreshCollection.setBounds(620, 200, 150, 30); + buttonRefreshCollection.setBounds(620, 500, 150, 30); buttonRefreshCollection.addActionListener(e -> repaint()); add(buttonRefreshCollection); + buttonShowDeleted = new JButton("Удаленные"); + buttonShowDeleted.setFocusable(false); + buttonShowDeleted.setBounds(620, 550, 150, 30); + + buttonShowDeleted.addActionListener(e -> { + if (removedTrucks.size() == 0){ + JOptionPane.showMessageDialog(null, "Коллекция пуста", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + var truck = removedTrucks.removeLast(); + FrameDumpTruck frameDumpTruck = new FrameDumpTruck(truck); + frameDumpTruck.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + }); + add(buttonShowDeleted); + buttonShowDop = new JButton("Показать доп"); buttonShowDop.setFocusable(false); - buttonShowDop.setBounds(620, 250, 150, 30); + buttonShowDop.setBounds(620, 650, 150, 30); buttonShowDop.addActionListener(e -> new FrameDop()); add(buttonShowDop); - setPreferredSize(new Dimension(800, 450)); + setPreferredSize(new Dimension(800, 750)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; - g2d.drawImage(_trucks.ShowTrucks(), 0, 0, null); + if (listStorage.getSelectedIndex() == -1) + return; + var obj = _storage.get(listStorage.getSelectedValue()); + if (obj == null) + return; + if (obj.ShowTrucks() == null) + return; + g2d.drawImage(obj.ShowTrucks(), 0, 0, null); } } diff --git a/src/DumpTruck/PictureBoxDumpTruck.java b/src/DumpTruck/PictureBoxDumpTruck.java index 8a94cc1..eea5686 100644 --- a/src/DumpTruck/PictureBoxDumpTruck.java +++ b/src/DumpTruck/PictureBoxDumpTruck.java @@ -26,16 +26,21 @@ public class PictureBoxDumpTruck extends JPanel { private JButton buttonStep; public JButton buttonSelectTruck; - public PictureBoxDumpTruck() { + public PictureBoxDumpTruck(DrawingTruck truck) { + Random random = new Random(); setLayout(null); setBounds(0, 0, 800, 450); buttonCreateTruck = new JButton("Создать грузовик"); buttonCreateTruck.setFocusable(false); buttonCreateTruck.setBounds(12, 415, 150, 30); add(buttonCreateTruck); + if (truck != null){ + drawingTruck = truck; + drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + repaint(); + } buttonCreateTruck.addActionListener(e -> { - Random random = new Random(); Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE); if (selectedColor != null) @@ -56,7 +61,6 @@ public class PictureBoxDumpTruck extends JPanel { add(buttonCreateDumpTruck); buttonCreateDumpTruck.addActionListener(e -> { - Random random = new Random(); Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); Color selectedColor = JColorChooser.showDialog(this, "Выберите цвет", Color.WHITE); if (selectedColor != null)