PIbd-21_Eliseev_E.E._Airbus.../Project/src/DrawingPlane.java

255 lines
8.6 KiB
Java

import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import java.util.Iterator;
import java.util.Random;
public class DrawingPlane extends JPanel implements Iterable<Object>, Iterator<Object>
{
protected float _startPosX; //левая координата отрисовки
protected float _startPosY; //верхняя координата отрисовки
private Integer _pictureWidth = null; //ширина окна отрисовки
private Integer _pictureHeight = null; //высота окна отрисовки
protected int _airbusWidth = 50; //ширина отрисовки самолёта
protected int _airbusHeight = 16; //высота отрисовки самолёта
protected EntityPlane Plane; //класс-сущность
public IAdditionalDrawingObject _airplaneWindow; //для дополнительной отрисовки
private int currentIndex = 0;
protected Object[] fields;
public int _countWindows; //для сравнения при вставке
public void SetColor(Color color)
{
Plane = new EntityPlane(Plane.GetSpeed(), Plane.GetWeight(), color);
}
public void SetTypeWindow(int countWindow, IAdditionalDrawingObject window)
{
_airplaneWindow = window;
_airplaneWindow.SetAddEnum(countWindow);
_countWindows = countWindow;
}
public EntityPlane GetPlane()
{
return Plane;
}
//конструктор
public DrawingPlane(int speed, float weight, Color corpusColor, int countWindow)
{
Random rnd = new Random();
int randomTypeWindow = rnd.nextInt(3);
Plane = new EntityPlane(speed, weight, corpusColor);
if(randomTypeWindow == 1)
{
_airplaneWindow = new DrawingAirplaneWindow();
}
else if(randomTypeWindow == 2)
{
_airplaneWindow = new DrawingRectAirplaneWindow();
}
else
{
_airplaneWindow = new DrawingTriangleAirplaneWindow();
}
_airplaneWindow.SetAddEnum(countWindow);
fields = new Object[]{speed, weight, corpusColor, _airplaneWindow.getClass().getSimpleName(), countWindow};
}
//кол-во иллюминаторов
public int GetWindows(){
return _airplaneWindow.GetAddEnum();
}
//конструктор
public DrawingPlane(int speed, float weight, Color corpusColor, int countWindow, int planeWidth, int planeHeight)
{
this(speed, weight, corpusColor, countWindow);
_airbusWidth = planeWidth;
_airbusHeight = planeHeight;
}
//второй конструктор
public DrawingPlane(EntityPlane plane, IAdditionalDrawingObject countWindow)
{
Plane = plane;
_airplaneWindow = countWindow;
fields = new Object[]{plane.GetSpeed(), plane.GetWeight(),
plane.GetColor(), _airplaneWindow.getClass().getSimpleName(), _countWindows};
}
//установка координат позиции самолёта
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: //вправо
if (_startPosX + _airbusWidth + Plane.GetStep() < _pictureWidth)
{
_startPosX += Plane.GetStep();
}
break;
case Left: //влево
if (_startPosX - _airbusWidth - Plane.GetStep() > 0)
{
_startPosX -= Plane.GetStep();
}
break;
case Up: //вверх
if (_startPosY - _airbusHeight - Plane.GetStep() > 0)
{
_startPosY -= Plane.GetStep();
}
break;
case Down: //вниз
if (_startPosY + _airbusHeight + Plane.GetStep() < _pictureHeight)
{
_startPosY += Plane.GetStep();
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null)
{
return;
}
if(GetPlane() == null){
return;
}
Graphics2D g2d = (Graphics2D)g;
//заливка корпуса
g2d.setColor(Plane.GetColor());
g2d.fillRect((int)_startPosX, (int)_startPosY + 10, 40, 10);
//крыло
g2d.setColor(Color.BLACK);
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);
//заливка хвостового крыла
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);
g2d.drawLine((int)_startPosX, (int)_startPosY + 10, (int)_startPosX, (int)_startPosY);
g2d.drawLine((int)_startPosX, (int)_startPosY, (int)_startPosX + 10, (int)_startPosY + 10);
//отрисовка контура самолёта
g2d.drawLine((int)_startPosX + 40, (int)_startPosY + 15, (int)_startPosX + 50, (int)_startPosY + 15);
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);
//контур корпуса
g2d.drawRect((int)_startPosX, (int)_startPosY + 10, 40, 10);
//заднее поперечное крыло
g2d.fillOval((int)_startPosX - 3, (int)_startPosY + 7, 10, 5);
//отрисовка иллюминаторов
_airplaneWindow.DrawAirplaneWindow(Plane.GetColor(), g, _startPosX, _startPosY);
}
//смена границ формы отрисовки
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;
}
}
public float[] GetCurrentPosition()
{
return new float[]{_startPosX, _startPosY, _startPosX + _airbusWidth, _startPosY + _airbusHeight};
}
@Override
public Iterator iterator() {
return this;
}
@Override
public boolean hasNext() {
return currentIndex < fields.length && fields[currentIndex] != null;
}
@Override
public Object next() {
return fields[currentIndex++];
}
}