Second stage: Fixes

Third stage: Completed
Beginning advanced task
This commit is contained in:
Сергей Полевой 2022-11-05 18:38:20 +04:00
parent e2f76ef916
commit b0e2bf7947
4 changed files with 158 additions and 57 deletions

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="497"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
@ -11,7 +11,7 @@
<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"/>
@ -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="Посмотреть карту"/>
@ -154,6 +138,72 @@
<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>
</children> </children>
</grid> </grid>
</children> </children>

View File

@ -1,15 +1,18 @@
import javax.swing.*; import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
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;
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 +23,25 @@ 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 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;
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 +49,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 +100,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 +126,9 @@ public class FormMapWithSetArtilleries extends JFrame {
int position = Integer.parseInt(text); 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); 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 +136,60 @@ 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();
}); });
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

@ -2,8 +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.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;
} }