PIbd-21_Markov_D.P._Contain.../DrawingShip.java
2022-12-02 00:06:03 +04:00

136 lines
4.2 KiB
Java

import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class DrawingShip extends JPanel {
protected EntityShip Ship;
public IAdditionalDrawingObject Deck;
public void SetEnum() {
Random r = new Random();
int numbEnum = r.nextInt(1, 4);
Deck.SetAddEnum(numbEnum);
}
public void SetFormEnum()
{
Random r = new Random();
int numbEnum = r.nextInt(1, 4);
if (numbEnum == 1) {
Deck = new DrawingDeck();
}
if (numbEnum == 2) {
Deck = new DrawingTrapezoidDeck();
}
if (numbEnum == 3) {
Deck=new DrawingTriangleDeck();
}
}
public EntityShip GetShip() {
return Ship;
}
protected float _startPosX;
protected float _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private int _shipWidth = 100;
private int _shipHeight = 60;
public DrawingShip(int speed, float weight, Color bodycolor) {
Ship = new EntityShip(speed, weight, bodycolor);
SetFormEnum();
SetEnum();
}
public DrawingShip(int speed,float weight,Color bodyColor,int shipWidth,int shipHeight)
{
this(speed,weight,bodyColor);
_shipWidth = shipWidth;
_shipHeight = shipHeight;
}
public void SetPosition(int x, int y, int width, int height) {
if (x < 0 || y < 0)
{
return;
}
if (x + _shipWidth > width || y + _shipHeight > height)
{
return;
}
_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 + _shipWidth + Ship.GetStep() < _pictureWidth)
{
_startPosX += Ship.GetStep();
}
break;
case Left:
if (_startPosX - Ship.GetStep() > 0)
{
_startPosX -= Ship.GetStep();
}
break;
case Up:
if (_startPosY - Ship.GetStep() > 0)
{
_startPosY -= Ship.GetStep();
}
break;
case Down:
if (_startPosY + _shipHeight + Ship.GetStep() < _pictureHeight)
{
_startPosY += Ship.GetStep();
}
break;
}
}
public void DrawTransport(Graphics g) {
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
if (GetShip() == null) {
return;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Ship.GetBodyColor());
int xValues[]={(int) _startPosX,(int) _startPosX + 100,(int) _startPosX + 80,(int) _startPosX + 20};
int yValues[]={(int) _startPosY + 30,(int) _startPosY + 30,(int) _startPosY + 60,(int) _startPosY + 60};
g2d.fillPolygon(xValues,yValues,4);
g2d.setPaint(Color.BLACK);
g2d.drawPolygon(xValues,yValues,4);
Deck.DrawDeck(Ship.GetBodyColor(), g, _startPosX, _startPosY);
}
public void ChangeBorders(int width, int height) {
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight) {
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _shipWidth > _pictureWidth) {
_startPosX = _pictureWidth - _shipWidth;
}
if (_startPosY + _shipHeight > _pictureHeight) {
_startPosY = _pictureHeight - _shipHeight;
}
}
public float[] GetCurrentPosition()
{
float[] position = new float[4];
position[0] = _startPosX;
position[1] = _startPosY;
position[2] = _startPosX + _shipWidth;
position[3] = _startPosY + _shipHeight;
return position;
}
}