Изменения в форме

This commit is contained in:
prodigygirl 2022-11-06 22:26:32 +04:00
parent 0b528dee91
commit 660ee2abb3
4 changed files with 169 additions and 55 deletions

View File

@ -8,6 +8,10 @@ public class DrawingObjectArmoredCar implements IDrawingObject{
this.armoredCar = armoredCar;
}
public DrawingArmoredCar getArmoredCar() {
return armoredCar;
}
@Override
public float getStep() {
if (armoredCar != null)

View File

@ -144,4 +144,9 @@ public class FormArmoredCar extends JFrame{
labelWeight.setText("Вес: " + armoredCar.getArmoredCar().getWeight());
labelColor.setText("Цвет: " + armoredCar.getArmoredCar().getBodyColor().getRGB());
}
public void setArmoredCar(DrawingArmoredCar armoredCar) {
this.armoredCar = armoredCar;
setData();
}
}

View File

@ -3,22 +3,25 @@
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="2" 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="887" height="526"/>
<xy x="20" y="20" width="947" height="629"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="d36a5" binding="btnPanel" layout-manager="GridLayoutManager" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="d36a5" binding="btnPanel" layout-manager="GridLayoutManager" row-count="13" column-count="3" 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="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<properties>
<name value=""/>
<toolTipText value=""/>
</properties>
<border type="none"/>
<children>
<component id="abbc4" class="javax.swing.JButton" binding="buttonAddArmoredCar">
<constraints>
<grid row="1" column="0" 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="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить бронемашину"/>
@ -26,7 +29,7 @@
</component>
<component id="ab78e" class="javax.swing.JButton" binding="buttonRemoveArmoredCar">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="7" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить бронемашину"/>
@ -34,7 +37,7 @@
</component>
<component id="28eda" class="javax.swing.JButton" binding="buttonShowStorage">
<constraints>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="8" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Посмотреть хранилище"/>
@ -42,7 +45,7 @@
</component>
<component id="633b9" class="javax.swing.JButton" binding="buttonShowOnMap">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="9" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Посмотреть карту"/>
@ -50,7 +53,7 @@
</component>
<component id="30614" class="javax.swing.JTextField" binding="maskedTextBoxPosition">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="6" column="0" 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"/>
</grid>
</constraints>
@ -58,7 +61,7 @@
</component>
<component id="4f2a5" class="javax.swing.JComboBox" binding="mapSelectorComboBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" 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>
@ -70,7 +73,7 @@
</component>
<component id="bf66e" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="6" 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="11" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
@ -81,7 +84,7 @@
</component>
<component id="56422" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="7" 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="12" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Down.png"/>
@ -90,7 +93,7 @@
</component>
<component id="f47b8" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="7" 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="12" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Right.png"/>
@ -99,7 +102,7 @@
</component>
<component id="a0cfb" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="12" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<icon value="Left.png"/>
@ -107,6 +110,46 @@
<visible value="true"/>
</properties>
</component>
<component id="b0b2e" class="javax.swing.JTextField" binding="textBoxNewMapName">
<constraints>
<grid row="0" column="0" 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"/>
</grid>
</constraints>
<properties/>
</component>
<component id="8adee" class="javax.swing.JButton" binding="AddMapButton">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Добавить карту"/>
</properties>
</component>
<component id="e7216" class="javax.swing.JButton" binding="DeleteMapButton">
<constraints>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Удалить карту"/>
</properties>
</component>
<component id="7099e" class="javax.swing.JList" binding="listBoxMaps">
<constraints>
<grid row="3" column="0" row-span="1" col-span="3" 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="ccba6" class="javax.swing.JButton" binding="lookDeletedButton">
<constraints>
<grid row="10" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Посмотреть удаленные объекты"/>
</properties>
</component>
</children>
</grid>
<hspacer id="da837">

View File

