Compare commits

...

11 Commits

Author SHA1 Message Date
5a56586e50 LabWork04 is done. 2022-12-05 18:41:18 +04:00
1yuee
65ee7b9c39 LabWork03 is done. 2022-11-22 16:56:54 +04:00
9754f55a89 LabWork03 is partly done. 2022-11-22 03:36:42 +04:00
81f05ddbef File DrawingEngines is deleted. 2022-11-08 21:23:45 +04:00
6896e331eb Merge branch 'LabWork01' into LabWork02
# Conflicts:
#	Classes/DrawingEngines.java
#	Classes/EntityAircraft.java
2022-11-08 21:21:52 +04:00
272b78a3fd Extra spaces were removed. 2022-11-08 21:07:31 +04:00
f16de68249 Extra spaces were removed. 2022-11-08 20:50:59 +04:00
6067a2668e Extra spaces were removed. 2022-11-08 20:50:19 +04:00
1992b7d08d Extra spaces were removed. 2022-11-08 20:49:56 +04:00
58305e652e LabWork02 is done! 2022-11-08 17:32:36 +04:00
1883bdcb8a Some of the basic logic is done. 2022-11-08 13:49:17 +04:00
27 changed files with 1785 additions and 127 deletions

View File

@ -1,8 +1,12 @@
import Form.FormAirFighter;
import Form.FormAirport;
import Form.FormMapWithSetAircrafts;
import javax.swing.*;
public class App
{
public static void main(String[] args) {
new FormAirFighter();
FormMapWithSetAircrafts form = new FormMapWithSetAircrafts();
}
}

View File

@ -6,24 +6,42 @@ import java.util.Random;
public class DrawingAircraft
{
public EntityAircraft Plane;
private DrawingEngines _engines;
private IDrawingEngines _engines;
private int _startPosX;
private int _startPosY;
protected int _startPosX;
protected int _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
public Integer _pictureWidth = null;
public Integer _pictureHeight = null;
protected final int _aircraftWidth = 100;
protected final int _aircraftHeight = 120;
public void Init(int speed, float weight, Color bodyColor)
public DrawingAircraft(int speed,float weight, Color bodyColor)
{
Random rnd = new Random();
Plane = new EntityAircraft();
Plane.Init(speed,weight,bodyColor);
_engines = new DrawingEngines();
int enginesType = rnd.nextInt(1,4);
if(enginesType == 1)
{
_engines = new DrawingArcEngines();
}
else if(enginesType == 2)
{
_engines = new DrawingRectangleEngines();
}
else if(enginesType == 3)
{
_engines = new DrawingOvalEngines();
}
_engines.setEngines(rnd.nextInt(2,7));
Plane = new EntityAircraft(speed,weight,bodyColor);
}
public DrawingAircraft(EntityAircraft aircraft,IDrawingEngines engine)
{
Plane = aircraft;
_engines = engine;
}
public void SetPosition(int x,int y,int width,int height)
@ -214,4 +232,9 @@ public class DrawingAircraft
_startPosY = _pictureHeight - _aircraftHeight;
}
}
public ObjectData getCurrentPosition()
{
return new ObjectData(_startPosX, _startPosY, _startPosX + _aircraftWidth - 1, _startPosY + _aircraftHeight - 1);
}
}

View File

@ -0,0 +1,70 @@
package Classes;
import java.awt.*;
public class DrawingArcEngines implements IDrawingEngines
{
private int enginesCount;
@Override
public void drawEngines(Graphics g, int startPosX, int startPosY,Color bodyColor)
{
switch(enginesCount)
{
case 2 -> {
g.setColor((Color.BLACK));
g.drawArc(startPosX + 40,startPosY + 5,30,10,80,200);
g.drawArc(startPosX + 40,startPosY + 90,30,10,80,200);
g.setColor((bodyColor));
g.fillArc(startPosX + 40,startPosY + 5,30,10,80,200);
g.fillArc(startPosX + 40,startPosY + 90,30,10,80,200);
}
case 4 -> {
g.setColor((Color.BLACK));
g.drawArc(startPosX + 40,startPosY + 5,30,10,80,200);
g.drawArc(startPosX + 40,startPosY + 90,30,10,80,200);
g.drawArc(startPosX + 40,startPosY + 20,30,10,80,200);
g.drawArc(startPosX + 40,startPosY + 75,30,10,80,200);
g.setColor(bodyColor);
g.fillArc(startPosX + 40,startPosY + 5,30,10,80,200);
g.fillArc(startPosX + 40,startPosY + 90,30,10,80,200);
g.fillArc(startPosX + 40,startPosY + 20,30,10,80,200);
g.fillArc(startPosX + 40,startPosY + 75,30,10,80,200);
}
case 6 -> {
g.setColor((Color.BLACK));
g.drawArc(startPosX + 40,startPosY + 5,30,10,80,200);
g.drawArc(startPosX + 40,startPosY + 90,30,10,80,200);
g.drawArc(startPosX + 40,startPosY + 20,30,10,80,200);
g.drawArc(startPosX + 40,startPosY + 75,30,10,80,200);
g.drawArc(startPosX + 40,startPosY + 35,30,10,80,200);
g.drawArc(startPosX + 40,startPosY + 60,30,10,80,200);
g.setColor(bodyColor);
g.fillArc(startPosX + 40,startPosY + 5,30,10,80,200);
g.fillArc(startPosX + 40,startPosY + 90,30,10,80,200);
g.fillArc(startPosX + 40,startPosY + 20,30,10,80,200);
g.fillArc(startPosX + 40,startPosY + 75,30,10,80,200);
g.fillArc(startPosX + 40,startPosY + 35,30,10,80,200);
g.fillArc(startPosX + 40,startPosY + 60,30,10,80,200);
}
}
}
@Override
public void setEngines(int count)
{
enginesCount = count;
}
@Override
public int getCount() {
return enginesCount;
}
}

View File

