import javax.swing.*; import java.awt.*; import java.util.Random; public class DrawningBoat extends JPanel { EntityBoat Boat; public EntityBoat GetBoat(){ return Boat; } int _startPosX; int _startPosY; public IDrawningOars OarsDraw; public Integer _pictureWidth = null; public Integer _pictureHeight = null; private int _BoatWidth = 80; private int _BoatHeight = 50; public void SetOars() { Random r = new Random(); int numIllum = r.nextInt(1,4); OarsDraw.SetOarsCount(numIllum); } public DrawningBoat(int speed, float weight, Color bodyColor) { Boat = new EntityBoat(speed, weight, bodyColor); Random random = new Random(); switch (random.nextInt(3)){ case 0: OarsDraw = new DrawningOars(); break; case 1: OarsDraw = new DrawningSqareOars(); break; case 2: OarsDraw = new DrawningOvalOars(); break; } SetOars(); } protected DrawningBoat(EntityBoat boat, IDrawningOars oar){ Boat = boat; OarsDraw = oar; } public void SetPosition(int x, int y, int width, int height) { if (x >= 0 && x + _BoatWidth <= width && y >= 0 && y + _BoatHeight <= height) { _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 + _BoatWidth + Boat.Step < _pictureWidth) { _startPosX += Boat.Step; } break; case Left: if (_startPosX - Boat.Step > 0) { _startPosX -= Boat.Step; } break; case Up: if (_startPosY - Boat.Step > 0) { _startPosY -= Boat.Step; } break; case Down: if (_startPosY + _BoatHeight + Boat.Step < _pictureHeight) { _startPosY += Boat.Step; } break; } } protected DrawningBoat(int speed, float weight, Color bodyColor, int boatWidth, int boatHeight) { this(speed, weight, bodyColor); _BoatWidth = boatWidth; _BoatHeight = boatHeight; } public void DrawTransport(Graphics g) { if (_startPosX < 0 || _startPosY < 0 || _pictureWidth == null || _pictureHeight == null) { return; } if (GetBoat() == null) { return; } super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.BLACK); //границы g2d.drawRect(_startPosX, _startPosY, _BoatWidth * 3 / 4, _BoatHeight); int[] xpoints = {(int)(_startPosX + _BoatWidth * 3 / 4), (int)(_startPosX + _BoatWidth), (int)(_startPosX + _BoatWidth * 3 / 4) }; int[] ypoints = {(int)_startPosY, (int)(_startPosY + _BoatHeight / 2), (int)(_startPosY + _BoatHeight)}; g2d.drawPolygon(xpoints, ypoints, 3); g2d.setPaint(Boat.GetBodyColor()); g2d.fillRect(_startPosX, _startPosY, _BoatWidth * 3 / 4, _BoatHeight); g2d.fillPolygon(xpoints, ypoints, 3); // середина g2d.setColor(Color.BLACK); g2d.drawOval(_startPosX + 5, _startPosY + 5, _BoatWidth - 15, _BoatHeight - 11); g2d.setPaint(Color.CYAN); g2d.fillOval(_startPosX + 5, _startPosY + 5, _BoatWidth - 15, _BoatHeight - 11); OarsDraw.DrawOars(g, _startPosX, _startPosY); repaint(); } public void ChangeBorders(int width, int height) { _pictureWidth = width; _pictureHeight = height; if (_pictureWidth <= _BoatWidth || _pictureHeight <= _BoatHeight) { _pictureWidth = null; _pictureHeight = null; return; } if (_startPosX + _BoatWidth > _pictureWidth) { _startPosX = _pictureWidth - _BoatWidth; } if (_startPosY + _BoatHeight > _pictureHeight) { _startPosY = _pictureHeight - _BoatHeight; } } public float[] GetCurrentPosition() { float[] dim = new float[4]; dim[0] = _startPosX; dim[1] =_startPosY; dim[2] = _startPosX + _BoatWidth; dim[3] = _startPosY + _BoatHeight; return dim; } }