171 lines
5.2 KiB
Java
171 lines
5.2 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.util.Random;
|
|
|
|
public class DrawningPlane extends JPanel {
|
|
|
|
EntityPlane Plane;
|
|
|
|
public EntityPlane GetPlane(){
|
|
return Plane;
|
|
}
|
|
|
|
protected int _startPosX;
|
|
protected int _startPosY;
|
|
public IDrawningIlluminator IlluminatorDraw;
|
|
public Integer _pictureWidth = null;
|
|
public Integer _pictureHeight = null;
|
|
private int _PlaneWidth = 130;
|
|
private int _PlaneHeight = 70;
|
|
|
|
public void SetIlluminator() {
|
|
Random r = new Random();
|
|
int numIllum = r.nextInt(1,4);
|
|
numIllum = numIllum * 10;
|
|
IlluminatorDraw.SetIlluminatorCount(numIllum);
|
|
}
|
|
|
|
public DrawningPlane(int speed, float weight, Color bodyColor)
|
|
{
|
|
Plane = new EntityPlane(speed, weight, bodyColor);
|
|
Random random = new Random();
|
|
switch (random.nextInt(3)){
|
|
case 0:
|
|
IlluminatorDraw = new DrawningIlluminator();
|
|
break;
|
|
case 1:
|
|
IlluminatorDraw = new DrawningSqareIlluminator();
|
|
break;
|
|
case 2:
|
|
IlluminatorDraw = new DrawningTriangleIlluminator();
|
|
break;
|
|
}
|
|
SetIlluminator();
|
|
}
|
|
|
|
protected DrawningPlane(EntityPlane plane, IDrawningIlluminator illum){
|
|
Plane = plane;
|
|
IlluminatorDraw = illum;
|
|
}
|
|
|
|
public void SetPosition(int x, int y, int width, int height)
|
|
{
|
|
if (x >= 0 && x + _PlaneWidth <= width && y >= 0 && y + _PlaneHeight <= height) {
|
|
_startPosX = x;
|
|
_startPosY = y;
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
}
|
|
}
|
|
|
|
public void MoveTransport(Direction direction)
|
|
{
|
|
if (_pictureWidth == null || _pictureHeight == null)
|
|
{
|
|
return;
|
|
}
|
|
switch (direction)
|
|
{
|
|
case Right:
|
|
if (_startPosX + _PlaneWidth + Plane.Step < _pictureWidth)
|
|
{
|
|
_startPosX += Plane.Step;
|
|
}
|
|
break;
|
|
case Left:
|
|
if (_startPosX - Plane.Step > 0)
|
|
{
|
|
_startPosX -= Plane.Step;
|
|
}
|
|
break;
|
|
case Up:
|
|
if (_startPosY - Plane.Step > 0)
|
|
{
|
|
_startPosY -= Plane.Step;
|
|
}
|
|
break;
|
|
case Down:
|
|
if (_startPosY + _PlaneHeight + Plane.Step < _pictureHeight)
|
|
{
|
|
_startPosY += Plane.Step;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
protected DrawningPlane(int speed, float weight, Color bodyColor, int planeWidth, int planeHeight)
|
|
{
|
|
this(speed, weight, bodyColor);
|
|
_PlaneWidth = planeWidth;
|
|
_PlaneHeight = planeHeight;
|
|
}
|
|
|
|
public void DrawTransport(Graphics g) {
|
|
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
|
|
return;
|
|
}
|
|
if (GetPlane() == null) {
|
|
return;
|
|
}
|
|
super.paintComponent(g);
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
g2d.setColor(Color.BLACK);
|
|
g2d.drawOval(_startPosX, _startPosY + 30, 20, 20);
|
|
g2d.drawRect(_startPosX + 10, _startPosY + 30, 100, 20);
|
|
|
|
g2d.drawLine(_startPosX + 110, _startPosY + 30, _startPosX + 130, _startPosY+40);
|
|
g2d.drawLine(_startPosX + 110, _startPosY+50, _startPosX + 130, _startPosY+40);
|
|
|
|
g2d.drawLine(_startPosX, _startPosY, _startPosX, _startPosY+40);
|
|
g2d.drawLine(_startPosX, _startPosY, _startPosX + 30, _startPosY+30);
|
|
|
|
g2d.drawLine(_startPosX + 40, _startPosY + 50, _startPosX + 40, _startPosY+55);
|
|
g2d.drawLine(_startPosX + 100, _startPosY + 50, _startPosX + 100, _startPosY+55);
|
|
|
|
g2d.drawOval(_startPosX + 95, _startPosY + 55, 10, 10);
|
|
g2d.drawOval(_startPosX + 29, _startPosY + 55, 10, 10);
|
|
g2d.drawOval(_startPosX + 41, _startPosY + 55, 10, 10);
|
|
|
|
g2d.setPaint(Plane.GetBodyColor());
|
|
g2d.fillOval(_startPosX, _startPosY + 31, 20, 19);
|
|
g2d.fillRect(_startPosX + 10, _startPosY + 31, 100, 19);
|
|
|
|
g2d.setPaint(Color.BLACK);
|
|
g2d.fillOval(_startPosX + 40, _startPosY + 40, 60, 5);
|
|
g2d.fillOval(_startPosX - 5, _startPosY + 25, 30, 10);
|
|
|
|
IlluminatorDraw.DrawIlluminator(g, _startPosX, _startPosY);
|
|
repaint();
|
|
}
|
|
|
|
public void ChangeBorders(int width, int height)
|
|
{
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
if (_pictureWidth <= _PlaneWidth || _pictureHeight <= _PlaneHeight)
|
|
{
|
|
_pictureWidth = null;
|
|
_pictureHeight = null;
|
|
return;
|
|
}
|
|
if (_startPosX + _PlaneWidth > _pictureWidth)
|
|
{
|
|
_startPosX = _pictureWidth - _PlaneWidth;
|
|
}
|
|
if (_startPosY + _PlaneHeight > _pictureHeight)
|
|
{
|
|
_startPosY = _pictureHeight - _PlaneHeight;
|
|
}
|
|
}
|
|
|
|
public float[] GetCurrentPosition() {
|
|
float[] dim = new float[4];
|
|
dim[0] = _startPosX;
|
|
dim[1] =_startPosY;
|
|
dim[2] = _startPosX + _PlaneWidth;
|
|
dim[3] = _startPosY + _PlaneHeight;
|
|
return dim;
|
|
}
|
|
}
|