PIbd-22_Bondarenko_M.S._War.../DrawingShip.java
Макс Бондаренко c36ffe6688 Suck_Pop
2022-11-16 18:24:14 +04:00

120 lines
5.2 KiB
Java

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class DrawingShip extends JPanel {
private EntityWarmlyShip warmlyShip; //Класс-сущность
public EntityWarmlyShip GetWarmlyShip(){return warmlyShip;}
public float _startPosX; //Координаты отрисовки по оси x
public float _startPosY; //Координаты отрисовки по оси y
private Integer _pictureWidth = null; //Ширина окна
private Integer _pictureHeight = null; //Высота окна
private final int _warmlyShipWidth = 125; //Ширина отрисовки корабля
private final int _warmlyShipHeight = 50; //Высота отрисовки корабля
private int deckCount = 1;
private DrawDeck dd = new DrawDeck();
//Инициализация
public void Init(int speed, float weight, Color bodyColor)
{
warmlyShip = new EntityWarmlyShip();
warmlyShip.Init(speed, weight, bodyColor);
Random random = new Random();
dd.SetDeckCount(random.nextInt(1, 4));
}
//Начальные коордитанты
public void SetPosition(int x, int y, int width, int height)
{
if (width < _warmlyShipWidth || height < _warmlyShipHeight) return;
Random random = new Random();
_startPosX = x < 0 || x + _warmlyShipWidth > width ? random.nextFloat(0, width - _warmlyShipWidth) : x;
_startPosY = y < 0 || y + _warmlyShipHeight > height ? random.nextFloat(0, height - _warmlyShipHeight) : y;
_pictureWidth = width;
_pictureHeight = height;
}
//Движение транспорта по координатам
public void MoveTransport(Direction direction)
{
if (_pictureWidth == null || _pictureHeight == null) return;
switch (direction)
{
case Left: //Влево
if (_startPosX - warmlyShip.GetStep() > 0) _startPosX -= warmlyShip.GetStep();
break;
case Up: //Вверх
if (_startPosY - warmlyShip.GetStep() > 0) _startPosY -= warmlyShip.GetStep();
break;
case Right: //Вправо
if (_startPosX + _warmlyShipWidth + warmlyShip.GetStep() < _pictureWidth) _startPosX += warmlyShip.GetStep();
break;
case Down: //Вниз
if (_startPosY + _warmlyShipHeight + warmlyShip.GetStep() < _pictureHeight) _startPosY += warmlyShip.GetStep();
break;
}
}
//Отрисовка транспорта
public void DrawTransport()
{
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null)
{
return;
}
repaint();
}
@Override
public void paintComponent(Graphics g){
if (GetWarmlyShip() == null) return;
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null)
{
return;
}
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(warmlyShip.GetBodyColor());
int[] xPol = new int[] {(int)(_startPosX), (int)(_startPosX + _warmlyShipWidth), (int)(_startPosX + _warmlyShipWidth - 25), (int)(_startPosX + 25)};
int[] yPol = new int[] {(int)(_startPosY + 20), (int)(_startPosY + 20), (int)(_startPosY + _warmlyShipHeight), (int)(_startPosY + _warmlyShipHeight)};
g2d.fillPolygon(new Polygon(xPol, yPol, xPol.length));
g2d.fillRect((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY, _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.CYAN);
g2d.fillOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20);
g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20);
g2d.fillOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20);
g2d.setColor(Color.BLACK);
g2d.drawPolygon(new Polygon(xPol, yPol, xPol.length));
g2d.drawRect((int)(_startPosX + _warmlyShipWidth / 5), (int)(_startPosY), _warmlyShipWidth * 3 / 5, 20);
g2d.setColor(Color.BLUE);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth / 5), (int)_startPosY + 25, 20, 20);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 3 / 5 + 5), (int)_startPosY + 25, 20, 20);
g2d.drawOval((int)(_startPosX + _warmlyShipWidth * 2 / 5 + 2.5f), (int)_startPosY + 25, 20, 20);
dd.DrawningDeck(_startPosX, _startPosY, _warmlyShipWidth, g2d, warmlyShip.GetBodyColor());
}
//Изменение границ отрисовки
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _warmlyShipWidth || _pictureHeight <= _warmlyShipHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _warmlyShipWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _warmlyShipWidth;
}
if (_startPosY + _warmlyShipHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _warmlyShipHeight;
}
}
}