@ -1,64 +0,0 @@
package Classes;
import java.awt.*;
public class DrawingEngines
{
private Engines enginesCount;
public void setEngines(int count)
{
enginesCount = Engines.getEnginesEnum(count);
}
public void drawEngines(Graphics g,int startPosX,int startPosY,Color bodyColor)
{
if(enginesCount != null)
{
switch(enginesCount)
{
case TwoEngines -> {
g.setColor((Color.BLACK));
g.drawOval(startPosX + 40,startPosY + 5,30,10);
g.drawOval(startPosX + 40,startPosY + 90,30,10);
g.setColor((bodyColor));
g.fillOval(startPosX + 40,startPosY + 5,30,10);
g.fillOval(startPosX + 40,startPosY + 90,30,10);
}
case FourEngines -> {
g.setColor((Color.BLACK));
g.drawOval(startPosX + 40,startPosY + 5,30,10);
g.drawOval(startPosX + 40,startPosY + 90,30,10);
g.drawOval(startPosX + 40,startPosY + 20,30,10);
g.drawOval(startPosX + 40,startPosY + 75,30,10);
g.setColor(bodyColor);
g.fillOval(startPosX + 40,startPosY + 5,30,10);
g.fillOval(startPosX + 40,startPosY + 90,30,10);
g.fillOval(startPosX + 40,startPosY + 20,30,10);
g.fillOval(startPosX + 40,startPosY + 75,30,10);
}
case SixEngines -> {
g.setColor((Color.BLACK));
g.drawOval(startPosX + 40,startPosY + 5,30,10);
g.drawOval(startPosX + 40,startPosY + 90,30,10);
g.drawOval(startPosX + 40,startPosY + 20,30,10);
g.drawOval(startPosX + 40,startPosY + 75,30,10);
g.drawOval(startPosX + 40,startPosY + 35,30,10);
g.drawOval(startPosX + 40,startPosY + 60,30,10);
g.setColor(bodyColor);
g.fillOval(startPosX + 40,startPosY + 5,30,10);
g.fillOval(startPosX + 40,startPosY + 90,30,10);
g.fillOval(startPosX + 40,startPosY + 20,30,10);
g.fillOval(startPosX + 40,startPosY + 75,30,10);
g.fillOval(startPosX + 40,startPosY + 35,30,10);
g.fillOval(startPosX + 40,startPosY + 60,30,10);
}
}
}
}
}

View File

@ -0,0 +1,101 @@
package Classes;
import java.awt.*;
public class DrawingMilitaryAircraft extends DrawingAircraft
{
public DrawingMilitaryAircraft(int speed, float weight, Color bodyColor, Color extraColor, boolean rockets, boolean extraWings)
{
super(speed, weight, bodyColor);
Plane = new EntityMilitaryAircraft(speed, weight, bodyColor, extraColor, rockets, extraWings);
}
public DrawingMilitaryAircraft(EntityAircraft aircraft,IDrawingEngines engine)
{
super(aircraft, engine);
}
@Override
public void DrawTransport(Graphics g)
{
if(!(Plane instanceof EntityMilitaryAircraft))
{
return;
}
super.DrawTransport(g);
EntityMilitaryAircraft militaryAircraft = (EntityMilitaryAircraft) Plane;
if(militaryAircraft.ExtraWings)
{
Polygon pathExtraWing1 = new Polygon();
Point point1W1 = new Point((int)(_startPosX + 30), (int)(_startPosY + 45));
Point point2W1 = new Point(point1W1.x , point1W1.y - 40);
Point point3W1 = new Point(point2W1.x + 10,point1W1.y);
pathExtraWing1.addPoint(point1W1.x,point1W1.y);
pathExtraWing1.addPoint(point2W1.x,point2W1.y);
pathExtraWing1.addPoint(point3W1.x,point3W1.y);
g.setColor(Color.black);
g.drawPolygon(pathExtraWing1);
g.setColor(militaryAircraft.ExtraColor);
g.fillPolygon(pathExtraWing1);
Polygon pathExtraWing2 = new Polygon();
Point point1W2 = new Point((int)(_startPosX + 30),(int)(_startPosY + 60));
Point point2W2 = new Point(point1W2.x , point1W2.y + 40);
Point point3W2 = new Point(point2W2.x + 10,point1W2.y);
pathExtraWing2.addPoint(point1W2.x,point1W2.y);
pathExtraWing2.addPoint(point2W2.x,point2W2.y);
pathExtraWing2.addPoint(point3W2.x,point3W2.y);
g.setColor(Color.black);
g.drawPolygon(pathExtraWing2);
g.setColor(militaryAircraft.ExtraColor);
g.fillPolygon(pathExtraWing2);
}
if(militaryAircraft.Rockets)
{
g.setColor(Color.black);
g.drawRect(_startPosX + 50, _startPosY + 30,17,4);
Polygon pathRocketHead1 = new Polygon();
Point point1R1 = new Point((int)(_startPosX + 50),(int)(_startPosY + 30));
Point point2R1 = new Point(point1R1.x - 5,point1R1.y + 2);
Point point3R1 = new Point(point1R1.x , point1R1.y + 4);
pathRocketHead1.addPoint(point1R1.x,point1R1.y);
pathRocketHead1.addPoint(point2R1.x,point2R1.y);
pathRocketHead1.addPoint(point3R1.x,point3R1.y);
g.drawPolygon(pathRocketHead1);
g.setColor(militaryAircraft.ExtraColor);
g.fillPolygon(pathRocketHead1);
g.setColor(Color.black);
g.drawRect(_startPosX + 50, _startPosY + 70, 17, 4);
Polygon pathRocketHead2 = new Polygon();
Point point1R2 = new Point((int)(_startPosX + 50),(int)(_startPosY + 70));
Point point2R2 = new Point(point1R2.x - 5,point1R2.y + 2);
Point point3R2 = new Point(point1R2.x, point1R2.y + 4);
pathRocketHead2.addPoint(point1R2.x,point1R2.y);
pathRocketHead2.addPoint(point2R2.x,point2R2.y);
pathRocketHead2.addPoint(point2R2.x,point2R2.y);
g.drawPolygon(pathRocketHead2);
g.setColor(militaryAircraft.ExtraColor);
g.fillPolygon(pathRocketHead2);
}
}
}

View File

@ -0,0 +1,45 @@
package Classes;
import java.awt.*;
public class DrawingObjectAircraft implements IDrawingObject {
private DrawingAircraft _aircraft;
public DrawingObjectAircraft(DrawingAircraft aircraft)
{
_aircraft = aircraft;
}
public DrawingAircraft getAircraft()
{
return _aircraft;
}
@Override
public float getStep()
{
return _aircraft.Plane.getStep();
}
@Override
public void SetObject(int x, int y, int width, int height)
{
_aircraft.SetPosition(x,y,width,height);
}
@Override
public void MoveObject(Direction direction)
{
_aircraft.MoveTransport(direction);
}
@Override
public void DrawingObject(Graphics g)
{
_aircraft.DrawTransport(g);
}
@Override
public ObjectData getCurrentPosition() {
return (_aircraft.getCurrentPosition());
}
}

