Files
PIbd-21_Sorokin_P.V._Liner.…/src/DrawingShip.java
2022-11-22 14:24:40 +04:00

138 lines
4.4 KiB
Java

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.Set;
public class DrawingShip extends JComponent{
protected EntityShip Ship;
protected IAdditionalDrawingObject Deck;
public void SetColor(Color color)
{
Ship=new EntityShip(Ship.GetSpeed(),Ship.GetWeight(),color);
}
public IAdditionalDrawingObject GetDeck()
{
return Deck;
}
public void SetFormEnum(int decksnum,IAdditionalDrawingObject deck)
{
Deck=deck;
Deck.SetAddEnum(decksnum);
}
public void SetDeck(IAdditionalDrawingObject deck)
{
Deck=deck;
}
public EntityShip GetShip() {
return Ship;
}
protected float _startPosX;
protected float _startPosY;
private Integer _pictureWidth = null;
private Integer _pictureHeight = null;
private int _ShipWidth = 120;
private int _ShipHeight = 40;
public DrawingShip(int speed, float weight, Color bodycolor,int numdeck) {
Ship = new EntityShip(speed, weight, bodycolor);
Deck = new DrawingTriangleDeck();
Random r = new Random();
int numbEnum = r.nextInt(1, 4);
if (numbEnum == 1) {
Deck=new DrawingDeck();
}
if (numbEnum == 2) {
Deck = new DrawingOvalDeck();
}
if (numbEnum == 3) {
Deck = new DrawingTriangleDeck();
}
Deck.SetAddEnum(numdeck);
}
public DrawingShip(int speed,float weight,Color bodyColor,int numdeck,int shipWidth,int shipHeight)
{
this(speed,weight,bodyColor,numdeck);
_ShipWidth = shipWidth;
_ShipHeight = shipHeight;
}
protected DrawingShip(EntityShip ship,IAdditionalDrawingObject deck)
{
Ship=ship;
Deck=deck;
}
public void SetPosition(int x, int y, int width, int height) {
if (width <= _ShipWidth + x || height <= _ShipHeight + y || x < 0 || y < 0) {
_pictureWidth = null;
_pictureHeight = null;
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
public void MoveTransport(Direction direction) {
if (GetShip() == null) {
return;
}
switch (direction.GetDirect()) {
case 4:
if (_startPosX + _ShipWidth + Ship.Step() < _pictureWidth) {
_startPosX += Ship.Step();
}
break;
case 3:
if (_startPosX - Ship.Step() > 0) {
_startPosX -= Ship.Step();
}
break;
case 1:
if (_startPosY - Ship.Step() > 0) {
_startPosY -= Ship.Step();
}
break;
case 2:
if (_startPosY + _ShipHeight + Ship.Step() < _pictureHeight) {
_startPosY += Ship.Step();
}
break;
}
}
public void DrawTransport(Graphics gr) {
Graphics2D g2d = (Graphics2D) gr;
if (GetShip() == null) {
return;
}
if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) {
return;
}
g2d.setPaint(Ship.GetBodyColor());
int xValues[]={(int) _startPosX,(int) _startPosX + 60 * 2,(int) _startPosX + 100,(int) _startPosX + 20};
int yValues[]={(int) _startPosY + 20,(int) _startPosY + 20,(int) _startPosY + 20 * 2,(int) _startPosY + 40};
g2d.fillPolygon(xValues,yValues,4);
g2d.setPaint(Color.BLACK);
g2d.drawPolygon(xValues,yValues,4);
Deck.DrawDeck(Ship.GetBodyColor(), gr, _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()
{
return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _ShipWidth, /*DOWN*/ _startPosY + _ShipHeight, /*LEFT*/ _startPosX};
}
}