diff --git a/src/FormAirFighter.java b/src/FormAirFighter.java index f2f70b6..a9e340e 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,70); + 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..d943257 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.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..e0bcf44 --- /dev/null +++ b/src/Generics/AirplaneTrashCollection.java @@ -0,0 +1,26 @@ +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 T Pop(){ + return _queue.remove(); + } +} 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; } }