diff --git a/DrawningObjectExcavator.java b/DrawningObjectExcavator.java
index db2e26a..46ebbde 100644
--- a/DrawningObjectExcavator.java
+++ b/DrawningObjectExcavator.java
@@ -2,12 +2,16 @@ import javax.print.DocFlavor;
import java.awt.*;
public class DrawningObjectExcavator implements IDrawningObject{
- private DrawningTracktor _tracktor = null;
+ private DrawningTracktor _tracktor;
public DrawningObjectExcavator(DrawningTracktor tracktor) {
this._tracktor = tracktor;
}
+ public DrawningTracktor getTracktor() {
+ return _tracktor;
+ }
+
public float getStep() {
if (_tracktor != null && _tracktor.Tracktor != null) {
return _tracktor.Tracktor.getStep();
diff --git a/FormMapWithSetTracktor.form b/FormMapWithSetTracktor.form
index f1cf28e..b7928e8 100644
--- a/FormMapWithSetTracktor.form
+++ b/FormMapWithSetTracktor.form
@@ -3,12 +3,12 @@
-
+
-
+
@@ -26,20 +26,9 @@
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -47,7 +36,7 @@
-
+
@@ -55,7 +44,7 @@
-
+
@@ -63,7 +52,7 @@
-
+
@@ -71,7 +60,7 @@
-
+
@@ -79,7 +68,7 @@
-
+
@@ -91,7 +80,7 @@
-
+
@@ -103,7 +92,7 @@
-
+
@@ -115,7 +104,7 @@
-
+
@@ -127,9 +116,83 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/FormMapWithSetTracktor.java b/FormMapWithSetTracktor.java
index c99edc4..49a6848 100644
--- a/FormMapWithSetTracktor.java
+++ b/FormMapWithSetTracktor.java
@@ -3,13 +3,16 @@ import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.text.ParseException;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Optional;
public class FormMapWithSetTracktor extends JFrame {
private JPanel ContentPanel;
private JPanel pictureBox;
private JPanel toolsGroup;
private JLabel toolsLabel;
- private JComboBox comboBoxMapSelector;
+ private JComboBox comboBoxMapSelector;
private JButton buttonAddTracktor;
private JFormattedTextField textBoxPosition;
private JButton buttonRemoveTracktor;
@@ -19,14 +22,27 @@ public class FormMapWithSetTracktor extends JFrame {
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonDown;
+ 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 MapWithSetTracktorGeneric _mapTracktorCollectionGeneric;
+ private final HashMap _mapsDict = new HashMap<>() {{
+ put("Простая карта", new SimpleMap());
+ put("Свалка карта", new DumpMap());
+ }};
+ private final MapsCollection _mapsCollection;
+ private final LinkedList deletedObjects = new LinkedList<>();
public FormMapWithSetTracktor() {
this.setTitle("Трактор");
this.setContentPane(ContentPanel);
- this.setSize(800,500);
+ this.setSize(800,600);
+ this.setVisible(true);
try {
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
@@ -34,22 +50,46 @@ public class FormMapWithSetTracktor extends JFrame {
e.printStackTrace();
}
- comboBoxMapSelector.addActionListener(e -> {
- AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) {
- case "Простая карта" -> new SimpleMap();
- case "Свалка карта" -> new DumpMap();
- default -> null;
- };
+ _mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight());
+ comboBoxMapSelector.removeAllItems();
+ for (var key : _mapsDict.keySet()) {
+ comboBoxMapSelector.addItem(key);
+ }
- if (map != null) {
- _mapTracktorCollectionGeneric = new MapWithSetTracktorGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map);
- } else {
- _mapTracktorCollectionGeneric = null;
+ 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();
+ }
+ });
+
+
buttonAddTracktor.addActionListener(e -> {
- if (_mapTracktorCollectionGeneric == null) {
+ if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
@@ -61,10 +101,10 @@ public class FormMapWithSetTracktor extends JFrame {
if (dialog.getSelectedTracktor() != null) {
DrawningObjectExcavator tracktor = new DrawningObjectExcavator(dialog.getSelectedTracktor());
- if (_mapTracktorCollectionGeneric.addTracktor(tracktor) != -1)
+ if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addTracktor(tracktor) != -1)
{
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
- bufferedImage = _mapTracktorCollectionGeneric.showSet();
+ bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
}
else
@@ -86,9 +126,11 @@ public class FormMapWithSetTracktor extends JFrame {
int position = Integer.parseInt(text);
- if (_mapTracktorCollectionGeneric.removeTracktorAt(position) != null) {
+ IDrawningObject deleted = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).removeTracktorAt(position);
+ if (deleted!=null) {
+ deletedObjects.add(deleted);
JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
- bufferedImage = _mapTracktorCollectionGeneric.showSet();
+ bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
@@ -96,50 +138,77 @@ public class FormMapWithSetTracktor extends JFrame {
});
buttonShowStorage.addActionListener(e -> {
- if (_mapTracktorCollectionGeneric == null) {
+ if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
- bufferedImage = _mapTracktorCollectionGeneric.showSet();
+ bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
});
buttonShowOnMap.addActionListener(e -> {
- if (_mapTracktorCollectionGeneric == null) {
+ if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
- bufferedImage = _mapTracktorCollectionGeneric.showOnMap();
+ bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showOnMap();
repaint();
});
+ buttonShowDeleted.addActionListener(e -> {
+ if (!deletedObjects.isEmpty()) {
+ DrawningObjectExcavator deleted = (DrawningObjectExcavator) deletedObjects.pop();
+ FormTracktor dialog = new FormTracktor(deleted.getTracktor());
+ 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 (_mapTracktorCollectionGeneric != null) {
- bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Left);
+ if (listBoxMaps.getSelectedIndex() != -1) {
+ bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left);
repaint();
}
});
buttonRight.addActionListener(e -> {
- if (_mapTracktorCollectionGeneric != null) {
- bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Right);
+ if (listBoxMaps.getSelectedIndex() != -1) {
+ bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Right);
repaint();
}
});
buttonUp.addActionListener(e -> {
- if (_mapTracktorCollectionGeneric != null) {
- bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Up);
+ if (listBoxMaps.getSelectedIndex() != -1) {
+ bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Up);
repaint();
}
});
buttonDown.addActionListener(e -> {
- if (_mapTracktorCollectionGeneric != null) {
- bufferedImage = _mapTracktorCollectionGeneric.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/FormTracktor.java b/FormTracktor.java
index ad16359..076040e 100644
--- a/FormTracktor.java
+++ b/FormTracktor.java
@@ -114,6 +114,11 @@ public class FormTracktor extends JDialog {
repaint();
}
+ public FormTracktor(DrawningTracktor tracktor) {
+ this();
+ _tracktor = tracktor;
+ repaint();
+ }
public DrawningTracktor getSelectedTracktor() {
return selectedTracktor;
}
diff --git a/MapWithSetTracktorGeneric.java b/MapWithSetTracktorGeneric.java
index 44fb57c..ce03b01 100644
--- a/MapWithSetTracktorGeneric.java
+++ b/MapWithSetTracktorGeneric.java
@@ -1,18 +1,19 @@
import java.awt.*;
import java.awt.image.BufferedImage;
+import java.util.LinkedList;
public class MapWithSetTracktorGeneric {
public final int _pictureWidth;
public final int _pictureHeight;
public final int _placeSizeWidth = 150;
public final int _placeSizeHeight = 100;
- private final SetTracktorGeneric _setTracktor;
+ public final SetTracktorGeneric _setTracktor;
private final U _map;
public MapWithSetTracktorGeneric(int picWidth, int picHeight, U map) {
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
- _setTracktor = new SetTracktorGeneric(width * height);
+ _setTracktor = new SetTracktorGeneric<>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
@@ -36,15 +37,14 @@ public class MapWithSetTracktorGeneric 0)
{
curWidth--;
diff --git a/MapsCollection.java b/MapsCollection.java
new file mode 100644
index 0000000..77796b0
--- /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 MapWithSetTracktorGeneric<>(_pictureWidth, _pictureHeight, map));
+ }
+ }
+
+ public void deleteMap(String name) {
+ _mapsStorage.remove(name);
+ }
+
+ public MapWithSetTracktorGeneric getMap(String name) {
+ return _mapsStorage.getOrDefault(name, null);
+ }
+}
diff --git a/Program.java b/Program.java
index 786e3d7..ee85d3d 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(500, 300);
- form.setVisible(true);
+ FormMapWithSetTracktor form = new FormMapWithSetTracktor();
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
diff --git a/SetTracktorGeneric.java b/SetTracktorGeneric.java
index 633a23a..c6606cc 100644
--- a/SetTracktorGeneric.java
+++ b/SetTracktorGeneric.java
@@ -1,12 +1,17 @@
+import java.util.ArrayList;
+import java.util.Objects;
+
public class SetTracktorGeneric {
- private final Object[] _places;
+ private final ArrayList _places;
+ private final int _maxCount;
public int getCount() {
- return _places.length;
+ return _places.size();
}
public SetTracktorGeneric(int count) {
- _places = new Object[count];
+ _maxCount = count;
+ _places = new ArrayList<>(count);
}
public int insert(T tracktor) {
@@ -14,43 +19,22 @@ public class SetTracktorGeneric {
}
public int insert(T tracktor, int position) {
- if (position < 0 || position >= getCount()) {
+ if (position < 0 || position > getCount() || getCount() == _maxCount) {
return -1;
}
-
- if (_places[position] == null) {
- _places[position] = tracktor;
- return position;
- }
-
- int tmp = -1;
-
- for(int i = position; i < getCount(); i++)
- {
- if (_places[i] == null)
- {
- tmp = i;
- break;
- }
- }
- if (tmp != -1)
- {
- System.arraycopy(_places, position, _places, position + 1, tmp - position);
- _places[position] = tracktor;
- return position;
- }
- return -1;
+ _places.add(position, tracktor);
+ return position;
}
@SuppressWarnings("unchecked")
public T remove(int position) {
- if (position < getCount() && position >= 0)
+ if (position < 0 || position >= getCount())
{
- T temp = (T) _places[position];
- _places[position] = null;
- return temp;
+ return null;
}
- return null;
+ T result = _places.get(position);
+ _places.remove(position);
+ return result;
}
@SuppressWarnings("unchecked")
@@ -58,7 +42,11 @@ public class SetTracktorGeneric {
if (position < 0 || position >= getCount()) {
return null;
}
+ return _places.get(position);
+ }
- return (T) _places[position];
+ public Iterable getTracktors()
+ {
+ return _places;
}
}