diff --git a/DrawingObjectArtillery.java b/DrawingObjectArtillery.java index 95a71e4..5292c9c 100644 --- a/DrawingObjectArtillery.java +++ b/DrawingObjectArtillery.java @@ -1,12 +1,16 @@ import java.awt.*; public class DrawingObjectArtillery implements IDrawingObject { - private DrawingArtillery _artillery = null; + private DrawingArtillery _artillery; public DrawingObjectArtillery(DrawingArtillery artillery) { _artillery = artillery; } + public DrawingArtillery getArtillery() { + return _artillery; + } + public float getStep() { if (_artillery != null && _artillery.artillery != null) { return _artillery.artillery.getStep(); diff --git a/FormArtillery.java b/FormArtillery.java index 4bf9cb8..6ba971b 100644 --- a/FormArtillery.java +++ b/FormArtillery.java @@ -90,6 +90,12 @@ public class FormArtillery extends JDialog { }); } + public FormArtillery(DrawingArtillery artillery) { + this(); + _artillery = artillery; + repaint(); + } + public DrawingArtillery getSelectedArtillery() { return selectedArtillery; } diff --git a/FormMapWithSetArtilleries.form b/FormMapWithSetArtilleries.form index 7cc983b..47d20d9 100644 --- a/FormMapWithSetArtilleries.form +++ b/FormMapWithSetArtilleries.form @@ -3,7 +3,7 @@ - + @@ -11,13 +11,13 @@ - + - + @@ -38,25 +38,9 @@ - - - - - - - - - - - - - - - - - + @@ -64,7 +48,7 @@ - + @@ -72,7 +56,7 @@ - + @@ -81,7 +65,7 @@ - + @@ -89,7 +73,7 @@ - + @@ -97,7 +81,7 @@ - + @@ -112,7 +96,7 @@ - + @@ -126,7 +110,7 @@ - + @@ -141,7 +125,7 @@ - + @@ -154,6 +138,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FormMapWithSetArtilleries.java b/FormMapWithSetArtilleries.java index f29ca56..84de017 100644 --- a/FormMapWithSetArtilleries.java +++ b/FormMapWithSetArtilleries.java @@ -2,14 +2,16 @@ import javax.swing.*; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.MaskFormatter; import java.awt.*; -import java.awt.image.BufferedImage; import java.text.ParseException; +import java.util.HashMap; +import java.util.Optional; +import java.util.Stack; public class FormMapWithSetArtilleries extends JFrame { private JPanel pictureBox; private JPanel toolsGroup; private JLabel toolsLabel; - private JComboBox comboBoxMapSelector; + private JComboBox comboBoxMapSelector; private JButton buttonAddArtillery; private JPanel paneArtilleries; private JFormattedTextField textBoxPosition; @@ -20,13 +22,27 @@ public class FormMapWithSetArtilleries extends JFrame { private JButton buttonLeft; private JButton buttonRight; private JButton buttonShowOnMap; + private JLabel mapsLabel; + private JTextField textFieldMapName; + private JButton buttonAddMap; + private JList listBoxMaps; + private JPanel mapsGroup; + private JButton buttonDeleteMap; + private JButton buttonShowDeleted; private Image bufferedImage; - private MapWithSetArtilleriesGeneric _mapArtilleriesCollectionGeneric; + private final HashMap _mapsDict = new HashMap<>() {{ + put("Простая карта", new SimpleMap()); + put("Лесная карта", new ForestMap()); + }}; + private final MapsCollection _mapsCollection; + private final Stack deletedObjects = new Stack<>(); public FormMapWithSetArtilleries() { this.setTitle("Artillery"); this.setContentPane(paneArtilleries); + this.setSize(640, 530); + this.setVisible(true); try { textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##"))); @@ -34,22 +50,46 @@ public class FormMapWithSetArtilleries extends JFrame { e.printStackTrace(); } - comboBoxMapSelector.addActionListener(e -> { - AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) { - case "Простая карта" -> new SimpleMap(); - case "Лесная карта" -> new ForestMap(); - default -> null; - }; + _mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight()); - if (map != null) { - _mapArtilleriesCollectionGeneric = new MapWithSetArtilleriesGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map); - } else { - _mapArtilleriesCollectionGeneric = null; + comboBoxMapSelector.removeAllItems(); + for (var key : _mapsDict.keySet()) { + comboBoxMapSelector.addItem(key); + } + + buttonAddMap.addActionListener(e -> { + if (comboBoxMapSelector.getSelectedIndex() == -1 || textFieldMapName.getText() == null || textFieldMapName.getText().isEmpty()) { + JOptionPane.showMessageDialog(this, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE); + return; + } + if (!_mapsDict.containsKey((String)comboBoxMapSelector.getSelectedItem())) { + JOptionPane.showMessageDialog(this, "Нет такой карты", "Ошибка", JOptionPane.ERROR_MESSAGE); + return; + } + _mapsCollection.addMap(textFieldMapName.getText(), _mapsDict.get((String)comboBoxMapSelector.getSelectedItem())); + reloadMaps(); + }); + + buttonDeleteMap.addActionListener(e -> { + if (listBoxMaps.getSelectedIndex() == -1) { + return; + } + + if (JOptionPane.showConfirmDialog(this, "Удалить карту " + listBoxMaps.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { + _mapsCollection.deleteMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")); + reloadMaps(); + } + }); + + listBoxMaps.addListSelectionListener(e -> { + if (listBoxMaps.getSelectedValue() != null) { + bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); + repaint(); } }); buttonAddArtillery.addActionListener(e -> { - if (_mapArtilleriesCollectionGeneric == null) { + if (listBoxMaps.getSelectedIndex() == -1) { return; } @@ -61,11 +101,11 @@ public class FormMapWithSetArtilleries extends JFrame { dialog.setVisible(true); if (dialog.getSelectedArtillery() != null) { - DrawingObjectArtillery car = new DrawingObjectArtillery(dialog.getSelectedArtillery()); - if (_mapArtilleriesCollectionGeneric.addArtillery(car) != -1) + DrawingObjectArtillery artillery = new DrawingObjectArtillery(dialog.getSelectedArtillery()); + if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addArtillery(artillery) != -1) { JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); - bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); + bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); repaint(); } else @@ -87,9 +127,11 @@ public class FormMapWithSetArtilleries extends JFrame { int position = Integer.parseInt(text); - if (_mapArtilleriesCollectionGeneric.removeArtilleryAt(position) != null) { + IDrawingObject deleted = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).removeArtilleryAt(position); + if (deleted != null) { + deletedObjects.push(deleted); JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE); - bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); + bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); repaint(); } else { JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE); @@ -97,47 +139,74 @@ public class FormMapWithSetArtilleries extends JFrame { }); buttonShowStorage.addActionListener(e -> { - if (_mapArtilleriesCollectionGeneric == null) { + if (listBoxMaps.getSelectedIndex() == -1) { return; } - bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); + bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); repaint(); }); buttonShowOnMap.addActionListener(e -> { - if (_mapArtilleriesCollectionGeneric == null) { + if (listBoxMaps.getSelectedIndex() == -1) { return; } - bufferedImage = _mapArtilleriesCollectionGeneric.showOnMap(); + bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showOnMap(); repaint(); }); + buttonShowDeleted.addActionListener(e -> { + if (!deletedObjects.empty()) { + DrawingObjectArtillery deleted = (DrawingObjectArtillery) deletedObjects.pop(); + FormArtillery dialog = new FormArtillery(deleted.getArtillery()); + dialog.setSize(800, 500); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + + dialog.setVisible(true); + } else { + JOptionPane.showMessageDialog(this, "Стек удалённых объектов пуст", "Провал", JOptionPane.INFORMATION_MESSAGE); + } + }); + buttonLeft.addActionListener(e -> { - if (_mapArtilleriesCollectionGeneric != null) { - bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Left); + if (listBoxMaps.getSelectedIndex() != -1) { + bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left); repaint(); } }); buttonRight.addActionListener(e -> { - if (_mapArtilleriesCollectionGeneric != null) { - bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Right); + if (listBoxMaps.getSelectedIndex() != -1) { + bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Right); repaint(); } }); buttonUp.addActionListener(e -> { - if (_mapArtilleriesCollectionGeneric != null) { - bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Up); + if (listBoxMaps.getSelectedIndex() != -1) { + bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Up); repaint(); } }); buttonDown.addActionListener(e -> { - if (_mapArtilleriesCollectionGeneric != null) { - bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Down); + if (listBoxMaps.getSelectedIndex() != -1) { + bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Down); repaint(); } }); } + private void reloadMaps() { + int index = listBoxMaps.getSelectedIndex(); + + listBoxMaps.setListData(_mapsCollection.getKeys().toArray(new String[0])); + int size = listBoxMaps.getModel().getSize(); + + if (size > 0 && (index == -1 || index >= size)) { + listBoxMaps.setSelectedIndex(0); + } else if (index > -1 && index < size) { + listBoxMaps.setSelectedIndex(index); + } + } + @Override public void paint(Graphics g) { super.paint(g); diff --git a/MapWithSetArtilleriesGeneric.java b/MapWithSetArtilleriesGeneric.java index 79fd53b..3c2774f 100644 --- a/MapWithSetArtilleriesGeneric.java +++ b/MapWithSetArtilleriesGeneric.java @@ -1,18 +1,19 @@ import java.awt.*; import java.awt.image.BufferedImage; +import java.util.Iterator; public class MapWithSetArtilleriesGeneric { public final int _pictureWidth; public final int _pictureHeight; public final int _placeSizeWidth = 210; public final int _placeSizeHeight = 90; - private final SetArtilleriesGeneric _setArtilleries; + public final SetArtilleriesGeneric _setArtilleries; private final U _map; public MapWithSetArtilleriesGeneric(int picWidth, int picHeight, U map) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; - _setArtilleries = new SetArtilleriesGeneric(width * height); + _setArtilleries = new SetArtilleriesGeneric<>(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; @@ -36,13 +37,8 @@ public class MapWithSetArtilleriesGeneric> _mapsStorage; + + private final int _pictureWidth; + private final int _pictureHeight; + + public Set getKeys() { + return _mapsStorage.keySet(); + } + + public MapsCollection(int pictureWidth, int pictureHeight) { + _mapsStorage = new HashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void addMap(String name, AbstractMap map) { + if (!_mapsStorage.containsKey(name)) { + _mapsStorage.put(name, new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, map)); + } + } + + public void deleteMap(String name) { + _mapsStorage.remove(name); + } + + public MapWithSetArtilleriesGeneric getMap(String name) { + return _mapsStorage.getOrDefault(name, null); + } + + public IDrawingObject getArtillery(String mapName, int index) { + var map = _mapsStorage.getOrDefault(mapName, null); + if (map != null) { + return map._setArtilleries.get(index); + } + return null; + } +} diff --git a/Program.java b/Program.java index 5a3c167..00da2ad 100644 --- a/Program.java +++ b/Program.java @@ -2,9 +2,7 @@ import javax.swing.*; public class Program { public static void main(String[] args) { - FormGallery form = new FormGallery(); - form.setSize(320, 240); - form.setVisible(true); + FormMapWithSetArtilleries form = new FormMapWithSetArtilleries(); form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } } diff --git a/SetArtilleriesGeneric.java b/SetArtilleriesGeneric.java index 77d1f52..7b8c6d0 100644 --- a/SetArtilleriesGeneric.java +++ b/SetArtilleriesGeneric.java @@ -1,12 +1,18 @@ +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Objects; + public class SetArtilleriesGeneric { - private final Object[] _places; + private final ArrayList _places; + private final int _maxCount; public int getCount() { - return _places.length; + return _places.size(); } public SetArtilleriesGeneric(int count) { - _places = new Object[count]; + _maxCount = count; + _places = new ArrayList<>(count); } public int insert(T artillery) { @@ -14,57 +20,37 @@ public class SetArtilleriesGeneric { } public int insert(T artillery, int position) { - if (position < 0 || position >= getCount()) { + if (position < 0 || position > getCount() || getCount() == _maxCount) { return -1; } - if (_places[position] == null) { - _places[position] = artillery; - return position; - } + _places.add(position, artillery); - int firstNull = -1; - - for (int i = position + 1; i < getCount(); i++) - { - if (_places[i] == null) - { - firstNull = i; - break; - } - } - - if (firstNull == -1) - { - return -1; - } - - System.arraycopy(_places, position, _places, position + 1, firstNull - position); - - _places[position] = artillery; return position; } - @SuppressWarnings("unchecked") public T remove(int position) { if (position < 0 || position >= getCount()) { return null; } - var result = _places[position]; + T result = _places.get(position); - _places[position] = null; + _places.remove(position); - return (T) result; + return result; } - @SuppressWarnings("unchecked") public T get(int position) { if (position < 0 || position >= getCount()) { return null; } - return (T) _places[position]; + return _places.get(position); + } + + public Iterable getArtilleries() { + return () -> _places.stream().filter(Objects::nonNull).iterator(); } }