diff --git a/src/DrawingObjectWarship.java b/src/DrawingObjectWarship.java
index 3fa8b93..0f9c909 100644
--- a/src/DrawingObjectWarship.java
+++ b/src/DrawingObjectWarship.java
@@ -14,6 +14,10 @@ public class DrawingObjectWarship implements IDrawingObject {
return 0;
}
+ public DrawingWarship GetWarship() {
+ return _warship;
+ }
+
@Override
public void SetObject(int x, int y, int width, int height) {
_warship.SetPosition(x, y, width, height);
diff --git a/src/FormMapWithSetWarships.form b/src/FormMapWithSetWarships.form
index 26b6e7e..fa5b030 100644
--- a/src/FormMapWithSetWarships.form
+++ b/src/FormMapWithSetWarships.form
@@ -3,7 +3,7 @@
-
+
@@ -37,33 +37,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -80,14 +53,6 @@
-
-
-
-
-
-
-
-
@@ -136,14 +101,9 @@
-
-
-
-
-
-
+
@@ -151,6 +111,93 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/FormMapWithSetWarships.java b/src/FormMapWithSetWarships.java
index 3f18958..8215d3a 100644
--- a/src/FormMapWithSetWarships.java
+++ b/src/FormMapWithSetWarships.java
@@ -1,5 +1,8 @@
import javax.swing.*;
import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.HashMap;
public class FormMapWithSetWarships extends JFrame{
private JPanel mainPanel;
@@ -15,8 +18,19 @@ public class FormMapWithSetWarships extends JFrame{
private JButton ButtonUp;
private JButton ButtonDown;
private JButton ButtonLeft;
+ private JButton deletedWarshipButtom;
+ private JPanel MapPanel;
+ private JTextField TextFieldMap;
+ private JButton CreateMapButton;
+ private JButton DeleteMapButton;
+ private JList ListBoxMaps;
private Image bufferedImage;
private MapWithSetWarshipsGeneric _mapWarshipsCollectionGeneric;
+ private MapsCollection _mapsCollection;
+ private final HashMap _mapsDict = new HashMap<>() {{
+ put("Простая карта", new SimpleMap());
+ put("Преграды-линии", new LineMap());
+ }};
public FormMapWithSetWarships(){
InitializeComponent();
@@ -31,13 +45,35 @@ public class FormMapWithSetWarships extends JFrame{
}
}
- public void InitializeComponent(){
+ private void ReloadMaps()
+ {
+ int index = ListBoxMaps.getSelectedIndex();
+ ListBoxMaps.setListData(_mapsCollection.Keys().toArray(new String[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);
+ }
+ repaint();
+ }
+
+ private void InitializeComponent(){
setContentPane(mainPanel);
setTitle("Warship");
- setSize(1000, 693);
+ setSize(935, 693);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
+ _mapsCollection = new MapsCollection(getWidth(), getHeight());
+ СomboBoxSelectorMap.removeAllItems();
+ for(String elem:_mapsDict.keySet()){
+ СomboBoxSelectorMap.addItem(elem);
+ }
+
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");
ButtonUp.setIcon(iconUp);
Icon iconDown = new ImageIcon("src\\Images\\ArrowDown.jpg");
@@ -47,24 +83,56 @@ public class FormMapWithSetWarships extends JFrame{
Icon iconRight = new ImageIcon("src\\Images\\ArrowRight.jpg");
ButtonRight.setIcon(iconRight);
- СomboBoxSelectorMap.addActionListener(e -> {
- AbstractMap map = switch (СomboBoxSelectorMap.getSelectedItem().toString()) {
- case "Простая карта" -> new SimpleMap();
- case "Преграды-линии" -> new LineMap();
- default -> null;
- };
- if( map != null){
- _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric(
- PictureBox.getWidth(), PictureBox.getHeight(), map);
- }
- else
+ ListBoxMaps.addListSelectionListener(e -> {
+ if(ListBoxMaps.getSelectedIndex() == -1)
+ return;
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
+ repaint();
+ });
+
+ DeleteMapButton.addActionListener(e -> {
+ if (ListBoxMaps.getSelectedIndex() == -1)
{
- _mapWarshipsCollectionGeneric = null;
+ return;
+ }
+ if(JOptionPane.showConfirmDialog(this,"Удалить карту " + ListBoxMaps.getSelectedValue().toString() + "?",
+ "Удаление",JOptionPane.YES_NO_OPTION) == 0)
+ {
+ _mapsCollection.DelMap(ListBoxMaps.getSelectedValue().toString());
+ ReloadMaps();
}
});
+ CreateMapButton.addActionListener(e -> {
+ if (СomboBoxSelectorMap.getSelectedIndex() == -1 || TextFieldMap.getText() == null || TextFieldMap.getText().equals(""))
+ {
+ JOptionPane.showMessageDialog(this,"Не все данные заполнены","Ошибка",JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ if (!_mapsDict.containsKey(СomboBoxSelectorMap.getSelectedItem()))
+ {
+ JOptionPane.showMessageDialog(this,"Нет такой карты","Ошибка",JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ _mapsCollection.AddMap(TextFieldMap.getText(), _mapsDict.get(СomboBoxSelectorMap.getSelectedItem().toString()));
+ ReloadMaps();
+ });
+
+ deletedWarshipButtom.addActionListener(e -> {
+ if (ListBoxMaps.getSelectedIndex() == -1)
+ return;
+ DrawingObjectWarship warship = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).GetWarshipInDeleted();
+ if (warship == null) {
+ JOptionPane.showMessageDialog(null, "Стек пуст", "Ошибка", JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ FormWarship form = new FormWarship(warship);
+ form.setSize(1000, 700);
+ form.setVisible(true);
+ });
+
ButtonAddWarship.addActionListener(e -> {
- if(_mapWarshipsCollectionGeneric == null){
+ if(ListBoxMaps.getSelectedIndex() == -1){
return;
}
FormWarshipCreator warshipCreator = new FormWarshipCreator();
@@ -75,10 +143,10 @@ public class FormMapWithSetWarships extends JFrame{
if (warshipCreator.getSelectedWarship() != null) {
DrawingObjectWarship warship = new DrawingObjectWarship(warshipCreator.getSelectedWarship());
- if (_mapWarshipsCollectionGeneric.Plus(warship) >= 0) {
+ if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Plus(warship)>=0) {
JOptionPane.showMessageDialog(this,
"Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
- bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
} else {
JOptionPane.showMessageDialog(this,
@@ -88,26 +156,26 @@ public class FormMapWithSetWarships extends JFrame{
});
ButtonRemoveWarship.addActionListener(e -> {
- String text = TextBoxPosition.getText();
- if (text == null || _mapWarshipsCollectionGeneric == null || text.isEmpty())
+
+ if (TextBoxPosition.getText().isEmpty())
{
return;
}
- int result = JOptionPane.showConfirmDialog(
- null,
+ int result = JOptionPane.showConfirmDialog(null,
"Удалить объект?",
"Удаление",
- JOptionPane.YES_NO_CANCEL_OPTION);
+ JOptionPane.YES_NO_OPTION,
+ JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.NO_OPTION)
{
return;
}
- int pos = Integer.parseInt(text);
- if (_mapWarshipsCollectionGeneric.Minus(pos) != null)
+ int pos = Integer.parseInt(TextBoxPosition.getText());
+ if (_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Minus(pos)!=null)
{
JOptionPane.showMessageDialog(this,
"Объект удален","Успех",JOptionPane.INFORMATION_MESSAGE);
- bufferedImage = _mapWarshipsCollectionGeneric.ShowSet();
+ bufferedImage = _mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet();
repaint();
}
else
diff --git a/src/FormWarship.java b/src/FormWarship.java
index cc451ec..0ef2ab9 100644
--- a/src/FormWarship.java
+++ b/src/FormWarship.java
@@ -4,9 +4,9 @@ import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
-public class FormWarship extends JFrame {
+public class FormWarship extends JDialog {
private DrawingWarship _warship;
- public DrawingWarship selectedWarship;
+ DrawingWarship selectedWarship;
public DrawingWarship getSelectedCar(){
return selectedWarship;
}
@@ -29,6 +29,13 @@ public class FormWarship extends JFrame {
InitializeComponent();
}
+ public FormWarship(DrawingObjectWarship warship){
+ InitializeComponent();
+ _warship = warship.GetWarship();
+ SetData();
+ }
+
+
private void Draw(){
Graphics2D graphics = (Graphics2D) drawPanel.getGraphics();
graphics.clearRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight());
@@ -36,6 +43,16 @@ public class FormWarship extends JFrame {
_warship.DrawTransport(graphics);
}
+ @Override
+ public void paint(Graphics g) {
+ super.paint(g);
+
+ if (_warship != null) {
+ mainPanel.paintComponents(mainPanel.getGraphics());
+ _warship.DrawTransport(g);
+ }
+ }
+
private void SetData(){
Random rnd = new Random();
_warship.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), mainPanel.getWidth(), mainPanel.getHeight());
@@ -54,7 +71,6 @@ public class FormWarship extends JFrame {
setContentPane(mainPanel);
setTitle("Warship");
setSize(900, 700);
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
Icon iconUp = new ImageIcon("src\\Images\\ArrowUp.jpg");
diff --git a/src/MapWithSetWarshipsGeneric.java b/src/MapWithSetWarshipsGeneric.java
index 471859b..820b941 100644
--- a/src/MapWithSetWarshipsGeneric.java
+++ b/src/MapWithSetWarshipsGeneric.java
@@ -1,5 +1,6 @@
import java.awt.*;
import java.awt.image.BufferedImage;
+import java.util.Stack;
public class MapWithSetWarshipsGeneric {
private final int _pictureWidth;
@@ -7,6 +8,7 @@ public class MapWithSetWarshipsGeneric _setWarships;
+ private Stack _deletedWarship;
private final U _map;
public MapWithSetWarshipsGeneric(int picWidth, int picHeight, U map)
@@ -17,6 +19,7 @@ public class MapWithSetWarshipsGeneric ();
}
public int Plus(T warship)
@@ -26,7 +29,9 @@ public class MapWithSetWarshipsGeneric > _mapStorages;
+ public ArrayList Keys() {
+ return new ArrayList<>(_mapStorages.keySet());
+ }
+ private final int _pictureWidth;
+ private final int _pictureHeight;
+
+ public MapsCollection(int pictureWidth, int pictureHeight)
+ {
+ _mapStorages = new HashMap<>();
+ _pictureWidth = pictureWidth;
+ _pictureHeight = pictureHeight;
+ }
+
+ public void AddMap(String name, AbstractMap map)
+ {
+ if (_mapStorages.containsKey(name))
+ {
+ return;
+ }
+ _mapStorages.put(name, new MapWithSetWarshipsGeneric<>(_pictureWidth,_pictureHeight,map));
+ }
+
+ public void DelMap(String name)
+ {
+ _mapStorages.remove(name);
+ }
+
+ public MapWithSetWarshipsGeneric get(String ind){
+ if (_mapStorages.containsKey(ind))
+ {
+ return _mapStorages.get(ind);
+ }
+ return null;
+ }
+
+ public DrawingObjectWarship Get(String name, int ind) {
+ if (_mapStorages.containsKey(name))
+ {
+ return _mapStorages.get(name).GetWarshipInList(ind);
+ }
+ return null;
+ }
+}
diff --git a/src/SetWarshipsGeneric.java b/src/SetWarshipsGeneric.java
index bea9594..91f269b 100644
--- a/src/SetWarshipsGeneric.java
+++ b/src/SetWarshipsGeneric.java
@@ -1,13 +1,17 @@
-public class SetWarshipsGeneric{
- private final Object[] _places;
+import java.util.ArrayList;
+import java.util.Iterator;
+public class SetWarshipsGeneric implements Iterable{
+ private ArrayList _places;
public int getCount() {
- return _places.length;
+ return _places.size();
}
+ private final int _maxCount;
public SetWarshipsGeneric(int count)
{
- _places = new Object[count];
+ _places = new ArrayList<>();
+ _maxCount = count;
}
public int Insert(T warship)
@@ -17,41 +21,30 @@ public class SetWarshipsGeneric{
public int Insert(T warship, int position)
{
- if (position < 0 || position >= getCount())
+ if (position < 0 || position >= _maxCount)
return -1;
-
- int empty = -1;
- for (int i = position + 1; i < getCount(); i++)
- {
- if (_places[i] == null)
- empty = i;
- }
- if (empty == -1)
- return 0;
- else
- {
- for (int i = empty; i > position; i--)
- _places[i] = _places[i - 1];
- }
- _places[position] = warship;
- return 1;
+ _places.add(position, warship);
+ return position;
}
public T Remove(int position)
{
- if (position >= getCount() || position < 0 || _places[position] == null)
+ if (position >= _maxCount || position < 0)
return null;
-
- T deleted =(T) _places[position];
- _places[position] = null;
+ T deleted = _places.get(position);
+ _places.remove(position);
return deleted;
}
public T Get(int position)
{
- if (position >= getCount() || position < 0)
+ if (position > getCount() || position < 0)
return null;
+ return _places.get(position);
+ }
- return (T) _places[position];
+ @Override
+ public Iterator iterator() {
+ return _places.iterator();
}
}