Compare commits

..

5 Commits

Author SHA1 Message Date
e60990067a LabWork04 completed 2022-11-05 19:18:29 +04:00
392e97f71f Fix 2022-11-05 18:48:58 +04:00
b0e2bf7947 Second stage: Fixes
Third stage: Completed
Beginning advanced task
2022-11-05 18:38:20 +04:00
e2f76ef916 Second stage: Completed 2022-11-05 16:40:00 +04:00
0ae6527361 First stage: Refactoring MapWithSetArtilleriesGeneric 2022-11-05 16:31:00 +04:00
8 changed files with 239 additions and 79 deletions

View File

@ -1,12 +1,16 @@
import java.awt.*; import java.awt.*;
public class DrawingObjectArtillery implements IDrawingObject { public class DrawingObjectArtillery implements IDrawingObject {
private DrawingArtillery _artillery = null; private DrawingArtillery _artillery;
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,6 +90,12 @@ 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="613" height="400"/> <xy x="20" y="20" width="649" height="532"/>
</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="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> <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"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children/> <children/>
</grid> </grid>
<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"> <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">
<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,25 +38,9 @@
<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="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <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"/>
</constraints> </constraints>
<properties> <properties>
<text value="Добавить артиллерию"/> <text value="Добавить артиллерию"/>
@ -64,7 +48,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="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"> <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">
<preferred-size width="150" height="-1"/> <preferred-size width="150" height="-1"/>
</grid> </grid>
</constraints> </constraints>
@ -72,7 +56,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="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <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"/>
</constraints> </constraints>
<properties> <properties>
<actionCommand value=""/> <actionCommand value=""/>
@ -81,7 +65,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="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <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"/>
</constraints> </constraints>
<properties> <properties>
<text value="Посмотреть хранилище"/> <text value="Посмотреть хранилище"/>
@ -89,7 +73,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="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> <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"/>
</constraints> </constraints>
<properties> <properties>
<text value="Посмотреть карту"/> <text value="Посмотреть карту"/>
@ -97,7 +81,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="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"> <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">
<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"/>
@ -112,7 +96,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="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"> <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">
<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"/>
@ -126,7 +110,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="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"> <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">
<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"/>
@ -141,7 +125,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="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"> <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">
<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"/>
@ -154,6 +138,80 @@
<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,14 +2,16 @@ 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 comboBoxMapSelector; private JComboBox<String> comboBoxMapSelector;
private JButton buttonAddArtillery; private JButton buttonAddArtillery;
private JPanel paneArtilleries; private JPanel paneArtilleries;
private JFormattedTextField textBoxPosition; private JFormattedTextField textBoxPosition;
@ -20,13 +22,27 @@ 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 MapWithSetArtilleriesGeneric<DrawingObjectArtillery, AbstractMap> _mapArtilleriesCollectionGeneric; private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
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("##")));
@ -34,22 +50,46 @@ public class FormMapWithSetArtilleries extends JFrame {
e.printStackTrace(); e.printStackTrace();
} }
comboBoxMapSelector.addActionListener(e -> { _mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight());
AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) {
case "Простая карта" -> new SimpleMap();
case "Лесная карта" -> new ForestMap();
default -> null;
};
if (map != null) { comboBoxMapSelector.removeAllItems();
_mapArtilleriesCollectionGeneric = new MapWithSetArtilleriesGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map); for (var key : _mapsDict.keySet()) {
} else { comboBoxMapSelector.addItem(key);
_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 (_mapArtilleriesCollectionGeneric == null) { if (listBoxMaps.getSelectedIndex() == -1) {
return; return;
} }
@ -61,11 +101,11 @@ public class FormMapWithSetArtilleries extends JFrame {
dialog.setVisible(true); dialog.setVisible(true);
if (dialog.getSelectedArtillery() != null) { if (dialog.getSelectedArtillery() != null) {
DrawingObjectArtillery car = new DrawingObjectArtillery(dialog.getSelectedArtillery()); DrawingObjectArtillery artillery = new DrawingObjectArtillery(dialog.getSelectedArtillery());
if (_mapArtilleriesCollectionGeneric.addArtillery(car) != -1) if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addArtillery(artillery) != -1)
{ {
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint(); repaint();
} }
else else
@ -87,9 +127,11 @@ public class FormMapWithSetArtilleries extends JFrame {
int position = Integer.parseInt(text); int position = Integer.parseInt(text);
if (_mapArtilleriesCollectionGeneric.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); JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint(); repaint();
} else { } else {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
@ -97,47 +139,72 @@ public class FormMapWithSetArtilleries extends JFrame {
}); });
buttonShowStorage.addActionListener(e -> { buttonShowStorage.addActionListener(e -> {
if (_mapArtilleriesCollectionGeneric == null) { if (listBoxMaps.getSelectedIndex() == -1) {
return; return;
} }
bufferedImage = _mapArtilleriesCollectionGeneric.showSet(); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint(); repaint();
}); });
buttonShowOnMap.addActionListener(e -> { buttonShowOnMap.addActionListener(e -> {
if (_mapArtilleriesCollectionGeneric == null) { if (listBoxMaps.getSelectedIndex() == -1) {
return; return;
} }
bufferedImage = _mapArtilleriesCollectionGeneric.showOnMap(); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).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 (_mapArtilleriesCollectionGeneric != null) { if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Left); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left);
repaint(); repaint();
} }
}); });
buttonRight.addActionListener(e -> { buttonRight.addActionListener(e -> {
if (_mapArtilleriesCollectionGeneric != null) { if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Right); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Right);
repaint(); repaint();
} }
}); });
buttonUp.addActionListener(e -> { buttonUp.addActionListener(e -> {
if (_mapArtilleriesCollectionGeneric != null) { if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Up); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Up);
repaint(); repaint();
} }
}); });
buttonDown.addActionListener(e -> { buttonDown.addActionListener(e -> {
if (_mapArtilleriesCollectionGeneric != null) { if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Down); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).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,5 +1,6 @@
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;
@ -12,7 +13,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<T>(width * height); _setArtilleries = new SetArtilleriesGeneric<>(width * height);
_pictureWidth = picWidth; _pictureWidth = picWidth;
_pictureHeight = picHeight; _pictureHeight = picHeight;
_map = map; _map = map;
@ -36,13 +37,8 @@ public class MapWithSetArtilleriesGeneric<T extends IDrawingObject, U extends Ab
public Image showOnMap() { public Image showOnMap() {
shaking(); shaking();
for (int i = 0; i < _setArtilleries.getCount(); i++) for (T artillery : _setArtilleries.getArtilleries()) {
{ 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);
} }
@ -120,14 +116,12 @@ 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;
for (int i = 0; i < _setArtilleries.getCount(); i++) int index = 0;
for (T artillery : _setArtilleries.getArtilleries())
{ {
var artillery = _setArtilleries.get(i); artillery.setObject(index % width * _placeSizeWidth + 10, (height - 1 - index / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
if (artillery != null) artillery.drawingObject(g);
{ index++;
artillery.setObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
artillery.drawingObject(g);
}
} }
} }
} }

33
MapsCollection.java Normal file
View File

@ -0,0 +1,33 @@
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,9 +2,7 @@ import javax.swing.*;
public class Program { public class Program {
public static void main(String[] args) { public static void main(String[] args) {
FormGallery form = new FormGallery(); FormMapWithSetArtilleries form = new FormMapWithSetArtilleries();
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;
} }