View File

@ -0,0 +1,69 @@
package Classes;
import java.awt.*;
public class DrawingOvalEngines implements IDrawingEngines
{
private int enginesCount;
@Override
public void drawEngines(Graphics g, int startPosX, int startPosY,Color bodyColor)
{
switch(enginesCount)
{
case 2 -> {
g.setColor((Color.BLACK));
g.drawOval(startPosX + 40,startPosY + 5,30,10);
g.drawOval(startPosX + 40,startPosY + 90,30,10);
g.setColor((bodyColor));
g.fillOval(startPosX + 40,startPosY + 5,30,10);
g.fillOval(startPosX + 40,startPosY + 90,30,10);
}
case 4 -> {
g.setColor((Color.BLACK));
g.drawOval(startPosX + 40,startPosY + 5,30,10);
g.drawOval(startPosX + 40,startPosY + 90,30,10);
g.drawOval(startPosX + 40,startPosY + 20,30,10);
g.drawOval(startPosX + 40,startPosY + 75,30,10);
g.setColor(bodyColor);
g.fillOval(startPosX + 40,startPosY + 5,30,10);
g.fillOval(startPosX + 40,startPosY + 90,30,10);
g.fillOval(startPosX + 40,startPosY + 20,30,10);
g.fillOval(startPosX + 40,startPosY + 75,30,10);
}
case 6 -> {
g.setColor((Color.BLACK));
g.drawOval(startPosX + 40,startPosY + 5,30,10);
g.drawOval(startPosX + 40,startPosY + 90,30,10);
g.drawOval(startPosX + 40,startPosY + 20,30,10);
g.drawOval(startPosX + 40,startPosY + 75,30,10);
g.drawOval(startPosX + 40,startPosY + 35,30,10);
g.drawOval(startPosX + 40,startPosY + 60,30,10);
g.setColor(bodyColor);
g.fillOval(startPosX + 40,startPosY + 5,30,10);
g.fillOval(startPosX + 40,startPosY + 90,30,10);
g.fillOval(startPosX + 40,startPosY + 20,30,10);
g.fillOval(startPosX + 40,startPosY + 75,30,10);
g.fillOval(startPosX + 40,startPosY + 35,30,10);
g.fillOval(startPosX + 40,startPosY + 60,30,10);
}
}
}
@Override
public void setEngines(int count)
{
enginesCount = count;
}
@Override
public int getCount() {
return enginesCount;
}
}

View File

@ -0,0 +1,70 @@
package Classes;
import java.awt.*;
public class DrawingRectangleEngines implements IDrawingEngines
{
private int enginesCount;
@Override
public void drawEngines(Graphics g, int startPosX, int startPosY,Color bodyColor)
{
switch(enginesCount)
{
case 2 -> {
g.setColor((Color.BLACK));
g.drawRect(startPosX + 40,startPosY + 5,30,10);
g.drawRect(startPosX + 40,startPosY + 90,30,10);
g.setColor((bodyColor));
g.fillRect(startPosX + 40,startPosY + 5,30,10);
g.fillRect(startPosX + 40,startPosY + 90,30,10);
}
case 4 -> {
g.setColor((Color.BLACK));
g.drawRect(startPosX + 40,startPosY + 5,30,10);
g.drawRect(startPosX + 40,startPosY + 90,30,10);
g.drawRect(startPosX + 40,startPosY + 20,30,10);
g.drawRect(startPosX + 40,startPosY + 75,30,10);
g.setColor(bodyColor);
g.fillRect(startPosX + 40,startPosY + 5,30,10);
g.fillRect(startPosX + 40,startPosY + 90,30,10);
g.fillRect(startPosX + 40,startPosY + 20,30,10);
g.fillRect(startPosX + 40,startPosY + 75,30,10);
}
case 6 -> {
g.setColor((Color.BLACK));
g.drawRect(startPosX + 40,startPosY + 5,30,10);
g.drawRect(startPosX + 40,startPosY + 90,30,10);
g.drawRect(startPosX + 40,startPosY + 20,30,10);
g.drawRect(startPosX + 40,startPosY + 75,30,10);
g.drawRect(startPosX + 40,startPosY + 35,30,10);
g.drawRect(startPosX + 40,startPosY + 60,30,10);
g.setColor(bodyColor);
g.fillRect(startPosX + 40,startPosY + 5,30,10);
g.fillRect(startPosX + 40,startPosY + 90,30,10);
g.fillRect(startPosX + 40,startPosY + 20,30,10);
g.fillRect(startPosX + 40,startPosY + 75,30,10);
g.fillRect(startPosX + 40,startPosY + 35,30,10);
g.fillRect(startPosX + 40,startPosY + 60,30,10);
}
}
}
@Override
public void setEngines(int count)
{
enginesCount = count;
}
@Override
public int getCount() {
return enginesCount;
}
}

View File

@ -9,7 +9,7 @@ public class EntityAircraft
public Color BodyColor;
public float Step;
public void Init(int speed,float weight,Color bodyColor)
public EntityAircraft(int speed,float weight,Color bodyColor)
{
Speed = speed;
Weight = weight;
@ -36,5 +36,4 @@ public class EntityAircraft
{
return BodyColor;
}
}

View File

@ -0,0 +1,18 @@
package Classes;
import java.awt.*;
public class EntityMilitaryAircraft extends EntityAircraft
{
public Color ExtraColor;
public boolean Rockets;
public boolean ExtraWings;
public EntityMilitaryAircraft(int speed, float weight, Color bodyColor, Color extraColor, boolean rockets, boolean extraWings)
{
super(speed,weight,bodyColor);
this.ExtraColor = extraColor;
this.Rockets = rockets;
this.ExtraWings = extraWings;
}
}

View File

