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; _aircraft = aircraft;
} }
public DrawingAircraft getAircraft() {
return _aircraft;
}
public void MoveObject(Direction direction) { public void MoveObject(Direction direction) {
if(_aircraft == null) return; if(_aircraft == null) return;
_aircraft.MoveTransport(direction); _aircraft.MoveTransport(direction);

View File

@ -25,6 +25,11 @@ public class FormAircraft extends JDialog implements Form {
return selectedAircraft; return selectedAircraft;
} }
public FormAircraft() {}
public FormAircraft(DrawingAircraft aircraft) {
_airFighter = aircraft;
}
public void run() { public void run() {
add(mainPanel); 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"> <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"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<xy x="20" y="20" width="500" height="400"/> <xy x="20" y="20" width="662" height="590"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
@ -16,7 +16,7 @@
<border type="none"/> <border type="none"/>
<children/> <children/>
</grid> </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"/> <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,9 +26,9 @@
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <children>
<component id="f23ee" class="javax.swing.JButton" binding="buttonAdd"> <component id="f23ee" class="javax.swing.JButton" binding="buttonAddAircraft">
<constraints> <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> </constraints>
<properties> <properties>
<text value="Добавить"/> <text value="Добавить"/>
@ -36,7 +36,7 @@
</component> </component>
<component id="2e61e" class="javax.swing.JComboBox" binding="comboBoxSelectorMap"> <component id="2e61e" class="javax.swing.JComboBox" binding="comboBoxSelectorMap">
<constraints> <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> </constraints>
<properties> <properties>
<model> <model>
@ -45,9 +45,9 @@
</model> </model>
</properties> </properties>
</component> </component>
<component id="5c428" class="javax.swing.JButton" binding="buttonRemove"> <component id="5c428" class="javax.swing.JButton" binding="buttonRemoveAircraft">
<constraints> <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> </constraints>
<properties> <properties>
<text value="Удалить"/> <text value="Удалить"/>
@ -55,7 +55,7 @@
</component> </component>
<component id="e24b9" class="javax.swing.JButton" binding="buttonShowStorage"> <component id="e24b9" class="javax.swing.JButton" binding="buttonShowStorage">
<constraints> <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> </constraints>
<properties> <properties>
<text value="Посмотреть Хранилище"/> <text value="Посмотреть Хранилище"/>
@ -63,7 +63,7 @@
</component> </component>
<component id="ac087" class="javax.swing.JButton" binding="buttonShowOnMap"> <component id="ac087" class="javax.swing.JButton" binding="buttonShowOnMap">
<constraints> <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> </constraints>
<properties> <properties>
<text value="Посмотреть карту"/> <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"> <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"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <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"/> <minimum-size width="150" height="-1"/>
</grid> </grid>
</constraints> </constraints>
@ -145,17 +145,64 @@
</grid> </grid>
<vspacer id="b833e"> <vspacer id="b833e">
<constraints> <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> </constraints>
</vspacer> </vspacer>
<component id="181ac" class="javax.swing.JTextField" binding="textBoxPosition"> <component id="181ac" class="javax.swing.JTextField" binding="textBoxPosition">
<constraints> <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"/> <preferred-size width="150" height="-1"/>
</grid> </grid>
</constraints> </constraints>
<properties/> <properties/>
</component> </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> </children>
</grid> </grid>
</children> </children>

View File

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

View File

@ -1,5 +1,5 @@
public class Main { public class Main {
public static void main(String[] args) { 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> public class SetAircraftsGeneric<T>
{ {
private T[] _places; private ArrayList<T> _places;
private int _maxCount;
public int getCount() { public int getCount() {
return _places.length; return _places.size();
} }
public SetAircraftsGeneric(int count) 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.size() == _maxCount) return -1;
{ _places.add(0, aircraft);
if (_places[i] == null) return 0;
{
_places[i] = car;
return i;
}
}
return -1;
} }
public int Insert(T car, int position) public int Insert(T aircraft, int position)
{ {
int index = position; if (_places.size() == _maxCount) return -1;
_places.add(position, aircraft);
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;
return position; return position;
} }
public T Remove(int position) public T Remove(int position)
{ {
if(position >= _places.length) return null; if(position > _maxCount || position < 0) return null;
T res = _places[position]; T res = _places.get(position);
_places[position] = null; _places.remove(res);
return res; return res;
} }
public T Get(int position) public T Get(int position)
{ {
return _places[position]; return _places.get(position);
} }
} }