Second stage: Fixes
Third stage: Completed Beginning advanced task
This commit is contained in:
parent
e2f76ef916
commit
b0e2bf7947
@ -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">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="613" height="400"/>
|
||||
<xy x="20" y="20" width="649" height="497"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<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">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<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>
|
||||
<properties/>
|
||||
<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"/>
|
||||
</constraints>
|
||||
</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">
|
||||
<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>
|
||||
<properties>
|
||||
<text value="Добавить артиллерию"/>
|
||||
@ -64,7 +48,7 @@
|
||||
</component>
|
||||
<component id="2f9ed" class="javax.swing.JFormattedTextField" binding="textBoxPosition">
|
||||
<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"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
@ -72,7 +56,7 @@
|
||||
</component>
|
||||
<component id="1dbb5" class="javax.swing.JButton" binding="buttonRemoveArtillery">
|
||||
<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>
|
||||
<properties>
|
||||
<actionCommand value=""/>
|
||||
@ -81,7 +65,7 @@
|
||||
</component>
|
||||
<component id="4e6ca" class="javax.swing.JButton" binding="buttonShowStorage">
|
||||
<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>
|
||||
<properties>
|
||||
<text value="Посмотреть хранилище"/>
|
||||
@ -89,7 +73,7 @@
|
||||
</component>
|
||||
<component id="51371" class="javax.swing.JButton" binding="buttonShowOnMap">
|
||||
<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>
|
||||
<properties>
|
||||
<text value="Посмотреть карту"/>
|
||||
@ -154,6 +138,72 @@
|
||||
<text value=""/>
|
||||
</properties>
|
||||
</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>
|
||||
</grid>
|
||||
</children>
|
||||
|
@ -1,15 +1,18 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import javax.swing.text.DefaultFormatterFactory;
|
||||
import javax.swing.text.MaskFormatter;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.text.ParseException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Optional;
|
||||
|
||||
public class FormMapWithSetArtilleries extends JFrame {
|
||||
private JPanel pictureBox;
|
||||
private JPanel toolsGroup;
|
||||
private JLabel toolsLabel;
|
||||
private JComboBox comboBoxMapSelector;
|
||||
private JComboBox<String> comboBoxMapSelector;
|
||||
private JButton buttonAddArtillery;
|
||||
private JPanel paneArtilleries;
|
||||
private JFormattedTextField textBoxPosition;
|
||||
@ -20,13 +23,25 @@ public class FormMapWithSetArtilleries extends JFrame {
|
||||
private JButton buttonLeft;
|
||||
private JButton buttonRight;
|
||||
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 MapWithSetArtilleriesGeneric<DrawingObjectArtillery, AbstractMap> _mapArtilleriesCollectionGeneric;
|
||||
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
|
||||
put("Простая карта", new SimpleMap());
|
||||
put("Лесная карта", new ForestMap());
|
||||
}};
|
||||
private final MapsCollection _mapsCollection;
|
||||
|
||||
public FormMapWithSetArtilleries() {
|
||||
this.setTitle("Artillery");
|
||||
this.setContentPane(paneArtilleries);
|
||||
this.setSize(640, 530);
|
||||
this.setVisible(true);
|
||||
|
||||
try {
|
||||
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
|
||||
@ -34,22 +49,46 @@ public class FormMapWithSetArtilleries extends JFrame {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
comboBoxMapSelector.addActionListener(e -> {
|
||||
AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) {
|
||||
case "Простая карта" -> new SimpleMap();
|
||||
case "Лесная карта" -> new ForestMap();
|
||||
default -> null;
|
||||
};
|
||||
_mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight());
|
||||
|
||||
if (map != null) {
|
||||
_mapArtilleriesCollectionGeneric = new MapWithSetArtilleriesGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map);
|
||||
} else {
|
||||
_mapArtilleriesCollectionGeneric = null;
|
||||
comboBoxMapSelector.removeAllItems();
|
||||
for (var key : _mapsDict.keySet()) {
|
||||
comboBoxMapSelector.addItem(key);
|
||||
}
|
||||
|
||||
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 -> {
|
||||
if (_mapArtilleriesCollectionGeneric == null) {
|
||||
if (listBoxMaps.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -61,11 +100,11 @@ public class FormMapWithSetArtilleries extends JFrame {
|
||||
dialog.setVisible(true);
|
||||
|
||||
if (dialog.getSelectedArtillery() != null) {
|
||||
DrawingObjectArtillery car = new DrawingObjectArtillery(dialog.getSelectedArtillery());
|
||||
if (_mapArtilleriesCollectionGeneric.addArtillery(car) != -1)
|
||||
DrawingObjectArtillery artillery = new DrawingObjectArtillery(dialog.getSelectedArtillery());
|
||||
if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addArtillery(artillery) != -1)
|
||||
{
|
||||
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
|
||||
bufferedImage = _mapArtilleriesCollectionGeneric.showSet();
|
||||
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
|
||||
repaint();
|
||||
}
|
||||
else
|
||||
@ -87,9 +126,9 @@ public class FormMapWithSetArtilleries extends JFrame {
|
||||
|
||||
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);
|
||||
bufferedImage = _mapArtilleriesCollectionGeneric.showSet();
|
||||
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
|
||||
repaint();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
|
||||
@ -97,47 +136,60 @@ public class FormMapWithSetArtilleries extends JFrame {
|
||||
});
|
||||
|
||||
buttonShowStorage.addActionListener(e -> {
|
||||
if (_mapArtilleriesCollectionGeneric == null) {
|
||||
if (listBoxMaps.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
bufferedImage = _mapArtilleriesCollectionGeneric.showSet();
|
||||
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
|
||||
repaint();
|
||||
});
|
||||
|
||||
buttonShowOnMap.addActionListener(e -> {
|
||||
if (_mapArtilleriesCollectionGeneric == null) {
|
||||
if (listBoxMaps.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
bufferedImage = _mapArtilleriesCollectionGeneric.showOnMap();
|
||||
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showOnMap();
|
||||
repaint();
|
||||
});
|
||||
|
||||
buttonLeft.addActionListener(e -> {
|
||||
if (_mapArtilleriesCollectionGeneric != null) {
|
||||
bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Left);
|
||||
if (listBoxMaps.getSelectedIndex() != -1) {
|
||||
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonRight.addActionListener(e -> {
|
||||
if (_mapArtilleriesCollectionGeneric != null) {
|
||||
bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Right);
|
||||
if (listBoxMaps.getSelectedIndex() != -1) {
|
||||
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Right);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonUp.addActionListener(e -> {
|
||||
if (_mapArtilleriesCollectionGeneric != null) {
|
||||
bufferedImage = _mapArtilleriesCollectionGeneric.moveObject(Direction.Up);
|
||||
if (listBoxMaps.getSelectedIndex() != -1) {
|
||||
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Up);
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
buttonDown.addActionListener(e -> {
|
||||
if (_mapArtilleriesCollectionGeneric != null) {
|
||||
bufferedImage = _mapArtilleriesCollectionGeneric.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);
|
||||
|
@ -2,8 +2,7 @@ import javax.swing.*;
|
||||
|
||||
public class Program {
|
||||
public static void main(String[] args) {
|
||||
FormGallery form = new FormGallery();
|
||||
form.setSize(320, 240);
|
||||
FormMapWithSetArtilleries form = new FormMapWithSetArtilleries();
|
||||
form.setVisible(true);
|
||||
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ public class SetArtilleriesGeneric<T> {
|
||||
}
|
||||
|
||||
public int insert(T artillery, int position) {
|
||||
if (position < 0 || position >= getCount() || getCount() == _maxCount) {
|
||||
if (position < 0 || position > getCount() || getCount() == _maxCount) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user