import javax.swing.*; import java.awt.*; import java.util.Random; public class DrawingShip extends JPanel { private EntityShip Ship; public DrawingDeck Deck; private float _startPosX; private float _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; protected final int _shipWidth = 120; protected final int _shipHeight = 40; public void SetEnum() { Random r = new Random(); int numbDecks = r.nextInt(1, 4); Deck.SetAddEnum(numbDecks); } public EntityShip GetShip() { return Ship; } public void Init(int speed, float weight, Color bodycolor) { Ship = new EntityShip(); Ship.Init(speed, weight, bodycolor); Deck = new DrawingDeck(); SetEnum(); } 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() { if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { return; } repaint(); } @Override public void paintComponent(Graphics g) { if (GetShip() == null) { return; } if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { return; } super.paintComponent(g); 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; } } }