@ -0,0 +1,45 @@
package Classes;
import java.util.ArrayList;
import java.util.Random;
public class EntityWithEngines<T extends EntityAircraft,U extends IDrawingEngines>
{
Random rnd = new Random();
public ArrayList<T> entities;
public ArrayList<U> engines;
public EntityWithEngines()
{
entities = new ArrayList<T>();
engines = new ArrayList<U>();
}
public void addEntity(T aircraft)
{
entities.add(aircraft);
}
public void addEngines(U engine)
{
engines.add(engine);
}
public DrawingObjectAircraft createObject()
{
if(entities.size() == 0 || engines.size() == 0)
{
return null;
}
EntityAircraft entity = (EntityAircraft) entities.get(rnd.nextInt(0, entities.size()));
IDrawingEngines engine = (IDrawingEngines) engines.get(rnd.nextInt(0,engines.size()));
if(entity instanceof EntityMilitaryAircraft modAircraft)
{
return new DrawingObjectAircraft(new DrawingMilitaryAircraft(modAircraft,engine));
}
return new DrawingObjectAircraft(new DrawingAircraft(entity,engine));
}
}

View File

@ -0,0 +1,10 @@
package Classes;
import java.awt.*;
public interface IDrawingEngines
{
void drawEngines(Graphics g, int startX, int startY,Color bodyColor);
void setEngines(int count);
int getCount();
}

View File

@ -0,0 +1,16 @@
package Classes;
import java.awt.*;
public interface IDrawingObject
{
public float getStep();
void SetObject(int x, int y, int width, int height);
void MoveObject(Direction direction);
void DrawingObject(Graphics g);
ObjectData getCurrentPosition();
}

View File

