Лаб 02
This commit is contained in:
parent
f97e47e9d1
commit
315ef45c93
188
RadarAirplane/src/AbstractMap.java
Normal file
188
RadarAirplane/src/AbstractMap.java
Normal 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)
|
||||
{
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
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);
|
||||
}
|
@ -3,9 +3,9 @@ import java.util.Random;
|
||||
class DrawingEntityPlain
|
||||
{
|
||||
public EntityAirPlane Airplane;
|
||||
public DrawingPorthole drawingPortholes = new DrawingPorthole();
|
||||
private float _startPosX;
|
||||
private float _startPosY;
|
||||
public IDrawingPorthole drawingPortholes;
|
||||
protected float _startPosX;
|
||||
protected float _startPosY;
|
||||
|
||||
private int _pictureWidth = -1;
|
||||
private int _pictureHeight = -1;
|
||||
@ -13,14 +13,35 @@ class DrawingEntityPlain
|
||||
private int _AirplaneWidth = 240;
|
||||
private int _AirplaneHeight = 150;
|
||||
|
||||
public void Init(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
private void peekRandomPortholes(Color color) {
|
||||
Random rnd = new Random();
|
||||
Airplane = new EntityAirPlane();
|
||||
Airplane.Init(speed, weight, bodyColor);
|
||||
drawingPortholes.Init(rnd.nextInt(1, 35),Airplane.BodyColor);
|
||||
int randPorthole = rnd.nextInt(1, 4);
|
||||
|
||||
switch(randPorthole) {
|
||||
case 1:
|
||||
drawingPortholes = new DrawingPorthole(rnd.nextInt(1, 35), color);
|
||||
break;
|
||||
case 2:
|
||||
drawingPortholes = new DrawingSquarePorthole(rnd.nextInt(1, 35), color);
|
||||
break;
|
||||
case 3:
|
||||
drawingPortholes = new DrawingTrianglePorthole(rnd.nextInt(1, 35), color);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public DrawingEntityPlain(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
Airplane = new EntityAirPlane(speed, weight, bodyColor);
|
||||
peekRandomPortholes(bodyColor);
|
||||
}
|
||||
|
||||
public DrawingEntityPlain(int speed, float weight, Color bodyColor, int airPlaneWidth, int airPlaneHeight)
|
||||
{
|
||||
this(speed, weight, bodyColor);
|
||||
_AirplaneWidth = airPlaneWidth;
|
||||
_AirplaneHeight = airPlaneHeight;
|
||||
}
|
||||
public void SetPosition(int x, int y, int width, int height)
|
||||
{
|
||||
if (width < _AirplaneWidth || height < _AirplaneHeight) return;
|
||||
@ -80,7 +101,7 @@ class DrawingEntityPlain
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
g.setPaint(Color.black);
|
||||
g.drawRect( (int)_startPosX, (int)_startPosY, 40, 60);
|
||||
g.drawRect( (int)_startPosX, (int)_startPosY + 60, 200, 60);
|
||||
g.drawRect( (int)_startPosX+100, (int)_startPosY + 80, 40, 30);
|
||||
@ -126,5 +147,12 @@ class DrawingEntityPlain
|
||||
_startPosY = _pictureHeight - _AirplaneHeight;
|
||||
}
|
||||
}
|
||||
public Point getLeftTop() {
|
||||
return new Point((int)_startPosX, (int)_startPosY);
|
||||
}
|
||||
|
||||
public Point getRightBottom() {
|
||||
return new Point((int)_startPosX + _AirplaneWidth, (int)_startPosY + _AirplaneHeight);
|
||||
}
|
||||
|
||||
}
|
41
RadarAirplane/src/DrawingObjectPlain.java
Normal file
41
RadarAirplane/src/DrawingObjectPlain.java
Normal file
@ -0,0 +1,41 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingObjectPlain implements IDrawingObject
|
||||
{
|
||||
private DrawingEntityPlain _airplain = null;
|
||||
public DrawingObjectPlain(DrawingEntityPlain aircraft){
|
||||
_airplain = aircraft;
|
||||
}
|
||||
|
||||
public void MoveObject(Direction direction) {
|
||||
if(_airplain == null) return;
|
||||
_airplain.MoveTransport(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getStep() {
|
||||
if(_airplain == null) return 0;
|
||||
return _airplain.Airplane.Step;
|
||||
}
|
||||
|
||||
public void SetObject(int x, int y, int width, int height)
|
||||
{
|
||||
_airplain.SetPosition(x, y, width, height);
|
||||
}
|
||||
public void DrawningObject(Graphics2D g)
|
||||
{
|
||||
_airplain.DrawTransport(g);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Point GetLeftTop() {
|
||||
if(_airplain == null) return new Point(0,0);
|
||||
return _airplain.getLeftTop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Point GetRightBottom() {
|
||||
if(_airplain == null) return new Point(0,0);
|
||||
return _airplain.getRightBottom();
|
||||
}
|
||||
}
|
@ -1,24 +1,24 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingPorthole {
|
||||
public class DrawingPorthole implements IDrawingPorthole {
|
||||
|
||||
private Porthole PortholeCount;
|
||||
Color color;
|
||||
|
||||
public void SetCount(int count){
|
||||
@Override
|
||||
public void setCount(int count){
|
||||
if(count <= 10) PortholeCount = Porthole.Ten;
|
||||
else if(count >= 30) PortholeCount = Porthole.Twenty;
|
||||
else PortholeCount = Porthole.Thirty;
|
||||
}
|
||||
public void Init(int count, Color bodyColor) {
|
||||
SetCount(count);
|
||||
public DrawingPorthole(int count, Color bodyColor) {
|
||||
setCount(count);
|
||||
color = bodyColor;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void draw(Graphics2D g, int startPosX, int startPosY) {
|
||||
g.setPaint(Color.white);
|
||||
int count = 0;
|
||||
int i = -1;
|
||||
int j = 0;
|
||||
@ -32,12 +32,12 @@ public class DrawingPorthole {
|
||||
if(PortholeCount == Porthole.Ten && count==10) break;
|
||||
else if(PortholeCount == Porthole.Twenty && count==20) break;
|
||||
else if(PortholeCount == Porthole.Thirty && count==30) break;
|
||||
g.setPaint(Color.black);
|
||||
g.drawOval(startPosX+i*12 , startPosY + j, 10, 10);
|
||||
g.setPaint(color);
|
||||
g.fillOval(startPosX+i*12, startPosY + j, 10, 10);
|
||||
count++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
32
RadarAirplane/src/DrawingRadarPlain.java
Normal file
32
RadarAirplane/src/DrawingRadarPlain.java
Normal file
@ -0,0 +1,32 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingRadarPlain extends DrawingEntityPlain
|
||||
{
|
||||
public DrawingRadarPlain(int speed, float weight, Color bodyColor, Color dopColor, boolean Radar, boolean OilBox)
|
||||
{
|
||||
super(speed, weight, bodyColor, 240, 150);
|
||||
Airplane = new EntityRadarPlain(speed, weight, bodyColor, dopColor, Radar, OilBox);
|
||||
}
|
||||
@Override
|
||||
public void DrawTransport(Graphics2D g)
|
||||
{
|
||||
if (!(Airplane instanceof EntityRadarPlain))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EntityRadarPlain radarPlain = (EntityRadarPlain)Airplane;
|
||||
super.DrawTransport(g);
|
||||
g.setPaint(radarPlain.DopColor);
|
||||
if (radarPlain.Radar)
|
||||
{
|
||||
g.fillRect( (int)_startPosX +110, (int)_startPosY + 40, 20, 20);
|
||||
g.fillOval( (int)_startPosX + 80, (int)_startPosY + 26, 80, 20);
|
||||
}
|
||||
|
||||
if (radarPlain.OilBox) {
|
||||
g.fillRect((int) _startPosX + 80, (int) _startPosY + 96, 40, 20);
|
||||
g.fillRect((int) _startPosX, (int) _startPosY + 30, 60, 20);
|
||||
}
|
||||
}
|
||||
}
|
41
RadarAirplane/src/DrawingSquarePorthole.java
Normal file
41
RadarAirplane/src/DrawingSquarePorthole.java
Normal file
@ -0,0 +1,41 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingSquarePorthole implements IDrawingPorthole {
|
||||
private Porthole PortholeCount;
|
||||
private Color color;
|
||||
|
||||
public DrawingSquarePorthole(int count, Color bodyColor) {
|
||||
setCount(count);
|
||||
color = bodyColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCount(int count) {
|
||||
if(count <= 10) PortholeCount = Porthole.Ten;
|
||||
else if(count >= 30) PortholeCount = Porthole.Twenty;
|
||||
else PortholeCount = Porthole.Thirty;
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g, int startPosX, int startPosY) {
|
||||
g.setPaint(color);
|
||||
int count = 0;
|
||||
int i = -1;
|
||||
int j = 0;
|
||||
while(true)
|
||||
{
|
||||
if(i==14) {
|
||||
i = 0;
|
||||
j=30;
|
||||
}
|
||||
else i++;
|
||||
if(PortholeCount == Porthole.Ten && count==10) break;
|
||||
else if(PortholeCount == Porthole.Twenty && count==20) break;
|
||||
else if(PortholeCount == Porthole.Thirty && count==30) break;
|
||||
g.setPaint(Color.black);
|
||||
g.drawRect(startPosX+i*12 , startPosY + j, 10, 10);
|
||||
g.setPaint(color);
|
||||
g.fillRect(startPosX+i*12, startPosY + j, 10, 10);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
50
RadarAirplane/src/DrawingTrianglePorthole.java
Normal file
50
RadarAirplane/src/DrawingTrianglePorthole.java
Normal file
@ -0,0 +1,50 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class DrawingTrianglePorthole implements IDrawingPorthole {
|
||||
private Porthole PortholeCount;
|
||||
private Color color;
|
||||
|
||||
public DrawingTrianglePorthole(int count, Color bodyColor) {
|
||||
setCount(count);
|
||||
color = bodyColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCount(int count) {
|
||||
if(count <= 10) PortholeCount = Porthole.Ten;
|
||||
else if(count >= 30) PortholeCount = Porthole.Twenty;
|
||||
else PortholeCount = Porthole.Thirty;
|
||||
}
|
||||
|
||||
private void drawEngine(Graphics2D g, int x, int y) {
|
||||
|
||||
g.setColor(Color.black);
|
||||
g.drawRect(x+2, y, 4, 5);
|
||||
g.drawRect(x, y+5, 5, 5);
|
||||
g.drawRect(x+5, y+5, 5, 5);
|
||||
g.setPaint(color);
|
||||
g.fillRect(x+2, y, 4, 5);
|
||||
g.fillRect(x, y+5, 5, 5);
|
||||
g.fillRect(x+5, y+5, 5, 5);
|
||||
|
||||
}
|
||||
|
||||
public void draw(Graphics2D g, int startPosX, int startPosY) {
|
||||
int count = 0;
|
||||
int i = -1;
|
||||
int j = 0;
|
||||
while(true)
|
||||
{
|
||||
if(i==14) {
|
||||
i = 0;
|
||||
j=30;
|
||||
}
|
||||
else i++;
|
||||
if(PortholeCount == Porthole.Ten && count==10) break;
|
||||
else if(PortholeCount == Porthole.Twenty && count==20) break;
|
||||
else if(PortholeCount == Porthole.Thirty && count==30) break;
|
||||
drawEngine(g,startPosX+i*12 , startPosY + j);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ class EntityAirPlane
|
||||
|
||||
public float Step;
|
||||
|
||||
public void Init(int speed, float weight, Color bodyColor)
|
||||
public EntityAirPlane(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
Random rnd = new Random();
|
||||
|
||||
|
18
RadarAirplane/src/EntityRadarPlain.java
Normal file
18
RadarAirplane/src/EntityRadarPlain.java
Normal file
@ -0,0 +1,18 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class EntityRadarPlain extends EntityAirPlane
|
||||
{
|
||||
public Color DopColor;
|
||||
public boolean Radar;
|
||||
public boolean OilBox;
|
||||
|
||||
|
||||
public EntityRadarPlain(int speed, float weight, Color bodyColor, Color
|
||||
dopColor, boolean radar, boolean oil)
|
||||
{
|
||||
super(speed, weight, bodyColor);
|
||||
DopColor = dopColor;
|
||||
Radar = radar;
|
||||
OilBox = oil;
|
||||
}
|
||||
}
|
@ -40,9 +40,7 @@ public class FormAirPlane implements Form {
|
||||
Dimension canvSize = canv.getSize();
|
||||
Random rnd = new Random();
|
||||
|
||||
_airPlane = new DrawingEntityPlain();
|
||||
|
||||
_airPlane.Init(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
|
||||
_airPlane = new DrawingEntityPlain(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
|
||||
|
||||
_airPlane.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height);
|
||||
|
11
RadarAirplane/src/IDrawingObject.java
Normal file
11
RadarAirplane/src/IDrawingObject.java
Normal 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();
|
||||
}
|
6
RadarAirplane/src/IDrawingPorthole.java
Normal file
6
RadarAirplane/src/IDrawingPorthole.java
Normal file
@ -0,0 +1,6 @@
|
||||
import java.awt.*;
|
||||
|
||||
public interface IDrawingPorthole {
|
||||
void setCount(int count);
|
||||
void draw(Graphics2D g, int x, int y);
|
||||
}
|
59
RadarAirplane/src/MyMap.java
Normal file
59
RadarAirplane/src/MyMap.java
Normal file
@ -0,0 +1,59 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
public class MyMap extends AbstractMap
|
||||
{
|
||||
|
||||
private Color barrierColor = Color.BLACK;
|
||||
private Color roadColor = new Color(235,252,255);
|
||||
Random rnd = new Random();
|
||||
|
||||
@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 sizeHole = 70;
|
||||
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 < 3)
|
||||
{
|
||||
Random rand = new Random();
|
||||
int iWall = rnd.nextInt(0, 100);
|
||||
int jWall = rnd.nextInt(0, 100);
|
||||
if (iWall > _map.length - sizeHole)
|
||||
continue;
|
||||
for (int i = 0; i < _map.length; i++)
|
||||
{
|
||||
|
||||
if (i < iWall || i > iWall + sizeHole)
|
||||
_map[jWall][i] = _barrier;
|
||||
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -3,6 +3,6 @@ import java.awt.*;
|
||||
|
||||
public class Program {
|
||||
public static void main(String[] args) {
|
||||
new FormAirPlane().run();
|
||||
new formMap().run();
|
||||
}
|
||||
}
|
47
RadarAirplane/src/SimpleMap.java
Normal file
47
RadarAirplane/src/SimpleMap.java
Normal file
@ -0,0 +1,47 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class SimpleMap extends AbstractMap
|
||||
{
|
||||
private Color barrierColor = Color.BLACK;
|
||||
private Color roadColor = new Color(235,252,255);
|
||||
|
||||
@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 < 20)
|
||||
{
|
||||
int x = _random.nextInt(0, 100);
|
||||
int y = _random.nextInt(0, 100);
|
||||
if (_map[x][y] == _freeRoad)
|
||||
{
|
||||
_map[x][y] = _barrier;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
176
RadarAirplane/src/formMap.form
Normal file
176
RadarAirplane/src/formMap.form
Normal file
@ -0,0 +1,176 @@
|
||||
<?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="700" height="700"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<minimumSize width="555" height="555"/>
|
||||
<preferredSize width="1200" height="1100"/>
|
||||
</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
RadarAirplane/src/formMap.java
Normal file
119
RadarAirplane/src/formMap.java
Normal 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 DrawingEntityPlain _airplane;
|
||||
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(DrawingEntityPlain airplane)
|
||||
{
|
||||
Color bodyColor = _airplane.Airplane.BodyColor;
|
||||
String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")";
|
||||
|
||||
speedLabel.setText("Скорость: " + _airplane.Airplane.Speed + " ");
|
||||
weightLabel.setText("Вес: " + _airplane.Airplane.Weight + " ");
|
||||
colorLabel.setText("Цвет: " + colorString);
|
||||
|
||||
Dimension canvSize = canv.getSize();
|
||||
|
||||
_abstractMap.CreateMap(canvSize.width, canvSize.height, new DrawingObjectPlain(airplane));
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
_airplane = new DrawingEntityPlain(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
|
||||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
|
||||
|
||||
SetData(_airplane);
|
||||
canv.repaint();
|
||||
});
|
||||
|
||||
modifiedButton.addActionListener(e -> {
|
||||
Random rnd = new Random();
|
||||
|
||||
_airplane = new DrawingRadarPlain(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(_airplane);
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user