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.*;
public class DrawningObjectExcavator implements IDrawningObject{
private DrawningTracktor _tracktor = null;
private DrawningTracktor _tracktor;
public DrawningObjectExcavator(DrawningTracktor tracktor) {
this._tracktor = tracktor;
}
public DrawningTracktor getTracktor() {
return _tracktor;
}
public float getStep() {
if (_tracktor != null && _tracktor.Tracktor != null) {
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">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="663" height="479"/>
<xy x="20" y="20" width="663" height="584"/>
</constraints>
<properties/>
<border type="none"/>
<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"/>
<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">
@ -26,20 +26,9 @@
<text value="Инструменты"/>
</properties>
</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">
<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>
<properties>
<text value="Добавить трактор"/>
@ -47,7 +36,7 @@
</component>
<component id="8925f" class="javax.swing.JFormattedTextField" binding="textBoxPosition">
<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"/>
</grid>
</constraints>
@ -55,7 +44,7 @@
</component>
<component id="e424c" class="javax.swing.JButton" binding="buttonRemoveTracktor">
<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>
<properties>
<text value="Удалить трактор"/>
@ -63,7 +52,7 @@
</component>
<component id="5e5ae" class="javax.swing.JButton" binding="buttonShowStorage">
<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>
<properties>
<text value="Посмотреть хранилище"/>
@ -71,7 +60,7 @@
</component>
<component id="52c38" class="javax.swing.JButton" binding="buttonShowOnMap">
<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>
<properties>
<text value="Показать карту"/>
@ -79,7 +68,7 @@
</component>
<component id="d1efe" class="javax.swing.JButton" binding="buttonLeft">
<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"/>
<maximum-size width="30" height="30"/>
</grid>
@ -91,7 +80,7 @@
</component>
<component id="11d8a" class="javax.swing.JButton" binding="buttonRight">
<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"/>
<maximum-size width="30" height="30"/>
</grid>
@ -103,7 +92,7 @@
</component>
<component id="bffec" class="javax.swing.JButton" binding="buttonUp">
<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"/>
<maximum-size width="30" height="30"/>
</grid>
@ -115,7 +104,7 @@
</component>
<component id="1f0dc" class="javax.swing.JButton" binding="buttonDown">
<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"/>
<maximum-size width="30" height="30"/>
</grid>
@ -127,9 +116,83 @@
</component>
<vspacer id="71dd1">
<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>
</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>
</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">

View File

@ -3,13 +3,16 @@ import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Optional;
public class FormMapWithSetTracktor extends JFrame {
private JPanel ContentPanel;
private JPanel pictureBox;
private JPanel toolsGroup;
private JLabel toolsLabel;
private JComboBox comboBoxMapSelector;
private JComboBox<String> comboBoxMapSelector;
private JButton buttonAddTracktor;
private JFormattedTextField textBoxPosition;
private JButton buttonRemoveTracktor;
@ -19,14 +22,27 @@ public class FormMapWithSetTracktor extends JFrame {
private JButton buttonLeft;
private JButton buttonRight;
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 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() {
this.setTitle("Трактор");
this.setContentPane(ContentPanel);
this.setSize(800,500);
this.setSize(800,600);
this.setVisible(true);
try {
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
@ -34,22 +50,46 @@ public class FormMapWithSetTracktor extends JFrame {
e.printStackTrace();
}
comboBoxMapSelector.addActionListener(e -> {
AbstractMap map = switch (((JComboBox) e.getSource()).getSelectedItem().toString()) {
case "Простая карта" -> new SimpleMap();
case "Свалка карта" -> new DumpMap();
default -> null;
};
_mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight());
comboBoxMapSelector.removeAllItems();
for (var key : _mapsDict.keySet()) {
comboBoxMapSelector.addItem(key);
}
if (map != null) {
_mapTracktorCollectionGeneric = new MapWithSetTracktorGeneric<>(pictureBox.getWidth(), pictureBox.getHeight(), map);
} else {
_mapTracktorCollectionGeneric = 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();
}
});
buttonAddTracktor.addActionListener(e -> {
if (_mapTracktorCollectionGeneric == null) {
if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
@ -61,10 +101,10 @@ public class FormMapWithSetTracktor extends JFrame {
if (dialog.getSelectedTracktor() != null) {
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);
bufferedImage = _mapTracktorCollectionGeneric.showSet();
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
}
else
@ -86,9 +126,11 @@ public class FormMapWithSetTracktor extends JFrame {
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);
bufferedImage = _mapTracktorCollectionGeneric.showSet();
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
@ -96,50 +138,77 @@ public class FormMapWithSetTracktor extends JFrame {
});
buttonShowStorage.addActionListener(e -> {
if (_mapTracktorCollectionGeneric == null) {
if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
bufferedImage = _mapTracktorCollectionGeneric.showSet();
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
});
buttonShowOnMap.addActionListener(e -> {
if (_mapTracktorCollectionGeneric == null) {
if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
bufferedImage = _mapTracktorCollectionGeneric.showOnMap();
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showOnMap();
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 -> {
if (_mapTracktorCollectionGeneric != null) {
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Left);
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left);
repaint();
}
});
buttonRight.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) {
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Right);
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Right);
repaint();
}
});
buttonUp.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) {
bufferedImage = _mapTracktorCollectionGeneric.moveObject(Direction.Up);
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Up);
repaint();
}
});
buttonDown.addActionListener(e -> {
if (_mapTracktorCollectionGeneric != null) {
bufferedImage = _mapTracktorCollectionGeneric.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);

View File

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

View File

@ -1,18 +1,19 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
public class MapWithSetTracktorGeneric <T extends IDrawningObject, U extends AbstractMap>{
public final int _pictureWidth;
public final int _pictureHeight;
public final int _placeSizeWidth = 150;
public final int _placeSizeHeight = 100;
private final SetTracktorGeneric<T> _setTracktor;
public final SetTracktorGeneric<T> _setTracktor;
private final U _map;
public MapWithSetTracktorGeneric(int picWidth, int picHeight, U map) {
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setTracktor = new SetTracktorGeneric<T>(width * height);
_setTracktor = new SetTracktorGeneric<>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
@ -36,15 +37,14 @@ public class MapWithSetTracktorGeneric <T extends IDrawningObject, U extends Abs
public Image showOnMap() {
shaking();
for (int i = 0; i < _setTracktor.getCount(); i++)
for (var tracktor : _setTracktor.getTracktors())
{
var tracktor = _setTracktor.get(i);
if (tracktor != null)
{
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) {
@ -137,18 +137,17 @@ public class MapWithSetTracktorGeneric <T extends IDrawningObject, U extends Abs
int width = _pictureWidth / _placeSizeWidth;
int curWidth = width-1;
int curHeight = 0;
for (int i = 0; i < _setTracktor.getCount(); i++)
{
for(var tracktor : _setTracktor.getTracktors()){
// установка позиции
// Влево - вниз
if (_setTracktor.get(i) != null){
_setTracktor.get(i).setObject(
if (tracktor!=null){
tracktor.setObject(
curWidth * _placeSizeWidth,
curHeight * _placeSizeHeight,
_pictureWidth,
_pictureHeight);
_setTracktor.get(i).drawningObject(g);
tracktor.drawningObject(g);
if (curWidth > 0)
{
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 static void main(String[] args){
FormGallery form = new FormGallery();
form.setSize(500, 300);
form.setVisible(true);
FormMapWithSetTracktor form = new FormMapWithSetTracktor();
form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

View File

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