@ -0,0 +1,156 @@
package Classes;
import Classes.Maps.AbstractMap;
import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetAircraftsGeneric <T extends IDrawingObject,U extends AbstractMap>
{
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 145;
private final int _placeSizeHeight = 145;
private final SetAircraftsGeneric<T> _setAircrafts;
private final U _map;
public MapWithSetAircraftsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setAircrafts = new SetAircraftsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int addAircraft(MapWithSetAircraftsGeneric<T, U> map, T aircraft)
{
return map._setAircrafts.Insert(aircraft);
}
public T removeAircraft(MapWithSetAircraftsGeneric<T, U> map, int position)
{
return map._setAircrafts.Remove(position);
}
public T getAircraft(MapWithSetAircraftsGeneric<T, U> map, int position)
{
return map._setAircrafts.get(position);
}
public Image showSet()
{
BufferedImage img = new BufferedImage(_pictureWidth,_pictureHeight,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) img.getGraphics();
DrawBackground(g2d);
DrawAircrafts(g2d);
return img;
}
public Image ShowOnMap()
{
Shaking();
for(int i = 0;i< _setAircrafts.getCount();i++)
{
if(_setAircrafts.get(i) != null)
{
return _map.CreateMap(_pictureWidth,_pictureHeight,_setAircrafts.get(i));
}
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
}
public Image MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
}
private void Shaking()
{
int j = _setAircrafts.getCount() - 1;
for (int i = 0; i < _setAircrafts.getCount(); i++)
{
if (_setAircrafts.get(i) == null)
{
for (; j > i; j--)
{
var aircraft = _setAircrafts.get(j);
if (aircraft != null)
{
_setAircrafts.Insert(aircraft, i);
_setAircrafts.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
g.setColor(Color.black);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
g2d.setStroke(new BasicStroke(2));
for (int i = 0; i < _pictureWidth / _placeSizeWidth + 1; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++)
{
g2d.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + 135, _placeSizeWidth - 80, j * _placeSizeHeight + 135);
if(i * _placeSizeWidth + _placeSizeWidth + 20 < _pictureWidth - 10)
{
g2d.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + 60, i * _placeSizeWidth + _placeSizeWidth / 2 + 20, j * _placeSizeHeight + 45);
}
g2d.drawLine(i * _placeSizeWidth + _placeSizeWidth / 2 + 20, j * _placeSizeHeight + 45, i * _placeSizeWidth + _placeSizeWidth + 20 , j * _placeSizeHeight + 60);
g2d.drawLine( i * _placeSizeWidth + 20, j * _placeSizeHeight + 135, i * _placeSizeWidth + 20, j * _placeSizeHeight + 60);
}
}
}
private void DrawAircrafts(Graphics g)
{
int curX = _pictureWidth / _placeSizeWidth - 1;
int curY = _pictureHeight / _placeSizeHeight - 1;
for(int i = 0; i < _setAircrafts.getCount();i++)
{
if(_setAircrafts.get(i) != null)
{
_setAircrafts.get(i).SetObject(curX * _placeSizeWidth + 30, curY * _placeSizeHeight + 40, _pictureWidth, _pictureHeight);
_setAircrafts.get(i).DrawingObject(g);
}
if (curX <= 0)
{
curX = _pictureWidth / _placeSizeWidth - 1;
curY--;
}
else
{
curX--;
}
}
}
}

View File

@ -0,0 +1,196 @@
package Classes.Maps;
import Classes.Direction;
import Classes.IDrawingObject;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public abstract class AbstractMap
{
private IDrawingObject _drawingObject = null;
protected int[][] _map = null;
protected int _width;
protected int _height;
protected float _size_x;
protected float _size_y;
protected final Random _random = new Random();
protected final int _freeRoad = 0;
protected final int _barrier = 1;
public Image CreateMap(int width, int height, IDrawingObject drawingObject)
{
_width = width;
_height = height;
_drawingObject = drawingObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
return DrawMapWithObject();
}
public Image MoveObject(Direction direction)
{
int CollisionFlag = 0;
Dimension objSize1 = new Dimension(100,100);
//COLLISION CHECK
if (direction == Direction.Up)
{
for (int i = 0; i < _map.length; i++)
{
for (int j = 0; j < _map[i].length; j++)
{
if (_map[i][j] == _barrier)
{
Point LeftTop = new Point((int)_drawingObject.getCurrentPosition().Left(), (int)(_drawingObject.getCurrentPosition().Top() - _drawingObject.getStep()));
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
if (objectRect.intersects(rectBarrier))
{
CollisionFlag = 1;
}
}
}
}
}
else if (direction == Direction.Down)
{
for (int i = 0; i < _map.length; i++)
{
for (int j = 0; j < _map[i].length; j++)
{
if (_map[i][j] == _barrier)
{
Point LeftTop = new Point((int)_drawingObject.getCurrentPosition().Left(), (int)(_drawingObject.getCurrentPosition().Top() + _drawingObject.getStep()));
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y),(int) _size_x,(int) _size_y);
if (objectRect.intersects(rectBarrier))
{
CollisionFlag = 1;
}
}
}
}
}
else if (direction == Direction.Left)
{
for (int i = 0; i < _map.length; i++)
{
for (int j = 0; j < _map[i].length; j++)
{
if (_map[i][j] == _barrier)
{
Point LeftTop = new Point((int)(_drawingObject.getCurrentPosition().Left() - _drawingObject.getStep()), (int)_drawingObject.getCurrentPosition().Top());
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
if (objectRect.intersects(rectBarrier))
{
CollisionFlag = 1;
}
}
}
}
}
else if (direction == Direction.Right)
{
for (int i = 0; i < _map.length; i++)
{
for (int j = 0; j < _map[i].length; j++)
{
if (_map[i][j] == _barrier)
{
Point LeftTop = new Point((int)(_drawingObject.getCurrentPosition().Left() + _drawingObject.getStep()), (int)_drawingObject.getCurrentPosition().Top());
Rectangle objectRect = new Rectangle(LeftTop, objSize1);
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
if (objectRect.intersects(rectBarrier))
{
CollisionFlag = 1;
}
}
}
}
}
if (CollisionFlag != 1)
{
_drawingObject.MoveObject(direction);
}
return DrawMapWithObject();
}
private boolean SetObjectOnMap()
{
if (_drawingObject == null || _map == null)
{
return false;
}
int x = _random.nextInt(0,10);
int y = _random.nextInt(0,10);
_drawingObject.SetObject(x, y, _width, _height);
Rectangle rectObject = new Rectangle(x, y, 100, 100);
for (int i = 0; i < _map.length; i++)
{
for (int j = 0; j < _map[i].length; j++)
{
if (_map[i][j] == _barrier)
{
Rectangle rectBarrier = new Rectangle((int)(i * _size_x), (int)(j * _size_y), (int)_size_x, (int)_size_y);
if (rectObject.intersects(rectBarrier))
{
return false;
}
}
}
}
return true;
}
private Image DrawMapWithObject()
{
Image img = new BufferedImage(_width, _height,BufferedImage.TYPE_INT_ARGB);
if (_drawingObject == null || _map == null)
{
return img;
}
Graphics gr = (Graphics2D) img.getGraphics();
for (int i = 0; i < _map.length; i++)
{
for(int j = 0; j < _map[i].length;j++)
{
if (_map[i][j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i][j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawingObject.DrawingObject(gr);
return img;
}
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

@ -0,0 +1,56 @@
package Classes.Maps;
import org.w3c.dom.css.RGBColor;
import java.awt.*;
public class NightSkyMap extends AbstractMap {
@Override
protected void GenerateMap()
{
_map = new int[100][100];
_size_x = (float)_width / _map[0].length;
_size_y = (float)_height / _map.length;
int counter = 0;
for (int i = 0; i < _map.length; i++)
{
for (int j = 0; j < _map[i].length; j++)
{
_map[i][j] = _freeRoad;
}
}
while (counter < 20)
{
int x = _random.nextInt(10,90);
int y = _random.nextInt(10,90);
_map[x][ y] = _barrier;
_map[x-1][y] = _barrier;
_map[x + 1][y] = _barrier;
_map[x][y - 1] = _barrier;
counter++;
}
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
g.setColor(Color.black);
g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
Color ThundercloudColor = new Color(0,0,139);
g.setColor(ThundercloudColor);
g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
}

View File

@ -0,0 +1,46 @@
package Classes.Maps;
import java.awt.*;
public class SimpleMap extends AbstractMap
{
@Override
protected void GenerateMap()
{
_map = new int[100][100];
_size_x = (float)_width / _map[0].length;
_size_y = (float)_height / _map.length;
int counter = 0;
for (int i = 0; i < _map.length; i++)
{
for (int j = 0; j < _map[i].length; j++)
{
if (_random.nextInt(0, 90) == 5)
{
_map[i][j] = _barrier;
}
else
{
_map[i][j] = _freeRoad;
}
}
}
}
@Override
protected void DrawRoadPart(Graphics g, int i, int j)
{
g.setColor(Color.GRAY);
g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
@Override
protected void DrawBarrierPart(Graphics g, int i, int j)
{
g.setColor(Color.BLACK);
g.fillRect((int) (i * _size_x), (int) (j * _size_y), (int) (i * (_size_x + 1)), (int) (j * (_size_y + 1)));
}
}

View File

@ -0,0 +1,47 @@
package Classes;
import Classes.Maps.AbstractMap;
import java.util.*;
public class MapsCollection
{
final HashMap<String,MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap>> _mapsVault;
public ArrayList<String> Keys;
private final int _pictureWidth;
private final int _pictureHeight;
public MapsCollection(int pictureWidth, int pictureHeight)
{
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_mapsVault = new HashMap<String, MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap>>();
Keys = new ArrayList<>(_mapsVault.keySet());
}
public void AddMap(String name, AbstractMap map)
{
if (!Keys.contains(name))
{
_mapsVault.put(name, new MapWithSetAircraftsGeneric<DrawingObjectAircraft, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
Keys = new ArrayList<>(_mapsVault.keySet());
}
public void DeleteMap(String name)
{
if (Keys.contains(name))
{
_mapsVault.remove(name);
}
Keys = new ArrayList<>(_mapsVault.keySet());
}
public MapWithSetAircraftsGeneric<DrawingObjectAircraft,AbstractMap> getMap(String ind)
{
return _mapsVault.getOrDefault(ind, null);
}
}

6
Classes/ObjectData.java Normal file
View File

@ -0,0 +1,6 @@
package Classes;
public record ObjectData(float Left, float Top, float Right, float Bottom)
{
}

View File

@ -0,0 +1,66 @@
package Classes;
import java.util.*;
public class SetAircraftsGeneric<T>
{
private final ArrayList<T> _places;
public int getCount()
{
return _places.size();
}
private final int _maxCount;
public SetAircraftsGeneric(int count)
{
_maxCount = count;
_places = new ArrayList<T>();
}
public int Insert(T aircraft)
{
return Insert(aircraft, 0);
}
public int Insert(T aircraft, int position)
{
if (position > getCount() || position < 0 || getCount() == _maxCount)
{
return -1;
}
_places.add(position, aircraft);
return position;
}
public T Remove(int position)
{
if (position < getCount() && position >= 0)
{
if (_places.get(position) != null)
{
T result = _places.get(position);
_places.remove(position);
return result;
}
return null;
}
return null;
}
public T get(int position)
{
if (position < _maxCount && position >= 0 && position < getCount())
{
return _places.get(position);
}
return null;
}
}

View File

@ -38,7 +38,7 @@
</component>
</children>
</grid>
<grid id="53a7f" binding="pictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="53a7f" binding="pictureBox" layout-manager="GridLayoutManager" row-count="3" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="Center"/>
<properties/>
@ -46,7 +46,7 @@
<children>
<component id="f5248" class="javax.swing.JButton" binding="moveRight">
<constraints>
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="2" column="6" 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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -59,19 +59,14 @@
<text value=""/>
</properties>
</component>
<hspacer id="69b03">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="2a7c5">
<constraints>
<grid row="0" 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="0" column="3" 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="71509" class="javax.swing.JButton" binding="moveDown">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="2" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -86,13 +81,14 @@
</component>
<component id="fb1e5" class="javax.swing.JButton" binding="moveLeft">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="2" column="4" 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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<icon value="Resources/leftarrow.png"/>
<name value="left"/>
@ -101,7 +97,7 @@
</component>
<component id="cc503" class="javax.swing.JButton" binding="moveUp">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="1" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -122,6 +118,22 @@
<text value="Create"/>
</properties>
</component>
<component id="a41e9" class="javax.swing.JButton" binding="btnModification">
<constraints>
<grid row="2" 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>
<text value="Modification"/>
</properties>
</component>
<component id="668e1" class="javax.swing.JButton" binding="SelectButton">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Select"/>
</properties>
</component>
</children>
</grid>
</children>

View File

@ -1,18 +1,18 @@
package Form;
import Classes.Direction;
import Classes.DrawingAircraft;
import Classes.DrawingMilitaryAircraft;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
public class FormAirFighter extends JFrame
public class FormAirFighter extends JDialog
{
private DrawingAircraft _aircraft;
public DrawingAircraft SelectedAircraft;
private JPanel Form;
private JPanel label;
private JPanel pictureBox;
@ -25,6 +25,8 @@ public class FormAirFighter extends JFrame
private JButton moveLeft;
private JButton moveUp;
private JButton btnCreate;
private JButton btnModification;
private JButton SelectButton;
private void moveButtonClick(ActionEvent event) {
if (_aircraft == null) return;
@ -43,50 +45,81 @@ public class FormAirFighter extends JFrame
_aircraft.MoveTransport(Direction.Down);
}
}
Draw();
repaint();
}
private void CreateEntity() {
Random random = new Random();
_aircraft = new DrawingAircraft();
_aircraft.Init(
random.nextInt(10, 300),
random.nextFloat(1000, 2000),
new Color(
random.nextInt(0,256),
random.nextInt(0,256),
random.nextInt(0,256)
)
);
private void SetData()
{
Random rnd = new Random();
_aircraft.SetPosition(
random.nextInt(0, 100),
random.nextInt(0, 100),
Form.getWidth(),
Form.getHeight()
rnd.nextInt(0, 100),
rnd.nextInt(0, 100),
Form.getWidth(),
Form.getHeight()
);
speedLabel.setText("Speed: " + _aircraft.Plane.getSpeed());
weightLabel.setText("Weight: " + _aircraft.Plane.getWeight());
colorLabel.setText("Color: " + _aircraft.Plane.getBodyColor());
}
private void SetData(int width, int height)
{
Random rnd = new Random();
Draw();
_aircraft.SetPosition(
rnd.nextInt(0, 100),
rnd.nextInt(0, 100),
width,
height
);
speedLabel.setText("Speed: " + _aircraft.Plane.getSpeed());
weightLabel.setText("Weight: " + _aircraft.Plane.getWeight());
colorLabel.setText("Color: " + _aircraft.Plane.getBodyColor());
}
private void CreateEntity() {
Random rnd = new Random();
Color color = JColorChooser.showDialog(this, "Цвет", new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
_aircraft = new DrawingAircraft(rnd.nextInt(10, 300),
rnd.nextFloat(1000, 2000),color);
SetData();
repaint();
}
private void CreateModifiedEntity()
{
Random rnd = new Random();
Color color = JColorChooser.showDialog(this, "Основной цвет", Color.white);
Color dopColor = JColorChooser.showDialog(this, "Дополнительный цвет", Color.white);
_aircraft = new DrawingMilitaryAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
color, dopColor, rnd.nextBoolean(), rnd.nextBoolean());
SetData();
repaint();
}
private void selectAircraft()
{
SelectedAircraft = _aircraft;
dispose();
}
public DrawingAircraft getSelectedAircraft() {
return SelectedAircraft;
}
private void resizeWindow() {
_aircraft.ChangeBorders(pictureBox.getWidth(), pictureBox.getHeight());
Draw();
}
private void Draw() {
Graphics2D graphics = (Graphics2D) pictureBox.getGraphics();
graphics.clearRect(0, 0, pictureBox.getWidth(), pictureBox.getHeight());
pictureBox.paintComponents(graphics);
_aircraft.DrawTransport(graphics);
repaint();
}
public FormAirFighter() {
super("AirFighter");
setContentPane(Form);
setSize(new Dimension(500, 500));
// action move //
ActionListener listener = this::moveButtonClick;
@ -95,15 +128,26 @@ public class FormAirFighter extends JFrame
moveLeft.addActionListener(listener);
moveUp.addActionListener(listener);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
btnCreate.addActionListener(e -> CreateEntity());
Form.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
resizeWindow();
}
});
btnModification.addActionListener(e -> CreateModifiedEntity());
SelectButton.addActionListener(e -> selectAircraft());
}
public FormAirFighter(DrawingAircraft aircraft)
{
this();
_aircraft = aircraft;
SetData(_aircraft._pictureWidth, aircraft._pictureHeight);
repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D graphics = (Graphics2D) pictureBox.getGraphics();
if(_aircraft != null)
{
_aircraft.DrawTransport(graphics);
}
}
}

39
Form/FormAirport.form Normal file
View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="Form.FormAirport">
<grid id="27dc6" binding="Form" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<xy x="5" y="1" width="815" height="568"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="b0240" binding="pictureBox" 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 border-constraint="Center"/>
<properties/>
<border type="none"/>
<children>
<component id="ad3ce" class="javax.swing.JButton" binding="UpdateButton">
<constraints>
<grid row="1" 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>
<actionCommand value="Button"/>
<text value="Update"/>
</properties>
</component>
<hspacer id="489c2">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="1961a">
<constraints>
<grid row="0" 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>
</children>
</grid>
</children>
</grid>
</form>

108
Form/FormAirport.java Normal file
View File

@ -0,0 +1,108 @@
package Form;
import Classes.*;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FormAirport extends JFrame{
private JPanel Form;
private JPanel pictureBox;
private JButton UpdateButton;
public Image bufferedImage;
private EntityWithEngines<EntityAircraft,IDrawingEngines> parts;
IDrawingObject aircraft1;
IDrawingObject aircraft2 ;
IDrawingObject aircraft3 ;
IDrawingObject aircraft4 ;
IDrawingObject aircraft5;
IDrawingEngines engineType;
private void UpdateAirport()
{
aircraft1 = parts.createObject();
aircraft2 = parts.createObject();
aircraft3 = parts.createObject();
aircraft4 = parts.createObject();
aircraft5 = parts.createObject();
aircraft1.SetObject(50,30, pictureBox.getWidth(), pictureBox.getHeight());
aircraft2.SetObject(150,130, pictureBox.getWidth(), pictureBox.getHeight());
aircraft3.SetObject(250,230, pictureBox.getWidth(), pictureBox.getHeight());
aircraft4.SetObject(350,330, pictureBox.getWidth(), pictureBox.getHeight());
aircraft5.SetObject(450,430, pictureBox.getWidth(), pictureBox.getHeight());
repaint();
}
public FormAirport()
{
setContentPane(Form);
Random rnd = new Random();
parts = new EntityWithEngines<>();
int mod = 0;
for(int i = 0 ; i < 15;i++)
{
mod = rnd.nextInt(1,3);
if(mod == 1)
{
parts.addEntity(new EntityAircraft(rnd.nextInt(10, 300),
rnd.nextFloat(1000, 2000),new Color(rnd.nextInt(0,256),rnd.nextInt(0,256),rnd.nextInt(0,256))));
}
else
{
parts.addEntity(new EntityMilitaryAircraft( rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
rnd.nextBoolean(),
rnd.nextBoolean()));
}
mod = rnd.nextInt(1,4);
if(mod == 1)
{
engineType = new DrawingArcEngines();
}
else if(mod == 2)
{
engineType = new DrawingOvalEngines();
}
else
{
engineType = new DrawingRectangleEngines();
}
engineType.setEngines(rnd.nextInt(2,7));
parts.addEngines(engineType);
}
UpdateButton.addActionListener(e -> {
UpdateAirport();
});
setSize(new Dimension(600, 600));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2g = (Graphics2D) pictureBox.getGraphics();
if (aircraft1 != null && aircraft2 != null && aircraft3 != null && aircraft4 != null && aircraft5 != null) {
aircraft1.DrawingObject(g2g);
aircraft2.DrawingObject(g2g);
aircraft3.DrawingObject(g2g);
aircraft4.DrawingObject(g2g);
aircraft5.DrawingObject(g2g);
}
}
}

View File

@ -0,0 +1,192 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="Form.FormMapWithSetAircrafts">
<grid id="27dc6" binding="Form" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<xy x="20" y="20" width="808" height="664"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="de9c4" binding="pictureBox" layout-manager="GridLayoutManager" row-count="7" 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 border-constraint="Center"/>
<properties/>
<border type="none"/>
<children>
<hspacer id="10e95">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
<grid id="4c74b" 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 border-constraint="East"/>
<properties/>
<border type="none"/>
<children>
<component id="e569a" class="javax.swing.JComboBox" binding="comboBoxMapSelection">
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="2" hsize-policy="2" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="1. Simple map"/>
<item value="2. NightSky map"/>
</model>
</properties>
</component>
<component id="d798e" class="javax.swing.JButton" binding="AddAircraftButton">
<constraints>
<grid row="7" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Add aircraft"/>
</properties>
</component>
<component id="f4aba" class="javax.swing.JFormattedTextField" binding="maskedTextBoxPosition">
<constraints>
<grid row="8" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="192c4" class="javax.swing.JButton" binding="DeleteAircraftButton">
<constraints>
<grid row="9" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Delete aircraft"/>
</properties>
</component>
<component id="f84e6" class="javax.swing.JButton" binding="ShowHangarButton">
<constraints>
<grid row="10" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Show hangar"/>
</properties>
</component>
<component id="1a170" class="javax.swing.JButton" binding="ShowMapButton">
<constraints>
<grid row="11" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Show map"/>
</properties>
</component>
<component id="32298" class="javax.swing.JButton" binding="moveUp">
<constraints>
<grid row="13" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="2" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/upbutton.png"/>
<name value="up"/>
<text value=""/>
</properties>
</component>
<component id="6a898" class="javax.swing.JButton" binding="moveLeft">
<constraints>
<grid row="14" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/leftarrow.png"/>
<name value="left"/>
<text value=""/>
</properties>
</component>
<component id="c514d" class="javax.swing.JButton" binding="moveDown">
<constraints>
<grid row="14" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/bottomarrow.png"/>
<name value="down"/>
<text value=""/>
</properties>
</component>
<component id="a2ec2" class="javax.swing.JButton" binding="moveRight">
<constraints>
<grid row="14" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="2" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/rightarrow.png"/>
<name value="right"/>
<text value=""/>
</properties>
</component>
<vspacer id="5a9ee">
<constraints>
<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>
<component id="afff2" class="javax.swing.JTextField" binding="newMapTextBox">
<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="7184d" 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="Add map"/>
</properties>
</component>
<component id="666a1" 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="302" 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="Delete map"/>
</properties>
</component>
<component id="310e8" class="javax.swing.JButton" binding="showDeletedAircrafts">
<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"/>
</constraints>
<properties>
<text value="Deleted"/>
</properties>
</component>
<vspacer id="9b864">
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<minimum-size width="-1" height="150"/>
</grid>
</constraints>
</vspacer>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,273 @@
package Form;
import Classes.Direction;
import Classes.DrawingObjectAircraft;
import Classes.IDrawingObject;
import Classes.Maps.AbstractMap;
import Classes.Maps.NightSkyMap;
import Classes.Maps.SimpleMap;
import Classes.MapsCollection;
import javax.swing.*;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Queue;
public class FormMapWithSetAircrafts extends JFrame
{
public JPanel Form;
private JPanel pictureBox;
private JComboBox comboBoxMapSelection;
private JButton AddAircraftButton;
private JButton DeleteAircraftButton;
private JButton ShowHangarButton;
private JButton ShowMapButton;
private JButton moveUp;
private JButton moveDown;
private JButton moveLeft;
private JButton moveRight;
private JFormattedTextField maskedTextBoxPosition;
private JTextField newMapTextBox;
private JButton AddMapButton;
private JList listBoxMaps;
DefaultListModel listModel;
private JButton DeleteMapButton;
private JButton showDeletedAircrafts;
public Image bufferedImage;
private final MapsCollection _mapsCollection;
public Queue<IDrawingObject> deletedAircrafts;
FormAirFighter form;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>(){{
put("1. Simple map", new SimpleMap());
put("2. NightSky map", new NightSkyMap());
}};
private void RefreshMaps()
{
int index = listBoxMaps.getSelectedIndex();
listBoxMaps.removeAll();
listModel = new DefaultListModel();
for (int i = 0; i < _mapsCollection.Keys.size(); i++)
{
listModel.addElement(_mapsCollection.Keys.get(i));
}
listBoxMaps.setModel(listModel);
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);
}
}
private void showMap()
{
if(listBoxMaps.getSelectedIndex() == -1)
{
return;
}
bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).ShowOnMap();
repaint();
}
private void indexChanged()
{
if(listBoxMaps.getSelectedValue() != null)
{
bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
}
}
private void addAircraft()
{
if(listBoxMaps.getSelectedIndex() == -1)
{
return;
}
form = new FormAirFighter();
form.setSize(800,500);
form.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
form.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
form.setVisible(true);
if(form.getSelectedAircraft() != null)
{
DrawingObjectAircraft aircraft = new DrawingObjectAircraft(form.SelectedAircraft);
if (_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addAircraft(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")),aircraft) != -1)
{
JOptionPane.showMessageDialog(this, "Object was added", "Success", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
}
else
{
JOptionPane.showMessageDialog(this, "Unable to add object", "Fail", JOptionPane.INFORMATION_MESSAGE);
}
}
}
private void DeleteAircraft()
{
String pos = maskedTextBoxPosition.getText();
if(listBoxMaps.getSelectedIndex() == -1)
{
return;
}
if(pos.isEmpty() || pos == null )
{
return;
}
if(JOptionPane.showConfirmDialog(this,"Delete object?","Deletion",JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
int position = Integer.parseInt(pos);
if(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).getAircraft(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")),position) != null)
{
deletedAircrafts.add(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).getAircraft(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")),position));
}
if(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).removeAircraft(_mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")),position) != null)
{
JOptionPane.showMessageDialog(this,"Object is removed.");
bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
}
else
{
JOptionPane.showMessageDialog(this,"Unable to remove object.");
}
}
private void ShowHangar()
{
if(listBoxMaps.getSelectedIndex() == -1)
{
return;
}
bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
}
private void moveObject(ActionEvent event)
{
if(listBoxMaps.getSelectedIndex() == -1)
{
return;
}
String name = ((JButton)event.getSource()).getName();
Direction dir = null;
switch (name)
{
case "left" -> dir = Direction.Left;
case "right" -> dir = Direction.Right;
case "up" -> dir = Direction.Up;
case "down" -> dir = Direction.Down;
}
bufferedImage = _mapsCollection.getMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).MoveObject(dir);
repaint();
}
private void addMap()
{
if (comboBoxMapSelection.getSelectedIndex() == -1 || newMapTextBox.getText() == null || newMapTextBox.getText().equals(""))
{
JOptionPane.showMessageDialog(this, "Not all data is filled", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (!_mapsDict.containsKey(comboBoxMapSelection.getSelectedItem()))
{
JOptionPane.showMessageDialog(this, "There is no such map", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
_mapsCollection.AddMap(newMapTextBox.getText(), _mapsDict.get(comboBoxMapSelection.getSelectedItem()));
RefreshMaps();
}
private void deleteMap()
{
if (listBoxMaps.getSelectedIndex() == -1)
{
return;
}
String str = String.format("Delete map %s?",listBoxMaps.getSelectedValue().toString());
if (JOptionPane.showConfirmDialog(this,str,"Deletion",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
_mapsCollection.DeleteMap((String) Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse(""));
RefreshMaps();
}
}
public FormMapWithSetAircrafts()
{
this.setContentPane(Form);
this.setSize(800,600);
this.setVisible(true);
_mapsCollection = new MapsCollection(pictureBox.getWidth(),pictureBox.getHeight());
try {
maskedTextBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
} catch (ParseException e) {
e.printStackTrace();
}
deletedAircrafts = new LinkedList<IDrawingObject>();
ActionListener listener = this::moveObject;
moveDown.addActionListener(listener);
moveUp.addActionListener(listener);
moveRight.addActionListener(listener);
moveLeft.addActionListener(listener);
ShowMapButton.addActionListener(e -> showMap());
listBoxMaps.addListSelectionListener(e -> indexChanged());
AddAircraftButton.addActionListener(e -> addAircraft());
DeleteAircraftButton.addActionListener(e -> DeleteAircraft());
ShowHangarButton.addActionListener(e -> ShowHangar());
AddMapButton.addActionListener(e -> addMap());
DeleteMapButton.addActionListener(e -> deleteMap());
showDeletedAircrafts.addActionListener(e -> {
if(deletedAircrafts.isEmpty())
{
JOptionPane.showMessageDialog(this, "There are not any deleted aircrafts", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
DrawingObjectAircraft deletedAircraft = (DrawingObjectAircraft)deletedAircrafts.remove();
form = new FormAirFighter(deletedAircraft.getAircraft());
form.setSize(800,500);
form.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
form.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
form.setVisible(true);
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
if (bufferedImage != null) {
pictureBox.paintComponents(bufferedImage.getGraphics());
pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
}
}
}

11
LisovJava.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>