LabWork04 completed

This commit is contained in:
Сергей Полевой 2022-11-05 19:18:29 +04:00
parent 392e97f71f
commit e60990067a
5 changed files with 45 additions and 12 deletions

View File

@ -1,12 +1,16 @@
import java.awt.*;
public class DrawingObjectArtillery implements IDrawingObject {
private DrawingArtillery _artillery = null;
private DrawingArtillery _artillery;
public DrawingObjectArtillery(DrawingArtillery artillery) {
_artillery = artillery;
}
public DrawingArtillery getArtillery() {
return _artillery;
}
public float getStep() {
if (_artillery != null && _artillery.artillery != null) {
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() {
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">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="649" height="497"/>
<xy x="20" y="20" width="649" height="532"/>
</constraints>
<properties/>
<border type="none"/>
@ -17,7 +17,7 @@
<border type="none"/>
<children/>
</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"/>
<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"/>
@ -81,7 +81,7 @@
</component>
<component id="fb3e4" class="javax.swing.JButton" binding="buttonUp">
<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"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -96,7 +96,7 @@
</component>
<component id="328f6" class="javax.swing.JButton" binding="buttonDown">
<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"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -110,7 +110,7 @@
</component>
<component id="4f2dc" class="javax.swing.JButton" binding="buttonLeft">
<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"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -125,7 +125,7 @@
</component>
<component id="bac06" class="javax.swing.JButton" binding="buttonRight">
<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"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -204,6 +204,14 @@
</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>
</grid>
</children>

View File

@ -1,12 +1,11 @@
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.text.ParseException;
import java.util.HashMap;
import java.util.Optional;
import java.util.Stack;
public class FormMapWithSetArtilleries extends JFrame {
private JPanel pictureBox;
@ -29,6 +28,7 @@ public class FormMapWithSetArtilleries extends JFrame {
private JList<String> listBoxMaps;
private JPanel mapsGroup;
private JButton buttonDeleteMap;
private JButton buttonShowDeleted;
private Image bufferedImage;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
@ -36,6 +36,7 @@ public class FormMapWithSetArtilleries extends JFrame {
put("Лесная карта", new ForestMap());
}};
private final MapsCollection _mapsCollection;
private final Stack<IDrawingObject> deletedObjects = new Stack<>();
public FormMapWithSetArtilleries() {
this.setTitle("Artillery");
@ -126,7 +127,9 @@ public class FormMapWithSetArtilleries extends JFrame {
int position = Integer.parseInt(text);
if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).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);
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
@ -151,6 +154,18 @@ public class FormMapWithSetArtilleries extends JFrame {
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 -> {
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left);

View File

@ -2,7 +2,7 @@ import java.util.HashMap;
import java.util.Set;
public class MapsCollection {
private final HashMap<String, MapWithSetArtilleriesGeneric<DrawingObjectArtillery, AbstractMap>> _mapsStorage;
private final HashMap<String, MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap>> _mapsStorage;
private final int _pictureWidth;
private final int _pictureHeight;
@ -27,7 +27,7 @@ public class MapsCollection {
_mapsStorage.remove(name);
}
public MapWithSetArtilleriesGeneric<DrawingObjectArtillery, AbstractMap> getMap(String name) {
public MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> getMap(String name) {
return _mapsStorage.getOrDefault(name, null);
}
}