This commit is contained in:
MaxKarme 2022-11-10 15:44:45 +03:00
parent f25feff68b
commit 9f2db8fe31
17 changed files with 884 additions and 21 deletions

188
AbstractMap.java Normal file
View File

@ -0,0 +1,188 @@
import java.awt.*;
import java.util.Random;
public abstract class AbstractMap
{
private IDrawingObject _drawningObject = null;
protected int[][] _map = null;
protected int _width;
protected int _height;
protected float _size_x;
protected float _size_y;
protected Random _random = new Random();
protected int _freeRoad = 0;
protected int _barrier = 1;
public void CreateMap(int width, int height, IDrawingObject drawningObject)
{
_width = width;
_height = height;
_drawningObject = drawningObject;
GenerateMap();
while (!SetObjectOnMap())
{
GenerateMap();
}
}
private Point checkBarrier(Point leftTop, Point rightBottom)
{
return checkBarrier(leftTop, rightBottom, false, false, true, false);
}
private Point checkBarrier(Point leftTop, Point rightBottom, boolean minLeft, boolean maxLeft, boolean isTop, boolean isBottom)
{
Point res = new Point(-1, -1);
for (int i = (int)(leftTop.y / _size_y); i <= (int)(rightBottom.y / _size_y) && i < _map.length; ++i)
{
for (int j = (int)(leftTop.x / _size_x); j <= (int)(rightBottom.x / _size_x) && j < _map[0].length; ++j)
{
if (j < 0) j = 0;
if (i < 0) i = 0;
if (_map[i][j] != _barrier) continue;
if (res.y == -1) res = new Point(j, i);
if (minLeft && res.x > j) res = new Point(j, i);
if (maxLeft && res.x < j) res = new Point(j, i);
if(isBottom) res = new Point(j, i);
if (isTop) return new Point(j, i);
}
}
return res;
}
public void MoveObject(Direction direction)
{
// TODO проверка, что объект может переместится в требуемом
Point leftTop = _drawningObject.GetLeftTop();
Point rightBottom = _drawningObject.GetRightBottom();
float drawningWidth = rightBottom.x - leftTop.x;
float drawningHeight = rightBottom.y - leftTop.y;
boolean minLeft = false;
boolean maxLeft = false;
boolean isTop = false;
boolean isBottom = false;
if (direction == Direction.Left)
{
leftTop.x -= _drawningObject.getStep();
maxLeft = true;
}
if (direction == Direction.Right)
{
leftTop.x += _drawningObject.getStep();
minLeft = true;
}
if (direction == Direction.Up) {
leftTop.y -= _drawningObject.getStep();
isTop = true;
}
if (direction == Direction.Down)
{
leftTop.y += _drawningObject.getStep();
isBottom = true;
}
rightBottom.x = leftTop.x + (int)drawningWidth;
rightBottom.y = leftTop.y + (int)drawningHeight;
Point currentBarrier = checkBarrier(leftTop, rightBottom, minLeft, maxLeft, isTop, isBottom);
if (currentBarrier.x == -1)
{
_drawningObject.MoveObject(direction);
}
else if (direction == Direction.Left)
leftTop.x = (int)((currentBarrier.x + 1) * _size_x) + 1;
else if (direction == Direction.Right)
leftTop.x = (int)(currentBarrier.x * _size_x) - (int)drawningWidth - 1;
else if (direction == Direction.Up)
leftTop.y = (int)((currentBarrier.y + 1) * _size_y) + 1;
else if (direction == Direction.Down)
leftTop.y = (int)(currentBarrier.y * _size_y) - (int)drawningHeight - 1;
if (currentBarrier.y != -1)
_drawningObject.SetObject(leftTop.x, leftTop.y, _width, _height);
}
private boolean SetObjectOnMap()
{
if (_drawningObject == null || _map == null)
{
return false;
}
int x = _random.nextInt(0, 10);
int y = _random.nextInt(0, 10);
_drawningObject.SetObject(x, y, _width, _height);
// TODO првоерка, что объект не "накладывается" на закрытые участки
Point leftTop = _drawningObject.GetLeftTop();
Point rightBottom = _drawningObject.GetRightBottom();
float drawningWidth = rightBottom.x - leftTop.x;
float drawningHeight = rightBottom.y - leftTop.y;
Point currentBarrier = checkBarrier(leftTop, rightBottom);
int minRowIndex = _map.length;
while(currentBarrier.y != -1)
{
minRowIndex = currentBarrier.y < minRowIndex ? currentBarrier.y : minRowIndex;
leftTop.x = (int)((currentBarrier.x + 1) * _size_x) + 1;
rightBottom.x = leftTop.x + (int)drawningWidth;
if(rightBottom.x > _width)
{
leftTop.y = (int)((minRowIndex + 1) * _size_y) + 1;
rightBottom.y = leftTop.y + (int)drawningHeight;
leftTop.x = 0;
rightBottom.x = (int)drawningWidth;
minRowIndex = _map.length;
}
if (rightBottom.y > _height) {
return false;
}
currentBarrier = checkBarrier(leftTop, rightBottom);
}
_drawningObject.SetObject((int)leftTop.x, (int)leftTop.y, _width, _height);
return true;
}
public void DrawMapWithObject(Graphics2D gr)
{
if (_drawningObject == null || _map == null)
{
return;
}
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[0].length; ++j)
{
if (_map[i][j] == _freeRoad)
{
DrawRoadPart(gr, i, j);
}
else if (_map[i][j] == _barrier)
{
DrawBarrierPart(gr, i, j);
}
}
}
_drawningObject.DrawningObject(gr);
}
protected abstract void GenerateMap();
protected abstract void DrawRoadPart(Graphics2D g, int i, int j);
protected abstract void DrawBarrierPart(Graphics2D g, int i, int j);
}

