From ace10a75f7fbd46478fed06d5616a4105ab7db59 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Fri, 25 Nov 2022 12:49:46 +0300 Subject: [PATCH] lab 4 full --- DrawingObjectAircraft.java | 4 + FormAircraft.java | 5 ++ FormMapWithSetAircrafts.form | 71 ++++++++++++--- FormMapWithSetAircrafts.java | 165 ++++++++++++++++++++++++----------- Main.java | 2 +- MapsCollection.java | 38 ++++++++ SetAircraftsGeneric.java | 45 ++++------ 7 files changed, 240 insertions(+), 90 deletions(-) create mode 100644 MapsCollection.java diff --git a/DrawingObjectAircraft.java b/DrawingObjectAircraft.java index 668cffd..296299c 100644 --- a/DrawingObjectAircraft.java +++ b/DrawingObjectAircraft.java @@ -7,6 +7,10 @@ public class DrawingObjectAircraft implements IDrawingObject _aircraft = aircraft; } + public DrawingAircraft getAircraft() { + return _aircraft; + } + public void MoveObject(Direction direction) { if(_aircraft == null) return; _aircraft.MoveTransport(direction); diff --git a/FormAircraft.java b/FormAircraft.java index e8a527e..a2b1dda 100644 --- a/FormAircraft.java +++ b/FormAircraft.java @@ -25,6 +25,11 @@ public class FormAircraft extends JDialog implements Form { return selectedAircraft; } + public FormAircraft() {} + + public FormAircraft(DrawingAircraft aircraft) { + _airFighter = aircraft; + } public void run() { add(mainPanel); diff --git a/FormMapWithSetAircrafts.form b/FormMapWithSetAircrafts.form index bfb4848..ea58ac0 100644 --- a/FormMapWithSetAircrafts.form +++ b/FormMapWithSetAircrafts.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/FormMapWithSetAircrafts.java b/FormMapWithSetAircrafts.java index 29546ae..1ed93a5 100644 --- a/FormMapWithSetAircrafts.java +++ b/FormMapWithSetAircrafts.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 FormMapWithSetAircrafts implements Form { private JPanel MainPane; - private JButton buttonAdd; + private JButton buttonAddAircraft; private JComboBox comboBoxSelectorMap; private JTextField textBoxPosition; - private JButton buttonRemove; + private JButton buttonRemoveAircraft; private JButton buttonShowStorage; private JButton buttonShowOnMap; private JButton upButton; @@ -18,10 +16,19 @@ public class FormMapWithSetAircrafts 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 MapWithSetAircraftsGeneric _mapCarsCollectionGeneric; + private MapsCollection _mapsCollection; + private HashMap _mapsDict = new HashMap<>(){{ + put( "Простая карта", new SimpleMap() ); + put( "Моя карта", new MyMap() ); + }}; private Canvas canv = new Canvas(this); + private Queue deletedAircrafts = new ArrayDeque<>(); JFrame jFrame = getFrame(); Image img; @@ -29,43 +36,83 @@ public class FormMapWithSetAircrafts 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 MapWithSetAircraftsGeneric( - 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; + buttonAddAircraft.addActionListener(e -> { + if (listBoxMaps.getSelectedIndex() == -1) + { + return; + } FormAircraft dialog = new FormAircraft(); dialog.run(); @@ -78,10 +125,10 @@ public class FormMapWithSetAircrafts implements Form { if (dialog.getSelectedAircraft() == null) return; DrawingObjectAircraft aircraft = new DrawingObjectAircraft(dialog.getSelectedAircraft()); - if (_mapCarsCollectionGeneric.addAircraft(aircraft) != -1) + if (_mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).addAircraft(aircraft) != -1) { JOptionPane.showMessageDialog(jFrame, "Объект добавлен"); - img = _mapCarsCollectionGeneric.ShowSet(); + img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet(); canv.repaint(); } else @@ -90,9 +137,7 @@ public class FormMapWithSetAircrafts implements Form { } }); - buttonRemove.addActionListener(e -> { - if(_mapCarsCollectionGeneric == null) return; - + buttonRemoveAircraft.addActionListener(e -> { String text = textBoxPosition.getText(); if(text.isEmpty()) return; @@ -110,9 +155,13 @@ public class FormMapWithSetAircrafts implements Form { } pos = Integer.parseInt(text); - if(_mapCarsCollectionGeneric.removeAircraft(pos) != null) { + String mapName = listBoxMaps.getSelectedValue().toString(); + IDrawingObject deleted = _mapsCollection.getMap(mapName).removeAircraft(pos); + + if(deleted != null) { JOptionPane.showMessageDialog(jFrame, "Объект удален"); - img = _mapCarsCollectionGeneric.ShowSet(); + img = _mapsCollection.getMap(mapName).ShowSet(); + deletedAircrafts.add(deleted); canv.repaint(); } else { JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект"); @@ -120,47 +169,63 @@ public class FormMapWithSetAircrafts 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(deletedAircrafts.size() == 0) { + JOptionPane.showMessageDialog(jFrame, "Очередь пуста"); + return; + } + + DrawingObjectAircraft deleted = (DrawingObjectAircraft) deletedAircrafts.peek(); + + FormAircraft dialog = new FormAircraft(deleted.getAircraft()); + deletedAircrafts.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(); }); diff --git a/Main.java b/Main.java index de98287..36c5cc4 100644 --- a/Main.java +++ b/Main.java @@ -1,5 +1,5 @@ public class Main { public static void main(String[] args) { - new formAircraftGenerator().run(); + new FormMapWithSetAircrafts().run(); } } diff --git a/MapsCollection.java b/MapsCollection.java new file mode 100644 index 0000000..ce52ac7 --- /dev/null +++ b/MapsCollection.java @@ -0,0 +1,38 @@ +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 void AddMap(String name, AbstractMap map) + { + // TODO Прописать логику для добавления + boolean check = _mapStorages.containsKey(name); + if (check) return; + + _mapStorages.put(name, new MapWithSetAircraftsGeneric(_pictureWidth, _pictureHeight, map)); + } + + public void DelMap(String name) + { + // TODO Прописать логику для удаления + _mapStorages.remove(name); + } + + public MapWithSetAircraftsGeneric getMap(String name) + { + return _mapStorages.getOrDefault(name, null); + } +} diff --git a/SetAircraftsGeneric.java b/SetAircraftsGeneric.java index 99e8af1..fbbc5fd 100644 --- a/SetAircraftsGeneric.java +++ b/SetAircraftsGeneric.java @@ -1,50 +1,41 @@ +import java.util.ArrayList; + public class SetAircraftsGeneric { - private T[] _places; - + private ArrayList _places; + private int _maxCount; public int getCount() { - return _places.length; + return _places.size(); } public SetAircraftsGeneric(int count) { - _places = (T[])(new Object[count]); + _places = new ArrayList<>(); + _maxCount = count; } - public int Insert(T car) + public int Insert(T aircraft) { - for(int i = 0; i < _places.length; i++) - { - if (_places[i] == null) - { - _places[i] = car; - return i; - } - } - return -1; + if (_places.size() == _maxCount) return -1; + _places.add(0, aircraft); + return 0; } - public int Insert(T car, int position) + public int Insert(T aircraft, 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] = car; + if (_places.size() == _maxCount) return -1; + _places.add(position, aircraft); 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); } }