From b5fd936a7cf587a7a13ae8bba9556940921070f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Sat, 17 Dec 2022 13:01:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=2004?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RadarAirplane/src/DrawingObjectPlain.java | 3 + RadarAirplane/src/FormAirPlane.java | 5 +- RadarAirplane/src/MapWithSetPlane.java | 19 +-- RadarAirplane/src/MapsCollection.java | 39 +++++ RadarAirplane/src/Program.java | 2 +- RadarAirplane/src/SetPlaneGeneric.java | 40 ++--- RadarAirplane/src/formMapWithSetPlane.form | 71 +++++++-- RadarAirplane/src/formMapWithSetPlane.java | 165 ++++++++++++++------- 8 files changed, 239 insertions(+), 105 deletions(-) create mode 100644 RadarAirplane/src/MapsCollection.java diff --git a/RadarAirplane/src/DrawingObjectPlain.java b/RadarAirplane/src/DrawingObjectPlain.java index f481706..1e1dd4b 100644 --- a/RadarAirplane/src/DrawingObjectPlain.java +++ b/RadarAirplane/src/DrawingObjectPlain.java @@ -6,6 +6,9 @@ public class DrawingObjectPlain implements IDrawingObject public DrawingObjectPlain(DrawingEntityPlain aircraft){ _airplain = aircraft; } + public DrawingEntityPlain getAirPlane() { + return _airplain; + } public void MoveObject(Direction direction) { if(_airplain == null) return; diff --git a/RadarAirplane/src/FormAirPlane.java b/RadarAirplane/src/FormAirPlane.java index ef85972..c1d2d80 100644 --- a/RadarAirplane/src/FormAirPlane.java +++ b/RadarAirplane/src/FormAirPlane.java @@ -19,8 +19,11 @@ public class FormAirPlane extends JDialog implements Form { private JButton selectButton; DrawingEntityPlain _airPlane; private DrawingEntityPlain selectedPlane; + public FormAirPlane() {} - + public FormAirPlane(DrawingEntityPlain aircraft) { + _airPlane = aircraft; + } public DrawingEntityPlain getSelectedPlane() { return selectedPlane; } diff --git a/RadarAirplane/src/MapWithSetPlane.java b/RadarAirplane/src/MapWithSetPlane.java index 658b32d..77cedad 100644 --- a/RadarAirplane/src/MapWithSetPlane.java +++ b/RadarAirplane/src/MapWithSetPlane.java @@ -163,20 +163,7 @@ public class MapWithSetPlane } } - - - /*int width = _pictureWidth / _placeSizeWidth; - - for (int i = 0; i < _setPlane.getCount(); i++) - { - int x = i % width; - int y = i / width; - - T current =_setPlane.Get(i); - - if(current == null) continue; - - current.SetObject(x * _placeSizeWidth, y * _placeSizeHeight, _pictureWidth, _pictureHeight); - current.DrawningObject(g); - }*/ + public T getPlane(int pos){ + return _setPlane.Get(pos); + } } \ No newline at end of file diff --git a/RadarAirplane/src/MapsCollection.java b/RadarAirplane/src/MapsCollection.java new file mode 100644 index 0000000..6a34424 --- /dev/null +++ b/RadarAirplane/src/MapsCollection.java @@ -0,0 +1,39 @@ +import java.util.LinkedHashMap; +import java.util.List; + +public class MapsCollection +{ + public LinkedHashMap> _mapStorages; + private int _pictureWidth; + private int _pictureHeight; + + public List getKeys() { return _mapStorages.keySet().stream().toList(); } + + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new LinkedHashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public DrawingObjectPlain Indexator (String key, int pos){ + return _mapStorages.get(key).getPlane(pos); + } + public void AddMap(String name, AbstractMap map) + { + boolean check = _mapStorages.containsKey(name); + if (check) return; + + _mapStorages.put(name, new MapWithSetPlane(_pictureWidth, _pictureHeight, map)); + } + + public void DelMap(String name) + { + _mapStorages.remove(name); + } + + public MapWithSetPlane getMap(String name) + { + return _mapStorages.getOrDefault(name, null); + } +} \ No newline at end of file diff --git a/RadarAirplane/src/Program.java b/RadarAirplane/src/Program.java index a74f0dd..df66758 100644 --- a/RadarAirplane/src/Program.java +++ b/RadarAirplane/src/Program.java @@ -3,6 +3,6 @@ import java.awt.*; public class Program { public static void main(String[] args) { - new formPlaneGenerator().run(); + new formMapWithSetPlane().run(); } } \ No newline at end of file diff --git a/RadarAirplane/src/SetPlaneGeneric.java b/RadarAirplane/src/SetPlaneGeneric.java index 7037d1d..875aeae 100644 --- a/RadarAirplane/src/SetPlaneGeneric.java +++ b/RadarAirplane/src/SetPlaneGeneric.java @@ -1,50 +1,40 @@ +import java.util.ArrayList; public class SetPlaneGeneric { - private T[] _places; - + private ArrayList _places; + private int _maxCount; public int getCount() { - return _places.length; + return _places.size(); } public SetPlaneGeneric(int count) { - _places = (T[])(new Object[count]); + _places = new ArrayList<>(); + _maxCount = count; } public int Insert(T plane) { - for(int i = 0; i < _places.length; i++) - { - if (_places[i] == null) - { - _places[i] = plane; - return i; - } - } - return -1; + if (_places.size() == _maxCount) return -1; + _places.add(0, plane); + return 0; } public int Insert(T plane, int position) { - int index = position; - - while (_places[index] != null && index < _places.length) index++; - - if (index == _places.length) return -1; - for (int i = index; i > position; --i) _places[i] = _places[i - 1]; - - _places[position] = plane; + if (_places.size() == _maxCount) return -1; + _places.add(position, plane); return position; } public T Remove(int position) { - if(position >= _places.length) return null; - T res = _places[position]; - _places[position] = null; + if(position > _maxCount || position < 0) return null; + T res = _places.get(position); + _places.remove(res); return res; } public T Get(int position) { - return _places[position]; + return _places.get(position); } } \ No newline at end of file diff --git a/RadarAirplane/src/formMapWithSetPlane.form b/RadarAirplane/src/formMapWithSetPlane.form index f4a798b..bc8c627 100644 --- a/RadarAirplane/src/formMapWithSetPlane.form +++ b/RadarAirplane/src/formMapWithSetPlane.form @@ -3,7 +3,7 @@ - + @@ -16,7 +16,7 @@ - + @@ -26,9 +26,9 @@ - + - + @@ -36,7 +36,7 @@ - + @@ -45,9 +45,9 @@ - + - + @@ -55,7 +55,7 @@ - + @@ -63,7 +63,7 @@ - + @@ -72,7 +72,7 @@ - + @@ -145,17 +145,64 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/RadarAirplane/src/formMapWithSetPlane.java b/RadarAirplane/src/formMapWithSetPlane.java index bb5fede..c79e721 100644 --- a/RadarAirplane/src/formMapWithSetPlane.java +++ b/RadarAirplane/src/formMapWithSetPlane.java @@ -1,16 +1,14 @@ import javax.swing.*; -import javax.swing.text.DefaultFormatterFactory; -import javax.swing.text.MaskFormatter; import java.awt.*; -import java.text.ParseException; -import java.util.Objects; +import java.util.*; +import java.util.List; public class formMapWithSetPlane implements Form { private JPanel MainPane; - private JButton buttonAdd; + private JButton buttonAddPlane; private JComboBox comboBoxSelectorMap; private JTextField textBoxPosition; - private JButton buttonRemove; + private JButton buttonRemovePlane; private JButton buttonShowStorage; private JButton buttonShowOnMap; private JButton upButton; @@ -18,10 +16,19 @@ public class formMapWithSetPlane implements Form { private JButton rightButton; private JButton downButton; private JPanel drawPanel; + private JTextField textBoxNewMapName; + private JList listBoxMaps; + private JButton buttonAddMap; + private JButton buttonDeleteMap; + private JButton buttonShowDeleted; - - private MapWithSetPlane _mapCarsCollectionGeneric; + private MapsCollection _mapsCollection; + private HashMap _mapsDict = new HashMap<>(){{ + put( "Простая карта", new SimpleMap() ); + put( "Моя карта", new MyMap() ); + }}; private Canvas canv = new Canvas(this); + private Queue deletedPlanes = new ArrayDeque<>(); JFrame jFrame = getFrame(); Image img; @@ -29,43 +36,83 @@ public class formMapWithSetPlane implements Form { private JFrame getFrame() { JFrame frame = new JFrame(); frame.setVisible(true); - frame.setBounds(300, 100, 800, 600); + frame.setBounds(300, 50, 1000, 750); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); return frame; } + private void ReloadMaps() + { + int index = listBoxMaps.getSelectedIndex(); + List items = _mapsCollection.getKeys(); + listBoxMaps.setListData(items.toArray()); + if (items.size() > 0 && (index == -1 || index >= items.size())) + { + listBoxMaps.setSelectedIndex(0); + } + else if (items.size() > 0 && index > -1 && index < items.size()) + { + listBoxMaps.setSelectedIndex(index); + } + } public void run() { jFrame.add(MainPane); drawPanel.add(canv); + jFrame.revalidate(); + + _mapsCollection = new MapsCollection(canv.getSize().width, canv.getSize().height); + comboBoxSelectorMap.removeAllItems(); + + _mapsDict.keySet().forEach(elem -> { + comboBoxSelectorMap.addItem(elem); + }); + comboBoxSelectorMap.setSelectedIndex(-1); - comboBoxSelectorMap.addActionListener(e -> { - AbstractMap map = null; - switch (comboBoxSelectorMap.getSelectedItem().toString()) + listBoxMaps.addListSelectionListener(e -> { + if(listBoxMaps.getSelectedValue() == null) return; + img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet(); + canv.repaint(); + }); + + buttonAddMap.addActionListener(e -> { + if (comboBoxSelectorMap.getSelectedIndex() == -1 || textBoxNewMapName.getText() == "") { - case "Простая карта": - map = new SimpleMap(); - break; - case "Моя карта": - map = new MyMap(); - break; + JOptionPane.showMessageDialog(jFrame, "Не все данные заполнены"); + return; } - if (map != null) + if (!_mapsDict.containsKey(comboBoxSelectorMap.getSelectedItem().toString())) { - Dimension canvSize = canv.getSize(); - _mapCarsCollectionGeneric = new MapWithSetPlane( - canvSize.width, canvSize.height, map); + JOptionPane.showMessageDialog(jFrame, "Нет такой карты"); + return; } - else + _mapsCollection.AddMap(textBoxNewMapName.getText(), + _mapsDict.get(comboBoxSelectorMap.getSelectedItem().toString())); + ReloadMaps(); + }); + + buttonDeleteMap.addActionListener(e -> { + if (listBoxMaps.getSelectedIndex() == -1) { - _mapCarsCollectionGeneric = null; + return; + } + + String mapName = listBoxMaps.getSelectedValue().toString(); + if (JOptionPane.showConfirmDialog(jFrame, "Удалить карту " + mapName, + "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) + { + _mapsCollection.DelMap(mapName); + ReloadMaps(); } }); - buttonAdd.addActionListener(e -> { - if (_mapCarsCollectionGeneric == null) return; + buttonAddPlane.addActionListener(e -> { + if (listBoxMaps.getSelectedIndex() == -1) + { + return; + } FormAirPlane dialog = new FormAirPlane(); dialog.run(); @@ -78,10 +125,10 @@ public class formMapWithSetPlane implements Form { if (dialog.getSelectedPlane() == null) return; DrawingObjectPlain aircraft = new DrawingObjectPlain(dialog.getSelectedPlane()); - if (_mapCarsCollectionGeneric.addAirPlane(aircraft) != -1) + if (_mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).addAirPlane(aircraft) != -1) { JOptionPane.showMessageDialog(jFrame, "Объект добавлен"); - img = _mapCarsCollectionGeneric.ShowSet(); + img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet(); canv.repaint(); } else @@ -90,9 +137,7 @@ public class formMapWithSetPlane implements Form { } }); - buttonRemove.addActionListener(e -> { - if(_mapCarsCollectionGeneric == null) return; - + buttonRemovePlane.addActionListener(e -> { String text = textBoxPosition.getText(); if(text.isEmpty()) return; @@ -110,9 +155,13 @@ public class formMapWithSetPlane implements Form { } pos = Integer.parseInt(text); - if(_mapCarsCollectionGeneric.removeAirPlane(pos) != null) { + String mapName = listBoxMaps.getSelectedValue().toString(); + IDrawingObject deleted = _mapsCollection.getMap(mapName).removeAirPlane(pos); + + if(deleted != null) { JOptionPane.showMessageDialog(jFrame, "Объект удален"); - img = _mapCarsCollectionGeneric.ShowSet(); + img = _mapsCollection.getMap(mapName).ShowSet(); + deletedPlanes.add(deleted); canv.repaint(); } else { JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект"); @@ -120,47 +169,63 @@ public class formMapWithSetPlane implements Form { }); buttonShowStorage.addActionListener(e -> { - if (_mapCarsCollectionGeneric == null) - { - return; - } + if(listBoxMaps.getSelectedValue() == null) return; - img = _mapCarsCollectionGeneric.ShowSet(); + img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet(); canv.repaint(); }); buttonShowOnMap.addActionListener(e -> { - if (_mapCarsCollectionGeneric == null) + if(listBoxMaps.getSelectedValue() == null) return; + + img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowOnMap(); + canv.repaint(); + }); + + buttonShowDeleted.addActionListener(e -> { + if (listBoxMaps.getSelectedIndex() == -1) { return; } - img = _mapCarsCollectionGeneric.ShowOnMap(); - canv.repaint(); + if(deletedPlanes.size() == 0) { + JOptionPane.showMessageDialog(jFrame, "Очередь пуста"); + return; + } + + DrawingObjectPlain deleted = (DrawingObjectPlain) deletedPlanes.peek(); + + FormAirPlane dialog = new FormAirPlane(deleted.getAirPlane()); + deletedPlanes.remove(); + dialog.run(); + dialog.setSize(800, 500); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + + dialog.setVisible(true); }); leftButton.addActionListener(e -> { - if(_mapCarsCollectionGeneric == null) return; - img = _mapCarsCollectionGeneric.MoveObject(Direction.Left); + if(listBoxMaps.getSelectedValue() == null) return; + img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left); canv.repaint(); }); rightButton.addActionListener(e -> { - if(_mapCarsCollectionGeneric == null) return; - img = _mapCarsCollectionGeneric.MoveObject(Direction.Right); + if(listBoxMaps.getSelectedValue() == null) return; + img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right); canv.repaint(); }); upButton.addActionListener(e -> { - if(_mapCarsCollectionGeneric == null) return; - img = _mapCarsCollectionGeneric.MoveObject(Direction.Up); + if(listBoxMaps.getSelectedValue() == null) return; + img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up); canv.repaint(); }); downButton.addActionListener(e -> { - - if(_mapCarsCollectionGeneric == null) return; - img = _mapCarsCollectionGeneric.MoveObject(Direction.Down); + if(listBoxMaps.getSelectedValue() == null) return; + img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down); canv.repaint(); }); -- 2.25.1