From b98cc07a1659a87c5a125293c971e9decc617db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Tue, 14 Nov 2023 15:00:54 +0400 Subject: [PATCH] done --- src/DoubleDeckerBus/CollectionCanvas.java | 11 +- src/DoubleDeckerBus/FormBusCollection.java | 101 ++++++++++++++++-- src/DoubleDeckerBus/FormDoubleDeckerBus.java | 6 ++ .../Generics/BusGenericStorage.java | 51 +++++++++ .../Generics/BusTrashCollection.java | 35 ++++++ src/DoubleDeckerBus/Generics/SetGeneric.java | 70 ++++++------ .../Generics/TheBusesGenericCollection.java | 21 +++- 7 files changed, 240 insertions(+), 55 deletions(-) create mode 100644 src/DoubleDeckerBus/Generics/BusGenericStorage.java create mode 100644 src/DoubleDeckerBus/Generics/BusTrashCollection.java diff --git a/src/DoubleDeckerBus/CollectionCanvas.java b/src/DoubleDeckerBus/CollectionCanvas.java index afd7948..ab50532 100644 --- a/src/DoubleDeckerBus/CollectionCanvas.java +++ b/src/DoubleDeckerBus/CollectionCanvas.java @@ -1,23 +1,22 @@ package DoubleDeckerBus; -import DoubleDeckerBus.DrawningObjects.DrawningBus; -import DoubleDeckerBus.Generics.TheBusesGenericCollection; -import DoubleDeckerBus.MovementStrategy.DrawningObjectBus; +import DoubleDeckerBus.Generics.BusGenericStorage; import javax.swing.*; import java.awt.*; public class CollectionCanvas extends JComponent { - public TheBusesGenericCollection _bus; + public BusGenericStorage _storage; + public JList listBoxStorages; @Override public void paintComponent (Graphics g){ - if (_bus == null) { + if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1) { return; } super.paintComponents(g); Graphics2D g2d = (Graphics2D)g; - g2d.drawImage(_bus.ShowTheBuses(), 0, 0, this); + g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowTheBuses(), 0, 0, this); super.repaint(); } } diff --git a/src/DoubleDeckerBus/FormBusCollection.java b/src/DoubleDeckerBus/FormBusCollection.java index c805d99..ede8d42 100644 --- a/src/DoubleDeckerBus/FormBusCollection.java +++ b/src/DoubleDeckerBus/FormBusCollection.java @@ -1,15 +1,21 @@ package DoubleDeckerBus; import DoubleDeckerBus.DrawningObjects.DrawningBus; +import DoubleDeckerBus.Generics.BusGenericStorage; +import DoubleDeckerBus.Generics.BusTrashCollection; import DoubleDeckerBus.Generics.TheBusesGenericCollection; import DoubleDeckerBus.MovementStrategy.DrawningObjectBus; - +import java.util.List; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class FormBusCollection { - private final TheBusesGenericCollection _bus; + private final BusGenericStorage _storage; + + private JList listBoxStorages; + private DefaultListModel listBoxModel; + private int pictureBoxWidth = 605; private int pictureBoxHeight = 426; @@ -22,10 +28,34 @@ public class FormBusCollection { canvas.repaint(); } + private void ReloadObjects() { + int index = listBoxStorages.getSelectedIndex(); + listBoxModel.clear(); + List keys = _storage.Keys(); + for (int i = 0; i < keys.size(); i++) { + listBoxModel.addElement(keys.get(i)); + } + if (listBoxModel.size() > 0 && (index == -1 || index >= listBoxModel.size())) { + listBoxStorages.setSelectedIndex(0); + } else if(listBoxModel.size() > 0) { + listBoxStorages.setSelectedIndex(index); + } + } + public FormBusCollection() { - _bus = new TheBusesGenericCollection<>(pictureBoxWidth, pictureBoxHeight); + BusTrashCollection _trashCollection = new BusTrashCollection<>(); + JButton callTrashButton = new JButton("мусор"); + _storage = new BusGenericStorage(pictureBoxWidth, pictureBoxHeight); + JScrollPane scrollPane = new JScrollPane(); canvas = new CollectionCanvas(); JPanel toolBox = new JPanel(); + JTextField storageName = new JTextField(); + JButton addStorageButton = new JButton("Добавить набор"); + listBoxModel = new DefaultListModel<>(); + listBoxStorages= new JList<>(listBoxModel); + scrollPane.setViewportView(listBoxStorages); + JButton delStorageButton = new JButton("Удалить набор"); + toolBox.setBounds(623,12, 227, 80); JFrame collectionFrame = new JFrame(); collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); collectionFrame.setSize(880,497); @@ -35,27 +65,70 @@ public class FormBusCollection { JButton removeButton = new JButton("Удалить"); JButton refreshButton = new JButton("Обновить"); JTextField busNumb = new JTextField(); - GridLayout lay = new GridLayout(4,1); + GridLayout lay = new GridLayout(9,1); + toolBox.add(storageName); + toolBox.add(addStorageButton); + toolBox.add(scrollPane); + toolBox.add(delStorageButton); toolBox.setLayout(lay); toolBox.add(addButton); toolBox.add(busNumb); toolBox.add(removeButton); toolBox.add(refreshButton); + toolBox.add(callTrashButton); collectionFrame.add(toolBox); collectionFrame.add(canvas); collectionFrame.setVisible(true); + canvas._storage = _storage; + canvas.listBoxStorages = listBoxStorages; + + addStorageButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (storageName.getText() == null) { + return; + } + _storage.AddSet(storageName.getText()); + ReloadObjects(); + } + }); + + delStorageButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (listBoxStorages.getSelectedIndex() == -1) { + return; + } + _storage.DelSet(listBoxStorages.getSelectedValue()); + ReloadObjects(); + } + }); + + callTrashButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_trashCollection.GetSize() == 0) { + return; + } + FormDoubleDeckerBus form = new FormDoubleDeckerBus(); + form.ChangeBus(_trashCollection.GetTop()); + _trashCollection.Pop(); + } + }); + addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_bus == null) { + if (listBoxStorages.getSelectedIndex() == -1) { return; } + TheBusesGenericCollection _bus = _storage.Get(listBoxStorages.getSelectedValue()); FormDoubleDeckerBus form = new FormDoubleDeckerBus(); form.buttonSelect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_bus.Insert(form.SelectedBus()) != -1) + if (_bus.Insert(form.SelectedBus())) { JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); form.SelectedBus()._pictureWidth = pictureBoxWidth; @@ -66,17 +139,21 @@ public class FormBusCollection { { JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); } - canvas._bus = _bus; form.BusFrame.dispose(); Draw(); } }); } }); + removeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_bus == null) { + if (listBoxStorages.getSelectedIndex() == -1) { + return; + } + TheBusesGenericCollection _bus = _storage.Get(listBoxStorages.getSelectedValue()); + if (_bus == null) { return; } String tmp = busNumb.getText(); @@ -88,6 +165,8 @@ public class FormBusCollection { JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE); return; } + DrawningBus curBus = _bus.Get(numb); + _trashCollection.Push(curBus); _bus.Remove(numb); _bus.ShowTheBuses(); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); @@ -98,7 +177,11 @@ public class FormBusCollection { refreshButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_bus == null) { + if (listBoxStorages.getSelectedIndex() == -1) { + return; + } + TheBusesGenericCollection _bus = _storage.Get(listBoxStorages.getSelectedValue()); + if (_bus == null) { return; } _bus.ShowTheBuses(); diff --git a/src/DoubleDeckerBus/FormDoubleDeckerBus.java b/src/DoubleDeckerBus/FormDoubleDeckerBus.java index 08bcac1..1e146c0 100644 --- a/src/DoubleDeckerBus/FormDoubleDeckerBus.java +++ b/src/DoubleDeckerBus/FormDoubleDeckerBus.java @@ -43,6 +43,12 @@ public class FormDoubleDeckerBus { canvas.repaint(); } + public void ChangeBus(DrawningBus newBus){ + newBus.SetPosition(0,0); + DrawningBus = newBus; + canvas.DrawningBus = DrawningBus; + } + public FormDoubleDeckerBus() { BusFrame = new JFrame(); JButton buttonCreate = new JButton("Создать"); diff --git a/src/DoubleDeckerBus/Generics/BusGenericStorage.java b/src/DoubleDeckerBus/Generics/BusGenericStorage.java new file mode 100644 index 0000000..247e34f --- /dev/null +++ b/src/DoubleDeckerBus/Generics/BusGenericStorage.java @@ -0,0 +1,51 @@ +package DoubleDeckerBus.Generics; + +import DoubleDeckerBus.DrawningObjects.DrawningBus; +import DoubleDeckerBus.MovementStrategy.DrawningObjectBus; + +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + +public class BusGenericStorage { + final HashMap> _busStorages; + public List Keys() { + if (_busStorages == null) { + return null; + } + return _busStorages.keySet().stream().collect(Collectors.toList()); + } + private final int _pictureWidth; + private final int _pictureHeight; + + public BusGenericStorage(int pictureWidth, int pictureHeight){ + _busStorages = new HashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(String name){ + if (_busStorages.containsKey(name)) { + return; + } + _busStorages.put(name, new TheBusesGenericCollection<>(_pictureWidth, _pictureHeight)); + } + + public void DelSet(String name) { + if (!_busStorages.containsKey(name)) { + return; + } + _busStorages.remove(name); + } + + public TheBusesGenericCollection Get(String name){ + if (!_busStorages.containsKey(name)) { + return null; + } + return _busStorages.get(name); + } + + public DrawningBus Get(String collectionName, int position) { + return _busStorages.get(collectionName).Get(position); + } +} diff --git a/src/DoubleDeckerBus/Generics/BusTrashCollection.java b/src/DoubleDeckerBus/Generics/BusTrashCollection.java new file mode 100644 index 0000000..f9bda33 --- /dev/null +++ b/src/DoubleDeckerBus/Generics/BusTrashCollection.java @@ -0,0 +1,35 @@ +package DoubleDeckerBus.Generics; + +import DoubleDeckerBus.DrawningObjects.DrawningBus; + +import java.util.Stack; + +public class BusTrashCollection { + Stack _stack; + + public BusTrashCollection(){ + _stack = new Stack<>(); + } + + public void Push(T bus){ + _stack.push(bus); + } + + public int GetSize(){ + return _stack.size(); + } + + public void Pop(){ + if (_stack.size() == 0) { + return; + } + _stack.pop(); + } + + public T GetTop() { + if (_stack.isEmpty()) { + return null; + } + return _stack.peek(); + } +} \ No newline at end of file diff --git a/src/DoubleDeckerBus/Generics/SetGeneric.java b/src/DoubleDeckerBus/Generics/SetGeneric.java index ef887ee..8160c76 100644 --- a/src/DoubleDeckerBus/Generics/SetGeneric.java +++ b/src/DoubleDeckerBus/Generics/SetGeneric.java @@ -1,53 +1,42 @@ package DoubleDeckerBus.Generics; +import java.util.ArrayList; +import java.util.List; + public class SetGeneric{ - private final Object[] _places; + private final List _places; public int Count; + private final int _maxCount; public SetGeneric(int count){ - _places = new Object[count]; - Count = count; + _maxCount = count; + _places = new ArrayList<>(); } - public int Insert(T bus){ - return Insert(bus, 0); + public boolean Insert(T bus){ + if (_places.size() == _maxCount) { + return false; + } + Insert(bus, 0); + return true; } - public int Insert(T bus, int position){ - if (!(position >= 0 && position < Count)) { - return -1; + public boolean Insert(T bus, int position){ + if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount)) { + return false; } - - if (_places[position] == null){ - _places[position] = bus; - } else { - int place = -1; - - for (int i = position; i < Count; i++) { - if (_places[i] == null) { - place = i; - break; - } - } - - if (place == -1) { - return -1; - } - - for (int i = place - 1; i >= position; i--) { - _places[i + 1] = _places[i]; - } - _places[position] = bus; - } - return position; + _places.add(position, bus); + Count++; + return true; } public boolean Remove(int position){ - if (!(position >= 0 && position < Count)) { + if (!(position >= 0 && position < _places.size())) { return false; } - _places[position] = null; + _places.remove(position); + Count--; return true; } @@ -55,6 +44,17 @@ public class SetGeneric{ if (!(position >= 0 && position < Count)) { return null; } - return (T)_places[position]; + return (T)_places.get(position); } -} + + public ArrayList GetBus(int maxBus){ + ArrayList toRet = new ArrayList<>(); + for (int i = 0; i < _places.size(); i++) { + toRet.add(_places.get(i)); + if (i == maxBus) { + return toRet; + } + } + return toRet; + } +} \ No newline at end of file diff --git a/src/DoubleDeckerBus/Generics/TheBusesGenericCollection.java b/src/DoubleDeckerBus/Generics/TheBusesGenericCollection.java index 03da8ee..32f2b15 100644 --- a/src/DoubleDeckerBus/Generics/TheBusesGenericCollection.java +++ b/src/DoubleDeckerBus/Generics/TheBusesGenericCollection.java @@ -25,9 +25,13 @@ public class TheBusesGenericCollection(width * height); } - public int Insert(T obj){ + public int Size() { + return _collection.Count; + } + + public boolean Insert(T obj) { if (obj == null) { - return -1; + return false; } return _collection.Insert(obj); } @@ -36,7 +40,7 @@ public class TheBusesGenericCollection= _collection.Count) { + return null; + } + return _collection.Get(position); + } + public BufferedImage ShowTheBuses() { BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR); Graphics gr = bmp.createGraphics(); @@ -57,10 +68,10 @@ public class TheBusesGenericCollection