From 8fc2d24e7e3b37f5e548660c80c8247a71636960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 5 Nov 2022 16:20:44 +0400 Subject: [PATCH 1/9] First stage: Array was replaced with ArrayList --- SetArtilleriesGeneric.java | 52 ++++++++++++++------------------------ 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/SetArtilleriesGeneric.java b/SetArtilleriesGeneric.java index 77d1f52..be9ad0c 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 Iterator getArtilleries() { + return _places.stream().filter(Objects::nonNull).iterator(); } } -- 2.25.1 From 9c4f1d43275f0ee207e8602cb70597525bdde32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 5 Nov 2022 16:26:26 +0400 Subject: [PATCH 2/9] First stage: Fixed SetArtilleriesGeneric --- SetArtilleriesGeneric.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SetArtilleriesGeneric.java b/SetArtilleriesGeneric.java index be9ad0c..aa8aa9a 100644 --- a/SetArtilleriesGeneric.java +++ b/SetArtilleriesGeneric.java @@ -50,7 +50,7 @@ public class SetArtilleriesGeneric { return _places.get(position); } - public Iterator getArtilleries() { - return _places.stream().filter(Objects::nonNull).iterator(); + public Iterable getArtilleries() { + return () -> _places.stream().filter(Objects::nonNull).iterator(); } } -- 2.25.1 From 0ae65273614611101147f4eead9db0e5e4e6c1e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 5 Nov 2022 16:31:00 +0400 Subject: [PATCH 3/9] First stage: Refactoring MapWithSetArtilleriesGeneric --- MapWithSetArtilleriesGeneric.java | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/MapWithSetArtilleriesGeneric.java b/MapWithSetArtilleriesGeneric.java index 79fd53b..826e6d4 100644 --- a/MapWithSetArtilleriesGeneric.java +++ b/MapWithSetArtilleriesGeneric.java @@ -1,5 +1,6 @@ import java.awt.*; import java.awt.image.BufferedImage; +import java.util.Iterator; public class MapWithSetArtilleriesGeneric { public final int _pictureWidth; @@ -12,7 +13,7 @@ public class MapWithSetArtilleriesGeneric(width * height); + _setArtilleries = new SetArtilleriesGeneric<>(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; @@ -36,13 +37,8 @@ public class MapWithSetArtilleriesGeneric Date: Sat, 5 Nov 2022 16:40:00 +0400 Subject: [PATCH 4/9] Second stage: Completed --- MapsCollection.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 MapsCollection.java diff --git a/MapsCollection.java b/MapsCollection.java new file mode 100644 index 0000000..6f040f5 --- /dev/null +++ b/MapsCollection.java @@ -0,0 +1,33 @@ +import java.util.HashMap; +import java.util.Set; + +public class MapsCollection { + private final HashMap> _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); + } +} -- 2.25.1 From b0e2bf7947e08a87775bf6dbb0d04e11e7b566dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 5 Nov 2022 18:38:20 +0400 Subject: [PATCH 5/9] Second stage: Fixes Third stage: Completed Beginning advanced task --- FormMapWithSetArtilleries.form | 96 ++++++++++++++++++++------- FormMapWithSetArtilleries.java | 114 ++++++++++++++++++++++++--------- Program.java | 3 +- SetArtilleriesGeneric.java | 2 +- 4 files changed, 158 insertions(+), 57 deletions(-) diff --git a/FormMapWithSetArtilleries.form b/FormMapWithSetArtilleries.form index 7cc983b..9ae064e 100644 --- a/FormMapWithSetArtilleries.form +++ b/FormMapWithSetArtilleries.form @@ -3,7 +3,7 @@ - + @@ -11,7 +11,7 @@ - + @@ -38,25 +38,9 @@ - - - - - - - - - - - - - - - - - + @@ -64,7 +48,7 @@ - + @@ -72,7 +56,7 @@ - + @@ -81,7 +65,7 @@ - + @@ -89,7 +73,7 @@ - + @@ -154,6 +138,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FormMapWithSetArtilleries.java b/FormMapWithSetArtilleries.java index f29ca56..3444f05 100644 --- a/FormMapWithSetArtilleries.java +++ b/FormMapWithSetArtilleries.java @@ -1,15 +1,18 @@ import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; 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; 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 +23,25 @@ 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 Image bufferedImage; - private MapWithSetArtilleriesGeneric _mapArtilleriesCollectionGeneric; + private final HashMap _mapsDict = new HashMap<>() {{ + put("Простая карта", new SimpleMap()); + put("Лесная карта", new ForestMap()); + }}; + private final MapsCollection _mapsCollection; public FormMapWithSetArtilleries() { this.setTitle("Artillery"); this.setContentPane(paneArtilleries); + this.setSize(640, 530); + this.setVisible(true); try { textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##"))); @@ -34,22 +49,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 +100,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 +126,9 @@ public class FormMapWithSetArtilleries extends JFrame { int position = Integer.parseInt(text); - if (_mapArtilleriesCollectionGeneric.removeArtilleryAt(position) != null) { + if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).removeArtilleryAt(position) != null) { 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 +136,60 @@ 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(); }); 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/Program.java b/Program.java index 5a3c167..5ed2e9e 100644 --- a/Program.java +++ b/Program.java @@ -2,8 +2,7 @@ import javax.swing.*; public class Program { public static void main(String[] args) { - FormGallery form = new FormGallery(); - form.setSize(320, 240); + FormMapWithSetArtilleries form = new FormMapWithSetArtilleries(); form.setVisible(true); form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } diff --git a/SetArtilleriesGeneric.java b/SetArtilleriesGeneric.java index aa8aa9a..7b8c6d0 100644 --- a/SetArtilleriesGeneric.java +++ b/SetArtilleriesGeneric.java @@ -20,7 +20,7 @@ public class SetArtilleriesGeneric { } public int insert(T artillery, int position) { - if (position < 0 || position >= getCount() || getCount() == _maxCount) { + if (position < 0 || position > getCount() || getCount() == _maxCount) { return -1; } -- 2.25.1 From 392e97f71f36c5e62f63cbeb96d1f1cb14008a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 5 Nov 2022 18:48:58 +0400 Subject: [PATCH 6/9] Fix --- Program.java | 1 - 1 file changed, 1 deletion(-) diff --git a/Program.java b/Program.java index 5ed2e9e..00da2ad 100644 --- a/Program.java +++ b/Program.java @@ -3,7 +3,6 @@ import javax.swing.*; public class Program { public static void main(String[] args) { FormMapWithSetArtilleries form = new FormMapWithSetArtilleries(); - form.setVisible(true); form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } } -- 2.25.1 From e60990067a607496e40a9e9932864d9eeebeca12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 5 Nov 2022 19:18:29 +0400 Subject: [PATCH 7/9] LabWork04 completed --- DrawingObjectArtillery.java | 6 +++++- FormArtillery.java | 6 ++++++ FormMapWithSetArtilleries.form | 20 ++++++++++++++------ FormMapWithSetArtilleries.java | 21 ++++++++++++++++++--- MapsCollection.java | 4 ++-- 5 files changed, 45 insertions(+), 12 deletions(-) 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 9ae064e..47d20d9 100644 --- a/FormMapWithSetArtilleries.form +++ b/FormMapWithSetArtilleries.form @@ -3,7 +3,7 @@ - + @@ -17,7 +17,7 @@ - + @@ -81,7 +81,7 @@ - + @@ -96,7 +96,7 @@ - + @@ -110,7 +110,7 @@ - + @@ -125,7 +125,7 @@ - + @@ -204,6 +204,14 @@ + + + + + + + + diff --git a/FormMapWithSetArtilleries.java b/FormMapWithSetArtilleries.java index 3444f05..2491a0c 100644 --- a/FormMapWithSetArtilleries.java +++ b/FormMapWithSetArtilleries.java @@ -1,12 +1,11 @@ import javax.swing.*; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.MaskFormatter; import java.awt.*; import java.text.ParseException; import java.util.HashMap; import java.util.Optional; +import java.util.Stack; public class FormMapWithSetArtilleries extends JFrame { private JPanel pictureBox; @@ -29,6 +28,7 @@ public class FormMapWithSetArtilleries extends JFrame { private JList listBoxMaps; private JPanel mapsGroup; private JButton buttonDeleteMap; + private JButton buttonShowDeleted; private Image bufferedImage; private final HashMap _mapsDict = new HashMap<>() {{ @@ -36,6 +36,7 @@ public class FormMapWithSetArtilleries extends JFrame { put("Лесная карта", new ForestMap()); }}; private final MapsCollection _mapsCollection; + private final Stack deletedObjects = new Stack<>(); public FormMapWithSetArtilleries() { this.setTitle("Artillery"); @@ -126,7 +127,9 @@ public class FormMapWithSetArtilleries extends JFrame { int position = Integer.parseInt(text); - if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).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 = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); repaint(); @@ -151,6 +154,18 @@ public class FormMapWithSetArtilleries extends JFrame { repaint(); }); + buttonShowDeleted.addActionListener(e -> { + if (!deletedObjects.empty()) { + DrawingObjectArtillery deleted = (DrawingObjectArtillery) deletedObjects.pop(); + FormArtillery dialog = new FormArtillery(deleted == null ? null : deleted.getArtillery()); + dialog.setSize(800, 500); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + + dialog.setVisible(true); + } + }); + buttonLeft.addActionListener(e -> { if (listBoxMaps.getSelectedIndex() != -1) { bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left); diff --git a/MapsCollection.java b/MapsCollection.java index 6f040f5..95b6399 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -2,7 +2,7 @@ import java.util.HashMap; import java.util.Set; public class MapsCollection { - private final HashMap> _mapsStorage; + private final HashMap> _mapsStorage; private final int _pictureWidth; private final int _pictureHeight; @@ -27,7 +27,7 @@ public class MapsCollection { _mapsStorage.remove(name); } - public MapWithSetArtilleriesGeneric getMap(String name) { + public MapWithSetArtilleriesGeneric getMap(String name) { return _mapsStorage.getOrDefault(name, null); } } -- 2.25.1 From 5c582f70b3e3d42cf1200658fbdacab48e33ce04 Mon Sep 17 00:00:00 2001 From: BlasphemyGod Date: Tue, 8 Nov 2022 16:19:52 +0400 Subject: [PATCH 8/9] Fixes --- FormMapWithSetArtilleries.java | 13 +++++-------- MapWithSetArtilleriesGeneric.java | 2 +- MapsCollection.java | 8 ++++++++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/FormMapWithSetArtilleries.java b/FormMapWithSetArtilleries.java index 2491a0c..552512b 100644 --- a/FormMapWithSetArtilleries.java +++ b/FormMapWithSetArtilleries.java @@ -155,15 +155,12 @@ public class FormMapWithSetArtilleries extends JFrame { }); buttonShowDeleted.addActionListener(e -> { - if (!deletedObjects.empty()) { - DrawingObjectArtillery deleted = (DrawingObjectArtillery) deletedObjects.pop(); - FormArtillery dialog = new FormArtillery(deleted == null ? null : deleted.getArtillery()); - dialog.setSize(800, 500); - dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + FormArtillery dialog = new FormArtillery(deletedObjects.empty() ? null : ((DrawingObjectArtillery)deletedObjects.pop()).getArtillery()); + dialog.setSize(800, 500); + dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - dialog.setVisible(true); - } + dialog.setVisible(true); }); buttonLeft.addActionListener(e -> { diff --git a/MapWithSetArtilleriesGeneric.java b/MapWithSetArtilleriesGeneric.java index 826e6d4..3c2774f 100644 --- a/MapWithSetArtilleriesGeneric.java +++ b/MapWithSetArtilleriesGeneric.java @@ -7,7 +7,7 @@ public class MapWithSetArtilleriesGeneric _setArtilleries; + public final SetArtilleriesGeneric _setArtilleries; private final U _map; public MapWithSetArtilleriesGeneric(int picWidth, int picHeight, U map) { diff --git a/MapsCollection.java b/MapsCollection.java index 95b6399..5b9f24c 100644 --- a/MapsCollection.java +++ b/MapsCollection.java @@ -30,4 +30,12 @@ public class MapsCollection { 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; + } } -- 2.25.1 From ccab9cd6204f38755676ae771e69d9b492b8fc96 Mon Sep 17 00:00:00 2001 From: BlasphemyGod Date: Tue, 8 Nov 2022 16:32:08 +0400 Subject: [PATCH 9/9] Fixes --- FormMapWithSetArtilleries.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/FormMapWithSetArtilleries.java b/FormMapWithSetArtilleries.java index 552512b..84de017 100644 --- a/FormMapWithSetArtilleries.java +++ b/FormMapWithSetArtilleries.java @@ -155,12 +155,17 @@ public class FormMapWithSetArtilleries extends JFrame { }); buttonShowDeleted.addActionListener(e -> { - FormArtillery dialog = new FormArtillery(deletedObjects.empty() ? null : ((DrawingObjectArtillery)deletedObjects.pop()).getArtillery()); - dialog.setSize(800, 500); - dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); - dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + 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); + dialog.setVisible(true); + } else { + JOptionPane.showMessageDialog(this, "Стек удалённых объектов пуст", "Провал", JOptionPane.INFORMATION_MESSAGE); + } }); buttonLeft.addActionListener(e -> { -- 2.25.1