View File

@ -4,10 +4,10 @@ import java.util.Random;
class DrawingAircraft
{
public EntityAircraft AirFighter;
public DrawingEngines drawingEngines = new DrawingEngines();
public IDrawingEngines drawingEngines;
private float _startPosX;
private float _startPosY;
protected float _startPosX;
protected float _startPosY;
private int _pictureWidth = -1;
private int _pictureHeight = -1;
@ -15,14 +15,37 @@ class DrawingAircraft
private int _airFighterWidth = 195;
private int _airFighterHeight = 166;
public void Init(int speed, float weight, Color bodyColor)
{
private void peekRandomEngines(Color color) {
Random rnd = new Random();
int randEngine = rnd.nextInt(1, 4);
AirFighter = new EntityAircraft();
AirFighter.Init(speed, weight, bodyColor);
drawingEngines.Init(rnd.nextInt(1, 8), bodyColor);
switch(randEngine) {
case 1:
drawingEngines = new DrawingEngines(rnd.nextInt(1, 8), color);
break;
case 2:
drawingEngines = new DrawingTruncatedEngines(rnd.nextInt(1, 8), color);
break;
case 3:
drawingEngines = new DrawingWavyEngines(rnd.nextInt(1, 8), color);
break;
}
}
public DrawingAircraft(int speed, float weight, Color bodyColor)
{
AirFighter = new EntityAircraft(speed, weight, bodyColor);
peekRandomEngines(bodyColor);
}
public DrawingAircraft(int speed, float weight, Color bodyColor, int airFighterWidth, int airFighterHeight)
{
this(speed, weight, bodyColor);
_airFighterWidth = airFighterWidth;
_airFighterHeight = airFighterHeight;
}
public void SetPosition(int x, int y, int width, int height)
{
@ -149,5 +172,12 @@ class DrawingAircraft
}
}
public Point getLeftTop() {
return new Point((int)_startPosX, (int)_startPosY);
}
public Point getRightBottom() {
return new Point((int)_startPosX + _airFighterWidth, (int)_startPosY + _airFighterHeight);
}
}

