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

171 lines
5.4 KiB
Java
Raw Permalink Normal View History

2022-11-28 00:15:39 +04:00
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class DrawningBomber extends JPanel
{
private EntityAirBomber AirBomber;
public EntityAirBomber getAirBomber() { return AirBomber; }
private float _startPosX;
private float _startPosY;
private int _pictureWidth = 0;
private int _pictureHeight = 0;
private final int _airBomberWidth = 100;
private final int _airBomberHeight = 100;
2022-11-29 20:50:45 +04:00
private int NumEngines = 1;
private DrawEngines drawEngines = new DrawEngines();
2022-11-28 00:15:39 +04:00
public void Init(int speed, float weight, Color bodyColor)
{
AirBomber = new EntityAirBomber();
AirBomber.Init(speed, weight, bodyColor);
2022-11-29 20:50:45 +04:00
Random random = new Random();
drawEngines.SetNumEngines(random.nextInt(1, 4));
2022-11-28 00:15:39 +04:00
}
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()
{
if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == 0 || _pictureWidth == 0)
{
return;
}
repaint();
}
@Override
public void paintComponent(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)};
2022-11-29 20:50:45 +04:00
g2d.fillPolygon(xPos, yPos, xPos.length);
2022-11-28 00:15:39 +04:00
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)};
2022-11-29 20:50:45 +04:00
g2d.fillPolygon(xPos, yPos, xPos.length);
2022-11-28 00:15:39 +04:00
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)};
2022-11-29 20:50:45 +04:00
g2d.fillPolygon(xPos, yPos, xPos.length);
drawEngines.DrawningEngines(_startPosX, _startPosY, g2d, _airBomberWidth, _airBomberHeight);
2022-11-28 00:15:39 +04:00
}
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;
}
}
}