Compare commits

..

No commits in common. "e60990067a607496e40a9e9932864d9eeebeca12" and "9c4f1d43275f0ee207e8602cb70597525bdde32c" have entirely different histories.

8 changed files with 79 additions and 239 deletions

View File

@ -1,16 +1,12 @@
import java.awt.*; import java.awt.*;
public class DrawingObjectArtillery implements IDrawingObject { public class DrawingObjectArtillery implements IDrawingObject {
private DrawingArtillery _artillery; private DrawingArtillery _artillery = null;
public DrawingObjectArtillery(DrawingArtillery artillery) { public DrawingObjectArtillery(DrawingArtillery artillery) {
_artillery = artillery; _artillery = artillery;
} }
public DrawingArtillery getArtillery() {
return _artillery;
}
public float getStep() { public float getStep() {
if (_artillery != null && _artillery.artillery != null) { if (_artillery != null && _artillery.artillery != null) {
return _artillery.artillery.getStep(); return _artillery.artillery.getStep();

View File

@ -90,12 +90,6 @@ public class FormArtillery extends JDialog {
}); });
} }
public FormArtillery(DrawingArtillery artillery) {
this();
_artillery = artillery;
repaint();
}
public DrawingArtillery getSelectedArtillery() { public DrawingArtillery getSelectedArtillery() {
return selectedArtillery; return selectedArtillery;
} }

View File

@ -3,7 +3,7 @@
<grid id="27dc6" binding="paneArtilleries" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="27dc6" binding="paneArtilleries" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<xy x="20" y="20" width="649" height="532"/> <xy x="20" y="20" width="613" height="400"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
@ -11,13 +11,13 @@
<grid id="fcfe1" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="fcfe1" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children/> <children/>
</grid> </grid>
<grid id="f232" binding="toolsGroup" layout-manager="GridLayoutManager" row-count="10" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="f232" binding="toolsGroup" layout-manager="GridLayoutManager" row-count="9" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/> <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -38,9 +38,25 @@
<grid row="0" column="1" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <grid row="0" column="1" row-span="1" col-span="3" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</hspacer> </hspacer>
<vspacer id="352bc">
<constraints>
<grid row="5" column="0" row-span="2" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="6a0f5" class="javax.swing.JComboBox" binding="comboBoxMapSelector">
<constraints>
<grid row="1" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Лесная карта"/>
</model>
</properties>
</component>
<component id="52049" class="javax.swing.JButton" binding="buttonAddArtillery"> <component id="52049" class="javax.swing.JButton" binding="buttonAddArtillery">
<constraints> <constraints>
<grid row="2" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <grid row="2" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<text value="Добавить артиллерию"/> <text value="Добавить артиллерию"/>
@ -48,7 +64,7 @@
</component> </component>
<component id="2f9ed" class="javax.swing.JFormattedTextField" binding="textBoxPosition"> <component id="2f9ed" class="javax.swing.JFormattedTextField" binding="textBoxPosition">
<constraints> <constraints>
<grid row="3" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"> <grid row="3" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/> <preferred-size width="150" height="-1"/>
</grid> </grid>
</constraints> </constraints>
@ -56,7 +72,7 @@
</component> </component>
<component id="1dbb5" class="javax.swing.JButton" binding="buttonRemoveArtillery"> <component id="1dbb5" class="javax.swing.JButton" binding="buttonRemoveArtillery">
<constraints> <constraints>
<grid row="4" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <grid row="4" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<actionCommand value=""/> <actionCommand value=""/>
@ -65,7 +81,7 @@
</component> </component>
<component id="4e6ca" class="javax.swing.JButton" binding="buttonShowStorage"> <component id="4e6ca" class="javax.swing.JButton" binding="buttonShowStorage">
<constraints> <constraints>
<grid row="5" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <grid row="5" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<text value="Посмотреть хранилище"/> <text value="Посмотреть хранилище"/>
@ -73,7 +89,7 @@
</component> </component>
<component id="51371" class="javax.swing.JButton" binding="buttonShowOnMap"> <component id="51371" class="javax.swing.JButton" binding="buttonShowOnMap">
<constraints> <constraints>
<grid row="6" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <grid row="6" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<text value="Посмотреть карту"/> <text value="Посмотреть карту"/>
@ -81,7 +97,7 @@
</component> </component>
<component id="fb3e4" class="javax.swing.JButton" binding="buttonUp"> <component id="fb3e4" class="javax.swing.JButton" binding="buttonUp">
<constraints> <constraints>
<grid row="8" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false"> <grid row="7" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/> <minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
@ -96,7 +112,7 @@
</component> </component>
<component id="328f6" class="javax.swing.JButton" binding="buttonDown"> <component id="328f6" class="javax.swing.JButton" binding="buttonDown">
<constraints> <constraints>
<grid row="9" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"> <grid row="8" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/> <minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
@ -110,7 +126,7 @@
</component> </component>
<component id="4f2dc" class="javax.swing.JButton" binding="buttonLeft"> <component id="4f2dc" class="javax.swing.JButton" binding="buttonLeft">
<constraints> <constraints>
<grid row="9" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"> <grid row="8" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/> <minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
@ -125,7 +141,7 @@
</component> </component>
<component id="bac06" class="javax.swing.JButton" binding="buttonRight"> <component id="bac06" class="javax.swing.JButton" binding="buttonRight">
<constraints> <constraints>
<grid row="9" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"> <grid row="8" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/> <minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/> <preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
@ -138,80 +154,6 @@
<text value=""/> <text value=""/>
</properties> </properties>
</component> </component>
<grid id="7448e" binding="mapsGroup" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="4" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="3bd55" class="javax.swing.JLabel" binding="mapsLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Карты"/>
</properties>
</component>
<hspacer id="b4c1d">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="6a0f5" class="javax.swing.JComboBox" binding="comboBoxMapSelector">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Лесная карта"/>
</model>
</properties>
</component>
<component id="a344e" class="javax.swing.JTextField" binding="textFieldMapName">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="b8791" class="javax.swing.JButton" binding="buttonAddMap">
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить карту"/>
</properties>
</component>
<component id="89547" class="javax.swing.JList" binding="listBoxMaps">
<constraints>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
</constraints>
<properties/>
</component>
<component id="643d3" class="javax.swing.JButton" binding="buttonDeleteMap">
<constraints>
<grid row="5" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить карту"/>
</properties>
</component>
</children>
</grid>
<component id="490e6" class="javax.swing.JButton" binding="buttonShowDeleted">
<constraints>
<grid row="7" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Посмотреть удалённую артиллерию"/>
</properties>
</component>
</children> </children>
</grid> </grid>
</children> </children>

View File

@ -2,16 +2,14 @@ import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter; import javax.swing.text.MaskFormatter;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap;
import java.util.Optional;
import java.util.Stack;
public class FormMapWithSetArtilleries extends JFrame { public class FormMapWithSetArtilleries extends JFrame {
private JPanel pictureBox; private JPanel pictureBox;
private JPanel toolsGroup; private JPanel toolsGroup;
private JLabel toolsLabel; private JLabel toolsLabel;
private JComboBox<String> comboBoxMapSelector; private JComboBox comboBoxMapSelector;
private JButton buttonAddArtillery; private JButton buttonAddArtillery;
private JPanel paneArtilleries; private JPanel paneArtilleries;
private JFormattedTextField textBoxPosition; private JFormattedTextField textBoxPosition;
@ -22,27 +20,13 @@ public class FormMapWithSetArtilleries extends JFrame {
private JButton buttonLeft; private JButton buttonLeft;
private JButton buttonRight; private JButton buttonRight;
private JButton buttonShowOnMap; private JButton buttonShowOnMap;
private JLabel mapsLabel;
private JTextField textFieldMapName;
private JButton buttonAddMap;
private JList<String> listBoxMaps;
private JPanel mapsGroup;
private JButton buttonDeleteMap;
private JButton buttonShowDeleted;
private Image bufferedImage; private Image bufferedImage;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{ private MapWithSetArtilleriesGeneric<DrawingObjectArtillery, AbstractMap> _mapArtilleriesCollectionGeneric;
put("Простая карта", new SimpleMap());
put("Лесная карта", new ForestMap());
}};
private final MapsCollection _mapsCollection;
private final Stack<IDrawingObject> deletedObjects = new Stack<>();
public FormMapWithSetArtilleries() { public FormMapWithSetArtilleries() {
this.setTitle("Artillery"); this.setTitle("Artillery");
this.setContentPane(paneArtilleries); this.setContentPane(paneArtilleries);
this.setSize(640, 530);
this.setVisible(true);
try { try {
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##"))); textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
@ -50,46 +34,22 @@ public class FormMapWithSetArtilleries extends JFrame {
e.printStackTrace(); e.printStackTrace();
} }
_mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight()); comboBoxMapSelector.addActionListener(e -> {
AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) {
case "Простая карта" -> new SimpleMap();
case "Лесная карта" -> new ForestMap();
default -> null;
};
comboBoxMapSelector.removeAllItems(); if (map != null) {
for (var key : _mapsDict.keySet()) { _mapArtilleriesCollectionGeneric = new MapWithSetArtilleriesGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map);
comboBoxMapSelector.addItem(key); } else {
} _mapArtilleriesCollectionGeneric = 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();
} }
}); });
buttonAddArtillery.addActionListener(e -> { buttonAddArtillery.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1) { if (_mapArtilleriesCollectionGeneric == null) {
return; return;
} }
@ -101,11 +61,11 @@ public class FormMapWithSetArtilleries extends JFrame {
dialog.setVisible(true); dialog.setVisible(true);
if (dialog.getSelectedArtillery() != null) { if (dialog.getSelectedArtillery() != null) {
DrawingObjectArtillery artillery = new DrawingObjectArtillery(dialog.getSelectedArtillery()); DrawingObjectArtillery car = new DrawingObjectArtillery(dialog.getSelectedArtillery());
if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addArtillery(artillery) != -1) if (_mapArtilleriesCollectionGeneric.addArtillery(car) != -1)
{ {
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); bufferedImage = _mapArtilleriesCollectionGeneric.showSet();
repaint(); repaint();
} }
else else
@ -127,11 +87,9 @@ public class FormMapWithSetArtilleries extends JFrame {
int position = Integer.parseInt(text); int position = Integer.parseInt(text);
IDrawingObject deleted = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).removeArtilleryAt(position); if (_mapArtilleriesCollectionGeneric.removeArtilleryAt(position) != null) {
if (deleted != null) {
deletedObjects.push(deleted);
JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); bufferedImage = _mapArtilleriesCollectionGeneric.showSet();
repaint(); repaint();
} else { } else {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
@ -139,72 +97,47 @@ public class FormMapWithSetArtilleries extends JFrame {
}); });
buttonShowStorage.addActionListener(e -> { buttonShowStorage.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1) { if (_mapArtilleriesCollectionGeneric == null) {
return; return;
} }
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet(); bufferedImage = _mapArtilleriesCollectionGeneric.showSet();
repaint(); repaint();
}); });
buttonShowOnMap.addActionListener(e -> { buttonShowOnMap.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1) { if (_mapArtilleriesCollectionGeneric == null) {
return; return;
} }
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showOnMap(); bufferedImage = _mapArtilleriesCollectionGeneric.showOnMap();
repaint(); 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 -> { buttonLeft.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) { if (_mapArtilleriesCollectionGeneric != null) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left); bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Left);
repaint(); repaint();
} }
}); });
buttonRight.addActionListener(e -> { buttonRight.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) { if (_mapArtilleriesCollectionGeneric != null) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Right); bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Right);
repaint(); repaint();
} }
}); });
buttonUp.addActionListener(e -> { buttonUp.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) { if (_mapArtilleriesCollectionGeneric != null) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Up); bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Up);
repaint(); repaint();
} }
}); });
buttonDown.addActionListener(e -> { buttonDown.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) { if (_mapArtilleriesCollectionGeneric != null) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Down); bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Down);
repaint(); 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 @Override
public void paint(Graphics g) { public void paint(Graphics g) {
super.paint(g); super.paint(g);

View File

@ -1,6 +1,5 @@
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.Iterator;
public class MapWithSetArtilleriesGeneric<T extends IDrawingObject, U extends AbstractMap> { public class MapWithSetArtilleriesGeneric<T extends IDrawingObject, U extends AbstractMap> {
public final int _pictureWidth; public final int _pictureWidth;
@ -13,7 +12,7 @@ public class MapWithSetArtilleriesGeneric<T extends IDrawingObject, U extends Ab
public MapWithSetArtilleriesGeneric(int picWidth, int picHeight, U map) { public MapWithSetArtilleriesGeneric(int picWidth, int picHeight, U map) {
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight; int height = picHeight / _placeSizeHeight;
_setArtilleries = new SetArtilleriesGeneric<>(width * height); _setArtilleries = new SetArtilleriesGeneric<T>(width * height);
_pictureWidth = picWidth; _pictureWidth = picWidth;
_pictureHeight = picHeight; _pictureHeight = picHeight;
_map = map; _map = map;
@ -37,8 +36,13 @@ public class MapWithSetArtilleriesGeneric<T extends IDrawingObject, U extends Ab
public Image showOnMap() { public Image showOnMap() {
shaking(); shaking();
for (T artillery : _setArtilleries.getArtilleries()) { for (int i = 0; i < _setArtilleries.getCount(); i++)
return _map.createMap(_pictureWidth, _pictureHeight, artillery); {
var car = _setArtilleries.get(i);
if (car != null)
{
return _map.createMap(_pictureWidth, _pictureHeight, car);
}
} }
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
} }
@ -116,12 +120,14 @@ public class MapWithSetArtilleriesGeneric<T extends IDrawingObject, U extends Ab
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight; int height = _pictureHeight / _placeSizeHeight;
int index = 0; for (int i = 0; i < _setArtilleries.getCount(); i++)
for (T artillery : _setArtilleries.getArtilleries())
{ {
artillery.setObject(index % width * _placeSizeWidth + 10, (height - 1 - index / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); var artillery = _setArtilleries.get(i);
artillery.drawingObject(g); if (artillery != null)
index++; {
artillery.setObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
artillery.drawingObject(g);
}
} }
} }
} }

View File

@ -1,33 +0,0 @@
import java.util.HashMap;
import java.util.Set;
public class MapsCollection {
private final HashMap<String, MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap>> _mapsStorage;
private final int _pictureWidth;
private final int _pictureHeight;
public Set<String> 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<IDrawingObject, AbstractMap> getMap(String name) {
return _mapsStorage.getOrDefault(name, null);
}
}

View File

@ -2,7 +2,9 @@ import javax.swing.*;
public class Program { public class Program {
public static void main(String[] args) { public static void main(String[] args) {
FormMapWithSetArtilleries form = new FormMapWithSetArtilleries(); FormGallery form = new FormGallery();
form.setSize(320, 240);
form.setVisible(true);
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} }
} }

View File

@ -20,7 +20,7 @@ public class SetArtilleriesGeneric<T> {
} }
public int insert(T artillery, int position) { public int insert(T artillery, int position) {
if (position < 0 || position > getCount() || getCount() == _maxCount) { if (position < 0 || position >= getCount() || getCount() == _maxCount) {
return -1; return -1;
} }