From 67ad38f49af01607fde668c25cee40c00096512f Mon Sep 17 00:00:00 2001 From: GokaPek <109132407+GokaPek@users.noreply.github.com> Date: Wed, 8 Nov 2023 20:01:23 +0400 Subject: [PATCH] i dont know --- .../DirectionType.java | 1 + .../DrawningObjects/DrawningRollers.java | 8 +- .../DrawningObjects/DrawningSPAU.java | 132 +++++++++++---- .../EntitySPAU.java | 1 + .../Form.java | 6 +- .../MovementStrategy/AbstractStrategy.java | 151 ++++++++++++++++++ .../MovementStrategy/DrawninObjectSPAU.java | 45 ++++++ .../NumbeRollers.java | 2 +- .../SelfPropelledArtilleryUnit.java | 1 + 9 files changed, 312 insertions(+), 35 deletions(-) rename DirectionType.java => SelfPropelledArtilleryUnit/DirectionType.java (86%) rename DrawingRollers.java => SelfPropelledArtilleryUnit/DrawningObjects/DrawningRollers.java (87%) rename DrawingSPAU.java => SelfPropelledArtilleryUnit/DrawningObjects/DrawningSPAU.java (50%) rename EntitySPAU.java => SelfPropelledArtilleryUnit/EntitySPAU.java (94%) rename Form.java => SelfPropelledArtilleryUnit/Form.java (97%) create mode 100644 SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.java create mode 100644 SelfPropelledArtilleryUnit/MovementStrategy/DrawninObjectSPAU.java rename NumbeRollers.java => SelfPropelledArtilleryUnit/NumbeRollers.java (94%) rename SelfPropelledArtilleryUnit.java => SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.java (88%) diff --git a/DirectionType.java b/SelfPropelledArtilleryUnit/DirectionType.java similarity index 86% rename from DirectionType.java rename to SelfPropelledArtilleryUnit/DirectionType.java index 8a36b1c..807e28a 100644 --- a/DirectionType.java +++ b/SelfPropelledArtilleryUnit/DirectionType.java @@ -1,3 +1,4 @@ +package SelfPropelledArtilleryUnit; public enum DirectionType { Up(1), diff --git a/DrawingRollers.java b/SelfPropelledArtilleryUnit/DrawningObjects/DrawningRollers.java similarity index 87% rename from DrawingRollers.java rename to SelfPropelledArtilleryUnit/DrawningObjects/DrawningRollers.java index 3be2998..f9c39f6 100644 --- a/DrawingRollers.java +++ b/SelfPropelledArtilleryUnit/DrawningObjects/DrawningRollers.java @@ -1,8 +1,12 @@ +package SelfPropelledArtilleryUnit.DrawningObjects; +import SelfPropelledArtilleryUnit.NumbeRollers; +import SelfPropelledArtilleryUnit.SelfPropelledArtilleryUnit.Entities; + import java.awt.Color; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; -public class DrawingRollers { +public class DrawningRollers { Graphics2D g2d; int startPosX; int startPosY; @@ -18,7 +22,7 @@ public class DrawingRollers { _numbeRollers = NumbeRollers.fromNumberToEnum(numRollers); } - public DrawingRollers(Graphics2D g2d, int startPosX, int startPosY, Color color, int numRollers){ + public DrawningRollers(Graphics2D g2d, int startPosX, int startPosY, Color color, int numRollers){ this.color = color; this.g2d = g2d; this.startPosX = startPosX; diff --git a/DrawingSPAU.java b/SelfPropelledArtilleryUnit/DrawningObjects/DrawningSPAU.java similarity index 50% rename from DrawingSPAU.java rename to SelfPropelledArtilleryUnit/DrawningObjects/DrawningSPAU.java index ae82ea5..37cdbcc 100644 --- a/DrawingSPAU.java +++ b/SelfPropelledArtilleryUnit/DrawningObjects/DrawningSPAU.java @@ -1,36 +1,59 @@ +package SelfPropelledArtilleryUnit.DrawningObjects; import java.awt.*; import java.awt.geom.Line2D; import java.awt.geom.Path2D; -public class DrawingSPAU { +import SelfPropelledArtilleryUnit.DirectionType; +import SelfPropelledArtilleryUnit.EntitySPAU; +import SelfPropelledArtilleryUnit.SelfPropelledArtilleryUnit.Entities; + + +public class DrawningSPAU { private EntitySPAU EntitySPAU; private int pictureWidth; private int pictureHeight; - private int startPosX; - private int startPosY; + protected int startPosX; + protected int startPosY; private int _numbeRollers; private final int carWidth = 135; private final int carHeight = 75; - public void setEntitySPAU(EntitySPAU entitySPAU) { + protected void setEntitySPAU(EntitySPAU entitySPAU) { this.EntitySPAU = entitySPAU; } - public boolean Init(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height) + + public DrawningSPAU(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, int width, int height) { this._numbeRollers = _numbeRollers; this.pictureWidth = width; this.pictureHeight = height; if (this.carHeight >= height) { - return false; + return; } if (this.carWidth >= width) { - return false; + return; } - EntitySPAU = new EntitySPAU(); - EntitySPAU.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); - return true; + EntitySPAU = new EntitySPAU(_numbeRollers, speed, weight, bodyColor, additionalColor, bodyKit); + } + + public DrawningSPAU(int _numbeRollers, int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, int width, int height, int carWidth, int carHeight) + { + this._numbeRollers = _numbeRollers; + this.pictureWidth = width; + this.pictureHeight = height; + this.carWidth = carWidth; + this.carHeight = carHeight; + if (this.carHeight >= height) + { + return; + } + if (this.carWidth >= width) + { + return; + } + EntitySPAU = new EntitySPAU(_numbeRollers, speed, weight, bodyColor, additionalColor, bodyKit); } public void SetPosition(int x, int y) @@ -48,7 +71,7 @@ public class DrawingSPAU { } public void MoveTransport(DirectionType direction) { - if (EntitySPAU == null) + if (!CanMove(direction) || EntitySPAU == null) { return; } @@ -56,31 +79,19 @@ public class DrawingSPAU { { //влево case Left: - if (this.startPosX - EntitySPAU.Step > 0) - { - this.startPosX -= (int)EntitySPAU.Step; - } + this.startPosX -= (int)EntitySPAU.Step; break; //вверх case Up: - if (this.startPosY - EntitySPAU.Step > 0) - { - this.startPosY -= (int)EntitySPAU.Step; - } + this.startPosY -= (int)EntitySPAU.Step; break; // вправо case Right: - if (this.startPosX + EntitySPAU.Step < this.pictureWidth - this.carWidth) - { - this.startPosX += (int)EntitySPAU.Step; - } + this.startPosX += (int)EntitySPAU.Step; break; //вниз case Down: - if (this.startPosY + EntitySPAU.Step < this.pictureHeight - this.carHeight) - { - this.startPosY += (int)EntitySPAU.Step; - } + this.startPosY += (int)EntitySPAU.Step; break; } } @@ -93,13 +104,13 @@ public class DrawingSPAU { Graphics2D g2d = (Graphics2D) g; BasicStroke penBlack = new BasicStroke(1); - Color additionalColor = EntitySPAU.AdditionalColor; + //Color additionalColor = EntitySPAU.AdditionalColor; Color bodyColor = EntitySPAU.BodyColor; boolean hasBodyKit = EntitySPAU.BodyKit; Path2D.Double path = new Path2D.Double(); - // Обвесы + /* Обвесы if (hasBodyKit) { path.moveTo(startPosX + 15, startPosY + 20); path.lineTo(startPosX + 35, startPosY + 20); @@ -118,9 +129,10 @@ public class DrawingSPAU { Line2D line = new Line2D.Double(startPosX + 5, startPosY + 20, startPosX + 15, startPosY + 25); g2d.draw(line); } + */ //гусеницы - DrawingRollers Rollers = new DrawingRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers); + DrawningRollers Rollers = new DrawningRollers(g2d, startPosX, startPosY, bodyColor, _numbeRollers); Rollers.Draw(); // пушка @@ -158,4 +170,64 @@ public class DrawingSPAU { g2d.fill(path); g2d.draw(path); } + + /** + * Координата X объекта + * + * @return значение координаты X + */ + public int getPosX() { + return this.startPosX; + } + + /** + * Координата Y объекта + * + * @return значение координаты Y + */ + public int getPosY() { + return this.startPosY; + } + + /** + * Ширина объекта + * + * @return значение ширины + */ + public int getWidth() { + return this.carWidth; + } + + /** + * Высота объекта + * + * @return значение высоты + */ + public int getHeight() { + return this.carHeight; + } + + /** + * Проверка, что объект может переместиться в указанном направлении + * + * @param direction Направление + * @return true - можно переместиться в указанном направлении + */ + public boolean canMove(DirectionType direction) { + if (EntitySPAU == null) { + return false; + } + switch (direction) { + case Left: // влево + return startPosX - EntitySPAU.getStep() > 0; + case Up: // вверх + return startPosY - EntitySPAU.getStep() > 0; + case Right: // вправо + return startPosX + EntitySPAU.getStep() < _pictureWidth - _carWidth; + case Down: // вниз + return startPosY + EntitySPAU.getStep() < _pictureHeight - _carHeight; + default: + return false; + } + } } diff --git a/EntitySPAU.java b/SelfPropelledArtilleryUnit/EntitySPAU.java similarity index 94% rename from EntitySPAU.java rename to SelfPropelledArtilleryUnit/EntitySPAU.java index 9dc1ee5..6b72dbc 100644 --- a/EntitySPAU.java +++ b/SelfPropelledArtilleryUnit/EntitySPAU.java @@ -1,3 +1,4 @@ +package SelfPropelledArtilleryUnit; import java.awt.Color; public class EntitySPAU { diff --git a/Form.java b/SelfPropelledArtilleryUnit/Form.java similarity index 97% rename from Form.java rename to SelfPropelledArtilleryUnit/Form.java index f2ab465..1cffc72 100644 --- a/Form.java +++ b/SelfPropelledArtilleryUnit/Form.java @@ -1,4 +1,6 @@ +package SelfPropelledArtilleryUnit; import javax.swing.*; + import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -14,7 +16,7 @@ public class Form extends JFrame { private JButton buttonLeft; private JButton buttonRight; private JLabel pictureBoxSPAU; - private DrawingSPAU drawingSPAU; + private DrawningSPAU drawingSPAU; private JTextField wheelsTextField; private JLabel wheelsLabel; @@ -125,7 +127,7 @@ public class Form extends JFrame { if (wheelCount > 0) { Random random = new Random(); - drawingSPAU = new DrawingSPAU(); + drawingSPAU = new DrawningSPAU(); drawingSPAU.Init(wheelCount, random.nextInt(200) + 100, random.nextInt(2000) + 1000, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), diff --git a/SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.java b/SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..b6af674 --- /dev/null +++ b/SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,151 @@ +package SelfPropelledArtilleryUnit.MovementStrategy; + +/** + * Класс-стратегия перемещения объекта + */ +public abstract class AbstractStrategy +{ + /** + * Перемещаемый объект + */ + private IMoveableObject moveableObject; + + /** + * Статус перемещения + */ + private Status state = Status.NotInit; + + /** + * Ширина поля + */ + private int fieldWidth; + + /** + * Высота поля + */ + private int fieldHeight; + + /** + * Статус перемещения + */ + public Status getStatus() { return state; } + + /** + * Получить ширину поля + */ + protected int getFieldWidth() { return fieldWidth; } + + /** + * Получить высоту поля + */ + protected int getFieldHeight() { return fieldHeight; } + + /** + * Установка данных + * @param moveableObject Перемещаемый объект + * @param width Ширина поля + * @param height Высота поля + */ + public void setData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + state = Status.NotInit; + return; + } + state = Status.InProgress; + this.moveableObject = moveableObject; + this.fieldWidth = width; + this.fieldHeight = height; + } + + /** + * Шаг перемещения + */ + public void makeStep() + { + if (state != Status.InProgress) + { + return; + } + if (isTargetDestination()) + { + state = Status.Finish; + return; + } + moveToTarget(); + } + + /** + * Перемещение влево + * @return Результат перемещения (true - удалось переместиться, false - неудача) + */ + protected boolean moveLeft() { return moveTo(DirectionType.Left); } + + /** + * Перемещение вправо + * @return Результат перемещения (true - удалось переместиться, false - неудача) + */ + protected boolean moveRight() { return moveTo(DirectionType.Right); } + + /** + * Перемещение вверх + * @return Результат перемещения (true - удалось переместиться, false - неудача) + */ + protected boolean moveUp() { return moveTo(DirectionType.Up); } + + /** + * Перемещение вниз + * @return Результат перемещения (true - удалось переместиться, false - неудача) + */ + protected boolean moveDown() { return moveTo(DirectionType.Down); } + + /** + * Параметры объекта + * @return Объект ObjectParameters + */ + protected ObjectParameters getObjectParameters() { return moveableObject != null ? moveableObject.getObjectPosition() : null; } + + /** + * Шаг объекта + * @return Шаг объекта + */ + protected Integer getStep() + { + if (state != Status.InProgress) + { + return null; + } + return moveableObject != null ? moveableObject.getStep() : null; + } + + /** + * Перемещение к цели + */ + protected abstract void moveToTarget(); + + /** + * Достигнута ли цель + * @return true если цель достигнута + */ + protected abstract boolean isTargetDestination(); + + /** + * Попытка перемещения в требуемом направлении + * @param directionType Направление + * @return Результат попытки (true - удалось переместиться, false - неудача) + */ + private boolean moveTo(DirectionType directionType) + { + if (state != Status.InProgress) + { + return false; + } + if (moveableObject != null && moveableObject.checkCanMove(directionType)) + { + moveableObject.moveObject(directionType); + return true; + } + return false; + } +} diff --git a/SelfPropelledArtilleryUnit/MovementStrategy/DrawninObjectSPAU.java b/SelfPropelledArtilleryUnit/MovementStrategy/DrawninObjectSPAU.java new file mode 100644 index 0000000..988b5bb --- /dev/null +++ b/SelfPropelledArtilleryUnit/MovementStrategy/DrawninObjectSPAU.java @@ -0,0 +1,45 @@ +package SelfPropelledArtilleryUnit.MovementStrategy; + +import SelfPropelledArtilleryUnit.DirectionType; +import SelfPropelledArtilleryUnit.DrawningObjects.DrawningSPAU; + +/** + * Класс-стратегия перемещения объекта, реализация интерфейса IMoveableObject для работы с объектом DrawningCar (паттерн Adapter) + */ +public class DrawningObjectSPAU implements IMoveableObject { + + private final DrawningSPAU drawningCar; + + public DrawningObjectSPAU(DrawningSPAU drawningCar) { + this.drawningCar = drawningCar; + } + + @Override + public ObjectParameters getObjectPosition() { + if (drawningCar == null || drawningCar.getEntitySPAU() == null) { + return null; + } + return new ObjectParameters(drawningCar.getPosX(), + drawningCar.getPosY(), + drawningCar.getWidth(), + drawningCar.getHeight()); + } + + @Override + public int getStep() { + return drawningCar != null && drawningCar.getEntitySPAU() != null + ? (int) drawningCar.getEntitySPAU().getStep() : 0; + } + + @Override + public boolean checkCanMove(DirectionType direction) { + return drawningCar != null && drawningCar.canMove(direction); + } + + @Override + public void moveObject(DirectionType direction) { + if (drawningCar != null) { + drawningCar.moveTransport(direction); + } + } +} \ No newline at end of file diff --git a/NumbeRollers.java b/SelfPropelledArtilleryUnit/NumbeRollers.java similarity index 94% rename from NumbeRollers.java rename to SelfPropelledArtilleryUnit/NumbeRollers.java index a6bf697..5e6f542 100644 --- a/NumbeRollers.java +++ b/SelfPropelledArtilleryUnit/NumbeRollers.java @@ -1,4 +1,4 @@ - +package SelfPropelledArtilleryUnit; public enum NumbeRollers { Min(4), Mid(5), diff --git a/SelfPropelledArtilleryUnit.java b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.java similarity index 88% rename from SelfPropelledArtilleryUnit.java rename to SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.java index 059f215..7ab91ca 100644 --- a/SelfPropelledArtilleryUnit.java +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.java @@ -1,3 +1,4 @@ +package SelfPropelledArtilleryUnit; import javax.swing.*; public class SelfPropelledArtilleryUnit {