View File

@ -1,14 +1,15 @@
import java.awt.*;
public class DrawingEngines {
public class DrawingEngines implements IDrawingEngines {
private EnginesCount enginesCount;
private Color color;
public void Init(int count, Color bodyColor) {
public DrawingEngines(int count, Color bodyColor) {
setCount(count);
color = bodyColor;
}
@Override
public void setCount(int count) {
if(count <= 2) enginesCount = EnginesCount.Two;
else if(count >= 6) enginesCount = EnginesCount.Six;
@ -17,17 +18,17 @@ public class DrawingEngines {
public void draw(Graphics2D g, int startPosX, int startPosY) {
g.setPaint(color);
g.fillOval(startPosX + 90, startPosY + 10, 30, 15);
g.fillOval(startPosX + 90, startPosY + 141, 30, 15);
g.fillOval(startPosX + 80, startPosY + 10, 30, 15);
g.fillOval(startPosX + 80, startPosY + 141, 30, 15);
if(enginesCount == EnginesCount.Two) return;
g.fillOval(startPosX + 90, startPosY + 30, 30, 15);
g.fillOval(startPosX + 90, startPosY + 121, 30, 15);
g.fillOval(startPosX + 80, startPosY + 30, 30, 15);
g.fillOval(startPosX + 80, startPosY + 121, 30, 15);
if(enginesCount == EnginesCount.Four) return;
g.fillOval(startPosX + 90, startPosY + 50, 30, 15);
g.fillOval(startPosX + 90, startPosY + 101, 30, 15);
g.fillOval(startPosX + 80, startPosY + 50, 30, 15);
g.fillOval(startPosX + 80, startPosY + 101, 30, 15);
}
}

View File

@ -0,0 +1,78 @@
import java.awt.*;
public class DrawingModernAircraft extends DrawingAircraft
{
public DrawingModernAircraft(int speed, float weight, Color bodyColor, Color dopColor, boolean dopWings, boolean rockets)
{
super(speed, weight, bodyColor, 195, 166);
AirFighter = new EntityModernAircraft(speed, weight, bodyColor, dopColor, dopWings, rockets);
}
@Override
public void DrawTransport(Graphics2D g)
{
if (!(AirFighter instanceof EntityModernAircraft))
{
return;
}
EntityModernAircraft modernAircraft = (EntityModernAircraft)AirFighter;
g.setPaint(modernAircraft.DopColor);
if (modernAircraft.DopWings)
{
Polygon topDopWing = new Polygon();
topDopWing.addPoint((int)_startPosX + 78, (int)_startPosY + 56);
topDopWing.addPoint((int)_startPosX + 75, (int)_startPosY + 70);
topDopWing.addPoint((int)_startPosX + 55, (int)_startPosY + 50);
topDopWing.addPoint((int)_startPosX + 60, (int)_startPosY + 45);
Polygon bottomDopWing = new Polygon();
bottomDopWing.addPoint((int)_startPosX + 78, (int)_startPosY + 110);
bottomDopWing.addPoint((int)_startPosX + 75, (int)_startPosY + 96);
bottomDopWing.addPoint((int)_startPosX + 55, (int)_startPosY + 116);
bottomDopWing.addPoint((int)_startPosX + 60, (int)_startPosY + 121);
g.fillPolygon(topDopWing);
g.fillPolygon(bottomDopWing);
}
if (modernAircraft.Rockets)
{
Polygon topRocket1 = new Polygon();
topRocket1.addPoint((int)_startPosX + 100, (int)_startPosY + 20);
topRocket1.addPoint((int)_startPosX + 100, (int)_startPosY + 30);
topRocket1.addPoint((int)_startPosX + 112, (int)_startPosY + 30);
topRocket1.addPoint((int)_startPosX + 120, (int)_startPosY + 25);
topRocket1.addPoint((int)_startPosX + 112, (int)_startPosY + 20);
Polygon topRocket2 = new Polygon();
topRocket2.addPoint((int)_startPosX + 100, (int)_startPosY + 35);
topRocket2.addPoint((int)_startPosX + 100, (int)_startPosY + 45);
topRocket2.addPoint((int)_startPosX + 112, (int)_startPosY + 45);
topRocket2.addPoint((int)_startPosX + 120, (int)_startPosY + 40);
topRocket2.addPoint((int)_startPosX + 112, (int)_startPosY + 35);
Polygon bottomRocket1 = new Polygon();
bottomRocket1.addPoint((int)_startPosX + 100, (int)_startPosY + 146);
bottomRocket1.addPoint((int)_startPosX + 100, (int)_startPosY + 136);
bottomRocket1.addPoint((int)_startPosX + 112, (int)_startPosY + 136);
bottomRocket1.addPoint((int)_startPosX + 120, (int)_startPosY + 141);
bottomRocket1.addPoint((int)_startPosX + 112, (int)_startPosY + 146);
Polygon bottomRocket2 = new Polygon();
bottomRocket2.addPoint((int)_startPosX + 100, (int)_startPosY + 131);
bottomRocket2.addPoint((int)_startPosX + 100, (int)_startPosY + 121);
bottomRocket2.addPoint((int)_startPosX + 112, (int)_startPosY + 121);
bottomRocket2.addPoint((int)_startPosX + 120, (int)_startPosY + 126);
bottomRocket2.addPoint((int)_startPosX + 112, (int)_startPosY + 131);
g.fillPolygon(topRocket1);
g.fillPolygon(topRocket2);
g.fillPolygon(bottomRocket1);
g.fillPolygon(bottomRocket2);
}
super.DrawTransport(g);
}
}

View File

@ -0,0 +1,42 @@
import java.awt.*;
public class DrawingObjectAircraft implements IDrawingObject
{
private DrawingAircraft _aircraft = null;
public DrawingObjectAircraft(DrawingAircraft aircraft){
_aircraft = aircraft;
}
public void MoveObject(Direction direction) {
if(_aircraft == null) return;
_aircraft.MoveTransport(direction);
}
@Override
public float getStep() {
if(_aircraft == null) return 0;
return _aircraft.AirFighter.Step;
}
public void SetObject(int x, int y, int width, int height)
{
_aircraft.SetPosition(x, y, width, height);
}
public void DrawningObject(Graphics2D g)
{
// TODO
_aircraft.DrawTransport(g);
}
@Override
public Point GetLeftTop() {
if(_aircraft == null) return new Point(0,0);
return _aircraft.getLeftTop();
}
@Override
public Point GetRightBottom() {
if(_aircraft == null) return new Point(0,0);
return _aircraft.getRightBottom();
}
}

View File

@ -0,0 +1,34 @@
import java.awt.*;
public class DrawingTruncatedEngines implements IDrawingEngines {
private EnginesCount enginesCount;
private Color color;
public DrawingTruncatedEngines(int count, Color bodyColor) {
setCount(count);
color = bodyColor;
}
@Override
public void setCount(int count) {
if(count <= 2) enginesCount = EnginesCount.Two;
else if(count >= 6) enginesCount = EnginesCount.Six;
else enginesCount = EnginesCount.Four;
}
public void draw(Graphics2D g, int startPosX, int startPosY) {
g.setPaint(color);
g.fillArc(startPosX + 90, startPosY + 10, 30, 15, 90, 180);
g.fillArc(startPosX + 90, startPosY + 141, 30, 15, 90, 180);
if(enginesCount == EnginesCount.Two) return;
g.fillArc(startPosX + 90, startPosY + 30, 30, 15, 90, 180);
g.fillArc(startPosX + 90, startPosY + 121, 30, 15, 90, 180);
if(enginesCount == EnginesCount.Four) return;
g.fillArc(startPosX + 90, startPosY + 50, 30, 15, 90, 180);
g.fillArc(startPosX + 90, startPosY + 101, 30, 15, 90, 180);
}
}

46
DrawingWavyEngines.java Normal file
View File

@ -0,0 +1,46 @@
import java.awt.*;
public class DrawingWavyEngines implements IDrawingEngines {
private EnginesCount enginesCount;
private Color color;
public DrawingWavyEngines(int count, Color bodyColor) {
setCount(count);
color = bodyColor;
}
@Override
public void setCount(int count) {
if(count <= 2) enginesCount = EnginesCount.Two;
else if(count >= 6) enginesCount = EnginesCount.Six;
else enginesCount = EnginesCount.Four;
}
private void drawEngine(Graphics2D g, int x, int y) {
g.setColor(color);
g.fillRect(x, y, 21, 10);
g.fillArc(x, y - 4, 7, 8, 0, 180);
g.fillArc(x + 7, y - 4, 7, 8, 0, 180);
g.fillArc(x + 14, y - 4, 7, 8, 0, 180);
g.fillArc(x, y + 6, 7, 8, 180, 180);
g.fillArc(x + 7, y + 6, 7, 8, 180, 180);
g.fillArc(x + 14, y + 6, 7, 8, 180, 180);
}
public void draw(Graphics2D g, int startPosX, int startPosY) {
drawEngine(g, startPosX + 84, startPosY + 10);
drawEngine(g, startPosX + 84, startPosY + 146);
if(enginesCount == EnginesCount.Two) return;
drawEngine(g, startPosX + 84, startPosY + 30);
drawEngine(g, startPosX + 84, startPosY + 125);
if(enginesCount == EnginesCount.Four) return;
drawEngine(g, startPosX + 84, startPosY + 50);
drawEngine(g, startPosX + 84, startPosY + 106);
}
}

View File

@ -9,7 +9,7 @@ class EntityAircraft
public float Step;
public void Init(int speed, float weight, Color bodyColor)
public EntityAircraft(int speed, float weight, Color bodyColor)
{
Random rnd = new Random();

18
EntityModernAircraft.java Normal file
View File

@ -0,0 +1,18 @@
import java.awt.*;
public class EntityModernAircraft extends EntityAircraft
{
public Color DopColor;
public boolean DopWings;
public boolean Rockets;
public EntityModernAircraft(int speed, float weight, Color bodyColor, Color
dopColor, boolean dopWings, boolean rockets)
{
super(speed, weight, bodyColor);
DopColor = dopColor;
DopWings = dopWings;
Rockets = rockets;
}
}

View File

@ -40,9 +40,7 @@ public class FormAircraft implements Form {
Dimension canvSize = canv.getSize();
Random rnd = new Random();
_airFighter = new DrawingAircraft();
_airFighter.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
_airFighter = new DrawingAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
_airFighter.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height);

6
IDrawingEngines.java Normal file
View File

@ -0,0 +1,6 @@
import java.awt.*;
public interface IDrawingEngines {
void setCount(int count);
void draw(Graphics2D g, int x, int y);
}

11
IDrawingObject.java Normal file
View File

@ -0,0 +1,11 @@
import java.awt.*;
public interface IDrawingObject
{
float getStep();
void SetObject(int x, int y, int width, int height);
void MoveObject(Direction direction);
void DrawningObject(Graphics2D g);
Point GetLeftTop();
Point GetRightBottom();
}

View File

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

72
MyMap.java Normal file
View File

@ -0,0 +1,72 @@
import java.awt.*;
public class MyMap extends AbstractMap
{
private Color barrierColor = Color.BLACK;
private Color roadColor = Color.GRAY;
@Override
protected void DrawBarrierPart(Graphics2D g, int i, int j)
{
g.setPaint(barrierColor);
g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void DrawRoadPart(Graphics2D g, int i, int j)
{
g.setPaint(roadColor);
g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void GenerateMap()
{
_map = new int[100][100];
_size_x = (float)_width / _map.length;
_size_y = (float)_height / _map[0].length;
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[0].length; ++j)
{
_map[i][j] = _freeRoad;
}
}
for(int i = 0; i < 20; ++i)
{
int x = _random.nextInt(0, 100);
int y = _random.nextInt(0, 100);
GenerateMap(x, y, _random.nextInt(13, 23));
}
}
private void GenerateMap(int x, int y, int depth)
{
if (depth <= 0) return;
boolean check = false;
while (!check)
{
int deltaX = _random.nextInt(-1, 2);
int deltaY = _random.nextInt(-1, 2);
if (x + deltaX < 0 || x + deltaX >= 100) continue;
if (y + deltaY < 0 || y + deltaY >= 100) continue;
if (_map[y + deltaY][x + deltaX] == _barrier) depth--;
x += deltaX;
y += deltaY;
_map[y][x] = _barrier;
check = true;
}
GenerateMap(x, y, depth - 1);
}
}

47
SimpleMap.java Normal file
View File

@ -0,0 +1,47 @@
import java.awt.*;
public class SimpleMap extends AbstractMap
{
private Color barrierColor = Color.BLACK;
private Color roadColor = Color.GRAY;
@Override
protected void DrawBarrierPart(Graphics2D g, int i, int j)
{
g.setPaint(barrierColor);
g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void DrawRoadPart(Graphics2D g, int i, int j)
{
g.setPaint(roadColor);
g.fillRect((int)(j * _size_x), (int)(i * _size_y), (int)Math.ceil(_size_x), (int)Math.ceil(_size_y));
}
@Override
protected void GenerateMap()
{
_map = new int[100][100];
_size_x = (float)_width / _map.length;
_size_y = (float)_height / _map[0].length;
int counter = 0;
for (int i = 0; i < _map.length; ++i)
{
for (int j = 0; j < _map[0].length; ++j)
{
_map[i][j] = _freeRoad;
}
}
while (counter < 50)
{
int x = _random.nextInt(0, 100);
int y = _random.nextInt(0, 100);
if (_map[x][y] == _freeRoad)
{
_map[x][y] = _barrier;
counter++;
}
}
}
}

173
formMap.form Normal file
View File

@ -0,0 +1,173 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="formMap">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" 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>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="e3a5c" binding="DrawPlace" layout-manager="CardLayout" hgap="0" vgap="0">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<toolTipText value=""/>
</properties>
<border type="none"/>
<children/>
</grid>
<grid id="6cd69" layout-manager="GridLayoutManager" row-count="2" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="73e7d">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="2f248" class="javax.swing.JButton" binding="createButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="создание"/>
</properties>
</component>
<component id="59723" class="javax.swing.JButton" binding="downButton" default-binding="true">
<constraints>
<grid row="1" column="4" 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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/down.png"/>
<text value=""/>
</properties>
</component>
<component id="c4a07" class="javax.swing.JButton" binding="leftButton" default-binding="true">
<constraints>
<grid row="1" column="3" 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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/left.png"/>
<text value=""/>
</properties>
</component>
<component id="c0037" class="javax.swing.JButton" binding="rightButton" default-binding="true">
<constraints>
<grid row="1" column="5" 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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/right.png"/>
<text value=""/>
</properties>
</component>
<component id="5b159" class="javax.swing.JButton" binding="upButton" default-binding="true">
<constraints>
<grid row="0" column="4" 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="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
</grid>
</constraints>
<properties>
<icon value="Resources/up.png"/>
<text value=""/>
</properties>
</component>
<component id="fb982" class="javax.swing.JButton" binding="modifiedButton">
<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>
<text value="Модификация"/>
</properties>
</component>
</children>
</grid>
<grid id="86a02" layout-manager="GridLayoutManager" row-count="1" 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="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="-1" height="20"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="81cff" class="javax.swing.JLabel" binding="weightLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Вес: "/>
</properties>
</component>
<component id="e587f" class="javax.swing.JLabel" binding="speedLabel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Скорость: "/>
</properties>
</component>
<component id="ea489" class="javax.swing.JLabel" binding="colorLabel">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Цвет"/>
</properties>
</component>
</children>
</grid>
<grid id="c8210" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="50374" class="javax.swing.JComboBox" binding="comboBoxSelectorMap">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="1" indent="0" use-parent-layout="false">
<minimum-size width="160" height="30"/>
<preferred-size width="160" height="30"/>
<maximum-size width="160" height="30"/>
</grid>
</constraints>
<properties>
<model>
<item value="Простая карта"/>
<item value="Моя карта"/>
</model>
<toolTipText value=""/>
</properties>
</component>
<hspacer id="d5463">
<constraints>
<grid row="0" 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>
</children>
</grid>
</children>
</grid>
</form>

119
formMap.java Normal file
View File

@ -0,0 +1,119 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class formMap implements Form {
private JPanel DrawPlace;
private JLabel weightLabel;
private JLabel speedLabel;
private JLabel colorLabel;
private JButton createButton;
private JButton downButton;
private JButton leftButton;
private JButton rightButton;
private JButton upButton;
private JComboBox comboBoxSelectorMap;
private JPanel mainPanel;
private JButton modifiedButton;
private Canvas canv = new Canvas(this);
private DrawingAircraft _aircraft;
private AbstractMap _abstractMap = new SimpleMap();
private JFrame jframe = getFrame();
public formMap() {
}
private JFrame getFrame() {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setBounds(300, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
private void SetData(DrawingAircraft aircraft)
{
Color bodyColor = _aircraft.AirFighter.BodyColor;
String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")";
speedLabel.setText("Скорость: " + _aircraft.AirFighter.Speed + " ");
weightLabel.setText("Вес: " + _aircraft.AirFighter.Weight + " ");
colorLabel.setText("Цвет: " + colorString);
Dimension canvSize = canv.getSize();
_abstractMap.CreateMap(canvSize.width, canvSize.height, new DrawingObjectAircraft(aircraft));
}
public void run() {
jframe.add(mainPanel);
DrawPlace.add(canv);
comboBoxSelectorMap.addActionListener(e -> {
String selectedItem = comboBoxSelectorMap.getSelectedItem().toString();
switch(selectedItem) {
case "Простая карта":
_abstractMap = new SimpleMap();
break;
case "Моя карта":
_abstractMap = new MyMap();
break;
}
});
createButton.addActionListener(e -> {
Random rnd = new Random();
_aircraft = new DrawingAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
SetData(_aircraft);
canv.repaint();
});
modifiedButton.addActionListener(e -> {
Random rnd = new Random();
_aircraft = new DrawingModernAircraft(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.nextInt(0, 2) == 1, rnd.nextInt(0, 2) == 1);
SetData(_aircraft);
canv.repaint();
});
downButton.addActionListener(e -> {
if(_abstractMap == null) return;
_abstractMap.MoveObject(Direction.Down);
canv.repaint();
});
upButton.addActionListener(e -> {
if(_abstractMap == null) return;
_abstractMap.MoveObject(Direction.Up);
canv.repaint();
});
leftButton.addActionListener(e -> {
if(_abstractMap == null) return;
_abstractMap.MoveObject(Direction.Left);
canv.repaint();
});
rightButton.addActionListener(e -> {
if(_abstractMap == null) return;
_abstractMap.MoveObject(Direction.Right);
canv.repaint();
});
}
@Override
public void Draw(Graphics2D g) {
_abstractMap.DrawMapWithObject(g);
}
}