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; }