diff --git a/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_.xml b/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_.xml
deleted file mode 100644
index a2e733d..0000000
--- a/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_.xml
+++ /dev/null
@@ -1,4 +0,0 @@
-
-
-
-
\ No newline at end of file
diff --git a/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_1.xml b/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_1.xml
deleted file mode 100644
index 97dcf23..0000000
--- a/.idea/shelf/Uncommitted_changes_before_Checkout_at_21_12_2023_16_01__Changes_1.xml
+++ /dev/null
@@ -1,4 +0,0 @@
-
-
-
-
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 0f4d59c..f75bf39 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,39 +5,63 @@
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
- {
- "keyToString": {
- "RunOnceActivity.OpenProjectViewOnStart": "true",
- "RunOnceActivity.ShowReadmeOnStart": "true"
+
+}]]>
@@ -47,8 +71,34 @@
1703061980189
+
+ 1703160472180
+
+
+
+ 1703160472180
+
+
+ 1703160484223
+
+
+
+ 1703160484223
+
+
+
+
+
+
+
diff --git a/src/AbstractStrategy.java b/src/AbstractStrategy.java
new file mode 100644
index 0000000..d3594f8
--- /dev/null
+++ b/src/AbstractStrategy.java
@@ -0,0 +1,123 @@
+
+public abstract class AbstractStrategy {
+ ///
+ /// Перемещаемый объект
+ ///
+ private IMoveableObject _moveableObject;
+ ///
+ /// Статус перемещения
+ ///
+ private Status _state = Status.NotInit;
+ ///
+ /// Ширина поля
+ ///
+ private int fieldWidth;
+ protected int getFieldWidth() {return fieldWidth;}
+ ///
+ /// Высота поля
+ ///
+ private int fieldHeight;
+ protected int getFieldHeight(){return fieldHeight;}
+ ///
+ /// Статус перемещения
+ ///
+ public Status GetStatus() { return _state; }
+ ///
+ /// Установка данных
+ ///
+ /// Перемещаемый объект
+ /// Ширина поля
+ /// Высота поля
+ public void SetData(IMoveableObject moveableObject, int width, int
+ height)
+ {
+ if (moveableObject == null)
+ {
+ _state = Status.NotInit;
+ return;
+ }
+ _state = Status.InProgress;
+ _moveableObject = moveableObject;
+ fieldWidth = width;
+ fieldHeight = height;
+ }
+ ///
+ /// Шаг перемещения
+ ///
+ public void MakeStep()
+ {
+ if (_state != Status.InProgress)
+ {
+ return;
+ }
+ if (IsTargetDestinaion())
+ {
+ _state = Status.Finish;
+ return;
+ }
+ MoveToTarget();
+ }
+ ///
+ /// Перемещение влево
+ ///
+ /// Результат перемещения (true - удалось переместиться, false - неудача)
+ protected boolean MoveLeft() { return MoveTo(DirectionType.LEFT);}
+ ///
+ /// Перемещение вправо
+ ///
+ /// Результат перемещения (true - удалось переместиться,false - неудача)
+ protected boolean MoveRight() { return MoveTo(DirectionType.RIGHT);}
+ ///
+ /// Перемещение вверх
+ ///
+ /// Результат перемещения (true - удалось переместиться,false - неудача)
+ protected boolean MoveUp() { return MoveTo(DirectionType.UP);}
+ ///
+ /// Перемещение вниз
+ ///
+ /// Результат перемещения (true - удалось переместиться,false - неудача)
+ protected boolean MoveDown() { return MoveTo(DirectionType.DOWN);}
+ ///
+ /// Параметры объекта
+ ///
+ protected ObjectParameters getObjectParameters() { return _moveableObject.GetObjectPosition(); }
+ ///
+ /// Шаг объекта
+ ///
+ ///
+ protected int GetStep()
+ {
+ if (_state != Status.InProgress)
+ {
+ return 0;
+ }
+ return _moveableObject.GetStep();
+ }
+ ///
+ /// Перемещение к цели
+ ///
+ protected abstract void MoveToTarget();
+ ///
+ /// Достигнута ли цель
+ ///
+ ///
+ protected abstract boolean IsTargetDestinaion();
+ ///
+ /// Попытка перемещения в требуемом направлении
+ ///
+ /// Направление
+ /// Результат попытки (true - удалось переместиться, false - неудача)
+ private boolean MoveTo(DirectionType Direction)
+ {
+ if (_state != Status.InProgress)
+ {
+ return false;
+ }
+ if (_moveableObject.CheckCanMove(Direction))
+ {
+ _moveableObject.MoveObject(Direction);
+ return true;
+ }
+ return false;
+ }
+}
\ No newline at end of file
diff --git a/src/DrawingBattleship.java b/src/DrawingBattleship.java
index cacb674..489ee84 100644
--- a/src/DrawingBattleship.java
+++ b/src/DrawingBattleship.java
@@ -1,110 +1,24 @@
import java.awt.*;
-public class DrawingBattleship {
- public EntityBattleship entityBattleship;
- private DrawingBlocks drawingBlocks;
- private int pictureWidth;
- private int pictureHeight;
- private int startPosX;
- private int startPosY;
- private final int shipWidth = 175;
- private final int shipHeight = 80;
+public class DrawingBattleship extends DrawningShip {
- public boolean Init(int speed, double weight, Color bodyColor, Color
- additionalColor, boolean turret, boolean rocketLauncher, int width, int height, int blocksNumber) {
- if (width < shipWidth || height < shipHeight)
- return false;
- pictureWidth = width;
- pictureHeight = height;
- entityBattleship = new EntityBattleship();
- entityBattleship.Init(speed, weight, bodyColor, additionalColor, turret, rocketLauncher);
- drawingBlocks = new DrawingBlocks();
- drawingBlocks.setNumber(blocksNumber);
- return true;
- }
- public void SetPosition(int x, int y) {
- if (x < 0 || y < 0 || x + shipWidth > pictureWidth || y + shipHeight > pictureHeight) {
- x = 0;
- y = 0;
- }
- startPosX = x;
- startPosY = y;
- }
- public void moveTransport(DirectionType direction) {
- if (entityBattleship == null)
- return;
- switch (direction) {
- //влево
- case LEFT:
- if (startPosX - entityBattleship.Step > 0) {
- startPosX -= entityBattleship.Step;
- }
- break;
- case UP:
- if (startPosY - entityBattleship.Step > 0) {
- startPosY -= entityBattleship.Step;
- }
- break;
- case RIGHT:
- if (startPosX + shipWidth + entityBattleship.Step < pictureWidth) {
- startPosX += entityBattleship.Step;
- }
- break;
- case DOWN:
- if (startPosY + shipHeight + entityBattleship.Step < pictureHeight) {
- startPosY += entityBattleship.Step;
- }
- break;
- default:
- break;
- }
+ public DrawingBattleship(int speed, double weight, Color bodyColor, Color additionalColor,boolean tower,
+ boolean section, int width, int height, int blocksType, int blocksNumber) {
+ super(speed, weight, bodyColor, width, height, blocksType, blocksNumber);
+ if (entityShip != null)
+ entityShip = new EntityBattleship(speed, weight, bodyColor, additionalColor, tower, section);
}
- public void drawTransport(Graphics2D g2d){
- if(entityBattleship == null)
- return;
- BasicStroke pen = new BasicStroke(2);
- g2d.setStroke(pen);
- Color bodyColor = entityBattleship.BodyColor;
- Color additionalColor = entityBattleship.AdditionalColor;
- //основа
- int[] xPoints = {startPosX + 5, startPosX + 120, startPosX + 160, startPosX + 120, startPosX + 5};
- int[] yPoints = {startPosY + 0, startPosY + 0, startPosY + 35, startPosY + 70, startPosY + 70};
- g2d.setColor(bodyColor);
- g2d.fillPolygon(xPoints, yPoints, 5);
- g2d.setPaint(Color.BLACK);
- g2d.drawPolygon(xPoints, yPoints, 5);
+ public void drawTransport(Graphics2D g2d){
+ if (entityShip == null)
+ return;
- //блоки
-
- g2d.setPaint(Color.DARK_GRAY);
- g2d.fillRect(startPosX + 70, startPosY + 15, 20, 40);
- g2d.setPaint(Color.BLACK);
- g2d.drawRect(startPosX + 70, startPosY + 15, 20, 40);
- g2d.setPaint(Color.DARK_GRAY);
- g2d.fillRect(startPosX + 40, startPosY + 25, 30, 20);
- g2d.setPaint(Color.BLACK);
- g2d.drawRect(startPosX + 40, startPosY + 25, 30, 20);
- g2d.setPaint(Color.DARK_GRAY);
- g2d.fillOval(startPosX + 100, startPosY + 20, 30, 30);
- g2d.setPaint(Color.BLACK);
- g2d.drawOval(startPosX + 100, startPosY + 20, 30, 30);
-
- //для ускорения
- g2d.setPaint(Color.YELLOW);
- g2d.fillRect(startPosX + 0, startPosY + 10, 5, 20);
- g2d.fillRect(startPosX + 0, startPosY + 40, 5, 20);
- g2d.setPaint(Color.BLACK);
- g2d.drawRect(startPosX + 0, startPosY + 40, 5, 20);
- g2d.drawRect(startPosX + 0, startPosY + 10, 5, 20);
-
- //блоки
- if (drawingBlocks != null) {
- drawingBlocks.drawBlocks(g2d, startPosX, startPosY);
- }
- //орудийная башня
- if (entityBattleship.Tower) {
- g2d.setColor(additionalColor);
+ super.drawTransport(g2d);
+ g2d.setColor(((EntityBattleship)entityShip).AdditionalColor);
+ BasicStroke pen = new BasicStroke(2);
+ g2d.setStroke(pen);
+ //орудийная башня
+ if (((EntityBattleship)entityShip).Tower) {
g2d.fillRect(startPosX + 108, startPosY + 28, 15, 15);
g2d.drawRect(startPosX + 108, startPosY + 28, 15, 15);
// добавить черный цвет пушке
@@ -115,16 +29,12 @@ public class DrawingBattleship {
g2d.drawRect(startPosX + 123, startPosY + 32, 55, 6);
}
//отсеки под ракеты
- if (entityBattleship.Section) {
- //добавить серый цвет
- g2d.setColor(additionalColor);
+ if (((EntityBattleship)entityShip).Section) {
g2d.fillRect(startPosX + 20, startPosY + 70, 40, 10);
g2d.fillRect(startPosX + 75, startPosY + 70, 40, 10);
g2d.setPaint(Color.BLACK);
g2d.drawRect(startPosX + 20, startPosY + 70, 40, 10);
g2d.drawRect(startPosX + 75, startPosY + 70, 40, 10);
}
-
}
-
}
diff --git a/src/DrawingBlocks.java b/src/DrawingBlocks.java
index 31ad076..18d155c 100644
--- a/src/DrawingBlocks.java
+++ b/src/DrawingBlocks.java
@@ -1,6 +1,6 @@
import java.awt.*;
-public class DrawingBlocks {
+public class DrawingBlocks implements IDrawBlocks{
private BlocksNumber number;
public void setNumber(int x){
if(x <= 2)
diff --git a/src/DrawningObjectShip.java b/src/DrawningObjectShip.java
new file mode 100644
index 0000000..45432ea
--- /dev/null
+++ b/src/DrawningObjectShip.java
@@ -0,0 +1,19 @@
+public class DrawningObjectShip implements IMoveableObject {
+ private DrawningShip drawingShip = null;
+ public DrawningObjectShip(DrawningShip _drawingShip)
+ {
+ drawingShip = _drawingShip;
+ }
+ public ObjectParameters GetObjectPosition(){
+ if (drawingShip == null || drawingShip.getEntityShip() == null)
+ {
+ return null;
+ }
+ return new ObjectParameters(drawingShip.GetPosX(),
+ drawingShip.GetPosY(), drawingShip.GetWidth(), drawingShip.GetHeight());
+ }
+ public int GetStep(){ return (int)drawingShip.getEntityShip().Step; }
+ public boolean CheckCanMove(DirectionType direction) { return drawingShip.CanMove(direction);}
+ public void MoveObject(DirectionType direction) { drawingShip.MoveTransport(direction); }
+
+}
diff --git a/src/DrawningRoundBlocks.java b/src/DrawningRoundBlocks.java
new file mode 100644
index 0000000..2437cc1
--- /dev/null
+++ b/src/DrawningRoundBlocks.java
@@ -0,0 +1,28 @@
+import java.awt.*;
+public class DrawningRoundBlocks implements IDrawBlocks {
+ private BlocksNumber number;
+
+ @Override
+ public void setNumber(int x) {
+ if (x <= 2)
+ number = BlocksNumber.TWO;
+ if (x == 4)
+ number = BlocksNumber.FOUR;
+ if (x >= 6)
+ number = BlocksNumber.SIX;
+ }
+
+ @Override
+ public void drawBlocks(Graphics2D graphics2D, int _startX, int _startY) {
+ graphics2D.fillOval(_startX + 50, _startY + 11, 8, 8);
+ graphics2D.fillOval(_startX + 50, _startY + 31, 8, 8);
+ if (number == BlocksNumber.FOUR || number == BlocksNumber.SIX) {
+ graphics2D.fillOval(_startX + 60, _startY + 11, 8, 8);
+ graphics2D.fillOval(_startX + 60, _startY + 31, 8, 8);
+ if (number == BlocksNumber.SIX) {
+ graphics2D.fillOval(_startX + 40, _startY + 11, 8, 8);
+ graphics2D.fillOval(_startX + 40, _startY + 31, 8, 8);
+ }
+ }
+ }
+}
diff --git a/src/DrawningShip.java b/src/DrawningShip.java
new file mode 100644
index 0000000..3d5a4ee
--- /dev/null
+++ b/src/DrawningShip.java
@@ -0,0 +1,185 @@
+import java.awt.*;
+import java.util.*;
+import javax.swing.*;
+import javax.swing.Timer;
+import java.awt.event.*;
+public class DrawningShip {
+ private IDrawBlocks drawingBlocks;
+
+ protected EntityShip entityShip;
+ public EntityShip getEntityShip(){return entityShip;}
+ ///
+ /// Ширина окна
+ ///
+ protected int _pictureWidth;
+ ///
+ /// Высота окна
+ ///
+ protected int _pictureHeight;
+ ///
+ /// Левая координата прорисовки локомотива
+ ///
+ protected int startPosX;
+ ///
+ /// Верхняя кооридната прорисовки локомотива
+ ///
+ protected int startPosY;
+ ///
+ /// Координата X объекта
+ ///
+ public int GetPosX (){return startPosX;}
+ ///
+ /// Координата Y объекта
+ ///
+ public int GetPosY (){return startPosY;}
+ ///
+ /// Ширина объекта
+ ///
+ public int GetWidth (){return shipWidth;}
+ ///
+ /// Высота объекта
+ ///
+ public int GetHeight (){return shipHeight;}
+ ///
+ /// Ширина прорисовки локомотива
+ ///
+ protected int shipWidth = 175;
+ protected int shipHeight = 80;
+ ///
+ /// Инициализация свойств
+ ///
+ /// Скорость
+ /// Вес
+ /// Цвет кузова
+ /// Ширина картинки
+ /// Высота картинки
+ public DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int blocksType, int blocksNumber)
+ {
+ _pictureWidth = width;
+ _pictureHeight = height;
+ if (width < shipWidth || height < shipHeight)
+ return;
+ entityShip = new EntityShip(speed, weight, bodyColor);
+ Random random = new Random();
+ switch(blocksType){
+ case 0:
+ drawingBlocks = new DrawingBlocks();
+ break;
+// case 1:
+// drawingBlocks = new DrawingCrossBlocks();
+// break;
+// case 2:
+// drawingBlocks = new DrawingRoundBlocks();
+// break;
+ default:
+ drawingBlocks = new DrawingBlocks();
+ break;
+ }
+ drawingBlocks.setNumber(blocksNumber);
+ }
+ ///
+ /// Установка позиции
+ ///
+ /// Координата X
+ /// Координата Y
+ public void SetPosition(int x, int y)
+ {
+ if (x < 0 || y < 0 || x + shipWidth > _pictureWidth || y + shipHeight > _pictureHeight) {
+ x = 0;
+ y = 0;
+ }
+ startPosX = x;
+ startPosY = y;
+ }
+
+ ///
+ /// Проверка, что объект может переместится по указанному направлению
+ ///
+ /// Направление
+ /// true - можно переместится по указанному направлению
+ public boolean CanMove(DirectionType 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 void MoveTransport(DirectionType direction)
+ {
+ if (!CanMove(direction) || entityShip == null)
+ {
+ return;
+ }
+ switch (direction)
+ {
+ //влево
+ case LEFT:
+ startPosX -= (int)entityShip.Step;
+ break;
+ //вверх
+ case UP:
+ startPosY -= (int)entityShip.Step;
+ break;
+ // вправо
+ case RIGHT:
+ startPosX += (int)entityShip.Step;
+ break;
+ //вниз
+ case DOWN:
+ startPosY += (int)entityShip.Step;
+ break;
+ }
+ }
+ public void drawTransport(Graphics2D g2d) {
+ if (entityShip == null) {
+ return;
+ }
+ BasicStroke pen = new BasicStroke(2);
+ g2d.setStroke(pen);
+ Color bodyColor = entityShip.BodyColor;
+ //основа
+ int[] xPoints = {startPosX + 5, startPosX + 120, startPosX + 160, startPosX + 120, startPosX + 5};
+ int[] yPoints = {startPosY + 0, startPosY + 0, startPosY + 35, startPosY + 70, startPosY + 70};
+ g2d.setColor(bodyColor);
+ g2d.fillPolygon(xPoints, yPoints, 5);
+ g2d.setPaint(Color.BLACK);
+ g2d.drawPolygon(xPoints, yPoints, 5);
+ //блоки
+ g2d.setPaint(Color.DARK_GRAY);
+ g2d.fillRect(startPosX + 70, startPosY + 15, 20, 40);
+ g2d.setPaint(Color.BLACK);
+ g2d.drawRect(startPosX + 70, startPosY + 15, 20, 40);
+ g2d.setPaint(Color.DARK_GRAY);
+ g2d.fillRect(startPosX + 40, startPosY + 25, 30, 20);
+ g2d.setPaint(Color.BLACK);
+ g2d.drawRect(startPosX + 40, startPosY + 25, 30, 20);
+ g2d.setPaint(Color.DARK_GRAY);
+ g2d.fillOval(startPosX + 100, startPosY + 20, 30, 30);
+ g2d.setPaint(Color.BLACK);
+ g2d.drawOval(startPosX + 100, startPosY + 20, 30, 30);
+ //для ускорения
+ g2d.setPaint(Color.YELLOW);
+ g2d.fillRect(startPosX + 0, startPosY + 10, 5, 20);
+ g2d.fillRect(startPosX + 0, startPosY + 40, 5, 20);
+ g2d.setPaint(Color.BLACK);
+ g2d.drawRect(startPosX + 0, startPosY + 40, 5, 20);
+ g2d.drawRect(startPosX + 0, startPosY + 10, 5, 20);
+ //блоки
+ if (drawingBlocks != null) {
+ drawingBlocks.drawBlocks(g2d, startPosX, startPosY);
+ }
+ }
+}
diff --git a/src/EntityBattleship.java b/src/EntityBattleship.java
index 6008b8a..02a9051 100644
--- a/src/EntityBattleship.java
+++ b/src/EntityBattleship.java
@@ -1,19 +1,14 @@
import java.awt.*;
-public class EntityBattleship {
- public int Speed;
- public double Weight;
- public Color BodyColor;
+public class EntityBattleship extends EntityShip{
public Color AdditionalColor;
public boolean Tower;
public boolean Section;
- public double Step = (double)Speed * 100 / Weight;
- public void Init(int speed, double weight, Color bodyColor,Color
- additionalColor, boolean tower, boolean section){
- Speed = speed;
- Weight = weight;
- Step = (double)Speed * 100 / Weight;
- BodyColor = bodyColor;
+
+ public EntityBattleship(int speed, double weight, Color bodyColor, Color
+ additionalColor, boolean tower, boolean section)
+ {
+ super(speed, weight, bodyColor);
AdditionalColor = additionalColor;
Tower = tower;
Section = section;
diff --git a/src/EntityShip.java b/src/EntityShip.java
new file mode 100644
index 0000000..7bfa81d
--- /dev/null
+++ b/src/EntityShip.java
@@ -0,0 +1,33 @@
+import java.awt.*;
+public class EntityShip {
+ ///
+ /// Скорость
+ ///
+ public int Speed;
+ ///
+ /// Вес
+ ///
+ public double Weight;
+ ///
+ /// Основной цвет
+ ///
+ public Color BodyColor;
+ ///
+ /// Шаг перемещения поезда
+ ///
+ public double Step;
+
+ ///
+ /// Инициализация полей объекта-класса Локомотива
+ ///
+ /// Скорость
+ /// Вес автомобиля
+ /// Основной цвет
+ public EntityShip(int speed, double weight, Color bodyColor)
+ {
+ Speed = speed;
+ Weight = weight;
+ BodyColor = bodyColor;
+ Step = (double)Speed * 100 / Weight;
+ }
+}
diff --git a/src/FrameBattleship.java b/src/FrameBattleship.java
index 0e96127..21c859b 100644
--- a/src/FrameBattleship.java
+++ b/src/FrameBattleship.java
@@ -1,3 +1,182 @@
+//import javax.imageio.ImageIO;
+//import javax.swing.*;
+//import java.awt.*;
+//import java.awt.event.ActionEvent;
+//import java.io.File;
+//import java.io.IOException;
+//import java.util.Random;
+//
+//public class FrameBattleship extends JFrame {
+// private DrawingBattleship drawingBattleship;
+// private DrawningShip drawningShip;
+// private AbstractStrategy abstractStrategy;
+// private JComboBox comboBoxStrategy;
+// private JComponent pictureBox;
+//
+// public FrameBattleship() throws IOException {
+// super("Линкор");
+// setSize(new Dimension(900, 500));
+// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+// //components initialisation
+// pictureBox = new JComponent() {
+// public void paintComponent(Graphics graphics) {
+// super.paintComponent(graphics);
+// Graphics2D graphics2D = (Graphics2D) graphics;
+// if (drawingBattleship != null) drawingBattleship.drawTransport(graphics2D);
+// super.repaint();
+// }
+// };
+// pictureBox.setBounds(0, 0, getContentPane().getWidth(), getContentPane().getHeight());
+// comboBoxStrategy = new JComboBox<>(new String[]{"К центру", "К границе"});
+// JButton stepButton = new JButton("Шаг");
+// JButton createShipButton = new JButton("Создать корабль");
+// JButton createBattleshipButton = new JButton("Создать военный корабль");
+// JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\right-arrow.png"))));
+// rightButton.setPreferredSize(new Dimension(30, 30));
+// JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\left-arrow.png"))));
+// leftButton.setPreferredSize(new Dimension(30, 30));
+// JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\upper-arrow.png"))));
+// upButton.setPreferredSize(new Dimension(30, 30));
+// JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\down-arrow.png"))));
+// downButton.setPreferredSize(new Dimension(30, 30));
+// //ActionListeners and ActionCommand addition
+// createShipButton.addActionListener(e -> buttonCreateShipClick());
+// createBattleshipButton.addActionListener(e -> buttonCreateBattleshipClick());
+// stepButton.addActionListener(e -> buttonStepClick());
+// rightButton.setActionCommand("right");
+// rightButton.addActionListener(this::buttonMoveClick);
+// leftButton.setActionCommand("left");
+// leftButton.addActionListener(this::buttonMoveClick);
+// upButton.setActionCommand("up");
+// upButton.addActionListener(this::buttonMoveClick);
+// downButton.setActionCommand("down");
+// downButton.addActionListener(this::buttonMoveClick);
+// //panels and constraints initialisation
+// JPanel panelBattleship = new JPanel(new BorderLayout());
+// JPanel rightPanel = new JPanel(new BorderLayout());
+// JPanel leftPanel = new JPanel(new BorderLayout());
+// JPanel createPanel = new JPanel(new BorderLayout());
+// JPanel movementPanel = new JPanel(new GridBagLayout());
+// GridBagConstraints constraints = new GridBagConstraints();
+// constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
+// //addition to createPanel
+// constraints.gridx = 0;
+// constraints.gridy = 0;
+// createPanel.add(createShipButton, BorderLayout.SOUTH);
+// constraints.gridx = 1;
+// constraints.gridy = 0;
+// createPanel.add(createBattleshipButton, constraints);
+// //addition to movementPanel
+// constraints.gridx = 2;
+// constraints.gridy = 1;
+// movementPanel.add(rightButton, constraints);
+// constraints.gridx = 0;
+// constraints.gridy = 1;
+// movementPanel.add(leftButton, constraints);
+// constraints.gridx = 1;
+// constraints.gridy = 0;
+// movementPanel.add(upButton, constraints);
+// constraints.gridx = 1;
+// constraints.gridy = 1;
+// movementPanel.add(downButton, constraints);
+// //addition to stepPanel
+// JPanel stepPanel = new JPanel(new GridBagLayout());
+// constraints.gridx = 0;
+// constraints.gridy = 0;
+// stepPanel.add(comboBoxStrategy, constraints);
+// constraints.gridx = 0;
+// constraints.gridy = 1;
+// stepPanel.add(stepButton, constraints);
+// //addition to frame
+// setLayout(new BorderLayout());
+// add(pictureBox);
+// rightPanel.add(movementPanel, BorderLayout.SOUTH);
+// rightPanel.add(stepPanel, BorderLayout.NORTH);
+// leftPanel.add(createPanel, BorderLayout.SOUTH);
+// panelBattleship.add(rightPanel, BorderLayout.EAST);
+// panelBattleship.add(leftPanel, BorderLayout.WEST);
+// add(panelBattleship, BorderLayout.CENTER);
+// setVisible(true);
+// }
+//
+// private void buttonCreateShipClick() {
+// Random random = new Random();
+// pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
+// drawningShip = new DrawningShip(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
+// pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
+// drawingBattleship.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
+// draw();
+// }
+// private void buttonCreateBattleshipClick() {
+// Random random = new Random();
+// pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
+// drawingBattleship = new DrawingBattleship(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
+// new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
+// drawingBattleship.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
+// draw();
+// }
+//
+// private void buttonStepClick(){
+// if (drawingBattleship == null) {
+// return;
+// }
+// if (comboBoxStrategy.isEnabled()) {
+//// switch (comboBoxStrategy.getSelectedIndex()) {
+//// case 0 -> abstractStrategy = new MoveToCenter();
+//// case 1 -> abstractStrategy = new MoveToBorder();
+//// default -> abstractStrategy = null;
+//// }
+// if (abstractStrategy == null) {
+// return;
+// }
+// abstractStrategy.SetData(new DrawningObjectShip(drawingBattleship), pictureBox.getWidth(), pictureBox.getHeight());
+// comboBoxStrategy.setEnabled(false);
+// }
+// if (abstractStrategy == null) {
+// return;
+// }
+// abstractStrategy.MakeStep();
+// draw();
+// if (abstractStrategy.GetStatus() == Status.Finish)
+// {
+// comboBoxStrategy.setEnabled(true);
+// abstractStrategy = null;
+// }
+// }
+// private void buttonMoveClick(ActionEvent event) {
+// if (drawingBattleship == null || drawingBattleship.getEntityShip() == null)
+// return;
+// switch (event.getActionCommand()) {
+// case "left":
+// drawingBattleship.MoveTransport(DirectionType.LEFT);
+// break;
+// case "right":
+// drawingBattleship.MoveTransport(DirectionType.RIGHT);
+// break;
+// case "up":
+// drawingBattleship.MoveTransport(DirectionType.UP);
+// break;
+// case "down":
+// drawingBattleship.MoveTransport(DirectionType.DOWN);
+// break;
+// default:
+// break;
+// }
+// draw();
+// }
+//
+// private void draw() {
+// if (drawingBattleship == null) {
+// return;
+// }
+// pictureBox.repaint();
+// }
+//
+//}
+
+
+
+
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
@@ -7,24 +186,28 @@ import java.io.IOException;
import java.util.Random;
public class FrameBattleship extends JFrame {
- private DrawingBattleship drawingBattleship;
- private JComponent pictureBox;
-
+ private DrawningShip drawningShip;
+ private AbstractStrategy abstractStrategy;
+ private JComboBox comboBoxStrategy;
+ private JComponent pictureBoxBattleship;
public FrameBattleship() throws IOException {
super("Линкор");
- setSize(new Dimension(900, 500));
+ setSize(new Dimension(900,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//components initialisation
- pictureBox = new JComponent() {
- public void paintComponent(Graphics graphics) {
+ pictureBoxBattleship = new JComponent(){
+ public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D graphics2D = (Graphics2D) graphics;
- if (drawingBattleship != null) drawingBattleship.drawTransport(graphics2D);
+ if (drawningShip != null) drawningShip.drawTransport(graphics2D);
super.repaint();
}
};
- pictureBox.setBounds(0, 0, getContentPane().getWidth(), getContentPane().getHeight());
- JButton createButton = new JButton("Создать");
+ pictureBoxBattleship.setBounds( 0, 0, getContentPane().getWidth(), getContentPane().getHeight());
+ comboBoxStrategy = new JComboBox<>(new String[]{"к центру", "к границе"});
+ JButton stepButton = new JButton("Шаг");
+ JButton createShipButton = new JButton("Создать корабль");
+ JButton createBattleshipButton = new JButton("Создать военный корабль");
JButton rightButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\right-arrow.png"))));
rightButton.setPreferredSize(new Dimension(30, 30));
JButton leftButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\left-arrow.png"))));
@@ -32,9 +215,11 @@ public class FrameBattleship extends JFrame {
JButton upButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\upper-arrow.png"))));
upButton.setPreferredSize(new Dimension(30, 30));
JButton downButton = new JButton(new ImageIcon(ImageIO.read(new File("C:\\RPP intellij\\LabWork_01\\images\\down-arrow.png"))));
- downButton.setPreferredSize(new Dimension(30, 30));
+ downButton.setPreferredSize(new Dimension(30,30));
//ActionListeners and ActionCommand addition
- createButton.addActionListener(e -> buttonCreateClick());
+ createShipButton.addActionListener(e -> buttonCreateShipClick());
+ createBattleshipButton.addActionListener(e -> buttonCreateBattleshipClick());
+ stepButton.addActionListener(e -> buttonStepClick());
rightButton.setActionCommand("right");
rightButton.addActionListener(this::buttonMoveClick);
leftButton.setActionCommand("left");
@@ -45,14 +230,19 @@ public class FrameBattleship extends JFrame {
downButton.addActionListener(this::buttonMoveClick);
//panels and constraints initialisation
JPanel panelBattleship = new JPanel(new BorderLayout());
- JPanel createPanel = new JPanel(new BorderLayout());
- createPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
- JPanel movementPanel = new JPanel(new GridBagLayout());
JPanel rightPanel = new JPanel(new BorderLayout());
+ JPanel leftPanel = new JPanel(new BorderLayout());
+ JPanel createPanel = new JPanel(new GridBagLayout());
+ JPanel movementPanel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets.left = constraints.insets.top = constraints.insets.bottom = constraints.insets.right = 2;
//addition to createPanel
- createPanel.add(createButton, BorderLayout.SOUTH);
+ constraints.gridx = 0;
+ constraints.gridy = 0;
+ createPanel.add(createShipButton, constraints);
+ constraints.gridx = 1;
+ constraints.gridy = 0;
+ createPanel.add(createBattleshipButton, constraints);
//addition to movementPanel
constraints.gridx = 2;
constraints.gridy = 1;
@@ -66,53 +256,102 @@ public class FrameBattleship extends JFrame {
constraints.gridx = 1;
constraints.gridy = 1;
movementPanel.add(downButton, constraints);
+ //addition to stepPanel
+ JPanel stepPanel = new JPanel(new GridBagLayout());
+ constraints.gridx = 0;
+ constraints.gridy = 0;
+ stepPanel.add(comboBoxStrategy, constraints);
+ constraints.gridx = 0;
+ constraints.gridy = 1;
+ stepPanel.add(stepButton, constraints);
//addition to frame
setLayout(new BorderLayout());
- add(pictureBox);
+ add(pictureBoxBattleship);
rightPanel.add(movementPanel, BorderLayout.SOUTH);
+ rightPanel.add(stepPanel, BorderLayout.NORTH);
+ leftPanel.add(createPanel, BorderLayout.SOUTH);
panelBattleship.add(rightPanel, BorderLayout.EAST);
- panelBattleship.add(createPanel, BorderLayout.WEST);
- add(panelBattleship, BorderLayout.CENTER);
+ panelBattleship.add(leftPanel, BorderLayout.WEST);
+ add(panelBattleship,BorderLayout.CENTER);
setVisible(true);
}
-
- private void buttonCreateClick() {
+ private void buttonCreateBattleshipClick() {
Random random = new Random();
- drawingBattleship = new DrawingBattleship();
- pictureBox.setBounds(0, 0, getContentPane().getWidth(), getContentPane().getHeight());
- drawingBattleship.Init(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
- new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight(), (random.nextInt(3) + 1) * 2);
- drawingBattleship.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
+ pictureBoxBattleship.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
+ drawningShip = new DrawingBattleship(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
+ new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBoxBattleship.getWidth(), pictureBoxBattleship.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
+ drawningShip.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
draw();
}
+ private void buttonCreateShipClick(){
+ Random random = new Random();
+ pictureBoxBattleship.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight());
+ drawningShip = new DrawningShip(random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
+ pictureBoxBattleship.getWidth(), pictureBoxBattleship.getHeight(), random.nextInt(3),(random.nextInt(3)+1)*2);
+ drawningShip.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10);
+ draw();
+ }
+ private void buttonStepClick(){
+ if (drawningShip == null) {
+ return;
+ }
+ if (comboBoxStrategy.isEnabled()) {
+ switch (comboBoxStrategy.getSelectedIndex())
+ {
+ case 0:
+ abstractStrategy = new MoveToCenter();
+ break;
+ case 1:
+ abstractStrategy = new MoveToBorder();
+ break;
+ default:
+ abstractStrategy = null;
+ break;
+ };
+ if (abstractStrategy == null) {
+ return;
+ }
+ abstractStrategy.SetData(new DrawningObjectShip(drawningShip), pictureBoxBattleship.getWidth(), pictureBoxBattleship.getHeight());
+ comboBoxStrategy.setEnabled(false);
+ }
+ if (abstractStrategy == null) {
+ return;
+ }
+ abstractStrategy.MakeStep();
+ draw();
+ if (abstractStrategy.GetStatus() == Status.Finish)
+ {
+ comboBoxStrategy.setEnabled(true);
+ abstractStrategy = null;
+ }
+ }
private void buttonMoveClick(ActionEvent event) {
- if (drawingBattleship == null || drawingBattleship.entityBattleship == null)
+ if (drawningShip == null || drawningShip.getEntityShip() == null)
return;
switch (event.getActionCommand()) {
case "left":
- drawingBattleship.moveTransport(DirectionType.LEFT);
+ drawningShip.MoveTransport(DirectionType.LEFT);
break;
case "right":
- drawingBattleship.moveTransport(DirectionType.RIGHT);
+ drawningShip.MoveTransport(DirectionType.RIGHT);
break;
case "up":
- drawingBattleship.moveTransport(DirectionType.UP);
+ drawningShip.MoveTransport(DirectionType.UP);
break;
case "down":
- drawingBattleship.moveTransport(DirectionType.DOWN);
+ drawningShip.MoveTransport(DirectionType.DOWN);
break;
default:
break;
}
draw();
}
-
private void draw() {
- if (drawingBattleship == null) {
+ if (drawningShip == null)
+ {
return;
}
- pictureBox.repaint();
+ pictureBoxBattleship.repaint();
}
-
-}
+}
\ No newline at end of file
diff --git a/src/IDrawBlocks.java b/src/IDrawBlocks.java
new file mode 100644
index 0000000..751113e
--- /dev/null
+++ b/src/IDrawBlocks.java
@@ -0,0 +1,5 @@
+import java.awt.*;
+public interface IDrawBlocks {
+ void setNumber(int x);
+ void drawBlocks(Graphics2D graphics2D, int _startX, int _startY);
+}
diff --git a/src/IMoveableObject.java b/src/IMoveableObject.java
new file mode 100644
index 0000000..6d317c2
--- /dev/null
+++ b/src/IMoveableObject.java
@@ -0,0 +1,6 @@
+public interface IMoveableObject {
+ ObjectParameters GetObjectPosition();
+ int GetStep();
+ boolean CheckCanMove(DirectionType direction);
+ void MoveObject(DirectionType direction);
+}
diff --git a/src/MoveToBorder.java b/src/MoveToBorder.java
new file mode 100644
index 0000000..56d1e74
--- /dev/null
+++ b/src/MoveToBorder.java
@@ -0,0 +1,37 @@
+public class MoveToBorder extends AbstractStrategy {
+
+ @Override
+ protected boolean IsTargetDestinaion() {
+ ObjectParameters objParams = getObjectParameters();
+ if (objParams == null) {
+ return false;
+ }
+ return objParams.RightBorder() <= getFieldWidth() &&
+ objParams.RightBorder() + GetStep() >= getFieldWidth() &&
+ objParams.DownBorder() <= getFieldHeight() &&
+ objParams.DownBorder() + GetStep() >= getFieldHeight();
+ }
+ @Override
+ protected void MoveToTarget() {
+ ObjectParameters objParams = getObjectParameters();
+ if (objParams == null) {
+ return;
+ }
+ double diffX = objParams.RightBorder() - getFieldWidth();
+ if (Math.abs(diffX) > GetStep()) {
+ if (diffX > 0) {
+ MoveLeft();
+ } else {
+ MoveRight();
+ }
+ }
+ double diffY = objParams.DownBorder() - getFieldHeight();
+ if (Math.abs(diffY) > GetStep()) {
+ if (diffY > 0) {
+ MoveUp();
+ } else {
+ MoveDown();
+ }
+ }
+ }
+}
diff --git a/src/MoveToCenter.java b/src/MoveToCenter.java
new file mode 100644
index 0000000..c817b87
--- /dev/null
+++ b/src/MoveToCenter.java
@@ -0,0 +1,36 @@
+public class MoveToCenter extends AbstractStrategy {
+ @Override
+ protected boolean IsTargetDestinaion() {
+ ObjectParameters objParams = getObjectParameters();
+ if (objParams == null) {
+ return false;
+ }
+ return objParams.ObjectMiddleHorizontal() <= getFieldWidth() / 2 &&
+ objParams.ObjectMiddleHorizontal() + GetStep() >= getFieldWidth() / 2 &&
+ objParams.ObjectMiddleVertical() <= getFieldHeight() / 2 &&
+ objParams.ObjectMiddleVertical() + GetStep() >= getFieldHeight() / 2;
+ }
+ @Override
+ protected void MoveToTarget() {
+ ObjectParameters objParams = getObjectParameters();
+ if (objParams == null) {
+ return;
+ }
+ double diffX = objParams.ObjectMiddleHorizontal() - getFieldWidth() / 2;
+ if (Math.abs(diffX) > GetStep()) {
+ if (diffX > 0) {
+ MoveLeft();
+ } else {
+ MoveRight();
+ }
+ }
+ double diffY = objParams.ObjectMiddleVertical() - getFieldHeight() / 2;
+ if (Math.abs(diffY) > GetStep()) {
+ if (diffY > 0) {
+ MoveUp();
+ } else {
+ MoveDown();
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ObjectParameters.java b/src/ObjectParameters.java
new file mode 100644
index 0000000..dc9f2df
--- /dev/null
+++ b/src/ObjectParameters.java
@@ -0,0 +1,18 @@
+public class ObjectParameters {
+ private final int POS_X;
+ private final int POS_Y;
+ private final int WIDTH;
+ private final int HEIGHT;
+ public int LeftBorder() {return POS_X;}
+ public int TopBorder() {return POS_Y;}
+ public int RightBorder() {return POS_X + WIDTH;}
+ public int DownBorder() {return POS_Y + HEIGHT;}
+ public int ObjectMiddleHorizontal() {return POS_X + this.WIDTH / 2;}
+ public int ObjectMiddleVertical() {return POS_Y + this.HEIGHT / 2;}
+ public ObjectParameters(int x, int y, int width, int height) {
+ POS_X = x;
+ POS_Y = y;
+ WIDTH = width;
+ HEIGHT = height;
+ }
+}
diff --git a/src/Status.java b/src/Status.java
new file mode 100644
index 0000000..6467ed7
--- /dev/null
+++ b/src/Status.java
@@ -0,0 +1,5 @@
+public enum Status {
+ NotInit,
+ InProgress,
+ Finish
+}