import java.awt.*; import java.util.Random; public class DrawningBattleship { EntityBattleship Battleship; private IDrawningBlocks iDrawingBlocks; public EntityBattleship Battleship() {return Battleship; } /// /// Левая координата отрисовки корабля /// protected float _startPosX; /// /// Верхняя кооридната отрисовки корабля /// protected float _startPosY; /// /// Ширина окна отрисовки /// private Integer _pictureWidth = null; /// /// Высота окна отрисовки /// private Integer _pictureHeight = null; /// /// Ширина отрисовки корабля /// private int _battleshipWidth = 120; /// /// Высота отрисовки корабля /// private int _battleshipHeight = 50; /// /// Инициализация свойств /// /// Скорость /// Вес корабля /// Цвет корпуса public DrawningBattleship(int speed, float weight, Color bodyColor) { Random random = new Random(); int[] ArrayBlocks = new int[]{2, 4, 6}; switch (random.nextInt(3)){ case 0: iDrawingBlocks = new DrawningBlocks(ArrayBlocks[random.nextInt(0, 3)], Color.BLACK); break; case 1: iDrawingBlocks = new DrawningRoundBlocks(ArrayBlocks[random.nextInt(0, 3)]); break; case 2: iDrawingBlocks = new DrawningTriangleBlocks(ArrayBlocks[random.nextInt(0, 3)]); break; } Battleship = new EntityBattleship(speed, weight, bodyColor); } public DrawningBattleship(EntityBattleship entity, IDrawningBlocks blocks){ Battleship = entity; iDrawingBlocks = blocks; } protected DrawningBattleship(int speed, float weight, Color bodyColor, int battleshipWidth, int battleshipHeight) { this(speed, weight, bodyColor); _battleshipWidth = battleshipWidth; _battleshipHeight = battleshipHeight; } /// /// Установка позиции корабля /// /// Координата X /// Координата Y /// Ширина картинки /// Высота картинки public void SetPosition(int x, int y, int width, int height) { if ((x > 0 && y > 0) && (_battleshipHeight + y < height) && (_battleshipWidth + x < width)) { _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 + _battleshipWidth + Battleship.GetStep() < _pictureWidth) { _startPosX += Battleship.GetStep(); } break; //влево case Left: if (_startPosX - Battleship.GetStep() >= 0) { _startPosX -= Battleship.GetStep(); } break; //вверх case Up: if (_startPosY - Battleship.GetStep() >= 0) { _startPosY -= Battleship.GetStep(); } break; //вниз case Down: if (_startPosY + _battleshipHeight + Battleship.GetStep() < _pictureHeight) { _startPosY += Battleship.GetStep(); } break; } } /// /// Отрисовка корабля /// /// public void DrawTransport(Graphics2D g) { if (_startPosX < 0 || _startPosY < 0 || _pictureHeight == null || _pictureWidth == null) { return; } g.setColor(Color.BLACK); //Корпус корабля g.setColor(Battleship.bodyColor); g.fillPolygon(new int[]{(int) _startPosX, (int) _startPosX , (int) _startPosX+80, (int) _startPosX + 120, (int)_startPosX + 80, (int)_startPosX}, new int[]{(int) _startPosY, (int) _startPosY+50 , (int) _startPosY+50, (int) _startPosY + 25, (int)_startPosY, (int)_startPosY}, 6); //Пушка g.setColor(Color.BLACK); g.drawRect((int)_startPosX + 20, (int)_startPosY + 20, 30, 10); g.drawRect((int)_startPosX + 50,(int) _startPosY + 10, 20, 30); g.fillRect((int)_startPosX + 20, (int)_startPosY + 20, 30, 10); g.fillRect((int)_startPosX + 50, (int)_startPosY + 10, 20, 30); //Отсек g.setColor(Color.BLUE); g.drawOval((int)_startPosX+80, (int)_startPosY+15, 20, 20); g.fillOval((int)_startPosX + 80, (int)_startPosY + 15, 20, 20); g.setColor(Color.BLACK); g.fillRect((int)_startPosX-5, (int)_startPosY+10, 5, 5); g.fillRect((int)_startPosX - 5, (int)_startPosY + 35, 5, 5); iDrawingBlocks.DrawBlocks(g, (int) _startPosX, (int) _startPosY, Color.BLACK); } /// /// Смена границ формы отрисовки /// /// Ширина картинки /// Высота картинки public void ChangeBorders(int width, int height) { _pictureWidth = width; _pictureHeight = height; if (_pictureWidth <= _battleshipWidth || _pictureHeight <= _battleshipHeight) { _pictureWidth = null; _pictureHeight = null; return; } if (_startPosX + _battleshipWidth > _pictureWidth) { _startPosX = _pictureWidth - _battleshipWidth; } if (_startPosY + _battleshipHeight > _pictureHeight) { _startPosY = _pictureHeight - _battleshipHeight; } } public float[] GetCurrentPosition() { return new float[] {_startPosY, _startPosX + _battleshipWidth, _startPosY + _battleshipHeight, _startPosX}; } }