PIbd21.LyovushkinaA.A.Conta.../DrawingShip.java

150 lines
5.0 KiB
Java
Raw Normal View History

2023-10-07 21:42:37 +04:00
import java.awt.*;
public class DrawingShip {
2023-10-24 01:06:14 +04:00
public IMoveableObject GetMoveableObject()
{
return new DrawningObjectShip(this);
}
2023-10-07 21:42:37 +04:00
public EntityShip EntityShip;
2023-10-09 23:11:13 +04:00
public int _pictureWidth;
public int _pictureHeight;
public int _startPosX;
public int _startPosY;
public int _shipWidth = 110;
public int _shipHeight = 65;
public int GetPosX(){
return _startPosX;
}
public int GetPosY(){
return _startPosY;
}
public int GetWidth(){
return _shipWidth;
}
public int GetHeight(){
return _shipHeight;
}
2023-10-07 21:42:37 +04:00
public boolean CanMove(Direction direction)
{
if(EntityShip == null) return false;
switch(direction)
{
case Left:
return _startPosX - EntityShip.Step > 0;
case Right:
return _startPosX + _shipWidth + EntityShip.Step < _pictureWidth;
case Up:
return _startPosY - EntityShip.Step > 0;
case Down:
return _startPosY + _shipHeight+ EntityShip.Step < _pictureHeight;
default:
return false;
}
}
public DrawingShip(int speed, double weight, Color bodyColor, int width, int height)
{
if(width < _shipWidth || height < _shipHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityShip = new EntityShip(speed, weight, bodyColor);
2023-10-24 01:06:14 +04:00
}
public DrawingShip(EntityShip ship, int width, int height){
if(width < _shipWidth || height < _shipHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
EntityShip = ship;
2023-10-07 21:42:37 +04:00
}
protected DrawingShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth, int shipHeight)
{
if (width < _shipWidth || height < _shipHeight)
{
return;
}
_pictureWidth = width;
_pictureHeight = height;
_shipHeight = shipHeight;
_shipWidth = shipWidth;
2023-11-04 19:41:05 +04:00
EntityShip = new EntityShip(speed, weight, bodyColor);
2023-10-24 01:06:14 +04:00
2023-10-07 21:42:37 +04:00
}
2023-10-24 01:06:14 +04:00
2023-10-07 21:42:37 +04:00
public void SetPosition(int x, int y)
{
_startPosX = Math.min(x, _pictureWidth - _shipWidth);
_startPosY = Math.min(y, _pictureHeight - _shipHeight);
}
public void MoveTransport(Direction direction)
{
if (EntityShip == null)
{
return;
}
switch (direction)
{
case Left:
if (_startPosX - EntityShip.Step > 0)
{
_startPosX -= (int)EntityShip.Step;
}
break;
case Up:
if (_startPosY - EntityShip.Step > 0)
{
_startPosY -= (int)EntityShip.Step;
}
break;
case Right:
if (_startPosX + EntityShip.Step + _shipWidth < _pictureWidth)
{
_startPosX += (int)EntityShip.Step;
}
break;
case Down:
if (_startPosY + EntityShip.Step + _shipHeight< _pictureHeight)
{
_startPosY += (int)EntityShip.Step;
}
break;
}
}
public void DrawShip(Graphics2D g)
{
if (EntityShip == null)
{
return;
}
2023-11-03 23:38:13 +04:00
2023-10-07 21:42:37 +04:00
g.setPaint(EntityShip.BodyColor);
// заполнение борта
int x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20};
int y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65};
g.fillPolygon(x, y, 5);
//борт корабля контур
g.setPaint(Color.BLACK);
int _x[] = {_startPosX+ 20, _startPosX+40, _startPosX+110, _startPosX+130, _startPosX+ 20};
int _y[] = {_startPosY+65,_startPosY+85, _startPosY+85, _startPosY+65, _startPosY+65};
g.drawPolyline(_x, _y, 5);
//рисунок на борту
g.drawLine(_startPosX + 43, _startPosY + 80, _startPosX + 47, _startPosY + 80);
g.drawLine(_startPosX + 45, _startPosY + 70, _startPosX + 45, _startPosY + 80);
g.drawLine(_startPosX + 40, _startPosY + 75, _startPosX + 50, _startPosY + 75);
2023-10-09 23:11:13 +04:00
2023-10-24 01:06:14 +04:00
2023-10-07 21:42:37 +04:00
}
}