From ceee7cf0284b3ce2d5b90bcf9c6a64ad34401ff5 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Sun, 26 Nov 2023 22:24:00 +0300 Subject: [PATCH 1/2] Done --- src/FormAirFighter.java | 5 + src/FormAirFighterCollection.java | 101 +++++++++++++++++--- src/Generics/AirplaneGenericCollection.java | 15 ++- src/Generics/AirplaneGenericStorage.java | 49 ++++++++++ src/Generics/AirplaneTrashCollection.java | 32 +++++++ src/Generics/SetGeneric.java | 63 ++++++------ 6 files changed, 217 insertions(+), 48 deletions(-) create mode 100644 src/Generics/AirplaneGenericStorage.java create mode 100644 src/Generics/AirplaneTrashCollection.java diff --git a/src/FormAirFighter.java b/src/FormAirFighter.java index f2f70b6..d9b068c 100644 --- a/src/FormAirFighter.java +++ b/src/FormAirFighter.java @@ -35,7 +35,12 @@ public class FormAirFighter { return; canv.repaint(); } + public void ChangeAirplane(DrawningAirplane newAirplane){ + newAirplane.SetPosition(0,0); + DrawningAirplane = newAirplane; + canv.DrawningAirplane = DrawningAirplane; + } public FormAirFighter(){ AirplaneFrame =new JFrame (); JButton buttonCreate = new JButton("Создать"); diff --git a/src/FormAirFighterCollection.java b/src/FormAirFighterCollection.java index 2413eb8..4d27689 100644 --- a/src/FormAirFighterCollection.java +++ b/src/FormAirFighterCollection.java @@ -3,16 +3,21 @@ package src; import src.DrawningObjects.DrawningAirplane; import src.Generics.AirplaneGenericCollection; import src.FormAirFighter; +import src.Generics.AirplaneGenericStorage; +import src.Generics.AirplaneTrashCollection; import src.MovementStrategy.DrawningObjectAirplane; - +import src.MovementStrategy.IMoveableObject; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; - +import java.util.List; public class FormAirFighterCollection { - private final AirplaneGenericCollection _airplanes; + private final AirplaneGenericStorage _storage; + + private JList listBoxStorages; + private DefaultListModel listBoxModel; private int pictureBoxWidth = 902; private int pictureBoxHeight = 453; @@ -23,11 +28,32 @@ public class FormAirFighterCollection { return; canv.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 FormAirFighterCollection(){ - _airplanes = new AirplaneGenericCollection<>(pictureBoxWidth, pictureBoxHeight); + AirplaneTrashCollection _trashCollection = new AirplaneTrashCollection<>(); + JButton callTrashButton = new JButton("мусор"); + _storage = new AirplaneGenericStorage(pictureBoxWidth, pictureBoxHeight); + JScrollPane scrollPane = new JScrollPane(); canv = 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); @@ -37,27 +63,67 @@ public class FormAirFighterCollection { JButton removeButton = new JButton("Удалить"); JButton refreshButton = new JButton("Обновить"); JTextField airplaneNumb = 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(airplaneNumb); toolBox.add(removeButton); toolBox.add(refreshButton); + toolBox.add(callTrashButton); collectionFrame.add(toolBox); collectionFrame.add(canv); collectionFrame.setVisible(true); + canv._storage = _storage; + canv.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; + FormAirFighter form = new FormAirFighter(); + form.ChangeAirplane(_trashCollection.GetTop()); + _trashCollection.Pop(); + } + }); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if(_airplanes == null) { + if(listBoxStorages.getSelectedIndex() == -1) { return; } + AirplaneGenericCollection _airplanes = _storage.Get(listBoxStorages.getSelectedValue()); + FormAirFighter form = new FormAirFighter(); form.buttonSelect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_airplanes.Insert(form.SelectedAirplane()) != -1) + if (_airplanes.Insert(form.SelectedAirplane())) { JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); form.SelectedAirplane()._pictureWidth = pictureBoxWidth; @@ -68,7 +134,7 @@ public class FormAirFighterCollection { { JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); } - canv._airplanes = _airplanes; + form.AirplaneFrame.dispose(); Draw(); } @@ -78,6 +144,10 @@ public class FormAirFighterCollection { removeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + if(listBoxStorages.getSelectedIndex() == -1) { + return; + } + AirplaneGenericCollection _airplanes = _storage.Get(listBoxStorages.getSelectedValue()); if(_airplanes == null) { return; } @@ -91,6 +161,8 @@ public class FormAirFighterCollection { JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE); return; } + DrawningAirplane curAirplane = _airplanes.Get(numb); + _trashCollection.Push(curAirplane); _airplanes.Remove(numb); _airplanes.ShowAirplanes(); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); @@ -101,6 +173,10 @@ public class FormAirFighterCollection { refreshButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + if(listBoxStorages.getSelectedIndex() == -1) { + return; + } + AirplaneGenericCollection _airplanes = _storage.Get(listBoxStorages.getSelectedValue()); if(_airplanes == null) { return; } @@ -113,18 +189,19 @@ public class FormAirFighterCollection { } class CollectionCanvas extends JComponent { - public AirplaneGenericCollection _airplanes; + public AirplaneGenericStorage _storage; + public JList listBoxStorages; public CollectionCanvas(){ } @Override public void paintComponent (Graphics g){ - if (_airplanes == null){ + if (listBoxStorages == null || listBoxStorages.getSelectedIndex() == -1){ return; } super.paintComponents (g) ; Graphics2D g2d = (Graphics2D)g; - g2d.drawImage(_airplanes.ShowAirplanes(), 0, 0, this); + g2d.drawImage(_storage.Get(listBoxStorages.getSelectedValue()).ShowAirplanes(), 0, 0, this); super.repaint(); } } diff --git a/src/Generics/AirplaneGenericCollection.java b/src/Generics/AirplaneGenericCollection.java index 5f7754b..3b18e2a 100644 --- a/src/Generics/AirplaneGenericCollection.java +++ b/src/Generics/AirplaneGenericCollection.java @@ -25,10 +25,13 @@ public class AirplaneGenericCollection(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); } @@ -42,7 +45,11 @@ public class AirplaneGenericCollection= _collection.Count) + return null; + return _collection.Get(position); + } private void DrawBackground(Graphics g) { g.setColor(Color.BLACK); @@ -68,7 +75,7 @@ public class AirplaneGenericCollection> _airplaneStorages; + public List Keys(){ + if(_airplaneStorages == null) + return null; + return _airplaneStorages.keySet().stream().collect(Collectors.toList()); + } + private final int _pictureWidth; + private final int _pictureHeight; + + public AirplaneGenericStorage(int pictureWidth, int pictureHeight){ + _airplaneStorages = new HashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(String name){ + if(_airplaneStorages.containsKey(name)) + return; + _airplaneStorages.put(name, new AirplaneGenericCollection<>(_pictureWidth, _pictureHeight)); + } + + public void DelSet(String name){ + if(!_airplaneStorages.containsKey(name)) + return; + _airplaneStorages.remove(name); + } + + public AirplaneGenericCollection Get(String name){ + if(!_airplaneStorages.containsKey(name)) + return null; + return _airplaneStorages.get(name); + } + + public DrawningAirplane Get(String collectionName, int position){ + return _airplaneStorages.get(collectionName).Get(position); + } +} diff --git a/src/Generics/AirplaneTrashCollection.java b/src/Generics/AirplaneTrashCollection.java new file mode 100644 index 0000000..37374a9 --- /dev/null +++ b/src/Generics/AirplaneTrashCollection.java @@ -0,0 +1,32 @@ +package src.Generics; + +import src.DrawningObjects.DrawningAirplane; + +import java.util.ArrayDeque; +import java.util.Queue; + +public class AirplaneTrashCollection { + Queue _queue; + + public AirplaneTrashCollection(){ + _queue = new ArrayDeque<>(); + } + + public void Push(T airplane){ + _queue.add(airplane); + } + + public int GetSize(){ + return _queue.size(); + } + + public void Pop(){ + if(_queue.size() ==0) + return; + _queue.remove(); + } + + public T GetTop(){ + return _queue.peek(); + } +} diff --git a/src/Generics/SetGeneric.java b/src/Generics/SetGeneric.java index 9607aec..3dbbc18 100644 --- a/src/Generics/SetGeneric.java +++ b/src/Generics/SetGeneric.java @@ -1,53 +1,52 @@ package src.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 airplane){ - return Insert(airplane, 0); + public boolean Insert(T monorail){ + if(_places.size() == _maxCount) + return false; + Insert(monorail, 0); + return true; } - public int Insert(T airplane, int position){ - if(!(position >= 0 && position < Count)) - return -1; - if(_places[position] == null){ - _places[position] = airplane; - } - 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] = airplane; - } - return position; + public boolean Insert(T monorail, int position){ + if (!(position >= 0 && position <= _places.size() && _places.size() < _maxCount)) + return false; + _places.add(position, monorail); + 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; } public T Get(int position){ if(!(position >= 0 && position < Count)) return null; - return (T)_places[position]; + return (T)_places.get(position); + } + + public ArrayList GetMonorails(int maxMonorails){ + ArrayList toRet = new ArrayList<>(); + for(int i = 0; i < _places.size(); i++){ + toRet.add(_places.get(i)); + if(i == maxMonorails) + return toRet; + } + return toRet; } } -- 2.25.1 From 807990537b843754aedd30e478c4a4382922a906 Mon Sep 17 00:00:00 2001 From: goblinrf Date: Wed, 29 Nov 2023 15:32:24 +0300 Subject: [PATCH 2/2] Done --- src/FormAirFighter.java | 2 +- src/FormAirFighterCollection.java | 4 ++-- src/Generics/AirplaneTrashCollection.java | 10 ++-------- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/FormAirFighter.java b/src/FormAirFighter.java index d9b068c..a9e340e 100644 --- a/src/FormAirFighter.java +++ b/src/FormAirFighter.java @@ -36,7 +36,7 @@ public class FormAirFighter { canv.repaint(); } public void ChangeAirplane(DrawningAirplane newAirplane){ - newAirplane.SetPosition(0,0); + newAirplane.SetPosition(0,70); DrawningAirplane = newAirplane; canv.DrawningAirplane = DrawningAirplane; diff --git a/src/FormAirFighterCollection.java b/src/FormAirFighterCollection.java index 4d27689..d943257 100644 --- a/src/FormAirFighterCollection.java +++ b/src/FormAirFighterCollection.java @@ -107,8 +107,8 @@ public class FormAirFighterCollection { if(_trashCollection.GetSize() == 0) return; FormAirFighter form = new FormAirFighter(); - form.ChangeAirplane(_trashCollection.GetTop()); - _trashCollection.Pop(); + form.ChangeAirplane(_trashCollection.Pop()); + } }); addButton.addActionListener(new ActionListener() { diff --git a/src/Generics/AirplaneTrashCollection.java b/src/Generics/AirplaneTrashCollection.java index 37374a9..e0bcf44 100644 --- a/src/Generics/AirplaneTrashCollection.java +++ b/src/Generics/AirplaneTrashCollection.java @@ -20,13 +20,7 @@ public class AirplaneTrashCollection { return _queue.size(); } - public void Pop(){ - if(_queue.size() ==0) - return; - _queue.remove(); - } - - public T GetTop(){ - return _queue.peek(); + public T Pop(){ + return _queue.remove(); } } -- 2.25.1