From de8c7eb3aeddfad9efbe3aa76c7b34be6f51ecef Mon Sep 17 00:00:00 2001 From: dimazhelovanov Date: Mon, 24 Oct 2022 21:01:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=82=D0=BE=D1=80=D0=BE=D0=B9=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=BC=D0=B8=D1=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Direction.java | 7 ++ DrawningBattleship.java | 169 ++++++++++++++++++++++++++++++++++++++++ EntityBattleship.java | 41 ++++++++++ 3 files changed, 217 insertions(+) create mode 100644 Direction.java create mode 100644 DrawningBattleship.java create mode 100644 EntityBattleship.java diff --git a/Direction.java b/Direction.java new file mode 100644 index 0000000..d4a5659 --- /dev/null +++ b/Direction.java @@ -0,0 +1,7 @@ +public enum Direction { + + Up, + Down, + Left, + Right +} diff --git a/DrawningBattleship.java b/DrawningBattleship.java new file mode 100644 index 0000000..20928c2 --- /dev/null +++ b/DrawningBattleship.java @@ -0,0 +1,169 @@ +import java.awt.*; + +public class DrawningBattleship { + EntityBattleship Battleship; + public EntityBattleship Battleship() + {return Battleship; } + /// + /// Левая координата отрисовки корабля + /// + private float _startPosX; + /// + /// Верхняя кооридната отрисовки корабля + /// + private float _startPosY; + /// + /// Ширина окна отрисовки + /// + private Integer _pictureWidth = 100; + /// + /// Высота окна отрисовки + /// + private Integer _pictureHeight = 100; + /// + /// Ширина отрисовки корабля + /// + private final int _battleshipWidth = 120; + /// + /// Высота отрисовки корабля + /// + private final int _battleshipHeight = 50; + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес корабля + /// Цвет корпуса + public void Init(int speed, float weight, Color bodyColor, EntityBattleship entityBattleship) + { + Battleship = new EntityBattleship(); + Battleship.Init(speed, weight, bodyColor); + } + /// + /// Установка позиции корабля + /// + /// Координата 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(Graphics g) + { + if (_startPosX < 0 || _startPosY < 0 + || _pictureHeight == null || _pictureWidth == null) + { + return; + } + g.setColor(Color.BLACK); + + //Корпус корабля + //Brush br = new SolidBrush(Battleship?.BodyColor ?? Color.Black); + // Polygon[] shipHullArrayPoints = {((int)_startPosX, (int)_startPosY)), (int)_startPosX, (int)_startPosY + 50), new Point((int)_startPosX + 80, (int)_startPosY + 50), + //(int)_startPosX + 120, (int)_startPosY + 25 ), new Point((int)_startPosX + 80, (int)_startPosY), new Point((int)_startPosX, (int)_startPosY)}; + 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) _startPosX + 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); + + } + /// + /// Смена границ формы отрисовки + /// + /// Ширина картинки + /// Высота картинки + + + 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; + } + } + +} diff --git a/EntityBattleship.java b/EntityBattleship.java new file mode 100644 index 0000000..0674e77 --- /dev/null +++ b/EntityBattleship.java @@ -0,0 +1,41 @@ +import java.awt.*; +import java.util.Random; + +public class EntityBattleship { + private int speed; + private float weight; + Color BodyColor; + public int GetSpeed() { + return speed; + } + public float GetWeight() { + return weight; + } + /// + /// Цвет корпуса + /// + public Color GetBodyColor() { + return BodyColor; + } + /// + /// Шаг перемещения корабля + /// + public float GetStep(){ + return speed * 100 / weight; + } + /// + /// Инициализация полей объекта-класса корабля + /// + /// + /// + /// + /// + public void Init(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + if(speed <= 0) speed = rnd.nextInt(50, 150); + if(weight <= 0) weight = rnd.nextInt(40, 70); + + BodyColor = bodyColor; + } +}