diff --git a/src/main/java/DrawingObjectArmoredCar.java b/src/main/java/DrawingObjectArmoredCar.java index 6868e1e..b3330ea 100644 --- a/src/main/java/DrawingObjectArmoredCar.java +++ b/src/main/java/DrawingObjectArmoredCar.java @@ -8,6 +8,10 @@ public class DrawingObjectArmoredCar implements IDrawingObject{ this.armoredCar = armoredCar; } + public DrawingArmoredCar getArmoredCar() { + return armoredCar; + } + @Override public float getStep() { if (armoredCar != null) diff --git a/src/main/java/FormArmoredCar.java b/src/main/java/FormArmoredCar.java index f6b3a6f..e6a12f8 100644 --- a/src/main/java/FormArmoredCar.java +++ b/src/main/java/FormArmoredCar.java @@ -144,4 +144,9 @@ public class FormArmoredCar extends JFrame{ labelWeight.setText("Вес: " + armoredCar.getArmoredCar().getWeight()); labelColor.setText("Цвет: " + armoredCar.getArmoredCar().getBodyColor().getRGB()); } + + public void setArmoredCar(DrawingArmoredCar armoredCar) { + this.armoredCar = armoredCar; + setData(); + } } \ No newline at end of file diff --git a/src/main/java/FormMapWithArmoredCars.form b/src/main/java/FormMapWithArmoredCars.form index e716cee..e62edd9 100644 --- a/src/main/java/FormMapWithArmoredCars.form +++ b/src/main/java/FormMapWithArmoredCars.form @@ -3,22 +3,25 @@ - + - + - + + + + - + @@ -26,7 +29,7 @@ - + @@ -34,7 +37,7 @@ - + @@ -42,7 +45,7 @@ - + @@ -50,7 +53,7 @@ - + @@ -58,7 +61,7 @@ - + @@ -70,7 +73,7 @@ - + @@ -81,7 +84,7 @@ - + @@ -90,7 +93,7 @@ - + @@ -99,7 +102,7 @@ - + @@ -107,6 +110,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/FormMapWithArmoredCars.java b/src/main/java/FormMapWithArmoredCars.java index 11762e5..4527591 100644 --- a/src/main/java/FormMapWithArmoredCars.java +++ b/src/main/java/FormMapWithArmoredCars.java @@ -1,6 +1,9 @@ import javax.swing.*; import java.awt.*; import java.awt.event.*; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Queue; public class FormMapWithArmoredCars extends JFrame{ private JButton buttonAddArmoredCar; @@ -16,47 +19,39 @@ public class FormMapWithArmoredCars extends JFrame{ private JButton buttonLeft; private JPanel mainPanel; private JPanel btnPanel; + private JTextField textBoxNewMapName; + private JButton AddMapButton; + private JButton DeleteMapButton; + private JList listBoxMaps; + private JButton lookDeletedButton; private Image img; - private MapWithSetArmoredCarsGeneric _mapArmoredCarsCollectionGeneric; + + private final HashMap _mapsDict = new HashMap<>() {{ + put("Простая карта", new SimpleMap()); + put("Карта 1", new MyMapLabyrinth()); + put("Карта 2", new MyMapWooden()); + }}; + + private final MapsCollection _mapsCollection; + private final Queue queue = new LinkedList<>(); public FormMapWithArmoredCars() { super("Карта с набором объектов"); setBounds(100, 100, 1000, 700); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - mapSelectorComboBox.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - AbstractMap map = null; - String name = (String) mapSelectorComboBox.getSelectedItem(); - switch (name) - { - case "Простая карта": - map = new SimpleMap(); - break; - case "Карта 1": - map = new MyMapWooden(); - break; - case "Карта 2": - map = new MyMapLabyrinth(); - break; - } - if (map != null) - { - _mapArmoredCarsCollectionGeneric = new MapWithSetArmoredCarsGeneric<>( - drawPanel.getWidth(), drawPanel.getHeight(), map); - } - else - { - _mapArmoredCarsCollectionGeneric = null; - } - } - }); + + mapSelectorComboBox.removeAllItems(); + for (var elem : _mapsDict.keySet()) + { + mapSelectorComboBox.addItem(elem); + } + buttonAddArmoredCar.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapArmoredCarsCollectionGeneric == null) + if (listBoxMaps.getSelectedIndex() == -1) { return; } @@ -67,11 +62,11 @@ public class FormMapWithArmoredCars extends JFrame{ if (form.getSelectedCar() == null) return; DrawingObjectArmoredCar armoredCar = new DrawingObjectArmoredCar(form.getSelectedCar()); - if (_mapArmoredCarsCollectionGeneric.add(armoredCar) > -1) + if (_mapsCollection.get((String)listBoxMaps.getSelectedValue()).add(armoredCar) > -1) { if (form.DialogResult) { JOptionPane.showMessageDialog(null, "Объект добавлен"); - img = _mapArmoredCarsCollectionGeneric.ShowSet(); + img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet(); repaint(); } } @@ -94,10 +89,12 @@ public class FormMapWithArmoredCars extends JFrame{ "Удаление", JOptionPane.YES_NO_OPTION); if (res == JOptionPane.YES_OPTION) { int pos = Integer.parseInt(maskedTextBoxPosition.getText()); - if (_mapArmoredCarsCollectionGeneric.remove(pos) != null) + DrawingObjectArmoredCar deletedArmoredCar = (DrawingObjectArmoredCar) _mapsCollection.get((String) listBoxMaps.getSelectedValue(), pos); + if (_mapsCollection.get((String) listBoxMaps.getSelectedValue()).remove(pos) != null) { + queue.add(deletedArmoredCar); JOptionPane.showMessageDialog(null, "Объект удален"); - img = _mapArmoredCarsCollectionGeneric.ShowSet(); + img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet(); } else { @@ -109,11 +106,11 @@ public class FormMapWithArmoredCars extends JFrame{ buttonShowStorage.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapArmoredCarsCollectionGeneric == null) + if (_mapsCollection.get((String) listBoxMaps.getSelectedValue()) == null) { return; } - img = _mapArmoredCarsCollectionGeneric.ShowSet(); + img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet(); repaint(); } }); @@ -121,11 +118,11 @@ public class FormMapWithArmoredCars extends JFrame{ buttonShowOnMap.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - if (_mapArmoredCarsCollectionGeneric == null) + if (_mapsCollection.get((String) listBoxMaps.getSelectedValue()) == null) { return; } - img = _mapArmoredCarsCollectionGeneric.ShowOnMap(); + img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowOnMap(); repaint(); } }); @@ -135,31 +132,85 @@ public class FormMapWithArmoredCars extends JFrame{ buttonLeft.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - img = _mapArmoredCarsCollectionGeneric.MoveObject(Direction.Left); + img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Left); repaint(); } }); buttonUp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - img = _mapArmoredCarsCollectionGeneric.MoveObject(Direction.Up); + img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Up); repaint(); } }); buttonDown.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - img = _mapArmoredCarsCollectionGeneric.MoveObject(Direction.Down); + img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Down); repaint(); } }); buttonRight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - img = _mapArmoredCarsCollectionGeneric.MoveObject(Direction.Right); + img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Right); repaint(); } }); + AddMapButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (mapSelectorComboBox.getSelectedIndex() == -1 || textBoxNewMapName.getText().equals("")) + { + JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE); + return; + } + if (!_mapsDict.containsKey((String) mapSelectorComboBox.getSelectedItem())) + { + JOptionPane.showMessageDialog(null, "Нет такой карты", "Ошибка", JOptionPane.ERROR_MESSAGE); + return; + } + _mapsCollection.AddMap(textBoxNewMapName.getText(), _mapsDict.get((String) mapSelectorComboBox.getSelectedItem())); + ReloadMaps(); + } + }); + _mapsCollection = new MapsCollection(drawPanel.getWidth(), drawPanel.getHeight()); + + listBoxMaps.addListSelectionListener(e -> { + if (listBoxMaps.getSelectedValue() != null) { + img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet(); + repaint(); + } + }); + + DeleteMapButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (listBoxMaps.getSelectedIndex() == -1) + { + return; + } + + if (JOptionPane.showConfirmDialog(null, "Удалить карту" + + listBoxMaps.getSelectedValue()+ "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) + { + _mapsCollection.DelMap((String) listBoxMaps.getSelectedValue()); + ReloadMaps(); + } + } + }); + lookDeletedButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + if (!queue.isEmpty()) { + FormArmoredCar form = new FormArmoredCar(); + form.setArmoredCar(queue.poll().getArmoredCar()); + } + else + JOptionPane.showMessageDialog(null, "Нет удаленных объектов"); + } + }); } private void createUIComponents() { @@ -172,4 +223,19 @@ public class FormMapWithArmoredCars extends JFrame{ } }; } + + private void ReloadMaps() { + int index = listBoxMaps.getSelectedIndex(); + listBoxMaps.setListData(_mapsCollection.getKeys().toArray(new Object[0])); + if (listBoxMaps.getModel().getSize() > 0 && (index == -1 || index >= + listBoxMaps.getModel().getSize())) + { + listBoxMaps.setSelectedIndex(0); + } + else if (listBoxMaps.getModel().getSize() > 0 && index > -1 && index < + listBoxMaps.getModel().getSize()) + { + listBoxMaps.setSelectedIndex(index); + } + } } diff --git a/src/main/java/MapWithSetArmoredCarsGeneric.java b/src/main/java/MapWithSetArmoredCarsGeneric.java index d1d7c75..25596e7 100644 --- a/src/main/java/MapWithSetArmoredCarsGeneric.java +++ b/src/main/java/MapWithSetArmoredCarsGeneric.java @@ -47,13 +47,9 @@ public class MapWithSetArmoredCarsGeneric get_setCars() { + return _setCars; + } } diff --git a/src/main/java/MapsCollection.java b/src/main/java/MapsCollection.java new file mode 100644 index 0000000..3efab5a --- /dev/null +++ b/src/main/java/MapsCollection.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Set; + +public class MapsCollection { + public HashMap> _mapStorages; + private int _pictureWidth; + private int _pictureHeight; + + public MapsCollection(int pictureWidth, int pictureHeight) + { + _mapStorages = new HashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public Set getKeys() { + return _mapStorages.keySet(); + } + + public void AddMap(String name, AbstractMap map) + { + if (!getKeys().contains(name)) + _mapStorages.put(name, new MapWithSetArmoredCarsGeneric<>(_pictureWidth, _pictureHeight, map)); + } + + public void DelMap(String name) + { + if (getKeys().contains(name)) + _mapStorages.remove(name); + } + + public MapWithSetArmoredCarsGeneric get(String ind) + { + if (getKeys().contains(ind)) + return _mapStorages.get(ind); + return null; + } + // дополнительное задание: метод "индексатор" с двумя параметрами + public IDrawingObject get(String ind1, int pos) { + if (_mapStorages.containsKey(ind1)) + return _mapStorages.get(ind1).get_setCars().Get(pos); + return null; + } +} diff --git a/src/main/java/SetArmoredCarsGeneric.java b/src/main/java/SetArmoredCarsGeneric.java index f342073..aca2008 100644 --- a/src/main/java/SetArmoredCarsGeneric.java +++ b/src/main/java/SetArmoredCarsGeneric.java @@ -1,16 +1,17 @@ -import java.lang.reflect.Array; -import java.util.ArrayList; +import java.util.*; public class SetArmoredCarsGeneric { - private T[] _places; + private ArrayList _places; + private int maxCount; public SetArmoredCarsGeneric(int count) { - _places = (T[]) new Object[count]; + _places = new ArrayList(); + maxCount = count; } public int getCount() { - return _places != null ? _places.length : 0; + return _places != null ? _places.size() : 0; } public int Insert(T armoredCar) @@ -20,47 +21,31 @@ public class SetArmoredCarsGeneric { public int Insert(T armoredCar, int position) { - if (position < 0 || position >= getCount()) + if (getCount() >= maxCount || position < 0 || position >= maxCount) return -1; - if (!(_places[position] == null)) - { - int index_empty = -1; - // поиск первого пустого элемента - for (int i = position + 1; i < getCount(); i++) - { - if (_places[i] == null) - { - index_empty = i; - } - } - if (index_empty == -1) - return -1; - else - { - for (int i = index_empty; i > position; i--) - { - _places[i] = _places[i - 1]; - } - } - } - _places[position] = armoredCar; + _places.add(position, armoredCar); return position; } public T Remove(int position) { - if (position < 0 || position >= getCount()) + if (position < 0 || position >= _places.size()) return null; - T armoredCar = _places[position]; - _places[position] = null; + T armoredCar = _places.get(position); + _places.remove(position); return armoredCar; } public T Get(int position) { - if (position < 0 || position >= getCount()) + if (position < 0 || position >= maxCount) return null; - return _places[position]; + return _places.get(position); + } + + public Iterable GetArmoredCars() + { + return () -> _places.stream().filter(Objects::nonNull).iterator(); } }