PIbd-22_Aleikin_A.M._AirBom.../DrawningBomber.java

185 lines
5.9 KiB
Java

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class DrawningBomber extends JPanel
{
public EntityAirBomber AirBomber;
public EntityAirBomber getAirBomber() { return AirBomber; }
public float _startPosX;
public float _startPosY;
private int _pictureWidth = 0;
private int _pictureHeight = 0;
private int _airBomberWidth = 100;
private int _airBomberHeight = 100;
private DrawEngines drawEngines = new DrawEngines();
private IDrawningEngines ide;
public DrawningBomber(int speed, float weight, Color bodyColor)
{
AirBomber = new EntityAirBomber(speed, weight, bodyColor);
Random random = new Random();
switch (random.nextInt(3))
{
case 0:
ide = new DrawEngines();
break;
case 1:
ide = new DrawRocketEngine();
break;
case 2:
ide = new DrawWings();
break;
}
ide.SetNumEngines(random.nextInt(1, 4));
}
protected DrawningBomber(int speed, float weight, Color bodyColor, int airBomberWidth, int airBomberHeight)
{
this(speed, weight, bodyColor);
_airBomberWidth = airBomberWidth;
_airBomberHeight = airBomberHeight;
}
public void SetPosition(int x, int y, int width, int height)
{
if (x + _airBomberWidth > width || x < 0) return;
else _startPosX = x;
if (y + _airBomberHeight> height || y < 0) return;
else _startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void MoveTransport(Direction direction)
{
if (!(_pictureWidth > 0 || _pictureHeight > 0))
{
return;
}
switch (direction)
{
case Right:
if (_startPosX + _airBomberWidth + AirBomber.GetStep() < _pictureWidth)
{
_startPosX += AirBomber.GetStep();
}
break;
case Left:
if (_startPosX - AirBomber.GetStep() > 0)
{
_startPosX -= AirBomber.GetStep();
}
break;
case Up:
if (_startPosY - AirBomber.GetStep() > 0)
{
_startPosY -= AirBomber.GetStep();
}
break;
case Down:
if (_startPosY + _airBomberHeight + AirBomber.GetStep() < _pictureHeight)
{
_startPosY += AirBomber.GetStep();
}
break;
}
}
public void DrawTransport(Graphics g)
{
if (getAirBomber() == null || _startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0)
{
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(AirBomber.GetColor());
int[] xPos = {
(int)(_startPosX + _airBomberWidth),
(int)(_startPosX + _airBomberWidth),
(int)(_startPosX + _airBomberWidth - 10),
(int)(_startPosX + _airBomberWidth - 10)};
int[] yPos = {
(int)(_startPosY + _airBomberHeight / 2 - 20),
(int)(_startPosY + _airBomberHeight / 2 + 20),
(int)(_startPosY + _airBomberHeight / 2 + 10),
(int)(_startPosY + _airBomberHeight / 2 - 10)};
g2d.fillPolygon(xPos, yPos, xPos.length);
xPos = new int[] {
(int)(_startPosX + _airBomberWidth / 2),
(int)(_startPosX + _airBomberWidth / 2 + 10),
(int)(_startPosX + _airBomberWidth / 2 + 20),
(int)(_startPosX + _airBomberWidth / 2 + 10),
(int)(_startPosX + _airBomberWidth / 2)};
yPos = new int[] {
(int)(_startPosY),
(int)(_startPosY),
(int)(_startPosY + _airBomberHeight / 2),
(int)(_startPosY + _airBomberHeight),
(int)(_startPosY + _airBomberHeight)};
g2d.fillPolygon(xPos, yPos, xPos.length);
xPos = new int[] {
(int)(_startPosX + 20),
(int)(_startPosX + _airBomberWidth),
(int)(_startPosX + _airBomberWidth),
(int)(_startPosX + 20)};
yPos = new int[] {
(int)(_startPosY + _airBomberHeight / 2 - 5),
(int)(_startPosY + _airBomberHeight / 2 - 5),
(int)(_startPosY + _airBomberHeight / 2 + 5),
(int)(_startPosY + _airBomberHeight / 2 + 5)};
g2d.fillPolygon(xPos, yPos, xPos.length);
xPos = new int[] {
(int)(_startPosX),
(int)(_startPosX + 20),
(int)(_startPosX + 20)};
yPos = new int[] {
(int)(_startPosY + _airBomberHeight / 2),
(int)(_startPosY + _airBomberHeight / 2 - 5),
(int)(_startPosY + _airBomberHeight / 2 + 5)};
g2d.fillPolygon(xPos, yPos, xPos.length);
ide.DrawningEngines(_startPosX, _startPosY, g2d, _airBomberWidth, _airBomberHeight);
}
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _airBomberWidth || _pictureHeight <= _airBomberHeight)
{
_pictureWidth = 0;
_pictureHeight = 0;
return;
}
if (_startPosX + _airBomberWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _airBomberWidth;
}
if (_startPosY + _airBomberHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _airBomberHeight;
}
}
public float[] GetCurrentPosition()
{
return new float[]{_startPosX, _startPosX + _airBomberWidth, _startPosY, _startPosY + _airBomberHeight};
}
}