2022-11-03 22:02:58 +04:00
|
|
|
import java.awt.*;
|
2022-11-03 23:34:20 +04:00
|
|
|
import javax.swing.*;
|
2022-11-03 22:02:58 +04:00
|
|
|
import java.awt.Color;
|
|
|
|
import java.util.Random;
|
|
|
|
|
2022-11-03 23:34:20 +04:00
|
|
|
public class DrawingPlane extends JPanel
|
2022-11-03 22:02:58 +04:00
|
|
|
{
|
2022-11-18 16:31:49 +04:00
|
|
|
protected float _startPosX; //левая координата отрисовки
|
|
|
|
protected float _startPosY; //верхняя координата отрисовки
|
|
|
|
private Integer _pictureWidth = null; //ширина окна отрисовки
|
|
|
|
private Integer _pictureHeight = null; //высота окна отрисовки
|
|
|
|
protected int _airbusWidth = 50; //ширина отрисовки самолёта
|
|
|
|
protected int _airbusHeight = 16; //высота отрисовки самолёта
|
|
|
|
|
2022-11-03 23:34:20 +04:00
|
|
|
protected EntityPlane Plane; //класс-сущность
|
|
|
|
public IAdditionalDrawingObject _airplaneWindow; //для дополнительной отрисовки
|
2022-11-03 22:02:58 +04:00
|
|
|
|
2022-11-18 16:31:49 +04:00
|
|
|
public void SetColor(Color color)
|
2022-11-03 22:02:58 +04:00
|
|
|
{
|
2022-11-18 16:31:49 +04:00
|
|
|
Plane = new EntityPlane(Plane.GetSpeed(), Plane.GetWeight(), color);
|
2022-11-03 22:02:58 +04:00
|
|
|
}
|
|
|
|
|
2022-11-18 16:31:49 +04:00
|
|
|
public void SetTypeWindow(int countWindow, IAdditionalDrawingObject window)
|
2022-11-03 22:02:58 +04:00
|
|
|
{
|
2022-11-18 16:31:49 +04:00
|
|
|
_airplaneWindow = window;
|
|
|
|
_airplaneWindow.SetAddEnum(countWindow);
|
2022-11-03 22:02:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public EntityPlane GetPlane()
|
|
|
|
{
|
2022-11-03 23:34:20 +04:00
|
|
|
return Plane;
|
|
|
|
}
|
|
|
|
|
|
|
|
//конструктор
|
2022-11-18 16:31:49 +04:00
|
|
|
public DrawingPlane(int speed, float weight, Color corpusColor, int countWindow)
|
2022-11-03 23:34:20 +04:00
|
|
|
{
|
2022-11-18 16:31:49 +04:00
|
|
|
Random rnd = new Random();
|
|
|
|
int randomTypeWindow = rnd.nextInt(3);
|
2022-11-03 23:34:20 +04:00
|
|
|
Plane = new EntityPlane(speed, weight, corpusColor);
|
2022-11-18 16:31:49 +04:00
|
|
|
|
|
|
|
if(randomTypeWindow == 1)
|
|
|
|
{
|
|
|
|
_airplaneWindow = new DrawingAirplaneWindow();
|
|
|
|
}
|
|
|
|
else if(randomTypeWindow == 2)
|
|
|
|
{
|
|
|
|
_airplaneWindow = new DrawingRectAirplaneWindow();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_airplaneWindow = new DrawingTriangleAirplaneWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
_airplaneWindow.SetAddEnum(countWindow);
|
2022-11-03 23:34:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//конструктор
|
2022-11-18 16:31:49 +04:00
|
|
|
public DrawingPlane(int speed, float weight, Color corpusColor, int countWindow, int planeWidth, int planeHeight)
|
2022-11-03 23:34:20 +04:00
|
|
|
{
|
2022-11-18 16:31:49 +04:00
|
|
|
this(speed, weight, corpusColor, countWindow);
|
2022-11-03 23:34:20 +04:00
|
|
|
_airbusWidth = planeWidth;
|
|
|
|
_airbusHeight = planeHeight;
|
2022-11-03 22:02:58 +04:00
|
|
|
}
|
|
|
|
|
2022-11-06 13:43:57 +04:00
|
|
|
//второй конструктор
|
|
|
|
public DrawingPlane(EntityPlane plane, IAdditionalDrawingObject countWindow)
|
|
|
|
{
|
|
|
|
Plane = plane;
|
|
|
|
_airplaneWindow = countWindow;
|
|
|
|
}
|
|
|
|
|
2022-11-03 22:02:58 +04:00
|
|
|
//установка координат позиции самолёта
|
|
|
|
public void SetPosition(int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
if (x < 0 || y < 0 || width <= x + _airbusWidth || height <= y + _airbusHeight)
|
|
|
|
{
|
|
|
|
_pictureHeight = null;
|
|
|
|
_pictureWidth = null;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_startPosX = x;
|
|
|
|
_startPosY = y;
|
|
|
|
_pictureWidth = width;
|
|
|
|
_pictureHeight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
//изменение направления движения
|
|
|
|
public void MoveTransport(Direction direction)
|
|
|
|
{
|
|
|
|
if (GetPlane() == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (direction)
|
|
|
|
{
|
|
|
|
case Right: //вправо
|
2022-11-03 23:34:20 +04:00
|
|
|
if (_startPosX + _airbusWidth + Plane.GetStep() < _pictureWidth)
|
2022-11-03 22:02:58 +04:00
|
|
|
{
|
2022-11-03 23:34:20 +04:00
|
|
|
_startPosX += Plane.GetStep();
|
2022-11-03 22:02:58 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Left: //влево
|
2022-11-03 23:34:20 +04:00
|
|
|
if (_startPosX - _airbusWidth - Plane.GetStep() > 0)
|
2022-11-03 22:02:58 +04:00
|
|
|
{
|
2022-11-03 23:34:20 +04:00
|
|
|
_startPosX -= Plane.GetStep();
|
2022-11-03 22:02:58 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Up: //вверх
|
2022-11-03 23:34:20 +04:00
|
|
|
if (_startPosY - _airbusHeight - Plane.GetStep() > 0)
|
2022-11-03 22:02:58 +04:00
|
|
|
{
|
2022-11-03 23:34:20 +04:00
|
|
|
_startPosY -= Plane.GetStep();
|
2022-11-03 22:02:58 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Down: //вниз
|
2022-11-03 23:34:20 +04:00
|
|
|
if (_startPosY + _airbusHeight + Plane.GetStep() < _pictureHeight)
|
2022-11-03 22:02:58 +04:00
|
|
|
{
|
2022-11-03 23:34:20 +04:00
|
|
|
_startPosY += Plane.GetStep();
|
2022-11-03 22:02:58 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DrawTransport(Graphics g)
|
|
|
|
{
|
|
|
|
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(GetPlane() == null){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Graphics2D g2d = (Graphics2D)g;
|
|
|
|
|
2022-11-06 12:34:47 +04:00
|
|
|
//заливка корпуса
|
2022-11-05 01:24:27 +04:00
|
|
|
g2d.setColor(Plane.GetColor());
|
|
|
|
g2d.fillRect((int)_startPosX, (int)_startPosY + 10, 40, 10);
|
2022-11-03 22:02:58 +04:00
|
|
|
|
|
|
|
//крыло
|
2022-11-06 12:34:47 +04:00
|
|
|
g2d.setColor(Color.BLACK);
|
2022-11-03 22:02:58 +04:00
|
|
|
g2d.fillRect((int)_startPosX + 10, (int)_startPosY + 15, 20, 2);
|
|
|
|
g2d.fillRect((int)_startPosX + 12, (int)_startPosY + 17, 16, 2);
|
|
|
|
|
|
|
|
//задние шасси
|
|
|
|
g2d.fillRect((int)_startPosX + 10, (int)_startPosY + 21, 2, 2);
|
|
|
|
g2d.fillRect((int)_startPosX + 12, (int)_startPosY + 23, 4, 4);
|
|
|
|
g2d.fillRect((int)_startPosX + 8, (int)_startPosY + 23, 2, 4);
|
|
|
|
|
|
|
|
//переднее шасси
|
|
|
|
g2d.fillRect((int)_startPosX + 35, (int)_startPosY + 21, 2, 2);
|
|
|
|
g2d.fillRect((int)_startPosX + 35, (int)_startPosY + 23, 4, 4);
|
|
|
|
|
2022-11-06 12:34:47 +04:00
|
|
|
//заливка хвостового крыла
|
|
|
|
g2d.setColor(Plane.GetColor());
|
|
|
|
int[] xCoordFirst = {(int)_startPosX, (int)_startPosX, (int)_startPosX + 10};
|
|
|
|
int[] yCoordFirst = {(int)_startPosY + 10, (int)_startPosY, (int)_startPosY + 10};
|
|
|
|
g2d.fillPolygon(xCoordFirst, yCoordFirst, xCoordFirst.length);
|
|
|
|
|
|
|
|
//заливка носа самолёта
|
|
|
|
int[] xCoordSecond = {(int)_startPosX + 40, (int)_startPosX + 47, (int)_startPosX + 40};
|
|
|
|
int[] yCoordSecond = {(int)_startPosY + 15, (int)_startPosY + 15, (int)_startPosY + 10};
|
|
|
|
g2d.fillPolygon(xCoordSecond, yCoordSecond, xCoordSecond.length);
|
|
|
|
|
|
|
|
int[] xCoordThird = {(int)_startPosX + 40, (int)_startPosX + 50, (int)_startPosX + 40};
|
|
|
|
int[] yCoordThird = {(int)_startPosY + 15, (int)_startPosY + 15, (int)_startPosY + 20};
|
|
|
|
g2d.fillPolygon(xCoordThird, yCoordThird, xCoordThird.length);
|
|
|
|
|
|
|
|
//контур хвостового крыла
|
|
|
|
g2d.setColor(Color.BLACK);
|
2022-11-03 22:02:58 +04:00
|
|
|
g2d.drawLine((int)_startPosX, (int)_startPosY + 10, (int)_startPosX, (int)_startPosY);
|
|
|
|
g2d.drawLine((int)_startPosX, (int)_startPosY, (int)_startPosX + 10, (int)_startPosY + 10);
|
|
|
|
|
2022-11-06 12:34:47 +04:00
|
|
|
//отрисовка контура самолёта
|
|
|
|
g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 15, (int)_startPosX + 50, (int)_startPosY + 15);
|
2022-11-03 22:02:58 +04:00
|
|
|
g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 10, (int)_startPosX + 47, (int)_startPosY + 15);
|
|
|
|
g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 20, (int)_startPosX + 50, (int)_startPosY + 15);
|
|
|
|
|
2022-11-06 12:34:47 +04:00
|
|
|
//контур корпуса
|
|
|
|
g2d.drawRect((int)_startPosX, (int)_startPosY + 10, 40, 10);
|
|
|
|
|
|
|
|
//заднее поперечное крыло
|
|
|
|
g2d.fillOval((int)_startPosX - 3, (int)_startPosY + 7, 10, 5);
|
|
|
|
|
2022-11-03 22:02:58 +04:00
|
|
|
//отрисовка иллюминаторов
|
2022-11-03 23:34:20 +04:00
|
|
|
_airplaneWindow.DrawAirplaneWindow(Plane.GetColor(), g, _startPosX, _startPosY);
|
2022-11-03 22:02:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//смена границ формы отрисовки
|
|
|
|
public void ChangeBorders(int width, int height)
|
|
|
|
{
|
|
|
|
_pictureWidth = width;
|
|
|
|
_pictureHeight = height;
|
|
|
|
|
|
|
|
if (_pictureWidth <= _airbusWidth || _pictureHeight <= _airbusHeight)
|
|
|
|
{
|
|
|
|
_pictureWidth = null;
|
|
|
|
_pictureHeight = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_startPosX + _airbusWidth > _pictureWidth)
|
|
|
|
{
|
|
|
|
_startPosX = _pictureWidth - _airbusWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_startPosY + _airbusHeight > _pictureHeight)
|
|
|
|
{
|
|
|
|
_startPosY = _pictureHeight - _airbusHeight;
|
|
|
|
}
|
|
|
|
}
|
2022-11-03 23:34:20 +04:00
|
|
|
|
|
|
|
public float[] GetCurrentPosition()
|
|
|
|
{
|
2022-11-05 01:24:27 +04:00
|
|
|
return new float[]{_startPosX, _startPosY, _startPosX + _airbusWidth, _startPosY + _airbusHeight};
|
2022-11-03 23:34:20 +04:00
|
|
|
}
|
|
|
|
}
|