@ -1,6 +1,9 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
public class FormMapWithArmoredCars extends JFrame{
private JButton buttonAddArmoredCar;
@ -16,47 +19,39 @@ public class FormMapWithArmoredCars extends JFrame{
private JButton buttonLeft;
private JPanel mainPanel;
private JPanel btnPanel;
private JTextField textBoxNewMapName;
private JButton AddMapButton;
private JButton DeleteMapButton;
private JList listBoxMaps;
private JButton lookDeletedButton;
private Image img;
private MapWithSetArmoredCarsGeneric<DrawingObjectArmoredCar, AbstractMap> _mapArmoredCarsCollectionGeneric;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
put("Простая карта", new SimpleMap());
put("Карта 1", new MyMapLabyrinth());
put("Карта 2", new MyMapWooden());
}};
private final MapsCollection _mapsCollection;
private final Queue<DrawingObjectArmoredCar> queue = new LinkedList<>();
public FormMapWithArmoredCars() {
super("Карта с набором объектов");
setBounds(100, 100, 1000, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mapSelectorComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AbstractMap map = null;
String name = (String) mapSelectorComboBox.getSelectedItem();
switch (name)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Карта 1":
map = new MyMapWooden();
break;
case "Карта 2":
map = new MyMapLabyrinth();
break;
}
if (map != null)
{
_mapArmoredCarsCollectionGeneric = new MapWithSetArmoredCarsGeneric<>(
drawPanel.getWidth(), drawPanel.getHeight(), map);
}
else
{
_mapArmoredCarsCollectionGeneric = null;
}
}
});
mapSelectorComboBox.removeAllItems();
for (var elem : _mapsDict.keySet())
{
mapSelectorComboBox.addItem(elem);
}
buttonAddArmoredCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapArmoredCarsCollectionGeneric == null)
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
@ -67,11 +62,11 @@ public class FormMapWithArmoredCars extends JFrame{
if (form.getSelectedCar() == null)
return;
DrawingObjectArmoredCar armoredCar = new DrawingObjectArmoredCar(form.getSelectedCar());
if (_mapArmoredCarsCollectionGeneric.add(armoredCar) > -1)
if (_mapsCollection.get((String)listBoxMaps.getSelectedValue()).add(armoredCar) > -1)
{
if (form.DialogResult) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
img = _mapArmoredCarsCollectionGeneric.ShowSet();
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
repaint();
}
}
@ -94,10 +89,12 @@ public class FormMapWithArmoredCars extends JFrame{
"Удаление", JOptionPane.YES_NO_OPTION);
if (res == JOptionPane.YES_OPTION) {
int pos = Integer.parseInt(maskedTextBoxPosition.getText());
if (_mapArmoredCarsCollectionGeneric.remove(pos) != null)
DrawingObjectArmoredCar deletedArmoredCar = (DrawingObjectArmoredCar) _mapsCollection.get((String) listBoxMaps.getSelectedValue(), pos);
if (_mapsCollection.get((String) listBoxMaps.getSelectedValue()).remove(pos) != null)
{
queue.add(deletedArmoredCar);
JOptionPane.showMessageDialog(null, "Объект удален");
img = _mapArmoredCarsCollectionGeneric.ShowSet();
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
}
else
{
@ -109,11 +106,11 @@ public class FormMapWithArmoredCars extends JFrame{
buttonShowStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapArmoredCarsCollectionGeneric == null)
if (_mapsCollection.get((String) listBoxMaps.getSelectedValue()) == null)
{
return;
}
img = _mapArmoredCarsCollectionGeneric.ShowSet();
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
repaint();
}
});
@ -121,11 +118,11 @@ public class FormMapWithArmoredCars extends JFrame{
buttonShowOnMap.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_mapArmoredCarsCollectionGeneric == null)
if (_mapsCollection.get((String) listBoxMaps.getSelectedValue()) == null)
{
return;
}
img = _mapArmoredCarsCollectionGeneric.ShowOnMap();
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowOnMap();
repaint();
}
});
@ -135,31 +132,81 @@ public class FormMapWithArmoredCars extends JFrame{
buttonLeft.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
img = _mapArmoredCarsCollectionGeneric.MoveObject(Direction.Left);
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Left);
repaint();
}
});
buttonUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
img = _mapArmoredCarsCollectionGeneric.MoveObject(Direction.Up);
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Up);
repaint();
}
});
buttonDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
img = _mapArmoredCarsCollectionGeneric.MoveObject(Direction.Down);
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Down);
repaint();
}
});
buttonRight.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
img = _mapArmoredCarsCollectionGeneric.MoveObject(Direction.Right);
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).MoveObject(Direction.Right);
repaint();
}
});
AddMapButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (mapSelectorComboBox.getSelectedIndex() == -1 || textBoxNewMapName.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
if (!_mapsDict.containsKey((String) mapSelectorComboBox.getSelectedItem()))
{
JOptionPane.showMessageDialog(null, "Нет такой карты", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.getText(), _mapsDict.get((String) mapSelectorComboBox.getSelectedItem()));
ReloadMaps();
}
});
_mapsCollection = new MapsCollection(drawPanel.getWidth(), drawPanel.getHeight());
listBoxMaps.addListSelectionListener(e -> {
if (listBoxMaps.getSelectedValue() != null) {
img = _mapsCollection.get((String) listBoxMaps.getSelectedValue()).ShowSet();
repaint();
}
});
DeleteMapButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить карту" +
listBoxMaps.getSelectedValue()+ "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
_mapsCollection.DelMap((String) listBoxMaps.getSelectedValue());
ReloadMaps();
}
}
});
lookDeletedButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FormArmoredCar form = new FormArmoredCar();
if (!queue.isEmpty())
form.setArmoredCar(queue.peek().getArmoredCar());
}
});
}
private void createUIComponents() {
@ -172,4 +219,19 @@ public class FormMapWithArmoredCars extends JFrame{
}
};
}
private void ReloadMaps() {
int index = listBoxMaps.getSelectedIndex();
listBoxMaps.setListData(_mapsCollection.getKeys().toArray(new Object[0]));
if (listBoxMaps.getModel().getSize() > 0 && (index == -1 || index >=
listBoxMaps.getModel().getSize()))
{
listBoxMaps.setSelectedIndex(0);
}
else if (listBoxMaps.getModel().getSize() > 0 && index > -1 && index <
listBoxMaps.getModel().getSize())
{
listBoxMaps.setSelectedIndex(index);
}
}
}