From 5a10560b04ceed52dd13457b2a6ec9e61e680a51 Mon Sep 17 00:00:00 2001 From: DjonniStorm Date: Tue, 16 Apr 2024 01:53:38 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=962=20(1=20=D1=87=D0=B0=D1=81=D1=82=D1=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/DrawningCleaningCar.java | 140 ------------------ .../src/{ => Drawnings}/CountWheels.java | 2 + .../src/{ => Drawnings}/DirectionType.java | 5 +- .../src/Drawnings/DrawningCleaningCar.java | 62 ++++++++ .../src/Drawnings/DrawningTruck.java | 135 +++++++++++++++++ .../src/{ => Drawnings}/DrawningWheels.java | 4 + .../src/Entities/EntityCleaningCar.java | 23 +++ .../src/Entities/EntityTruck.java | 26 ++++ ProjectCleaningCar/src/EntityCleaningCar.java | 45 ------ ProjectCleaningCar/src/FormCleaningCar.form | 69 +++++++-- ProjectCleaningCar/src/FormCleaningCar.java | 115 +++++++++++--- .../MovementStrategy/AbstractStrategy.java | 55 +++++++ .../MovementStrategy/IMoveableObjects.java | 9 ++ .../src/MovementStrategy/MoveToBorder.java | 37 +++++ .../src/MovementStrategy/MoveToCenter.java | 37 +++++ .../src/MovementStrategy/MoveableTruck.java | 38 +++++ .../MovementStrategy/MovementDirection.java | 8 + .../MovementStrategy/ObjectParameters.java | 25 ++++ .../src/MovementStrategy/StrategyStatus.java | 7 + 19 files changed, 620 insertions(+), 222 deletions(-) delete mode 100644 ProjectCleaningCar/src/DrawningCleaningCar.java rename ProjectCleaningCar/src/{ => Drawnings}/CountWheels.java (91%) rename ProjectCleaningCar/src/{ => Drawnings}/DirectionType.java (57%) create mode 100644 ProjectCleaningCar/src/Drawnings/DrawningCleaningCar.java create mode 100644 ProjectCleaningCar/src/Drawnings/DrawningTruck.java rename ProjectCleaningCar/src/{ => Drawnings}/DrawningWheels.java (95%) create mode 100644 ProjectCleaningCar/src/Entities/EntityCleaningCar.java create mode 100644 ProjectCleaningCar/src/Entities/EntityTruck.java delete mode 100644 ProjectCleaningCar/src/EntityCleaningCar.java create mode 100644 ProjectCleaningCar/src/MovementStrategy/AbstractStrategy.java create mode 100644 ProjectCleaningCar/src/MovementStrategy/IMoveableObjects.java create mode 100644 ProjectCleaningCar/src/MovementStrategy/MoveToBorder.java create mode 100644 ProjectCleaningCar/src/MovementStrategy/MoveToCenter.java create mode 100644 ProjectCleaningCar/src/MovementStrategy/MoveableTruck.java create mode 100644 ProjectCleaningCar/src/MovementStrategy/MovementDirection.java create mode 100644 ProjectCleaningCar/src/MovementStrategy/ObjectParameters.java create mode 100644 ProjectCleaningCar/src/MovementStrategy/StrategyStatus.java diff --git a/ProjectCleaningCar/src/DrawningCleaningCar.java b/ProjectCleaningCar/src/DrawningCleaningCar.java deleted file mode 100644 index 486434c..0000000 --- a/ProjectCleaningCar/src/DrawningCleaningCar.java +++ /dev/null @@ -1,140 +0,0 @@ -import java.awt.*; -import java.util.Random; - -public class DrawningCleaningCar { - private EntityCleaningCar EntityCleaningCar; - public EntityCleaningCar getEntityCleaningCar() { - return EntityCleaningCar; - } - private Integer _pictureWidth; - private Integer _pictureHeight; - private Integer _startPosX; - private Integer _startPosY; - private final Integer _drawningCarWidth = 138; - private final Integer _drawningCarHeight = 68; - public DrawningWheels drawningWheels; - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean tank, boolean sweepingBrush, boolean flashlight) { - EntityCleaningCar = new EntityCleaningCar(); - EntityCleaningCar.Init(speed, weight, bodyColor, additionalColor, tank, sweepingBrush, flashlight); - _pictureWidth = null; - _pictureHeight = null; - _startPosX = null; - _startPosY = null; - - drawningWheels = new DrawningWheels(); - Random random = new Random(); - drawningWheels.setCountWheels(random.nextInt(1, 4)); - } - public boolean SetPictureSize(int width, int height) { - if (width < _drawningCarWidth || height < _drawningCarHeight) {return false;} - _pictureWidth = width; - _pictureHeight = height; - if (_startPosX != null && _startPosY != null) - { - if (_startPosX + _drawningCarWidth > _pictureWidth) - { - _startPosX = -_drawningCarWidth + _pictureWidth; - } - else if (_startPosX < 0) - { - _startPosX = 0; - } - if (_startPosY + _drawningCarHeight > _pictureHeight) - { - _startPosY = -_drawningCarHeight + _pictureHeight; - } - else if (_startPosY < 0) - { - _startPosY = 0; - } - } - return true; - } - public void SetPosition(int x, int y) { - if (_pictureHeight == null || _pictureWidth == null) return; - if (x + _drawningCarWidth > _pictureWidth) { - _startPosX = _pictureWidth - _drawningCarWidth; - } else if (x < 0) { - _startPosX = 0; - } - else { - _startPosX = x; - } - if (y + _drawningCarHeight > _pictureHeight) { - _startPosY = _pictureHeight - _drawningCarHeight; - } else if (y < 0) { - _startPosY = 0; - } else { - _startPosY = y; - } - } - public boolean MoveTransport(DirectionType directionType) { - if (EntityCleaningCar == null || _startPosX == null || _startPosY == null) { - return false; - } - switch (directionType) { - case Left: - if (_startPosX - EntityCleaningCar.Step() > 0) { - _startPosX -= (int) EntityCleaningCar.Step(); - } - return true; - case Right: - if (_startPosX + _drawningCarWidth + EntityCleaningCar.Step() < _pictureWidth) { - _startPosX += (int) EntityCleaningCar.Step(); - } - return true; - case Up: - if (_startPosY - EntityCleaningCar.Step() > 0) { - _startPosY -= (int) EntityCleaningCar.Step(); - } - return true; - case Down: - if (_startPosY + _drawningCarHeight + EntityCleaningCar.Step() < _pictureHeight) { - _startPosY += (int) EntityCleaningCar.Step(); - } - return true; - default: - return false; - } - } - public void DrawTransport(Graphics g) { - if (EntityCleaningCar == null || _startPosX == null || _startPosY == null) { - return; - } - Graphics2D g2d = (Graphics2D) g; - if (EntityCleaningCar.getTank()) { - g2d.setColor(EntityCleaningCar.getAdditionalColor()); - g2d.fillRect(_startPosX, _startPosY, 100, 38); - g2d.setColor(Color.black); - g2d.drawRect(_startPosX, _startPosY, 100, 38); - g2d.setColor(EntityCleaningCar.getBodyColor()); - g2d.fillRect(_startPosX, _startPosY + 20, 100, 10); - } - if (EntityCleaningCar.getSweepingBrush()) { - g2d.setColor(EntityCleaningCar.getAdditionalColor()); - g2d.fillOval(_startPosX + 120, _startPosY + 50, 20, 20); - g2d.setColor(EntityCleaningCar.getBodyColor()); - g2d.fillOval(_startPosX + 125, _startPosY + 55, 10, 10); - g2d.setColor(Color.black); - g2d.drawOval(_startPosX + 120, _startPosY + 50, 19, 19); - g2d.drawOval(_startPosX + 125, _startPosY + 55, 9, 9); - g2d.drawLine(_startPosX + 122, _startPosY + 48, _startPosX + 130, _startPosY + 60); - } - if (EntityCleaningCar.getFlashLight()) { - g2d.setColor(EntityCleaningCar.getAdditionalColor()); - g2d.fillRect(_startPosX + 108, _startPosY + 3, 5, 8); - } - g2d.setColor(EntityCleaningCar.getBodyColor()); - g2d.fillRect(_startPosX, _startPosY + 40, 122, 10); - g2d.fillRect(_startPosX + 102, _startPosY + 10, 20, 30); - - drawningWheels.DrawCleaningCarWheels(g, EntityCleaningCar.getBodyColor(), EntityCleaningCar.getAdditionalColor(), _startPosX, _startPosY); - g2d.setColor(Color.black); - - g2d.drawRect(_startPosX, _startPosY + 40, 122, 10); - g2d.drawRect(_startPosX + 102, _startPosY + 10, 20, 30); - - g2d.fillRect(_startPosX + 112, _startPosY + 15, 10, 12); - } -} diff --git a/ProjectCleaningCar/src/CountWheels.java b/ProjectCleaningCar/src/Drawnings/CountWheels.java similarity index 91% rename from ProjectCleaningCar/src/CountWheels.java rename to ProjectCleaningCar/src/Drawnings/CountWheels.java index 0b4114c..daecfb5 100644 --- a/ProjectCleaningCar/src/CountWheels.java +++ b/ProjectCleaningCar/src/Drawnings/CountWheels.java @@ -1,3 +1,5 @@ +package Drawnings; + public enum CountWheels { One(1), Two(2), diff --git a/ProjectCleaningCar/src/DirectionType.java b/ProjectCleaningCar/src/Drawnings/DirectionType.java similarity index 57% rename from ProjectCleaningCar/src/DirectionType.java rename to ProjectCleaningCar/src/Drawnings/DirectionType.java index 35657f0..7c802aa 100644 --- a/ProjectCleaningCar/src/DirectionType.java +++ b/ProjectCleaningCar/src/Drawnings/DirectionType.java @@ -1,6 +1,9 @@ +package Drawnings; + public enum DirectionType { Up, Down, Left, - Right + Right, + Unknown } diff --git a/ProjectCleaningCar/src/Drawnings/DrawningCleaningCar.java b/ProjectCleaningCar/src/Drawnings/DrawningCleaningCar.java new file mode 100644 index 0000000..10bf5d1 --- /dev/null +++ b/ProjectCleaningCar/src/Drawnings/DrawningCleaningCar.java @@ -0,0 +1,62 @@ +package Drawnings; + +import java.awt.*; +import java.util.Random; + +import Entities.*; + +public class DrawningCleaningCar extends DrawningTruck { + public DrawningWheels drawningWheels; + + public DrawningCleaningCar(int speed, double weight, Color bodyColor, Color additionalColor, boolean tank, boolean sweepingBrush, boolean flashlight) { + super(138,68); + EntityTruck = new EntityCleaningCar(speed, weight, bodyColor, additionalColor, tank, sweepingBrush, flashlight); + + + drawningWheels = new DrawningWheels(); + Random random = new Random(); + drawningWheels.setCountWheels(random.nextInt(1, 4)); + } + + @Override + public void DrawTransport(Graphics g) { + super.DrawTransport(g); + if (EntityTruck == null || !(EntityTruck instanceof EntityCleaningCar cleaningCar) || _startPosX == null || _startPosY == null) { + return; + } + Graphics2D g2d = (Graphics2D) g; + if (cleaningCar.getTank()) { + g2d.setColor(cleaningCar.getAdditionalColor()); + g2d.fillRect(_startPosX, _startPosY, 100, 38); + g2d.setColor(Color.black); + g2d.drawRect(_startPosX, _startPosY, 100, 38); + g2d.setColor(cleaningCar.getBodyColor()); + g2d.fillRect(_startPosX, _startPosY + 20, 100, 10); + } + if (cleaningCar.getSweepingBrush()) { + g2d.setColor(cleaningCar.getAdditionalColor()); + g2d.fillOval(_startPosX + 120, _startPosY + 50, 20, 20); + g2d.setColor(cleaningCar.getBodyColor()); + g2d.fillOval(_startPosX + 125, _startPosY + 55, 10, 10); + g2d.setColor(Color.black); + g2d.drawOval(_startPosX + 120, _startPosY + 50, 19, 19); + g2d.drawOval(_startPosX + 125, _startPosY + 55, 9, 9); + g2d.drawLine(_startPosX + 122, _startPosY + 48, _startPosX + 130, _startPosY + 60); + } + if (cleaningCar.getFlashLight()) { + g2d.setColor(cleaningCar.getAdditionalColor()); + g2d.fillRect(_startPosX + 108, _startPosY + 3, 5, 8); + } +// g2d.setColor(EntityCleaningCar.getBodyColor()); +// g2d.fillRect(_startPosX, _startPosY + 40, 122, 10); +// g2d.fillRect(_startPosX + 102, _startPosY + 10, 20, 30); + + drawningWheels.DrawCleaningCarWheels(g, cleaningCar.getBodyColor(), cleaningCar.getAdditionalColor(), _startPosX, _startPosY); + g2d.setColor(Color.black); +// +// g2d.drawRect(_startPosX, _startPosY + 40, 122, 10); +// g2d.drawRect(_startPosX + 102, _startPosY + 10, 20, 30); +// +// g2d.fillRect(_startPosX + 112, _startPosY + 15, 10, 12); + } +} diff --git a/ProjectCleaningCar/src/Drawnings/DrawningTruck.java b/ProjectCleaningCar/src/Drawnings/DrawningTruck.java new file mode 100644 index 0000000..6731042 --- /dev/null +++ b/ProjectCleaningCar/src/Drawnings/DrawningTruck.java @@ -0,0 +1,135 @@ +package Drawnings; + +import Entities.*; + +import java.awt.*; +import java.util.Random; + +public class DrawningTruck { + protected EntityTruck EntityTruck; + public EntityTruck getEntityTruck() { + return EntityTruck; + } + private Integer _pictureWidth; + private Integer _pictureHeight; + protected Integer _startPosX; + protected Integer _startPosY; + private Integer _drawningCarWidth = 138; + private Integer _drawningCarHeight = 68; + public DrawningWheels drawningWheels; + + public int GetPosX(){return _startPosX;} + public int GetPosY(){return _startPosY;} + public int GetWidth(){return _drawningCarWidth;} + public int GetHeight(){return _drawningCarHeight;} + private DrawningTruck() { + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + } + + public DrawningTruck(int speed, double weight, Color bodyColor) { + this(); + EntityTruck = new EntityTruck(speed, weight, bodyColor); + + drawningWheels = new DrawningWheels(); + Random random = new Random(); + drawningWheels.setCountWheels(random.nextInt(1, 4)); + } + protected DrawningTruck(Integer drawningCarWidth, Integer drawningCarHeight) { + this(); + _drawningCarWidth = drawningCarWidth; + _drawningCarHeight = drawningCarHeight; + } + public boolean SetPictureSize(int width, int height) { + if (width < _drawningCarWidth || height < _drawningCarHeight) {return false;} + _pictureWidth = width; + _pictureHeight = height; + if (_startPosX != null && _startPosY != null) + { + if (_startPosX + _drawningCarWidth > _pictureWidth) + { + _startPosX = -_drawningCarWidth + _pictureWidth; + } + else if (_startPosX < 0) + { + _startPosX = 0; + } + if (_startPosY + _drawningCarHeight > _pictureHeight) + { + _startPosY = -_drawningCarHeight + _pictureHeight; + } + else if (_startPosY < 0) + { + _startPosY = 0; + } + } + return true; + } + public void SetPosition(int x, int y) { + if (_pictureHeight == null || _pictureWidth == null) return; + if (x + _drawningCarWidth > _pictureWidth) { + _startPosX = _pictureWidth - _drawningCarWidth; + } else if (x < 0) { + _startPosX = 0; + } + else { + _startPosX = x; + } + if (y + _drawningCarHeight > _pictureHeight) { + _startPosY = _pictureHeight - _drawningCarHeight; + } else if (y < 0) { + _startPosY = 0; + } else { + _startPosY = y; + } + } + public boolean MoveTransport(DirectionType directionType) { + if (EntityTruck == null || _startPosX == null || _startPosY == null) { + return false; + } + switch (directionType) { + case Left: + if (_startPosX - EntityTruck.Step() > 0) { + _startPosX -= (int) EntityTruck.Step(); + } + return true; + case Right: + if (_startPosX + _drawningCarWidth + EntityTruck.Step() < _pictureWidth) { + _startPosX += (int) EntityTruck.Step(); + } + return true; + case Up: + if (_startPosY - EntityTruck.Step() > 0) { + _startPosY -= (int) EntityTruck.Step(); + } + return true; + case Down: + if (_startPosY + _drawningCarHeight + EntityTruck.Step() < _pictureHeight) { + _startPosY += (int) EntityTruck.Step(); + } + return true; + default: + return false; + } + } + public void DrawTransport(Graphics g) { + if (EntityTruck == null || _startPosX == null || _startPosY == null) { + return; + } + Graphics2D g2d = (Graphics2D) g; + + g2d.setColor(EntityTruck.getBodyColor()); + g2d.fillRect(_startPosX, _startPosY + 40, 122, 10); + g2d.fillRect(_startPosX + 102, _startPosY + 10, 20, 30); + +// drawningWheels.DrawCleaningCarWheels(g, EntityTruck.getBodyColor(), EntityCleaningCar.getAdditionalColor(), _startPosX, _startPosY); + g2d.setColor(Color.black); + + g2d.drawRect(_startPosX, _startPosY + 40, 122, 10); + g2d.drawRect(_startPosX + 102, _startPosY + 10, 20, 30); + + g2d.fillRect(_startPosX + 112, _startPosY + 15, 10, 12); + } +} diff --git a/ProjectCleaningCar/src/DrawningWheels.java b/ProjectCleaningCar/src/Drawnings/DrawningWheels.java similarity index 95% rename from ProjectCleaningCar/src/DrawningWheels.java rename to ProjectCleaningCar/src/Drawnings/DrawningWheels.java index 652b97c..65a09dc 100644 --- a/ProjectCleaningCar/src/DrawningWheels.java +++ b/ProjectCleaningCar/src/Drawnings/DrawningWheels.java @@ -1,3 +1,7 @@ +package Drawnings; + +import Drawnings.CountWheels; + import java.awt.*; public class DrawningWheels { diff --git a/ProjectCleaningCar/src/Entities/EntityCleaningCar.java b/ProjectCleaningCar/src/Entities/EntityCleaningCar.java new file mode 100644 index 0000000..8cfa43f --- /dev/null +++ b/ProjectCleaningCar/src/Entities/EntityCleaningCar.java @@ -0,0 +1,23 @@ +package Entities; +import java.awt.*; + +public class EntityCleaningCar extends EntityTruck { + private Color AdditionalColor; + public Color getAdditionalColor() { + return AdditionalColor; + } + private boolean Tank; + public boolean getTank() {return Tank;} + private boolean SweepingBrush; + public boolean getSweepingBrush() {return SweepingBrush;} + private boolean FlashLight; + public boolean getFlashLight() {return FlashLight;} + public EntityCleaningCar(int speed, double weight, Color bodyColor, Color additionalColor, boolean tank, boolean sweepingBrush, boolean flashLight) { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + Tank = tank; + SweepingBrush = sweepingBrush; + FlashLight = flashLight; + } + +} diff --git a/ProjectCleaningCar/src/Entities/EntityTruck.java b/ProjectCleaningCar/src/Entities/EntityTruck.java new file mode 100644 index 0000000..3bce4f6 --- /dev/null +++ b/ProjectCleaningCar/src/Entities/EntityTruck.java @@ -0,0 +1,26 @@ +package Entities; + +import java.awt.*; + +public class EntityTruck { + private int Speed; + public int getSpeed() { + return Speed; + } + private double Weight; + public double getWeight() { + return Weight; + } + private Color BodyColor; + public Color getBodyColor() { + return BodyColor; + } + public double Step() { + return Speed * 200 / Weight; + } + public EntityTruck(int speed, double weight, Color bodyColor) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectCleaningCar/src/EntityCleaningCar.java b/ProjectCleaningCar/src/EntityCleaningCar.java deleted file mode 100644 index 34e84cf..0000000 --- a/ProjectCleaningCar/src/EntityCleaningCar.java +++ /dev/null @@ -1,45 +0,0 @@ -import java.awt.*; - -public class EntityCleaningCar { - private int Speed; - public int getSpeed() { - return Speed; - } - private double Weight; - public double getWeight() { - return Weight; - } - private Color BodyColor; - public Color getBodyColor() { - return BodyColor; - } - private Color AdditionalColor; - public Color getAdditionalColor() { - return AdditionalColor; - } - private boolean Tank; - public boolean getTank() { - return Tank; - } - private boolean SweepingBrush; - public boolean getSweepingBrush() { - return SweepingBrush; - } - private boolean FlashLight; - public boolean getFlashLight() { - return FlashLight; - } - public double Step() { - return Speed * 200 / Weight; - } - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean tank, boolean sweepingBrush, boolean flashLight) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Tank = tank; - SweepingBrush = sweepingBrush; - FlashLight = flashLight; - } - -} diff --git a/ProjectCleaningCar/src/FormCleaningCar.form b/ProjectCleaningCar/src/FormCleaningCar.form index 55e2f3f..fd4782e 100644 --- a/ProjectCleaningCar/src/FormCleaningCar.form +++ b/ProjectCleaningCar/src/FormCleaningCar.form @@ -1,16 +1,16 @@
- + - + - + @@ -26,36 +26,60 @@ - + - + - + - + - + - - - - + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + - + @@ -66,9 +90,22 @@ + + + + + + + + + + + + + - + diff --git a/ProjectCleaningCar/src/FormCleaningCar.java b/ProjectCleaningCar/src/FormCleaningCar.java index 17e1600..2622dbf 100644 --- a/ProjectCleaningCar/src/FormCleaningCar.java +++ b/ProjectCleaningCar/src/FormCleaningCar.java @@ -1,3 +1,8 @@ +import Drawnings.DirectionType; +import Drawnings.DrawningCleaningCar; +import Drawnings.DrawningTruck; +import MovementStrategy.*; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -7,15 +12,20 @@ import java.util.List; import java.util.LinkedList; public class FormCleaningCar extends JFrame { - protected DrawningCleaningCar _drawningCleaningCar = new DrawningCleaningCar(); + protected DrawningTruck _drawningTruck; JPanel pictureBox; - private JButton buttonCreate; + private JButton buttonCreateCleaningCar; private JButton buttonRight; private JButton buttonLeft; private JButton buttonDown; private JButton buttonUp; + private JButton buttonCreateTruck; + private JComboBox comboBoxStrategy; + private JButton buttonStrategyStep; private List controls; + private AbstractStrategy _strategy; + public FormCleaningCar() { buttonUp.setName("buttonUp"); buttonDown.setName("buttonDown"); @@ -23,19 +33,17 @@ public class FormCleaningCar extends JFrame { buttonRight.setName("buttonRight"); InitializeControlsRepaintList(); - buttonCreate.addActionListener(new ActionListener() { +// _strategy = null; + buttonCreateCleaningCar.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - Random random = new Random(); - _drawningCleaningCar.Init(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)), - random.nextBoolean(), - random.nextBoolean(), - random.nextBoolean()); - _drawningCleaningCar.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight()); - _drawningCleaningCar.SetPosition(random.nextInt(0, 200), random.nextInt(0, 200)); - Draw(); + CreateObject("DrawningTruck"); + } + }); + buttonCreateTruck.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawningCleaningCar"); } }); ActionListener buttonMoveClickedListener = new ActionListener() { @@ -46,19 +54,19 @@ public class FormCleaningCar extends JFrame { switch (buttonName) { case "buttonUp": { - result = _drawningCleaningCar.MoveTransport(DirectionType.Up); + result = _drawningTruck.MoveTransport(DirectionType.Up); } break; case "buttonDown": { - result = _drawningCleaningCar.MoveTransport(DirectionType.Down); + result = _drawningTruck.MoveTransport(DirectionType.Down); } break; case "buttonLeft": { - result = _drawningCleaningCar.MoveTransport(DirectionType.Left); + result = _drawningTruck.MoveTransport(DirectionType.Left); } break; case "buttonRight": { - result = _drawningCleaningCar.MoveTransport(DirectionType.Right); + result = _drawningTruck.MoveTransport(DirectionType.Right); } break; @@ -68,19 +76,81 @@ public class FormCleaningCar extends JFrame { } }; + buttonStrategyStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_drawningTruck == null) { + return; + } + if (comboBoxStrategy.isEnabled()) { + switch (comboBoxStrategy.getSelectedIndex()) { + case 0: + _strategy = new MoveToCenter(); + break; + case 1: + _strategy = new MoveToBorder(); + break; + default: + _strategy = null; + break; + } + if (_strategy == null) { + return; + } + _strategy.SetData(new MoveableTruck(_drawningTruck), pictureBox.getWidth(), pictureBox.getHeight()); + } + if (_strategy == null) { + return; + } + comboBoxStrategy.setEnabled(false); + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) { + comboBoxStrategy.setEnabled(true); + _strategy = null; + } + } + }); buttonRight.addActionListener(buttonMoveClickedListener); buttonDown.addActionListener(buttonMoveClickedListener); buttonLeft.addActionListener(buttonMoveClickedListener); buttonUp.addActionListener(buttonMoveClickedListener); + + + comboBoxStrategy.addItem("К центру"); + comboBoxStrategy.addItem("К краю"); + } + private void CreateObject(String type) { + Random random = new Random(); + switch (type) { + case "DrawningTruck": + _drawningTruck = new DrawningTruck(random.nextInt(100, 300), random.nextInt(100, 300), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); + break; + case "DrawningCleaningCar": + _drawningTruck = new DrawningCleaningCar(random.nextInt(100, 300), random.nextInt(100, 300), + 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(), random.nextBoolean()); + break; + default: return; + } + _drawningTruck.SetPictureSize(pictureBox.getWidth(), pictureBox.getHeight()); + _drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(0, 100)); + + _strategy = null; + comboBoxStrategy.setEnabled(true); + Draw(); } private void Draw() { - if (_drawningCleaningCar.getEntityCleaningCar() == null || + if (_drawningTruck.getEntityTruck() == null || pictureBox.getWidth() == 0 || pictureBox.getHeight() == 0) return; Graphics g = pictureBox.getGraphics(); g.setColor(pictureBox.getBackground()); g.fillRect(0,0, pictureBox.getWidth(), pictureBox.getHeight()); - _drawningCleaningCar.DrawTransport(g); + _drawningTruck.DrawTransport(g); RepaintControls(); @@ -92,10 +162,15 @@ public class FormCleaningCar extends JFrame { } private void InitializeControlsRepaintList() { controls = new LinkedList<>(); - controls.add(buttonCreate); + controls.add(buttonCreateCleaningCar); + controls.add(buttonCreateTruck); + controls.add(buttonUp); controls.add(buttonDown); controls.add(buttonLeft); controls.add(buttonRight); + + controls.add(comboBoxStrategy); + controls.add(buttonStrategyStep); } } \ No newline at end of file diff --git a/ProjectCleaningCar/src/MovementStrategy/AbstractStrategy.java b/ProjectCleaningCar/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..22a2327 --- /dev/null +++ b/ProjectCleaningCar/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,55 @@ +package MovementStrategy; + +public abstract class AbstractStrategy { + private IMoveableObjects _moveableObject; + private StrategyStatus _state = StrategyStatus.NotInit; + protected int FieldWidth; + public int getFieldWidth() {return FieldWidth;} + + protected int FieldHeight; + public int getFieldHeight() {return FieldHeight;} + + public StrategyStatus GetStatus() {return _state;} + + public void SetData(IMoveableObjects moveableObjects, int width, int height) { + if (moveableObjects == null) { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObjects; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() { + if (_state != StrategyStatus.InProgress) { + return; + } + if (IsTargetDestination()) { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + protected boolean MoveLeft() { return MoveTo(MovementDirection.Left); } + protected boolean MoveRight() { return MoveTo(MovementDirection.Right); } + protected boolean MoveUp() { return MoveTo(MovementDirection.Up); } + protected boolean MoveDown() { return MoveTo(MovementDirection.Down); } + protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); } + protected abstract void MoveToTarget(); + protected abstract boolean IsTargetDestination(); + + protected Integer GetStep() { + if (_state != StrategyStatus.InProgress) { + return null; + } + return _moveableObject.GetStep(); + } + private boolean MoveTo(MovementDirection directionType) { + if (_state != StrategyStatus.InProgress) { + return false; + } + return _moveableObject.TryMoveObject(directionType); + } +} diff --git a/ProjectCleaningCar/src/MovementStrategy/IMoveableObjects.java b/ProjectCleaningCar/src/MovementStrategy/IMoveableObjects.java new file mode 100644 index 0000000..1118bb7 --- /dev/null +++ b/ProjectCleaningCar/src/MovementStrategy/IMoveableObjects.java @@ -0,0 +1,9 @@ +package MovementStrategy; + + +public interface IMoveableObjects { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean TryMoveObject(MovementDirection direction); + +} diff --git a/ProjectCleaningCar/src/MovementStrategy/MoveToBorder.java b/ProjectCleaningCar/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..9128c7f --- /dev/null +++ b/ProjectCleaningCar/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,37 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy { + @Override + protected boolean IsTargetDestination() { + ObjectParameters objParameters = GetObjectParameters(); + if (objParameters == null) { + return false; + } + return objParameters.RightBorder() + GetStep() >= FieldWidth + && objParameters.DownBorder() + GetStep() >= FieldHeight; + } + + @Override + protected void MoveToTarget() { + ObjectParameters objParameters = GetObjectParameters(); + if (objParameters == null) { + return; + } + int diffX = objParameters.RightBorder(); + if (Math.abs(diffX) > GetStep()) { + if (diffX < 0) { + MoveLeft(); + } else { + MoveRight(); + } + } + int diffY = objParameters.DownBorder(); + if (Math.abs(diffY) > GetStep()) { + if (diffY < 0) { + MoveUp(); + } else { + MoveDown(); + } + } + } +} diff --git a/ProjectCleaningCar/src/MovementStrategy/MoveToCenter.java b/ProjectCleaningCar/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..dcb5633 --- /dev/null +++ b/ProjectCleaningCar/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,37 @@ +package MovementStrategy; + +public class MoveToCenter extends AbstractStrategy { + @Override + protected boolean IsTargetDestination() { + ObjectParameters objParameters = GetObjectParameters(); + if (objParameters == null) { + return false; + } + return objParameters.ObjectMiddleHorizontal() - GetStep() <= FieldWidth / 2 && objParameters.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 + && objParameters.ObjectMiddleVertical() - GetStep() <= FieldHeight / 2 && objParameters.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2; + } + + @Override + protected void MoveToTarget() { + ObjectParameters objParameters = GetObjectParameters(); + if (objParameters == null) { + return; + } + int diffX = objParameters.ObjectMiddleHorizontal() - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) { + if (diffX > 0) { + MoveLeft(); + } else { + MoveRight(); + } + } + int diffY = objParameters.ObjectMiddleHorizontal() - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) { + if (diffY > 0) { + MoveUp(); + } else { + MoveDown(); + } + } + } +} diff --git a/ProjectCleaningCar/src/MovementStrategy/MoveableTruck.java b/ProjectCleaningCar/src/MovementStrategy/MoveableTruck.java new file mode 100644 index 0000000..dd413d3 --- /dev/null +++ b/ProjectCleaningCar/src/MovementStrategy/MoveableTruck.java @@ -0,0 +1,38 @@ +package MovementStrategy; + +import Drawnings.DirectionType; +import Drawnings.DrawningTruck; + +public class MoveableTruck implements IMoveableObjects { + private DrawningTruck _truck = null; + public MoveableTruck(DrawningTruck drawningTruck) { + _truck = drawningTruck; + } + public ObjectParameters GetObjectPosition() { + if (_truck == null || _truck.getEntityTruck() == null) { + return null; + } + return new ObjectParameters(_truck.GetPosX(), _truck.GetPosY(), _truck.GetWidth(), _truck.GetHeight()); + } + + public int GetStep() {return (int)(_truck.getEntityTruck().Step());} + + public boolean TryMoveObject(MovementDirection direction) { + if (_truck == null || _truck.getEntityTruck() == null) { + return false; + } + return _truck.MoveTransport(GetDirectionType(direction)); + } + + private static DirectionType GetDirectionType(MovementDirection direction) { + switch (direction) { + case MovementDirection.Up -> {return DirectionType.Up;} + case MovementDirection.Right -> {return DirectionType.Right;} + case MovementDirection.Down -> {return DirectionType.Down;} + case MovementDirection.Left -> {return DirectionType.Left;} + default -> { + return DirectionType.Unknown; + } + } + } +} diff --git a/ProjectCleaningCar/src/MovementStrategy/MovementDirection.java b/ProjectCleaningCar/src/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..c52f124 --- /dev/null +++ b/ProjectCleaningCar/src/MovementStrategy/MovementDirection.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public enum MovementDirection { + Up, + Down, + Left, + Right +} diff --git a/ProjectCleaningCar/src/MovementStrategy/ObjectParameters.java b/ProjectCleaningCar/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..2003f34 --- /dev/null +++ b/ProjectCleaningCar/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,25 @@ +package MovementStrategy; + +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; + } + +} diff --git a/ProjectCleaningCar/src/MovementStrategy/StrategyStatus.java b/ProjectCleaningCar/src/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..4f087ea --- /dev/null +++ b/ProjectCleaningCar/src/MovementStrategy/StrategyStatus.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum StrategyStatus { + NotInit, + InProgress, + Finish +}