LabWork04: Работа готова, возможен рефакторинг

This commit is contained in:
Safgerd 2022-11-22 00:21:46 +04:00
parent a2cf154aa4
commit 2e82bbe98d
8 changed files with 260 additions and 101 deletions

View File

@ -2,12 +2,16 @@ import javax.print.DocFlavor;
import java.awt.*; import java.awt.*;
public class DrawningObjectExcavator implements IDrawningObject{ public class DrawningObjectExcavator implements IDrawningObject{
private DrawningTracktor _tracktor = null; private DrawningTracktor _tracktor;
public DrawningObjectExcavator(DrawningTracktor tracktor) { public DrawningObjectExcavator(DrawningTracktor tracktor) {
this._tracktor = tracktor; this._tracktor = tracktor;
} }
public DrawningTracktor getTracktor() {
return _tracktor;
}
public float getStep() { public float getStep() {
if (_tracktor != null && _tracktor.Tracktor != null) { if (_tracktor != null && _tracktor.Tracktor != null) {
return _tracktor.Tracktor.getStep(); return _tracktor.Tracktor.getStep();

View File

@ -3,12 +3,12 @@
<grid id="27dc6" binding="ContentPanel" 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="ContentPanel" 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="663" height="479"/> <xy x="20" y="20" width="663" height="584"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <children>
<grid id="afeba" binding="toolsGroup" layout-manager="GridLayoutManager" row-count="10" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="afeba" binding="toolsGroup" layout-manager="GridLayoutManager" row-count="15" column-count="3" 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">
@ -26,20 +26,9 @@
<text value="Инструменты"/> <text value="Инструменты"/>
</properties> </properties>
</component> </component>
<component id="c455c" class="javax.swing.JComboBox" binding="comboBoxMapSelector">
<constraints>
<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>
<item value="Простая карта"/>
<item value="Свалка карта"/>
</model>
</properties>
</component>
<component id="ca274" class="javax.swing.JButton" binding="buttonAddTracktor"> <component id="ca274" class="javax.swing.JButton" binding="buttonAddTracktor">
<constraints> <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"/> <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> </constraints>
<properties> <properties>
<text value="Добавить трактор"/> <text value="Добавить трактор"/>
@ -47,7 +36,7 @@
</component> </component>
<component id="8925f" class="javax.swing.JFormattedTextField" binding="textBoxPosition"> <component id="8925f" class="javax.swing.JFormattedTextField" binding="textBoxPosition">
<constraints> <constraints>
<grid row="3" 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="8" 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"/> <preferred-size width="150" height="-1"/>
</grid> </grid>
</constraints> </constraints>
@ -55,7 +44,7 @@
</component> </component>
<component id="e424c" class="javax.swing.JButton" binding="buttonRemoveTracktor"> <component id="e424c" class="javax.swing.JButton" binding="buttonRemoveTracktor">
<constraints> <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="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> </constraints>
<properties> <properties>
<text value="Удалить трактор"/> <text value="Удалить трактор"/>
@ -63,7 +52,7 @@
</component> </component>
<component id="5e5ae" class="javax.swing.JButton" binding="buttonShowStorage"> <component id="5e5ae" class="javax.swing.JButton" binding="buttonShowStorage">
<constraints> <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="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> </constraints>
<properties> <properties>
<text value="Посмотреть хранилище"/> <text value="Посмотреть хранилище"/>
@ -71,7 +60,7 @@
</component> </component>
<component id="52c38" class="javax.swing.JButton" binding="buttonShowOnMap"> <component id="52c38" class="javax.swing.JButton" binding="buttonShowOnMap">
<constraints> <constraints>
<grid row="6" 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="11" 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> </constraints>
<properties> <properties>
<text value="Показать карту"/> <text value="Показать карту"/>
@ -79,7 +68,7 @@
</component> </component>
<component id="d1efe" class="javax.swing.JButton" binding="buttonLeft"> <component id="d1efe" class="javax.swing.JButton" binding="buttonLeft">
<constraints> <constraints>
<grid row="9" 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="14" column="0" 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"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
</grid> </grid>
@ -91,7 +80,7 @@
</component> </component>
<component id="11d8a" class="javax.swing.JButton" binding="buttonRight"> <component id="11d8a" class="javax.swing.JButton" binding="buttonRight">
<constraints> <constraints>
<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"> <grid row="14" 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"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
</grid> </grid>
@ -103,7 +92,7 @@
</component> </component>
<component id="bffec" class="javax.swing.JButton" binding="buttonUp"> <component id="bffec" class="javax.swing.JButton" binding="buttonUp">
<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="13" 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"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
</grid> </grid>
@ -115,7 +104,7 @@
</component> </component>
<component id="1f0dc" class="javax.swing.JButton" binding="buttonDown"> <component id="1f0dc" class="javax.swing.JButton" binding="buttonDown">
<constraints> <constraints>
<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"> <grid row="14" 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"/>
<maximum-size width="30" height="30"/> <maximum-size width="30" height="30"/>
</grid> </grid>
@ -127,9 +116,83 @@
</component> </component>
<vspacer id="71dd1"> <vspacer id="71dd1">
<constraints> <constraints>
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> <grid row="12" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</vspacer> </vspacer>
<grid id="c0175" binding="mapsGroup" layout-manager="GridLayoutManager" row-count="6" 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="1" column="0" row-span="4" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="5cce4" 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>
<component id="65411" class="javax.swing.JTextField" binding="textFieldMapName">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" 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="c455c" class="javax.swing.JComboBox" binding="comboBoxMapSelector">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" 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="e0b94" class="javax.swing.JButton" binding="buttonAddMap">
<constraints>
<grid row="3" 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>
<text value="Добавить карту"/>
</properties>
</component>
<component id="3ffeb" class="javax.swing.JList" binding="listBoxMaps">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" 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="7fd9" class="javax.swing.JButton" binding="buttonDeleteMap">
<constraints>
<grid row="5" 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>
<text value="Удалить карту"/>
</properties>
</component>
</children>
</grid>
<vspacer id="f967a">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="edacb" class="javax.swing.JButton" binding="buttonShowDeleted">
<constraints>
<grid row="6" 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> </children>
</grid> </grid>
<grid id="c1cce" 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="c1cce" binding="pictureBox" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

View File

@ -3,13 +3,16 @@ import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter; import javax.swing.text.MaskFormatter;
import java.awt.*; import java.awt.*;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Optional;
public class FormMapWithSetTracktor extends JFrame { public class FormMapWithSetTracktor extends JFrame {
private JPanel ContentPanel; private JPanel ContentPanel;
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 buttonAddTracktor; private JButton buttonAddTracktor;
private JFormattedTextField textBoxPosition; private JFormattedTextField textBoxPosition;
private JButton buttonRemoveTracktor; private JButton buttonRemoveTracktor;
@ -19,14 +22,27 @@ public class FormMapWithSetTracktor extends JFrame {
private JButton buttonLeft; private JButton buttonLeft;
private JButton buttonRight; private JButton buttonRight;
private JButton buttonDown; private JButton buttonDown;
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 MapWithSetTracktorGeneric<DrawningObjectExcavator, AbstractMap> _mapTracktorCollectionGeneric; private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
put("Простая карта", new SimpleMap());
put("Свалка карта", new DumpMap());
}};
private final MapsCollection _mapsCollection;
private final LinkedList<IDrawningObject> deletedObjects = new LinkedList<>();
public FormMapWithSetTracktor() { public FormMapWithSetTracktor() {
this.setTitle("Трактор"); this.setTitle("Трактор");
this.setContentPane(ContentPanel); this.setContentPane(ContentPanel);
this.setSize(800,500); this.setSize(800,600);
this.setVisible(true);
try { try {
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##"))); textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
@ -34,22 +50,46 @@ public class FormMapWithSetTracktor extends JFrame {
e.printStackTrace(); e.printStackTrace();
} }
comboBoxMapSelector.addActionListener(e -> { _mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight());
AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) { comboBoxMapSelector.removeAllItems();
case "Простая карта" -> new SimpleMap(); for (var key : _mapsDict.keySet()) {
case "Свалка карта" -> new DumpMap(); comboBoxMapSelector.addItem(key);
default -> null; }
};
if (map != null) { buttonAddMap.addActionListener(e -> {
_mapTracktorCollectionGeneric = new MapWithSetTracktorGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map); if (comboBoxMapSelector.getSelectedIndex() == -1 || textFieldMapName.getText() == null || textFieldMapName.getText().isEmpty()) {
} else { JOptionPane.showMessageDialog(this, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE);
_mapTracktorCollectionGeneric = null; 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();
}
});
buttonAddTracktor.addActionListener(e -> { buttonAddTracktor.addActionListener(e -> {
if (_mapTracktorCollectionGeneric == null) { if (listBoxMaps.getSelectedIndex() == -1) {
return; return;
} }
@ -61,10 +101,10 @@ public class FormMapWithSetTracktor extends JFrame {
if (dialog.getSelectedTracktor() != null) { if (dialog.getSelectedTracktor() != null) {
DrawningObjectExcavator tracktor = new DrawningObjectExcavator(dialog.getSelectedTracktor()); DrawningObjectExcavator tracktor = new DrawningObjectExcavator(dialog.getSelectedTracktor());
if (_mapTracktorCollectionGeneric.addTracktor(tracktor) != -1) if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addTracktor(tracktor) != -1)
{ {
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapTracktorCollectionGeneric.showSet(); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint(); repaint();
} }
else else
@ -86,9 +126,11 @@ public class FormMapWithSetTracktor extends JFrame {
int position = Integer.parseInt(text); int position = Integer.parseInt(text);
if (_mapTracktorCollectionGeneric.removeTracktorAt(position) != null) { IDrawningObject deleted = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).removeTracktorAt(position);
if (deleted!=null) {
deletedObjects.add(deleted);
JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapTracktorCollectionGeneric.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);
@ -96,50 +138,77 @@ public class FormMapWithSetTracktor extends JFrame {
}); });
buttonShowStorage.addActionListener(e -> { buttonShowStorage.addActionListener(e -> {
if (_mapTracktorCollectionGeneric == null) { if (listBoxMaps.getSelectedIndex() == -1) {
return; return;
} }
bufferedImage = _mapTracktorCollectionGeneric.showSet(); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint(); repaint();
}); });
buttonShowOnMap.addActionListener(e -> { buttonShowOnMap.addActionListener(e -> {
if (_mapTracktorCollectionGeneric == null) { if (listBoxMaps.getSelectedIndex() == -1) {
return; return;
} }
bufferedImage = _mapTracktorCollectionGeneric.showOnMap(); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showOnMap();
repaint(); repaint();
}); });
buttonShowDeleted.addActionListener(e -> {
if (!deletedObjects.isEmpty()) {
DrawningObjectExcavator deleted = (DrawningObjectExcavator) deletedObjects.pop();
FormTracktor dialog = new FormTracktor(deleted.getTracktor());
dialog.setSize(800, 500);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} else {
JOptionPane.showMessageDialog(this, "Связанный список удалённых объектов пуст", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
});
buttonLeft.addActionListener(e -> { buttonLeft.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) { if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Left); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left);
repaint(); repaint();
} }
}); });
buttonRight.addActionListener(e -> { buttonRight.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) { if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Right); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Right);
repaint(); repaint();
} }
}); });
buttonUp.addActionListener(e -> { buttonUp.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) { if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Up); bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Up);
repaint(); repaint();
} }
}); });
buttonDown.addActionListener(e -> { buttonDown.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) { if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapTracktorCollectionGeneric.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

@ -114,6 +114,11 @@ public class FormTracktor extends JDialog {
repaint(); repaint();
} }
public FormTracktor(DrawningTracktor tracktor) {
this();
_tracktor = tracktor;
repaint();
}
public DrawningTracktor getSelectedTracktor() { public DrawningTracktor getSelectedTracktor() {
return selectedTracktor; return selectedTracktor;
} }

View File

@ -1,18 +1,19 @@
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.LinkedList;
public class MapWithSetTracktorGeneric <T extends IDrawningObject, U extends AbstractMap>{ public class MapWithSetTracktorGeneric <T extends IDrawningObject, U extends AbstractMap>{
public final int _pictureWidth; public final int _pictureWidth;
public final int _pictureHeight; public final int _pictureHeight;
public final int _placeSizeWidth = 150; public final int _placeSizeWidth = 150;
public final int _placeSizeHeight = 100; public final int _placeSizeHeight = 100;
private final SetTracktorGeneric<T> _setTracktor; public final SetTracktorGeneric<T> _setTracktor;
private final U _map; private final U _map;
public MapWithSetTracktorGeneric(int picWidth, int picHeight, U map) { public MapWithSetTracktorGeneric(int picWidth, int picHeight, U map) {
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight; int height = picHeight / _placeSizeHeight;
_setTracktor = new SetTracktorGeneric<T>(width * height); _setTracktor = new SetTracktorGeneric<>(width * height);
_pictureWidth = picWidth; _pictureWidth = picWidth;
_pictureHeight = picHeight; _pictureHeight = picHeight;
_map = map; _map = map;
@ -36,15 +37,14 @@ public class MapWithSetTracktorGeneric <T extends IDrawningObject, U extends Abs
public Image showOnMap() { public Image showOnMap() {
shaking(); shaking();
for (int i = 0; i < _setTracktor.getCount(); i++) for (var tracktor : _setTracktor.getTracktors())
{ {
var tracktor = _setTracktor.get(i);
if (tracktor != null) if (tracktor != null)
{ {
return _map.CreateMap(_pictureWidth, _pictureHeight, tracktor); return _map.CreateMap(_pictureWidth, _pictureHeight, tracktor);
} }
} }
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
} }
public Image moveObject(Direction direction) { public Image moveObject(Direction direction) {
@ -137,18 +137,17 @@ public class MapWithSetTracktorGeneric <T extends IDrawningObject, U extends Abs
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int curWidth = width-1; int curWidth = width-1;
int curHeight = 0; int curHeight = 0;
for (int i = 0; i < _setTracktor.getCount(); i++)
{ for(var tracktor : _setTracktor.getTracktors()){
// установка позиции // установка позиции
// Влево - вниз // Влево - вниз
if (_setTracktor.get(i) != null){ if (tracktor!=null){
_setTracktor.get(i).setObject( tracktor.setObject(
curWidth * _placeSizeWidth, curWidth * _placeSizeWidth,
curHeight * _placeSizeHeight, curHeight * _placeSizeHeight,
_pictureWidth, _pictureWidth,
_pictureHeight); _pictureHeight);
tracktor.drawningObject(g);
_setTracktor.get(i).drawningObject(g);
if (curWidth > 0) if (curWidth > 0)
{ {
curWidth--; curWidth--;

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, MapWithSetTracktorGeneric<DrawningObjectExcavator, 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 MapWithSetTracktorGeneric<>(_pictureWidth, _pictureHeight, map));
}
}
public void deleteMap(String name) {
_mapsStorage.remove(name);
}
public MapWithSetTracktorGeneric<DrawningObjectExcavator, 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(); FormMapWithSetTracktor form = new FormMapWithSetTracktor();
form.setSize(500, 300);
form.setVisible(true);
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} }
} }

View File

@ -1,12 +1,17 @@
import java.util.ArrayList;
import java.util.Objects;
public class SetTracktorGeneric<T> { public class SetTracktorGeneric<T> {
private final Object[] _places; private final ArrayList<T> _places;
private final int _maxCount;
public int getCount() { public int getCount() {
return _places.length; return _places.size();
} }
public SetTracktorGeneric(int count) { public SetTracktorGeneric(int count) {
_places = new Object[count]; _maxCount = count;
_places = new ArrayList<>(count);
} }
public int insert(T tracktor) { public int insert(T tracktor) {
@ -14,43 +19,22 @@ public class SetTracktorGeneric<T> {
} }
public int insert(T tracktor, int position) { public int insert(T tracktor, int position) {
if (position < 0 || position >= getCount()) { if (position < 0 || position > getCount() || getCount() == _maxCount) {
return -1; return -1;
} }
_places.add(position, tracktor);
if (_places[position] == null) { return position;
_places[position] = tracktor;
return position;
}
int tmp = -1;
for(int i = position; i < getCount(); i++)
{
if (_places[i] == null)
{
tmp = i;
break;
}
}
if (tmp != -1)
{
System.arraycopy(_places, position, _places, position + 1, tmp - position);
_places[position] = tracktor;
return position;
}
return -1;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T remove(int position) { public T remove(int position) {
if (position < getCount() && position >= 0) if (position < 0 || position >= getCount())
{ {
T temp = (T) _places[position]; return null;
_places[position] = null;
return temp;
} }
return null; T result = _places.get(position);
_places.remove(position);
return result;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -58,7 +42,11 @@ public class SetTracktorGeneric<T> {
if (position < 0 || position >= getCount()) { if (position < 0 || position >= getCount()) {
return null; return null;
} }
return _places.get(position);
}
return (T) _places[position]; public Iterable<T> getTracktors()
{
return _places;
} }
} }