Начало работ по 8-й лабораторной.

This commit is contained in:
Programmist73 2022-12-03 20:26:20 +04:00
parent 8feb7ccbc4
commit 4327f0f4b6
9 changed files with 321 additions and 17 deletions

View File

@ -2,3 +2,4 @@ INFO FormMapWithSetPlanesGeneric:157 - Добавлена карта 123 02-12-
INFO FormMapWithSetPlanesGeneric:191 - Осуществлён переход на карту под названием 123 02-12-2022
INFO FormMapWithSetPlanesGeneric:157 - Добавлена карта 454 02-12-2022
INFO FormMapWithSetPlanesGeneric:191 - Осуществлён переход на карту под названием 454 02-12-2022
INFO FormMapWithSetPlanesGeneric:193 - Осуществлён переход на карту под названием 123 03-12-2022

View File

@ -181,6 +181,31 @@ public abstract class AbstractMap
return bmp;
}
//сравнение
@Override
public boolean equals(Object obj)
{
AbstractMap other = (AbstractMap)obj;
if (other == null || _map != other._map || _height != other._height || _width != other._width
|| _size_x != other._size_x || _size_y != other._size_y || getClass() != other.getClass())
{
return false;
}
for(int i = 0; i < _map.length; i++)
{
for(int j = 0; j < _map[i].length; j++)
{
if (_map[i][j] != other._map[i][j])
{
return false;
}
}
}
return true;
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics g, int i, int j);
protected abstract void DrawBarrierPart(Graphics g, int i, int j);

View File

