Borschevskaya A.A. Lab Work 4 Hard #5

Closed
pgirl1 wants to merge 4 commits from lab4 into lab3
7 changed files with 248 additions and 99 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -47,13 +47,9 @@ public class MapWithSetArmoredCarsGeneric<T extends IDrawingObject, U extends Ab
public Image ShowOnMap()
{
Shaking();
for (int i = 0; i < _setCars.getCount(); i++)
for (var armoredCar : _setCars.GetArmoredCars())
{
var car = _setCars.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, armoredCar);
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_BGR);
}
@ -135,12 +131,16 @@ public class MapWithSetArmoredCarsGeneric<T extends IDrawingObject, U extends Ab
private void DrawArmoredCars(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _setCars.getCount(); i++)
int i = 0;
for (var armoredCar : _setCars.GetArmoredCars())
{
if (_setCars.Get(i) != null) {
_setCars.Get(i).SetObject(i % width * _placeSizeWidth + 5, i / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
_setCars.Get(i).DrawingObject((Graphics2D) g);
}
armoredCar.SetObject(i % width * _placeSizeWidth + 5, i / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
armoredCar.DrawingObject((Graphics2D) g);
i++;
}
}
public SetArmoredCarsGeneric<T> get_setCars() {
return _setCars;
}
}

View File

@ -0,0 +1,46 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
public class MapsCollection {
public HashMap<String, MapWithSetArmoredCarsGeneric<IDrawingObject, AbstractMap>> _mapStorages;
private int _pictureWidth;
private int _pictureHeight;
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new HashMap<>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public Set<String> getKeys() {
return _mapStorages.keySet();
}
public void AddMap(String name, AbstractMap map)
{
if (!getKeys().contains(name))
_mapStorages.put(name, new MapWithSetArmoredCarsGeneric<>(_pictureWidth, _pictureHeight, map));
}
public void DelMap(String name)
{
if (getKeys().contains(name))
_mapStorages.remove(name);
}
public MapWithSetArmoredCarsGeneric<IDrawingObject, AbstractMap> get(String ind)
{
if (getKeys().contains(ind))
return _mapStorages.get(ind);
return null;
}
// дополнительное задание: метод "индексатор" с двумя параметрами
public IDrawingObject get(String ind1, int pos) {
if (_mapStorages.containsKey(ind1))
return _mapStorages.get(ind1).get_setCars().Get(pos);
return null;
}
}

View File

@ -1,16 +1,17 @@
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.*;
public class SetArmoredCarsGeneric<T> {
private T[] _places;
private ArrayList<T> _places;
private int maxCount;
public SetArmoredCarsGeneric(int count)
{
_places = (T[]) new Object[count];
_places = new ArrayList<T>();
maxCount = count;
}
public int getCount() {
return _places != null ? _places.length : 0;
return _places != null ? _places.size() : 0;
}
public int Insert(T armoredCar)
@ -20,47 +21,31 @@ public class SetArmoredCarsGeneric<T> {
public int Insert(T armoredCar, int position)
{
if (position < 0 || position >= getCount())
if (getCount() >= maxCount || position < 0 || position >= maxCount)
return -1;
if (!(_places[position] == null))
{
int index_empty = -1;
// поиск первого пустого элемента
for (int i = position + 1; i < getCount(); i++)
{
if (_places[i] == null)
{
index_empty = i;
}
}
if (index_empty == -1)
return -1;
else
{
for (int i = index_empty; i > position; i--)
{
_places[i] = _places[i - 1];
}
}
}
_places[position] = armoredCar;
_places.add(position, armoredCar);
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= getCount())
if (position < 0 || position >= _places.size())
return null;
T armoredCar = _places[position];
_places[position] = null;
T armoredCar = _places.get(position);
_places.remove(position);
return armoredCar;
}
public T Get(int position)
{
if (position < 0 || position >= getCount())
if (position < 0 || position >= maxCount)
return null;
return _places[position];
return _places.get(position);
}
public Iterable<T> GetArmoredCars()
{
return () -> _places.stream().filter(Objects::nonNull).iterator();
}
}