lab 4 full

This commit is contained in:
MaxKarme 2022-11-25 12:49:46 +03:00
parent 8d9e0c9eb8
commit ace10a75f7
7 changed files with 240 additions and 90 deletions

View File

@ -7,6 +7,10 @@ public class DrawingObjectAircraft implements IDrawingObject
_aircraft = aircraft;
}
public DrawingAircraft getAircraft() {
return _aircraft;
}
public void MoveObject(Direction direction) {
if(_aircraft == null) return;
_aircraft.MoveTransport(direction);

View File

@ -25,6 +25,11 @@ public class FormAircraft extends JDialog implements Form {
return selectedAircraft;
}
public FormAircraft() {}
public FormAircraft(DrawingAircraft aircraft) {
_airFighter = aircraft;
}
public void run() {
add(mainPanel);

View File

@ -3,7 +3,7 @@
<grid id="27dc6" binding="MainPane" 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="500" height="400"/>
<xy x="20" y="20" width="662" height="590"/>
</constraints>
<properties/>
<border type="none"/>
@ -16,7 +16,7 @@
<border type="none"/>
<children/>
</grid>
<grid id="e9a63" layout-manager="GridLayoutManager" row-count="8" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="e9a63" layout-manager="GridLayoutManager" row-count="14" 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="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
@ -26,9 +26,9 @@
<properties/>
<border type="none"/>
<children>
<component id="f23ee" class="javax.swing.JButton" binding="buttonAdd">
<component id="f23ee" class="javax.swing.JButton" binding="buttonAddAircraft">
<constraints>
<grid row="1" 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="6" 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="Добавить"/>
@ -36,7 +36,7 @@
</component>
<component id="2e61e" class="javax.swing.JComboBox" binding="comboBoxSelectorMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" 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="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
@ -45,9 +45,9 @@
</model>
</properties>
</component>
<component id="5c428" class="javax.swing.JButton" binding="buttonRemove">
<component id="5c428" class="javax.swing.JButton" binding="buttonRemoveAircraft">
<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"/>
<grid row="8" 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="Удалить"/>
@ -55,7 +55,7 @@
</component>
<component id="e24b9" class="javax.swing.JButton" binding="buttonShowStorage">
<constraints>
<grid row="4" 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="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"/>
</constraints>
<properties>
<text value="Посмотреть Хранилище"/>
@ -63,7 +63,7 @@
</component>
<component id="ac087" class="javax.swing.JButton" binding="buttonShowOnMap">
<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"/>
<grid row="10" 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="Посмотреть карту"/>
@ -72,7 +72,7 @@
<grid id="2821b" layout-manager="GridLayoutManager" row-count="2" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="13" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="0" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="150" height="-1"/>
</grid>
</constraints>
@ -145,17 +145,64 @@
</grid>
<vspacer id="b833e">
<constraints>
<grid row="6" column="0" 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="0" 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="181ac" class="javax.swing.JTextField" binding="textBoxPosition">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="7" 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="55673" class="javax.swing.JTextField" binding="textBoxNewMapName">
<constraints>
<grid row="0" 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="204a1" class="javax.swing.JButton" binding="buttonAddMap">
<constraints>
<grid row="2" 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="113db" class="javax.swing.JList" binding="listBoxMaps">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="-1" height="100"/>
<preferred-size width="150" height="50"/>
<maximum-size width="-1" height="100"/>
</grid>
</constraints>
<properties/>
</component>
<component id="694cc" class="javax.swing.JButton" binding="buttonDeleteMap">
<constraints>
<grid row="4" 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>
<vspacer id="e3260">
<constraints>
<grid row="5" column="0" 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="8891" class="javax.swing.JButton" binding="buttonShowDeleted">
<constraints>
<grid row="11" 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>
</children>

View File

@ -1,16 +1,14 @@
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.text.ParseException;
import java.util.Objects;
import java.util.*;
import java.util.List;
public class FormMapWithSetAircrafts implements Form {
private JPanel MainPane;
private JButton buttonAdd;
private JButton buttonAddAircraft;
private JComboBox comboBoxSelectorMap;
private JTextField textBoxPosition;
private JButton buttonRemove;
private JButton buttonRemoveAircraft;
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton upButton;
@ -18,10 +16,19 @@ public class FormMapWithSetAircrafts implements Form {
private JButton rightButton;
private JButton downButton;
private JPanel drawPanel;
private JTextField textBoxNewMapName;
private JList listBoxMaps;
private JButton buttonAddMap;
private JButton buttonDeleteMap;
private JButton buttonShowDeleted;
private MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap> _mapCarsCollectionGeneric;
private MapsCollection _mapsCollection;
private HashMap<String, AbstractMap> _mapsDict = new HashMap<>(){{
put( "Простая карта", new SimpleMap() );
put( "Моя карта", new MyMap() );
}};
private Canvas canv = new Canvas(this);
private Queue<IDrawingObject> deletedAircrafts = new ArrayDeque<>();
JFrame jFrame = getFrame();
Image img;
@ -29,43 +36,83 @@ public class FormMapWithSetAircrafts implements Form {
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setBounds(300, 100, 800, 600);
frame.setBounds(300, 50, 1000, 750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
private void ReloadMaps()
{
int index = listBoxMaps.getSelectedIndex();
List<String> items = _mapsCollection.getKeys();
listBoxMaps.setListData(items.toArray());
if (items.size() > 0 && (index == -1 || index >= items.size()))
{
listBoxMaps.setSelectedIndex(0);
}
else if (items.size() > 0 && index > -1 && index < items.size())
{
listBoxMaps.setSelectedIndex(index);
}
}
public void run() {
jFrame.add(MainPane);
drawPanel.add(canv);
jFrame.revalidate();
_mapsCollection = new MapsCollection(canv.getSize().width, canv.getSize().height);
comboBoxSelectorMap.removeAllItems();
_mapsDict.keySet().forEach(elem -> {
comboBoxSelectorMap.addItem(elem);
});
comboBoxSelectorMap.setSelectedIndex(-1);
comboBoxSelectorMap.addActionListener(e -> {
AbstractMap map = null;
switch (comboBoxSelectorMap.getSelectedItem().toString())
listBoxMaps.addListSelectionListener(e -> {
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet();
canv.repaint();
});
buttonAddMap.addActionListener(e -> {
if (comboBoxSelectorMap.getSelectedIndex() == -1 || textBoxNewMapName.getText() == "")
{
case "Простая карта":
map = new SimpleMap();
break;
case "Моя карта":
map = new MyMap();
break;
JOptionPane.showMessageDialog(jFrame, "Не все данные заполнены");
return;
}
if (map != null)
if (!_mapsDict.containsKey(comboBoxSelectorMap.getSelectedItem().toString()))
{
Dimension canvSize = canv.getSize();
_mapCarsCollectionGeneric = new MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap>(
canvSize.width, canvSize.height, map);
JOptionPane.showMessageDialog(jFrame, "Нет такой карты");
return;
}
else
_mapsCollection.AddMap(textBoxNewMapName.getText(),
_mapsDict.get(comboBoxSelectorMap.getSelectedItem().toString()));
ReloadMaps();
});
buttonDeleteMap.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1)
{
_mapCarsCollectionGeneric = null;
return;
}
String mapName = listBoxMaps.getSelectedValue().toString();
if (JOptionPane.showConfirmDialog(jFrame, "Удалить карту " + mapName,
"Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
_mapsCollection.DelMap(mapName);
ReloadMaps();
}
});
buttonAdd.addActionListener(e -> {
if (_mapCarsCollectionGeneric == null) return;
buttonAddAircraft.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
FormAircraft dialog = new FormAircraft();
dialog.run();
@ -78,10 +125,10 @@ public class FormMapWithSetAircrafts implements Form {
if (dialog.getSelectedAircraft() == null) return;
DrawingObjectAircraft aircraft = new DrawingObjectAircraft(dialog.getSelectedAircraft());
if (_mapCarsCollectionGeneric.addAircraft(aircraft) != -1)
if (_mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).addAircraft(aircraft) != -1)
{
JOptionPane.showMessageDialog(jFrame, "Объект добавлен");
img = _mapCarsCollectionGeneric.ShowSet();
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet();
canv.repaint();
}
else
@ -90,9 +137,7 @@ public class FormMapWithSetAircrafts implements Form {
}
});
buttonRemove.addActionListener(e -> {
if(_mapCarsCollectionGeneric == null) return;
buttonRemoveAircraft.addActionListener(e -> {
String text = textBoxPosition.getText();
if(text.isEmpty()) return;
@ -110,9 +155,13 @@ public class FormMapWithSetAircrafts implements Form {
}
pos = Integer.parseInt(text);
if(_mapCarsCollectionGeneric.removeAircraft(pos) != null) {
String mapName = listBoxMaps.getSelectedValue().toString();
IDrawingObject deleted = _mapsCollection.getMap(mapName).removeAircraft(pos);
if(deleted != null) {
JOptionPane.showMessageDialog(jFrame, "Объект удален");
img = _mapCarsCollectionGeneric.ShowSet();
img = _mapsCollection.getMap(mapName).ShowSet();
deletedAircrafts.add(deleted);
canv.repaint();
} else {
JOptionPane.showMessageDialog(jFrame, "Не удалось удалить объект");
@ -120,47 +169,63 @@ public class FormMapWithSetAircrafts implements Form {
});
buttonShowStorage.addActionListener(e -> {
if (_mapCarsCollectionGeneric == null)
{
return;
}
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapCarsCollectionGeneric.ShowSet();
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowSet();
canv.repaint();
});
buttonShowOnMap.addActionListener(e -> {
if (_mapCarsCollectionGeneric == null)
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
canv.repaint();
});
buttonShowDeleted.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
img = _mapCarsCollectionGeneric.ShowOnMap();
canv.repaint();
if(deletedAircrafts.size() == 0) {
JOptionPane.showMessageDialog(jFrame, "Очередь пуста");
return;
}
DrawingObjectAircraft deleted = (DrawingObjectAircraft) deletedAircrafts.peek();
FormAircraft dialog = new FormAircraft(deleted.getAircraft());
deletedAircrafts.remove();
dialog.run();
dialog.setSize(800, 500);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
});
leftButton.addActionListener(e -> {
if(_mapCarsCollectionGeneric == null) return;
img = _mapCarsCollectionGeneric.MoveObject(Direction.Left);
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Left);
canv.repaint();
});
rightButton.addActionListener(e -> {
if(_mapCarsCollectionGeneric == null) return;
img = _mapCarsCollectionGeneric.MoveObject(Direction.Right);
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Right);
canv.repaint();
});
upButton.addActionListener(e -> {
if(_mapCarsCollectionGeneric == null) return;
img = _mapCarsCollectionGeneric.MoveObject(Direction.Up);
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Up);
canv.repaint();
});
downButton.addActionListener(e -> {
if(_mapCarsCollectionGeneric == null) return;
img = _mapCarsCollectionGeneric.MoveObject(Direction.Down);
if(listBoxMaps.getSelectedValue() == null) return;
img = _mapsCollection.getMap(listBoxMaps.getSelectedValue().toString()).MoveObject(Direction.Down);
canv.repaint();
});

View File

@ -1,5 +1,5 @@
public class Main {
public static void main(String[] args) {
new formAircraftGenerator().run();
new FormMapWithSetAircrafts().run();
}
}

38
MapsCollection.java Normal file
View File

@ -0,0 +1,38 @@
import java.util.LinkedHashMap;
import java.util.List;
public class MapsCollection
{
public LinkedHashMap<String, MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap>> _mapStorages;
private int _pictureWidth;
private int _pictureHeight;
public List<String> getKeys() { return _mapStorages.keySet().stream().toList(); }
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new LinkedHashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(String name, AbstractMap map)
{
// TODO Прописать логику для добавления
boolean check = _mapStorages.containsKey(name);
if (check) return;
_mapStorages.put(name, new MapWithSetAircraftsGeneric(_pictureWidth, _pictureHeight, map));
}
public void DelMap(String name)
{
// TODO Прописать логику для удаления
_mapStorages.remove(name);
}
public MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap> getMap(String name)
{
return _mapStorages.getOrDefault(name, null);
}
}

View File

@ -1,50 +1,41 @@
import java.util.ArrayList;
public class SetAircraftsGeneric<T>
{
private T[] _places;
private ArrayList<T> _places;
private int _maxCount;
public int getCount() {
return _places.length;
return _places.size();
}
public SetAircraftsGeneric(int count)
{
_places = (T[])(new Object[count]);
_places = new ArrayList<>();
_maxCount = count;
}
public int Insert(T car)
public int Insert(T aircraft)
{
for(int i = 0; i < _places.length; i++)
{
if (_places[i] == null)
{
_places[i] = car;
return i;
}
}
return -1;
if (_places.size() == _maxCount) return -1;
_places.add(0, aircraft);
return 0;
}
public int Insert(T car, int position)
public int Insert(T aircraft, int position)
{
int index = position;
while (_places[index] != null && index < _places.length) index++;
if (index == _places.length) return -1;
for (int i = index; i > position; --i) _places[i] = _places[i - 1];
_places[position] = car;
if (_places.size() == _maxCount) return -1;
_places.add(position, aircraft);
return position;
}
public T Remove(int position)
{
if(position >= _places.length) return null;
T res = _places[position];
_places[position] = null;
if(position > _maxCount || position < 0) return null;
T res = _places.get(position);
_places.remove(res);
return res;
}
public T Get(int position)
{
return _places[position];
return _places.get(position);
}
}