@ -11,6 +11,11 @@ public class DrawningObjectPlane implements IDrawningObject
public DrawningObjectPlane(DrawingPlane plane){ _plane = plane; }
public DrawingPlane GetPlane()
{
return _plane;
}
@Override
public float Step()
{
@ -60,6 +65,62 @@ public class DrawningObjectPlane implements IDrawningObject
return null;
}
@Override
public boolean equals(Object obj)
{
if(obj == null)
{
return false;
}
var otherPlane = (DrawningObjectPlane) obj;
if(otherPlane == null)
{
return false;
}
var plane = _plane.Plane;
var otherPlanePlane = otherPlane._plane.Plane;
if(plane.GetSpeed() != otherPlanePlane.GetSpeed())
{
return false;
}
if(plane.GetWeight() != otherPlanePlane.GetWeight())
{
return false;
}
if(plane.GetColor().getRGB() != otherPlanePlane.GetColor().getRGB())
{
return false;
}
if(plane.getClass().getSimpleName() != otherPlanePlane.getClass().getSimpleName())
{
return false;
}
if (plane instanceof EntityAirbus entitySuperAirbus && otherPlanePlane instanceof EntityAirbus otherEntitySuperAirbus)
{
if(entitySuperAirbus.AddEngine() != otherEntitySuperAirbus.AddEngine())
{
return false;
}
if(entitySuperAirbus.AddCompartment() != otherEntitySuperAirbus.AddCompartment())
{
return false;
}
return entitySuperAirbus.AddColor().getRGB() == otherEntitySuperAirbus.AddColor().getRGB();
}
return true;
}
public static IDrawningObject Create(String data)
{
return new DrawningObjectPlane(ExtentionPlane.CreateDrawingPlane(data));

View File

@ -3,7 +3,7 @@
<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="994" height="683"/>
<xy x="20" y="20" width="994" height="713"/>
</constraints>
<properties/>
<border type="none"/>
@ -18,9 +18,16 @@
</constraints>
<properties/>
<border type="none"/>
<children/>
<children>
<scrollpane id="6113d">
<constraints border-constraint="East"/>
<properties/>
<border type="none"/>
<children/>
</scrollpane>
</children>
</grid>
<grid id="4871f" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="17" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="4871f" binding="GroupBoxTools" layout-manager="GridLayoutManager" row-count="20" 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="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
@ -34,7 +41,7 @@
<children>
<component id="d0967" class="javax.swing.JButton" binding="ButtonLeft">
<constraints>
<grid row="16" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
<grid row="19" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="45" height="45"/>
<preferred-size width="45" height="45"/>
<maximum-size width="45" height="45"/>
@ -46,7 +53,7 @@
</component>
<component id="d9af3" class="javax.swing.JButton" binding="ButtonDown">
<constraints>
<grid row="16" 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="19" 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="45" height="45"/>
<preferred-size width="45" height="45"/>
<maximum-size width="45" height="45"/>
@ -58,7 +65,7 @@
</component>
<component id="9e43" class="javax.swing.JButton" binding="ButtonRight">
<constraints>
<grid row="16" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<grid row="19" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="45" height="45"/>
<preferred-size width="45" height="45"/>
<maximum-size width="45" height="45"/>
@ -70,7 +77,7 @@
</component>
<component id="4d1dd" class="javax.swing.JButton" binding="ButtonUp">
<constraints>
<grid row="15" 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="18" 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="45" height="45"/>
<preferred-size width="45" height="45"/>
<maximum-size width="45" height="45"/>
@ -82,7 +89,7 @@
</component>
<component id="67190" class="javax.swing.JButton" binding="ButtonShowOnMap">
<constraints>
<grid row="13" 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="16" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -94,7 +101,7 @@
</component>
<component id="28576" class="javax.swing.JButton" binding="ButtonShowStorage">
<constraints>
<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">
<grid row="14" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -106,7 +113,7 @@
</component>
<component id="7119" class="javax.swing.JButton" binding="ButtonAddPlane">
<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="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">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -118,7 +125,7 @@
</component>
<component id="6f119" class="javax.swing.JTextField" binding="MaskedTextBoxPosition">
<constraints>
<grid row="7" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="10" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -131,7 +138,7 @@
</component>
<component id="d75e9" class="javax.swing.JButton" binding="ButtonRemovePlane">
<constraints>
<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">
<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">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -143,22 +150,22 @@
</component>
<vspacer id="f972a">
<constraints>
<grid row="10" 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="13" 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>
<vspacer id="65c78">
<constraints>
<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"/>
<grid row="15" 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>
<vspacer id="7d70b">
<constraints>
<grid row="14" 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="17" 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>
<vspacer id="d4b8a">
<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"/>
<grid row="8" 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>
<grid id="fe895" binding="GroupBoxMap" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
@ -235,7 +242,7 @@
</grid>
<component id="860bc" class="javax.swing.JButton" binding="ButtonShowDelete">
<constraints>
<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">
<grid row="12" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
@ -245,6 +252,35 @@
<text value="Посмотреть удалённый самолёт"/>
</properties>
</component>
<component id="75db3" class="javax.swing.JButton" binding="ButtonSortByType">
<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">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
</grid>
</constraints>
<properties>
<text value="Сортировка по типу"/>
</properties>
</component>
<vspacer id="5ec73">
<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="beb6c" class="javax.swing.JButton" binding="ButtonSortByColor">
<constraints>
<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">
<minimum-size width="220" height="30"/>
<preferred-size width="220" height="30"/>
<maximum-size width="220" height="30"/>
</grid>
</constraints>
<properties>
<text value="Сортировка по цвету"/>
</properties>
</component>
</children>
</grid>
<grid id="bb85f" class="javax.swing.JMenuBar" binding="MenuBarSaveLoad" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

View File

@ -38,6 +38,8 @@ public class FormMapWithSetPlanesGeneric extends JFrame{
private JMenuItem MenuItemSaveData;
private JMenuItem MenuItemLoadOneMapData;
private JMenuItem MenuItemSaveOneMapData;
private JButton ButtonSortByType;
private JButton ButtonSortByColor;
//объект от коллекции карт
private final MapsCollection _mapsCollection;
@ -439,6 +441,34 @@ public class FormMapWithSetPlanesGeneric extends JFrame{
}
});
//сортировка по типу
ButtonSortByType.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Sort(new PlaneCompareByType());
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
});
//сортировка по цвету
ButtonSortByColor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(ListBoxMaps.getSelectedIndex() == -1)
{
return;
}
_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).Sort(new PlaneCompareByColor());
UpdateWindow(_mapsCollection.get(ListBoxMaps.getSelectedValue().toString()).ShowSet());
}
});
//обработка нажатия загрузки всех карт
MenuItemLoadData.addActionListener(new ActionListener() {
@Override

View File

@ -1,6 +1,8 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.SortedSet;
public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends AbstractMap>
{
@ -109,6 +111,12 @@ public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends Abstr
}
}
//сортировка
public void Sort(Comparator<T> comparer)
{
_setPlanes.SortSet(comparer);
}
//"взламываем" набор, чтобы все элементы оказались в начале
private void Shaking() throws StorageOverflowException, PlaneNotFoundException {
int j = _setPlanes.Count() - 1;

View File

@ -0,0 +1,66 @@
import java.awt.*;
import java.util.Comparator;
public class PlaneCompareByColor implements Comparator<IDrawningObject>
{
@Override
public int compare(IDrawningObject x, IDrawningObject y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y != null)
{
return -1;
}
DrawningObjectPlane xPlane = (DrawningObjectPlane)x;
DrawningObjectPlane yPlane = (DrawningObjectPlane)y;
if (xPlane == null && yPlane == null)
{
return 0;
}
if (xPlane == null && yPlane != null)
{
return 1;
}
if (xPlane != null && yPlane == null)
{
return 1;
}
if(xPlane.GetPlane().Plane.GetSpeed() != yPlane.GetPlane().Plane.GetSpeed())
{
return 1;
}
if (xPlane.GetPlane().Plane.GetWeight() != yPlane.GetPlane().Plane.GetWeight())
{
return 1;
}
var xEntity = xPlane.GetPlane().Plane;
var yEntity = yPlane.GetPlane().Plane;
var colorCompare = xEntity.GetColor().toString().compareTo(yEntity.GetColor().toString());
if (colorCompare != 0 || xEntity instanceof EntityAirbus || yEntity instanceof EntityAirbus)
{
return colorCompare;
}
EntityAirbus xEntityAirbus = (EntityAirbus)xEntity;
EntityAirbus yEntityAirbus = (EntityAirbus)yEntity;
return xEntityAirbus.AddColor().toString().compareTo(yEntityAirbus.AddColor().toString());
}
}

View File

@ -0,0 +1,65 @@
import java.util.Comparator;
public class PlaneCompareByType implements Comparator<IDrawningObject>
{
@Override
public int compare(IDrawningObject x, IDrawningObject y)
{
if (x == null && y == null)
{
return 0;
}
if (x == null && y != null)
{
return 1;
}
if (x != null && y != null)
{
return -1;
}
DrawningObjectPlane xPlane = (DrawningObjectPlane)x;
DrawningObjectPlane yPlane = (DrawningObjectPlane)y;
if (xPlane == null && yPlane == null)
{
return 0;
}
if (xPlane == null && yPlane != null)
{
return 1;
}
if (xPlane != null && yPlane == null)
{
return 1;
}
if(xPlane.GetPlane().Plane.GetSpeed() != yPlane.GetPlane().Plane.GetSpeed())
{
return 1;
}
if (xPlane.GetPlane().Plane.GetWeight() != yPlane.GetPlane().Plane.GetWeight())
{
return 1;
}
var xEntity = xPlane.GetPlane().Plane;
var yEntity = yPlane.GetPlane().Plane;
var colorCompare = xEntity.CorpusColor.toString().compareTo(yEntity.CorpusColor.toString());
if (colorCompare != 0 || xEntity instanceof EntityAirbus || yEntity instanceof EntityAirbus yEntityAirbus)
{
return colorCompare;
}
EntityAirbus xEntityAirbus = (EntityAirbus) xEntity;
EntityAirbus yEntityAirbus = (EntityAirbus) yEntity;
return xEntityAirbus.AddColor().toString().compareTo(yEntityAirbus.AddColor().toString());
}
}

View File

@ -1,4 +1,5 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
public class SetPlanesGeneric<T extends Object> implements Iterable<T>
@ -88,6 +89,17 @@ public class SetPlanesGeneric<T extends Object> implements Iterable<T>
Insert(value, position);
}
//сортировка набора объектов
public void SortSet(Comparator<T> comparer)
{
if(comparer == null)
{
return;
}
_places.sort(comparer);
}
@Override
public Iterator<T> iterator()
{