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

165 lines
5.1 KiB
Java

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;
public void Init(int speed, float weight, Color bodyColor)
{
AirBomber = new EntityAirBomber();
AirBomber.Init(speed, weight, bodyColor);
}
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)};
g.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)};
g.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)};
g.fillPolygon(xPos, yPos, xPos.length);
}
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;
}
}
}