From b664cc97fff5bde71cf1dd8c392bc2ae0035b387 Mon Sep 17 00:00:00 2001 From: Factorino73 Date: Tue, 24 Oct 2023 21:15:37 +0400 Subject: [PATCH] PIbd-21_MasenkinMS_LabWork02 --- AccordionBus/AbstractStrategy.java | 86 +++++++++ AccordionBus/AccordionBusForm.form | 43 ++++- AccordionBus/AccordionBusForm.java | 85 +++++++-- AccordionBus/DrawingAccordionBus.java | 143 ++------------- AccordionBus/DrawingBus.java | 221 ++++++++++++++++++++++++ AccordionBus/DrawingDoors.java | 1 + AccordionBus/DrawingDoorsTypeOne.java | 42 +++++ AccordionBus/DrawingDoorsTypeThree.java | 51 ++++++ AccordionBus/DrawingDoorsTypeTwo.java | 53 ++++++ AccordionBus/DrawingObjectBus.java | 23 +++ AccordionBus/EntityAccordionBus.java | 35 +--- AccordionBus/EntityBus.java | 34 ++++ AccordionBus/IDrawingDoors.java | 10 ++ AccordionBus/IMoveableObject.java | 16 ++ AccordionBus/Main.java | 2 +- AccordionBus/MainFrameAccordionBus.java | 21 +++ AccordionBus/MoveToBorder.java | 33 ++++ AccordionBus/MoveToCenter.java | 43 +++++ AccordionBus/ObjectParameters.java | 35 ++++ AccordionBus/Status.java | 10 ++ 20 files changed, 817 insertions(+), 170 deletions(-) create mode 100644 AccordionBus/AbstractStrategy.java create mode 100644 AccordionBus/DrawingBus.java create mode 100644 AccordionBus/DrawingDoorsTypeOne.java create mode 100644 AccordionBus/DrawingDoorsTypeThree.java create mode 100644 AccordionBus/DrawingDoorsTypeTwo.java create mode 100644 AccordionBus/DrawingObjectBus.java create mode 100644 AccordionBus/EntityBus.java create mode 100644 AccordionBus/IDrawingDoors.java create mode 100644 AccordionBus/IMoveableObject.java create mode 100644 AccordionBus/MainFrameAccordionBus.java create mode 100644 AccordionBus/MoveToBorder.java create mode 100644 AccordionBus/MoveToCenter.java create mode 100644 AccordionBus/ObjectParameters.java create mode 100644 AccordionBus/Status.java diff --git a/AccordionBus/AbstractStrategy.java b/AccordionBus/AbstractStrategy.java new file mode 100644 index 0000000..2300cdb --- /dev/null +++ b/AccordionBus/AbstractStrategy.java @@ -0,0 +1,86 @@ +package AccordionBus; + +// Класс-стратегия перемещения объекта +public abstract class AbstractStrategy { + // Перемещаемый объект + private IMoveableObject _moveableObject; + + // Статус перемещения + private Status _state = Status.NotInit; + + // Ширина поля + protected int FieldWidth; + + // Высота поля + protected int 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 (IsTargetDestination()) { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + + // Перемещение влево + protected boolean MoveLeft() { return MoveTo(DirectionType.Left); } + + // Перемещение вправо + protected boolean MoveRight() { return MoveTo(DirectionType.Right); } + + // Перемещение вверх + protected boolean MoveUp() { return MoveTo(DirectionType.Up); } + + // Перемещение вниз + 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 IsTargetDestination(); + + // Попытка перемещения в требуемом направлении + private boolean MoveTo(DirectionType directionType) { + if (_state != Status.InProgress) { + return false; + } + if (_moveableObject.CheckCanMove(directionType)) { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } +} diff --git a/AccordionBus/AccordionBusForm.form b/AccordionBus/AccordionBusForm.form index 56443d0..4cbce88 100644 --- a/AccordionBus/AccordionBusForm.form +++ b/AccordionBus/AccordionBusForm.form @@ -3,12 +3,12 @@ - + - + @@ -24,6 +24,14 @@ + + + + + + + + @@ -31,7 +39,7 @@ - + @@ -85,6 +93,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/AccordionBus/AccordionBusForm.java b/AccordionBus/AccordionBusForm.java index e2f60cd..60d8f66 100644 --- a/AccordionBus/AccordionBusForm.java +++ b/AccordionBus/AccordionBusForm.java @@ -6,19 +6,25 @@ import java.awt.event.ActionListener; import java.util.Random; public class AccordionBusForm { - private DrawingAccordionBus drawingAccordionBus; + private DrawingBus drawingBus; + private AbstractStrategy abstractStrategy; + private JPanel pictureBox; public JPanel getPictureBox() { return pictureBox; } private JPanel createPanel; private JButton buttonCreateAccordionBus; - private JButton buttonCreate; + private JPanel movePanel; private JButton buttonLeft; private JButton buttonDown; private JButton buttonRight; private JButton buttonUp; - private JPanel MovePanel; + private JPanel strategyPanel; + private JPanel controlPanel; + private JButton buttonCreateBus; + private JComboBox comboBoxStrategy; + private JButton buttonStep; public AccordionBusForm() { buttonUp.setName("buttonUp"); @@ -26,10 +32,12 @@ public class AccordionBusForm { buttonLeft.setName("buttonLeft"); buttonRight.setName("buttonRight"); + comboBoxStrategy.addItem("MoveToCenter"); + comboBoxStrategy.addItem("MoveToBorder"); + buttonCreateAccordionBus.addActionListener(e -> { Random random = new Random(); - drawingAccordionBus = new DrawingAccordionBus(); - drawingAccordionBus.Init(random.nextInt(100, 300), + drawingBus = new DrawingAccordionBus(random.nextInt(100, 300), random.nextInt(1000, 3000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), @@ -37,8 +45,19 @@ public class AccordionBusForm { random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight()); - drawingAccordionBus.SetNumDoors(random.nextInt(3, 6)); - drawingAccordionBus.SetPosition(random.nextInt(10, 100), + drawingBus.SetPosition(random.nextInt(10, 100), + random.nextInt(10, 100)); + Draw(); + }); + + buttonCreateBus.addActionListener(e -> { + Random random = new Random(); + drawingBus = new DrawingBus(random.nextInt(100, 300), + random.nextInt(1000, 3000), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + pictureBox.getWidth(), + pictureBox.getHeight()); + drawingBus.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); Draw(); }); @@ -47,21 +66,61 @@ public class AccordionBusForm { String name = ((JButton)e.getSource()).getName(); switch (name) { case "buttonUp" -> { - drawingAccordionBus.MoveTransport(DirectionType.Up); + drawingBus.MoveTransport(DirectionType.Up); } case "buttonDown" -> { - drawingAccordionBus.MoveTransport(DirectionType.Down); + drawingBus.MoveTransport(DirectionType.Down); } case "buttonLeft" -> { - drawingAccordionBus.MoveTransport(DirectionType.Left); + drawingBus.MoveTransport(DirectionType.Left); } case "buttonRight" -> { - drawingAccordionBus.MoveTransport(DirectionType.Right); + drawingBus.MoveTransport(DirectionType.Right); } } Draw(); }; + buttonStep.addActionListener(e -> { + if (drawingBus == 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 DrawingObjectBus(drawingBus), + pictureBox.getWidth(), + pictureBox.getHeight()); + comboBoxStrategy.setEditable(false); + } + + if (abstractStrategy == null) { + return; + } + + abstractStrategy.MakeStep(); + Draw(); + if (abstractStrategy.GetStatus() == Status.Finish) { + comboBoxStrategy.setEnabled(true); + abstractStrategy = null; + } + }); + buttonUp.addActionListener(buttonMoveClickedListener); buttonDown.addActionListener(buttonMoveClickedListener); buttonLeft.addActionListener(buttonMoveClickedListener); @@ -69,12 +128,12 @@ public class AccordionBusForm { } public void Draw() { - if (drawingAccordionBus.AccordionBus == null) { + if (drawingBus == null) { return; } Graphics g = pictureBox.getGraphics(); pictureBox.paint(g); - drawingAccordionBus.DrawTransport(g); + drawingBus.DrawTransport(g); } } diff --git a/AccordionBus/DrawingAccordionBus.java b/AccordionBus/DrawingAccordionBus.java index 0f9d010..98cbea8 100644 --- a/AccordionBus/DrawingAccordionBus.java +++ b/AccordionBus/DrawingAccordionBus.java @@ -1,145 +1,38 @@ package AccordionBus; import java.awt.*; +import java.awt.event.*; +import javax.swing.*; import java.util.Random; -// Класс, отвечающий за прорисовку объекта -public class DrawingAccordionBus { - // Класс-сущность - public EntityAccordionBus AccordionBus; - - // Прорисовка дверей - public DrawingDoors drawingDoors; - - // Ширина окна - private int _pictureWidth; - - // Высота окна - private int _pictureHeight; - - // Левая координата прорисовки автобуса - private int _startPosX; - - // Верхняя координата прорисовки автобуса - private int _startPosY; - - // Ширина прорисовки автобуса - private int _busWidth = 100; - - // Высота прорисовки автобуса - private final int _busHeight = 50; - - // Инициализация свойста - public boolean Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean additionalBody, boolean additionalDoor, int width, int height) { - // Проверка на вместимость объекта в размеры картинки - if ((_busWidth >= width) || (_busHeight >= height)) { - return false; - } - - _pictureWidth = width; - _pictureHeight = height; - if (additionalBody) { - _busWidth = 215; - } - AccordionBus = new EntityAccordionBus(); - AccordionBus.Init(speed, weight, - bodyColor, additionalColor, - additionalBody, additionalDoor); - drawingDoors = new DrawingDoors(); - - return true; - } - - // Количество дополнительных дверей - public void SetNumDoors(int numDoors) { - drawingDoors.setNumDoors(numDoors); - } - - // Установка позиции - public void SetPosition(int x, int y) { - if (x < 0 || y < 0) { - _startPosX = 0; - _startPosY = 0; - } - else if ((x + _busWidth > _pictureWidth) || (y + _busHeight > _pictureHeight)) { - _startPosX = _pictureWidth - _busWidth; - _startPosY = _pictureHeight - _busHeight; - } - else { - _startPosX = x; - _startPosY = y; - } - } - - // Изменение направления движения - public void MoveTransport(DirectionType direction) { - if (AccordionBus == null) { - return; - } - - switch (direction) { - case Left: - if (_startPosX - AccordionBus.Step > 0) { - _startPosX -= (int)AccordionBus.Step; - } - break; - case Up: - if (_startPosY - AccordionBus.Step > 0) { - _startPosY -= (int)AccordionBus.Step; - } - break; - case Right: - if (_startPosX + AccordionBus.Step < _pictureWidth - _busWidth) { - _startPosX += (int)AccordionBus.Step; - } - break; - case Down: - if (_startPosY + AccordionBus.Step < _pictureHeight - _busHeight) { - _startPosY += (int)AccordionBus.Step; - } - break; +// Класс, отвечающий за прорисовку и перемещение объекта-сущности +public class DrawingAccordionBus extends DrawingBus { + // Конструктор + public DrawingAccordionBus(int speed, double weight, Color bodyColor, Color additionalColor, boolean additionalBody, boolean additionalDoor, int width, int height) { + super(speed, weight, bodyColor, width, height, 215, 50); + if (EntityBus != null) { + EntityBus = new EntityAccordionBus(speed, weight, bodyColor, additionalColor, additionalBody, additionalDoor); } } // Прорисовка объекта public void DrawTransport(Graphics g) { - if (AccordionBus == null) { + if (EntityBus == null) { return; } Graphics2D g2d = (Graphics2D)g; - Color bodyColor = AccordionBus.getBodyColor(); - Color additionalColor = AccordionBus.getAdditionalColor(); + super.DrawTransport(g2d); + + Color bodyColor = EntityBus.getBodyColor(); + Color additionalColor = ((EntityAccordionBus)EntityBus).getAdditionalColor(); Color blackColor = Color.BLACK; Color whiteColor = Color.WHITE; - // Граница главного отсека - g2d.setColor(blackColor); - g2d.setStroke(new BasicStroke(3)); - g2d.drawRect(_startPosX, _startPosY, 100, 40); - - // Кузов главного отсека - g2d.setColor(bodyColor); - g2d.fillRect(_startPosX + 1, _startPosY + 1, 99, 39); - - // Окна главного отсека - g2d.setColor(Color.CYAN); - g2d.fillRect(_startPosX, _startPosY + 5, 100, 15); - g2d.setColor(blackColor); - g2d.setStroke(new BasicStroke(2)); - g2d.drawLine(_startPosX, _startPosY + 5, _startPosX + 100, _startPosY + 5); - g2d.drawLine(_startPosX, _startPosY + 20, _startPosX + 100, _startPosY + 20); - - // Дверь главного отсека - g2d.setColor(blackColor); - g2d.setStroke(new BasicStroke(2)); - g2d.drawRect(_startPosX + 10, _startPosY + 10, 15, 30); - g2d.setColor(whiteColor); - g2d.fillRect(_startPosX + 11, _startPosY + 11, 14, 29); // Дополнительный отсек - if (AccordionBus.isAdditionalBody()) { + if (((EntityAccordionBus)EntityBus).isAdditionalBody()) { // Граница дополнительного отсека g2d.setColor(blackColor); g2d.setStroke(new BasicStroke(3)); @@ -158,7 +51,7 @@ public class DrawingAccordionBus { g2d.drawLine(_startPosX + 115, _startPosY + 20, _startPosX + 215, _startPosY + 20); // Дверь дополнительного отсека - if (AccordionBus.isAdditionalDoor()) { + if (((EntityAccordionBus)EntityBus).isAdditionalDoor()) { g2d.setColor(blackColor); g2d.setStroke(new BasicStroke(2)); g2d.drawRect(_startPosX + 175, _startPosY + 10, 15, 30); @@ -175,7 +68,7 @@ public class DrawingAccordionBus { } // Дополнительные двери - drawingDoors.DrawDoors(g2d, AccordionBus, additionalColor, _startPosX, _startPosY); + drawingDoors.DrawDoors(g2d, additionalColor, ((EntityAccordionBus) EntityBus).isAdditionalBody(), _startPosX, _startPosY); // Колеса главного отсека g2d.setColor(blackColor); @@ -187,7 +80,7 @@ public class DrawingAccordionBus { g2d.fillOval(_startPosX + 70, _startPosY + 33, 15, 15); // Колеса дополнительного отсека - if (AccordionBus.isAdditionalBody()) { + if (((EntityAccordionBus)EntityBus).isAdditionalBody()) { g2d.setColor(blackColor); g2d.drawOval(_startPosX + 130, _startPosY + 33, 15, 15); g2d.drawOval(_startPosX + 185, _startPosY + 33, 15, 15); diff --git a/AccordionBus/DrawingBus.java b/AccordionBus/DrawingBus.java new file mode 100644 index 0000000..9f4809e --- /dev/null +++ b/AccordionBus/DrawingBus.java @@ -0,0 +1,221 @@ +package AccordionBus; + +import java.awt.*; +import java.awt.event.*; +import java.util.Random; +import javax.swing.*; + +// Класс, отвечающий за прорисовку и перемещение объекта-сущности +public class DrawingBus { + // Класс-сущность + public EntityBus EntityBus; + + // Прорисовка дверей + public IDrawingDoors drawingDoors; + + // Ширина окна + private int _pictureWidth; + + // Высота окна + private int _pictureHeight; + + // Левая координата прорисовки автобуса + protected int _startPosX; + + // Координата X объекта + public int GetPosX() { return _startPosX; } + + // Верхняя координата прорисовки автобуса + protected int _startPosY; + + // Координата Y объекта + public int GetPosY() { return _startPosY; } + + // Ширина прорисовки автобуса + private int _busWidth = 100; + + // Ширина объекта + public int GetWidth() { return _busWidth; } + + // Высота прорисовки автобуса + private int _busHeight = 50; + + // Высота объекта + public int GetHeigth() { return _busHeight; } + + // Проверка, что объект может перемещаться по указанному направлению + public boolean CanMove(DirectionType direction) { + if (EntityBus == null) { + return false; + } + + switch (direction) { + // Влево + case Left: + return _startPosX - EntityBus.Step() > 0; + // Вверх + case Up: + return _startPosY - EntityBus.Step() > 0; + // Вправо + case Right: + return _startPosX + EntityBus.Step() < _pictureWidth - _busWidth; + // Вниз + case Down: + return _startPosY + EntityBus.Step() < _pictureHeight - _busHeight; + default: + return false; + } + } + + // Конструктор + public DrawingBus(int speed, int weight, Color bodyColor, int width, int height) { + // Проверка на вместимость объекта в размеры картинки + if ((_busWidth >= width) || (_busHeight >= height)) { + return; + } + + _pictureWidth = width; + _pictureHeight = height; + EntityBus = new EntityBus(speed, weight, bodyColor); + + Random random = new Random(); + switch (random.nextInt(0, 3)) { + case 0: + drawingDoors = new DrawingDoorsTypeOne(); + break; + case 1: + drawingDoors = new DrawingDoorsTypeTwo(); + break; + case 2: + drawingDoors = new DrawingDoorsTypeThree(); + break; + default: + drawingDoors = new DrawingDoorsTypeOne(); + break; + } + drawingDoors.setNumDoors(random.nextInt(3, 6)); + } + + // Конструктор + protected DrawingBus(int speed, double weight, Color bodyColor, int width, int height, int busWidth, int busHeight) { + // Проверка на вместимость объекта в размеры картинки + if ((_busWidth >= width) || (_busHeight >= height)) { + return; + } + + _pictureWidth = width; + _pictureHeight = height; + _busWidth = busWidth; + _busHeight = busHeight; + EntityBus = new EntityBus(speed, weight, bodyColor); + + Random random = new Random(); + switch (random.nextInt(0, 3)) { + case 0: + drawingDoors = new DrawingDoorsTypeOne(); + break; + case 1: + drawingDoors = new DrawingDoorsTypeTwo(); + break; + case 2: + drawingDoors = new DrawingDoorsTypeThree(); + break; + default: + drawingDoors = new DrawingDoorsTypeOne(); + break; + } + drawingDoors.setNumDoors(random.nextInt(3, 6)); + } + + // Установка позиции + public void SetPosition(int x, int y) { + if (x < 0 || y < 0) { + x = 0; + y = 0; + } + else if ((x + _busWidth > _pictureWidth) || (y + _busHeight > _pictureHeight)) { + _startPosX = _pictureWidth - _busWidth; + _startPosY = _pictureHeight - _busHeight; + } + else { + _startPosX = x; + _startPosY = y; + } + } + + // Изменение направления перемещения + public void MoveTransport(DirectionType direction) { + if (!CanMove(direction) || EntityBus == null) { + return; + } + + switch (direction) { + // Влево + case Left: + _startPosX -= (int)EntityBus.Step(); + break; + // Вверх + case Up: + _startPosY -= (int)EntityBus.Step(); + break; + // Вправо + case Right: + _startPosX += (int)EntityBus.Step(); + break; + // Вниз + case Down: + _startPosY += (int)EntityBus.Step(); + break; + } + } + + // Прорисовка объекта + public void DrawTransport(Graphics g) { + if (EntityBus == null) + { + return; + } + + Graphics2D g2d = (Graphics2D)g; + + Color bodyColor = EntityBus.getBodyColor(); + Color blackColor = Color.BLACK; + Color whiteColor = Color.WHITE; + + // Граница главного отсека + g2d.setColor(blackColor); + g2d.setStroke(new BasicStroke(3)); + g2d.drawRect(_startPosX, _startPosY, 100, 40); + + // Кузов главного отсека + g2d.setColor(bodyColor); + g2d.fillRect(_startPosX + 1, _startPosY + 1, 99, 39); + + // Окна главного отсека + g2d.setColor(Color.CYAN); + g2d.fillRect(_startPosX, _startPosY + 5, 100, 15); + g2d.setColor(blackColor); + g2d.setStroke(new BasicStroke(2)); + g2d.drawLine(_startPosX, _startPosY + 5, _startPosX + 100, _startPosY + 5); + g2d.drawLine(_startPosX, _startPosY + 20, _startPosX + 100, _startPosY + 20); + + // Дверь главного отсека + g2d.setColor(blackColor); + g2d.setStroke(new BasicStroke(2)); + g2d.drawRect(_startPosX + 10, _startPosY + 10, 15, 30); + g2d.setColor(whiteColor); + g2d.fillRect(_startPosX + 11, _startPosY + 11, 14, 29); + + // Дополнительные двери + drawingDoors.DrawDoors(g2d, bodyColor, false, _startPosX, _startPosY); + + // Колеса главного отсека + g2d.setColor(blackColor); + g2d.setStroke(new BasicStroke(3)); + g2d.drawOval(_startPosX + 15, _startPosY + 33, 15, 15); + g2d.drawOval(_startPosX + 70, _startPosY + 33, 15, 15); + g2d.setColor(whiteColor); + g2d.fillOval(_startPosX + 15, _startPosY + 33, 15, 15); + g2d.fillOval(_startPosX + 70, _startPosY + 33, 15, 15); + } +} diff --git a/AccordionBus/DrawingDoors.java b/AccordionBus/DrawingDoors.java index 837b78e..e0cd396 100644 --- a/AccordionBus/DrawingDoors.java +++ b/AccordionBus/DrawingDoors.java @@ -2,6 +2,7 @@ package AccordionBus; import java.awt.*; +// Класс, отвечающий за прорисовку дополнительных дверей public class DrawingDoors { private NumDoors numDoors; diff --git a/AccordionBus/DrawingDoorsTypeOne.java b/AccordionBus/DrawingDoorsTypeOne.java new file mode 100644 index 0000000..894733d --- /dev/null +++ b/AccordionBus/DrawingDoorsTypeOne.java @@ -0,0 +1,42 @@ +package AccordionBus; + +import java.awt.*; + +// Класс, отвечающий за первый тип орнамента отрисовки дополнительных дверей +public class DrawingDoorsTypeOne implements IDrawingDoors { + private NumDoors numDoors; + + public void setNumDoors(int num) { + for (NumDoors temp : numDoors.values()) { + if (temp.getNumDoors() == num) { + numDoors = temp; + return; + } + } + } + + public void DrawDoors(Graphics2D g2d, Color color, boolean isAdditionalBody, int _startPosX, int _startPosY) { + int value = numDoors.getNumDoors(); + if (value >= 3) { + g2d.setColor(Color.BLACK); + g2d.setStroke(new BasicStroke(2)); + g2d.drawRect(_startPosX + 40, _startPosY + 10, 15, 30); + g2d.setColor(color); + g2d.fillRect(_startPosX + 41, _startPosY + 11, 14, 29); + } + if (value >= 4) { + g2d.setColor(Color.BLACK); + g2d.setStroke(new BasicStroke(2)); + g2d.drawRect(_startPosX + 75, _startPosY + 10, 15, 30); + g2d.setColor(color); + g2d.fillRect(_startPosX + 76, _startPosY + 11, 14, 29); + } + if (value >= 5 && isAdditionalBody) { + g2d.setColor(Color.BLACK); + g2d.setStroke(new BasicStroke(2)); + g2d.drawRect(_startPosX + 145, _startPosY + 10, 15, 30); + g2d.setColor(color); + g2d.fillRect(_startPosX + 146, _startPosY + 11, 14, 29); + } + } +} diff --git a/AccordionBus/DrawingDoorsTypeThree.java b/AccordionBus/DrawingDoorsTypeThree.java new file mode 100644 index 0000000..e28610a --- /dev/null +++ b/AccordionBus/DrawingDoorsTypeThree.java @@ -0,0 +1,51 @@ +package AccordionBus; + +import java.awt.*; + +// Класс, отвечающий за третий тип орнамента отрисовки дополнительных дверей +public class DrawingDoorsTypeThree implements IDrawingDoors { + private NumDoors numDoors; + + public void setNumDoors(int num) { + for (NumDoors temp : numDoors.values()) { + if (temp.getNumDoors() == num) { + numDoors = temp; + return; + } + } + } + + public void DrawDoors(Graphics2D g2d, Color color, boolean isAdditionalBody, int _startPosX, int _startPosY) { + int value = numDoors.getNumDoors(); + if (value >= 3) { + g2d.setStroke(new BasicStroke(2)); + Polygon firstDoor = new Polygon(new int[] { _startPosX + 45, _startPosX + 50, _startPosX + 55, _startPosX + 50, _startPosX + 55, _startPosX + 50, _startPosX + 55, _startPosX + 50, _startPosX + 45, _startPosX + 40, _startPosX + 45, _startPosX + 40, _startPosX + 45,_startPosX + 40 }, + new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 15, _startPosY + 20, _startPosY + 25, _startPosY + 30, _startPosY + 35, _startPosY + 40, _startPosY + 40, _startPosY + 35, _startPosY + 30, _startPosY + 25, _startPosY + 20, _startPosY + 15 }, + 14); + g2d.setColor(color); + g2d.fillPolygon(firstDoor); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(firstDoor); + } + if (value >= 4) { + g2d.setStroke(new BasicStroke(2)); + Polygon secondDoor = new Polygon(new int[] { _startPosX + 80, _startPosX + 85, _startPosX + 90, _startPosX + 85, _startPosX + 90, _startPosX + 85, _startPosX + 90, _startPosX + 85, _startPosX + 80, _startPosX + 75, _startPosX + 80, _startPosX + 75, _startPosX + 80, _startPosX + 75 }, + new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 15, _startPosY + 20, _startPosY + 25, _startPosY + 30, _startPosY + 35, _startPosY + 40, _startPosY + 40, _startPosY + 35, _startPosY + 30, _startPosY + 25, _startPosY + 20, _startPosY + 15 }, + 14); + g2d.setColor(color); + g2d.fillPolygon(secondDoor); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(secondDoor); + } + if (value >= 5 && isAdditionalBody) { + g2d.setStroke(new BasicStroke(2)); + Polygon thirdDoor = new Polygon(new int[] { _startPosX + 150, _startPosX + 155, _startPosX + 160, _startPosX + 155, _startPosX + 160, _startPosX + 155, _startPosX + 160, _startPosX + 155, _startPosX + 150, _startPosX + 145, _startPosX + 150, _startPosX + 145, _startPosX + 150, _startPosX + 145 }, + new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 15, _startPosY + 20, _startPosY + 25, _startPosY + 30, _startPosY + 35, _startPosY + 40, _startPosY + 40, _startPosY + 35, _startPosY + 30, _startPosY + 25, _startPosY + 20, _startPosY + 15 }, + 14); + g2d.setColor(color); + g2d.fillPolygon(thirdDoor); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(thirdDoor); + } + } +} diff --git a/AccordionBus/DrawingDoorsTypeTwo.java b/AccordionBus/DrawingDoorsTypeTwo.java new file mode 100644 index 0000000..c5a1ae8 --- /dev/null +++ b/AccordionBus/DrawingDoorsTypeTwo.java @@ -0,0 +1,53 @@ +package AccordionBus; + +import java.awt.*; + +// Класс, отвечающий за второй тип орнамента отрисовки дополнительных дверей +public class DrawingDoorsTypeTwo implements IDrawingDoors { + private NumDoors numDoors; + + public void setNumDoors(int num) { + for (NumDoors temp : numDoors.values()) { + if (temp.getNumDoors() == num) { + numDoors = temp; + return; + } + } + } + + public void DrawDoors(Graphics2D g2d, Color color, boolean isAdditionalBody, int _startPosX, int _startPosY) { + int value = numDoors.getNumDoors(); + if (value >= 3) { + g2d.setStroke(new BasicStroke(2)); + Polygon firstDoor = new Polygon(new int[] { _startPosX + 45, _startPosX + 50, _startPosX + 55, _startPosX + 55, _startPosX + 50, _startPosX + 45, _startPosX + 40, _startPosX + 40 }, + new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 20, _startPosY + 30, _startPosY + 40, _startPosY + 40, _startPosY + 30, _startPosY + 20 }, + 8); + g2d.setColor(color); + g2d.fillPolygon(firstDoor); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(firstDoor); + } + if (value >= 4) { + g2d.setColor(Color.BLACK); + g2d.setStroke(new BasicStroke(2)); + Polygon secondDoor = new Polygon(new int[] { _startPosX + 80, _startPosX + 85, _startPosX + 90, _startPosX + 90, _startPosX + 85, _startPosX + 80, _startPosX + 75, _startPosX + 75 }, + new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 20, _startPosY + 30, _startPosY + 40, _startPosY + 40, _startPosY + 30, _startPosY + 20 }, + 8); + g2d.setColor(color); + g2d.fillPolygon(secondDoor); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(secondDoor); + } + if (value >= 5 && isAdditionalBody) { + g2d.setColor(Color.BLACK); + g2d.setStroke(new BasicStroke(2)); + Polygon thirdDoor = new Polygon(new int[] { _startPosX + 150, _startPosX + 155, _startPosX + 160, _startPosX + 160, _startPosX + 155, _startPosX + 150, _startPosX + 145, _startPosX + 145 }, + new int[] { _startPosY + 10, _startPosY + 10, _startPosY + 20, _startPosY + 30, _startPosY + 40, _startPosY + 40, _startPosY + 30, _startPosY + 20 }, + 8); + g2d.setColor(color); + g2d.fillPolygon(thirdDoor); + g2d.setColor(Color.BLACK); + g2d.drawPolygon(thirdDoor); + } + } +} diff --git a/AccordionBus/DrawingObjectBus.java b/AccordionBus/DrawingObjectBus.java new file mode 100644 index 0000000..9f10503 --- /dev/null +++ b/AccordionBus/DrawingObjectBus.java @@ -0,0 +1,23 @@ +package AccordionBus; + +// Реализация интерфейса IMoveableObject для работы с объектом DrawingBus (паттерн Adapter) +public class DrawingObjectBus implements IMoveableObject { + private DrawingBus _drawingBus = null; + + public DrawingObjectBus(DrawingBus drawingBus) { + _drawingBus = drawingBus; + } + + public ObjectParameters GetObjectPosition() { + if (_drawingBus == null || _drawingBus.EntityBus == null) { + return null; + } + return new ObjectParameters(_drawingBus.GetPosX(), _drawingBus.GetPosY(), _drawingBus.GetWidth(), _drawingBus.GetHeigth()); + } + + public int GetStep() { return (int)(_drawingBus.EntityBus.Step()); } + + public boolean CheckCanMove(DirectionType direction) { return _drawingBus.CanMove(direction); } + + public void MoveObject(DirectionType direction) { _drawingBus.MoveTransport(direction); } +} diff --git a/AccordionBus/EntityAccordionBus.java b/AccordionBus/EntityAccordionBus.java index c58b201..a006f86 100644 --- a/AccordionBus/EntityAccordionBus.java +++ b/AccordionBus/EntityAccordionBus.java @@ -1,24 +1,11 @@ package AccordionBus; import java.awt.*; +import java.awt.event.*; +import javax.swing.*; -public class EntityAccordionBus { - // Скорость - private int Speed; - public int getSpeed() { return Speed; } - - // Вес - private float Weight; - public float getWeight() { - return Weight; - } - - // Основной цвет - private Color BodyColor; - public Color getBodyColor() { - return BodyColor; - } - +// Класс-сущность "Автобус-гармошка" +public class EntityAccordionBus extends EntityBus { // Дополнительный цвет private Color AdditionalColor;; public Color getAdditionalColor() { @@ -37,19 +24,11 @@ public class EntityAccordionBus { return AdditionalDoor; } - // Шаг перемещения автобуса - public double Step; - - // Инициализация полей - public void Init(int speed, float weight, - Color bodyColor, Color additionalColor, - boolean additionalBody, boolean additionalDoor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; + // Конструктор с параметрами + public EntityAccordionBus(int speed, double weight, Color bodyColor, Color additionalColor, boolean additionalBody, boolean additionalDoor) { + super(speed, weight, bodyColor); AdditionalColor = additionalColor; AdditionalBody = additionalBody; AdditionalDoor = additionalDoor; - Step = (double)Speed * 100 / Weight; } } diff --git a/AccordionBus/EntityBus.java b/AccordionBus/EntityBus.java new file mode 100644 index 0000000..4565373 --- /dev/null +++ b/AccordionBus/EntityBus.java @@ -0,0 +1,34 @@ +package AccordionBus; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; + +// Класс-сущность "Автобус" +public class EntityBus { + // Скорость + public int Speed; + public int getSpeed() { return Speed; } + + // Вес + public double Weight; + public double getWeight() { + return Weight; + } + + // Основной цвет + public Color BodyColor; + public Color getBodyColor() { + return BodyColor; + } + + // Шаг перемещения автобуса + public double Step() { return (double)Speed * 100 / Weight; } + + // Конструктор с параметрами + public EntityBus(int speed, double weight, Color bodyColor) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/AccordionBus/IDrawingDoors.java b/AccordionBus/IDrawingDoors.java new file mode 100644 index 0000000..6ea94d9 --- /dev/null +++ b/AccordionBus/IDrawingDoors.java @@ -0,0 +1,10 @@ +package AccordionBus; + +import java.awt.*; + +// Интерфейс для отрисовки дополнительных дверей +public interface IDrawingDoors { + public void setNumDoors(int num); + + public void DrawDoors(Graphics2D g2d, Color additionalColor, boolean isAdditionalBody, int _startPosX, int _startPosY); +} diff --git a/AccordionBus/IMoveableObject.java b/AccordionBus/IMoveableObject.java new file mode 100644 index 0000000..a46e9b3 --- /dev/null +++ b/AccordionBus/IMoveableObject.java @@ -0,0 +1,16 @@ +package AccordionBus; + +// Интерфейс для работы с перемещаемым объектом +public interface IMoveableObject { + // Получение координаты X объекта + ObjectParameters GetObjectPosition(); + + // Шаг объекта + int GetStep(); + + // Проверка, можно ли переместиться по нужному направлению + boolean CheckCanMove(DirectionType direction); + + // Изменение направления перемещения объекта + void MoveObject(DirectionType direction); +} diff --git a/AccordionBus/Main.java b/AccordionBus/Main.java index 95f5f8e..ab56656 100644 --- a/AccordionBus/Main.java +++ b/AccordionBus/Main.java @@ -2,6 +2,6 @@ package AccordionBus; public class Main { public static void main(String[] args) { - new FrameAccordionBus(); + new MainFrameAccordionBus(); } } \ No newline at end of file diff --git a/AccordionBus/MainFrameAccordionBus.java b/AccordionBus/MainFrameAccordionBus.java new file mode 100644 index 0000000..e396ad8 --- /dev/null +++ b/AccordionBus/MainFrameAccordionBus.java @@ -0,0 +1,21 @@ +package AccordionBus; + +import javax.swing.*; +import java.awt.*; + +public class MainFrameAccordionBus extends JFrame { + private AccordionBusForm accordionBusForm; + + public MainFrameAccordionBus() { + super(); + setTitle("AccordionBus"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + accordionBusForm = new AccordionBusForm(); + setContentPane(accordionBusForm.getPictureBox()); + setDefaultLookAndFeelDecorated(false); + setPreferredSize(new Dimension(900, 500)); + setLocation(500, 500); + pack(); + setVisible(true); + } +} diff --git a/AccordionBus/MoveToBorder.java b/AccordionBus/MoveToBorder.java new file mode 100644 index 0000000..ca21e87 --- /dev/null +++ b/AccordionBus/MoveToBorder.java @@ -0,0 +1,33 @@ +package AccordionBus; + +// Стратегия перемещения объекта в правый нижний угол экрана +public class MoveToBorder extends AbstractStrategy { + protected boolean IsTargetDestination() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + + return objParams.RightBorder() <= FieldWidth && + objParams.RightBorder() + GetStep() >= FieldWidth && + objParams.DownBorder() <= FieldHeight && + objParams.DownBorder() + GetStep() >= FieldHeight; + } + + protected void MoveToTarget() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + + var diffX = objParams.RightBorder() - FieldWidth; + if (Math.abs(diffX) > GetStep()) { + MoveRight(); + } + + var diffY = objParams.ObjectMiddleVertical() - FieldWidth; + if (Math.abs(diffY) > GetStep()) { + MoveDown(); + } + } +} diff --git a/AccordionBus/MoveToCenter.java b/AccordionBus/MoveToCenter.java new file mode 100644 index 0000000..e713ecd --- /dev/null +++ b/AccordionBus/MoveToCenter.java @@ -0,0 +1,43 @@ +package AccordionBus; + +// Стратегия перемещения объекта в центр экрана +public class MoveToCenter extends AbstractStrategy { + protected boolean IsTargetDestination() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + + return objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical() <= FieldHeight / 2 && + objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2; + } + + protected void MoveToTarget() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + + var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) { + if (diffX > 0) { + MoveLeft(); + } + else { + MoveRight(); + } + } + + var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) { + if (diffY > 0) { + MoveUp(); + } + else { + MoveDown(); + } + } + } +} diff --git a/AccordionBus/ObjectParameters.java b/AccordionBus/ObjectParameters.java new file mode 100644 index 0000000..ef1f43d --- /dev/null +++ b/AccordionBus/ObjectParameters.java @@ -0,0 +1,35 @@ +package AccordionBus; + +// Параметры-координаты рбъекта +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private int _height; + + // Левая граница + public int LeftBorder() { return _x; } + + // Верхняя граница + public int TopBorder() { return _y; } + + // Правая граница + public int RightBorder() { return _x + _width; } + + // Нижняя граница + public int DownBorder() { return _y + _height; } + + // Середина объекта + public int ObjectMiddleHorizontal() { return _x + _width / 2; } + + // Середина объекта + public int ObjectMiddleVertical() { return _y + _height / 2; } + + // Конструктор + public ObjectParameters(int x, int y, int width, int height) { + _x = x; + _y = y; + _width = width; + _height = height; + } +} \ No newline at end of file diff --git a/AccordionBus/Status.java b/AccordionBus/Status.java new file mode 100644 index 0000000..6788acb --- /dev/null +++ b/AccordionBus/Status.java @@ -0,0 +1,10 @@ +package AccordionBus; + +// Статус выполнения операции перемещения +public enum Status { + NotInit, + + InProgress, + + Finish +}