From 352e557d378d898d73d00b24bb202f7723ef1d32 Mon Sep 17 00:00:00 2001 From: BoiledMilk123 Date: Tue, 7 May 2024 03:39:52 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=20LabWork02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/vcs.xml | 2 +- .../src/AdditionalClass.java | 58 ----- .../src/AdditionalType.java | 7 - .../src/CountWheels.java | 19 ++ .../src/DirectionType.java | 20 +- .../src/DrawningElectricLocomotive.java | 233 ------------------ .../Drawnings/DrawingElectricLocomotive.java | 83 +++++++ .../src/Drawnings/DrawingField.java | 111 +++++++++ .../src/Drawnings/DrawingLocomotive.java | 170 +++++++++++++ .../src/Drawnings/DrawingOrnamentWheels.java | 17 ++ .../src/Drawnings/DrawingWheels.java | 43 ++++ .../src/Drawnings/IDrawingOrnament.java | 9 + .../Entities/EntityElectricLocomotive.java | 47 ++++ .../src/Entities/EntityLocomotive.java | 36 +++ .../src/EntityElectricLocomotive.java | 117 --------- .../src/FormElectricLocomotive.form | 62 ----- .../src/FormElectricLocomotive.java | 231 +++++++++-------- .../MovementStrategy/AbstractStrategy.java | 74 ++++++ .../src/MovementStrategy/IMoveableObject.java | 8 + .../src/MovementStrategy/MoveToBorder.java | 36 +++ .../src/MovementStrategy/MoveToCenter.java | 37 +++ .../MovementStrategy/MoveableLocomotive.java | 47 ++++ .../MovementStrategy/MovementDirection.java | 18 ++ .../MovementStrategy/ObjectParameters.java | 39 +++ .../src/MovementStrategy/StrategyStatus.java | 10 + 25 files changed, 953 insertions(+), 581 deletions(-) delete mode 100644 ProjectElectricLocomotive/src/AdditionalClass.java delete mode 100644 ProjectElectricLocomotive/src/AdditionalType.java create mode 100644 ProjectElectricLocomotive/src/CountWheels.java delete mode 100644 ProjectElectricLocomotive/src/DrawningElectricLocomotive.java create mode 100644 ProjectElectricLocomotive/src/Drawnings/DrawingElectricLocomotive.java create mode 100644 ProjectElectricLocomotive/src/Drawnings/DrawingField.java create mode 100644 ProjectElectricLocomotive/src/Drawnings/DrawingLocomotive.java create mode 100644 ProjectElectricLocomotive/src/Drawnings/DrawingOrnamentWheels.java create mode 100644 ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java create mode 100644 ProjectElectricLocomotive/src/Drawnings/IDrawingOrnament.java create mode 100644 ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java create mode 100644 ProjectElectricLocomotive/src/Entities/EntityLocomotive.java delete mode 100644 ProjectElectricLocomotive/src/EntityElectricLocomotive.java delete mode 100644 ProjectElectricLocomotive/src/FormElectricLocomotive.form create mode 100644 ProjectElectricLocomotive/src/MovementStrategy/AbstractStrategy.java create mode 100644 ProjectElectricLocomotive/src/MovementStrategy/IMoveableObject.java create mode 100644 ProjectElectricLocomotive/src/MovementStrategy/MoveToBorder.java create mode 100644 ProjectElectricLocomotive/src/MovementStrategy/MoveToCenter.java create mode 100644 ProjectElectricLocomotive/src/MovementStrategy/MoveableLocomotive.java create mode 100644 ProjectElectricLocomotive/src/MovementStrategy/MovementDirection.java create mode 100644 ProjectElectricLocomotive/src/MovementStrategy/ObjectParameters.java create mode 100644 ProjectElectricLocomotive/src/MovementStrategy/StrategyStatus.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 35eb1dd..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/ProjectElectricLocomotive/src/AdditionalClass.java b/ProjectElectricLocomotive/src/AdditionalClass.java deleted file mode 100644 index 401f894..0000000 --- a/ProjectElectricLocomotive/src/AdditionalClass.java +++ /dev/null @@ -1,58 +0,0 @@ -package ProjectElectricLocomotive.src; -import java.awt.*; - - -public class AdditionalClass { - private AdditionalType _additionalType; - - public AdditionalType getAdditionalType() - { - return _additionalType; - } - public void setAdditonalType(int value) - { - switch (value) - { - case 1: - _additionalType = AdditionalType.ONE; - break; - case 2: - _additionalType = AdditionalType.TWO; - break; - case 3: - _additionalType = AdditionalType.THREE; - break; - case 4: - _additionalType = AdditionalType.FOURTH; - break; - default: - _additionalType = AdditionalType.ONE; - break; - } - } - - public void drawingWheels(Graphics g, Double startPosX, Double startPosY, Color wheelsColor) - { - Graphics2D g2d = (Graphics2D) g; - - - g.setColor(wheelsColor); - - switch (_additionalType) - { - case FOURTH: - g.fillOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 10, 10); - g.drawOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 8, 8); - case THREE: - g.fillOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 10, 10); - g.drawOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 8, 8); - case TWO: - g.fillOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 10, 10); - g.drawOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 8, 8); - case ONE: - g.fillOval(startPosX.intValue() + 9, startPosY.intValue() + 37, 10, 10); - g.drawOval(startPosX.intValue() + 9, startPosY.intValue() + 37, 8, 8); - break; - } - } -} diff --git a/ProjectElectricLocomotive/src/AdditionalType.java b/ProjectElectricLocomotive/src/AdditionalType.java deleted file mode 100644 index b91f626..0000000 --- a/ProjectElectricLocomotive/src/AdditionalType.java +++ /dev/null @@ -1,7 +0,0 @@ -package ProjectElectricLocomotive.src; -public enum AdditionalType { - ONE, - TWO, - THREE, - FOURTH; -} diff --git a/ProjectElectricLocomotive/src/CountWheels.java b/ProjectElectricLocomotive/src/CountWheels.java new file mode 100644 index 0000000..cf6da0e --- /dev/null +++ b/ProjectElectricLocomotive/src/CountWheels.java @@ -0,0 +1,19 @@ +package ProjectElectricLocomotive.src; + +public enum CountWheels +{ + One(1), + Two(2), + Three(3), + Fourth(4); + private final int Value; + + CountWheels(int value) { + Value = value; + } + + public int GetCountWheels() { + return Value; + } +} + diff --git a/ProjectElectricLocomotive/src/DirectionType.java b/ProjectElectricLocomotive/src/DirectionType.java index 71fe1dd..d470a15 100644 --- a/ProjectElectricLocomotive/src/DirectionType.java +++ b/ProjectElectricLocomotive/src/DirectionType.java @@ -1,11 +1,19 @@ package ProjectElectricLocomotive.src; -public enum DirectionType { - Up, +public enum DirectionType +{ + Unknown(-1), + Up(1), + Down(2), + Left(3), + Right(4); + private final int Value; - Down, + DirectionType(int value) { + Value = value; + } - Left, - - Right, + public int getValue() { + return Value; + } } diff --git a/ProjectElectricLocomotive/src/DrawningElectricLocomotive.java b/ProjectElectricLocomotive/src/DrawningElectricLocomotive.java deleted file mode 100644 index b2a3e4e..0000000 --- a/ProjectElectricLocomotive/src/DrawningElectricLocomotive.java +++ /dev/null @@ -1,233 +0,0 @@ -package ProjectElectricLocomotive.src; - -import javax.swing.*; -import java.awt.*; -import java.awt.image.BufferedImage; - -public class DrawningElectricLocomotive { - - public EntityElectricLocomotive EntityElectricLocomotive; - - private Double pictureWidth; - private Double pictureHeight; - private Double startPosX; - private Double startPosY; - - - - public AdditionalClass _additionalClass; - - private final int drawningElectricLocomotiveWidth = 83; - private final int drawningElectricLocomotiveHeight = 45; - - public void init(int speed, double weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement, int wheelsCount) { - EntityElectricLocomotive = new EntityElectricLocomotive(); - _additionalClass = new AdditionalClass(); - _additionalClass.setAdditonalType(wheelsCount); - EntityElectricLocomotive.init(speed, weight, bodyColor, additionalColor, electricHorns, batteryPlacement); - pictureWidth = null; - pictureHeight = null; - startPosX = null; - startPosY = null; - } - - public boolean setPictureSize(int width, int height) { - if (drawningElectricLocomotiveHeight > height || drawningElectricLocomotiveWidth > width) { - return false; - } - pictureWidth = (double)width; - pictureHeight = (double)height; - if (startPosY != null && startPosY + drawningElectricLocomotiveHeight < pictureHeight) { - startPosY = pictureHeight - drawningElectricLocomotiveHeight; - } - if (startPosX != null && startPosX + drawningElectricLocomotiveWidth < pictureWidth) { - startPosX = pictureWidth - drawningElectricLocomotiveWidth; - } - return true; - } - - public void setPosition(int x, int y) { - if (pictureHeight == null || pictureWidth == null) { - return; - } - - startPosX = (double)x; - startPosY = (double)y; - - if (drawningElectricLocomotiveHeight + y > pictureHeight) { - startPosY = pictureHeight - drawningElectricLocomotiveHeight; - } - if (drawningElectricLocomotiveWidth + x > pictureWidth) { - startPosX = pictureWidth - drawningElectricLocomotiveWidth; - } - if (x < 0) { - startPosX = (double)0; - } - if (y < 0) { - startPosY = (double)0; - } - } - - public boolean moveTransport(DirectionType direction) { - if (EntityElectricLocomotive == null || startPosX == null || startPosY == null) { - return false; - } - - switch (direction) { - case Left: - if (startPosX - EntityElectricLocomotive.getStep() > 0) { - startPosX -= EntityElectricLocomotive.getStep(); - } - return true; - case Up: - if (startPosY - EntityElectricLocomotive.getStep() > 0) { - startPosY -= EntityElectricLocomotive.getStep(); - } - return true; - case Right: - if (startPosX + drawningElectricLocomotiveWidth + EntityElectricLocomotive.getStep() < pictureWidth) { - startPosX += EntityElectricLocomotive.getStep(); - } - return true; - case Down: - if (startPosY + drawningElectricLocomotiveHeight + EntityElectricLocomotive.getStep() < pictureHeight) { - startPosY += EntityElectricLocomotive.getStep(); - } - return true; - default: - return false; - } - } - - public Image getImage() { - // Создаем новое изображение с заданными размерами - BufferedImage image = new BufferedImage(pictureWidth.intValue(), pictureHeight.intValue(), BufferedImage.TYPE_INT_ARGB); - Graphics2D g2d = image.createGraphics(); - - // Вызываем метод прорисовки нашего объекта DrawningElectricLocomotive - drawTransport(g2d); - - _additionalClass.drawingWheels(g2d, startPosX, startPosY, EntityElectricLocomotive.getAdditionalColor()); - - // Освобождаем ресурсы графического контекста - g2d.dispose(); - - return image; - } - - public void drawTransport(Graphics g) { - if (EntityElectricLocomotive == null || startPosX == null || startPosY == null) { - return; - } - - // Создание перьев и кистей для прорисовки электровоза - Color blackColor = Color.BLACK; - Color deepSkyBlueColor = new Color(0, 191, 255); - Color yellowColor = Color.YELLOW; - - Graphics2D g2d = (Graphics2D) g; - - // Инициализация опорных точек для прорисовки корпуса - int pointStartX = startPosX.intValue() + 75; - int pointStartY = startPosY.intValue() + 20; - - Point pointStart = new Point(pointStartX, pointStartY); - Point point1 = new Point(pointStartX, pointStartY - 15); - Point point2 = new Point(startPosX.intValue() + 10, pointStartY - 15); - Point pointFinish = new Point(startPosX.intValue() + 5, pointStartY); - - // Инициализация опорных точек для прорисовки первой "юбки" - Point point3 = new Point(startPosX.intValue() + 7, startPosY.intValue() + 37); - Point point4 = new Point(startPosX.intValue(), startPosY.intValue() + 43); - Point point5 = new Point(startPosX.intValue() + 7, startPosY.intValue() + 43); - - // Инициализация опорных точек для прорисовки второй "юбки" - Point point6 = new Point(startPosX.intValue() + 69, startPosY.intValue() + 37); - Point point7 = new Point(startPosX.intValue() + 82, startPosY.intValue() + 43); - Point point8 = new Point(startPosX.intValue() + 69, startPosY.intValue() + 43); - - // Совокупность точек полигона уголка первой "юбки" - Point[] firstTrianglePoints = { point3, point4, point5 }; - - // Совокупность точек полигона уголка второй "юбки" - Point[] secondTrianglePoints = { point6, point7, point8 }; - - // Прорисовка уголков "юбок" - g.setColor(blackColor); - g.fillPolygon(new Polygon(new int[]{ point3.x, point4.x, point5.x }, new int[]{ point3.y, point4.y, point5.y }, 3)); - g.fillPolygon(new Polygon(new int[]{ point6.x, point7.x, point8.x }, new int[]{ point6.y, point7.y, point8.y }, 3)); - - // Прорисовка корпуса - g.drawRect(startPosX.intValue() + 5, startPosY.intValue() + 20, 70, 17); - g.drawLine(point3.x, point3.y, point4.x, point4.y); - g.drawLine(pointStart.x, pointStart.y, point1.x, point1.y); - g.drawLine(point1.x, point1.y, point2.x, point2.y); - g.drawLine(point2.x, point2.y, pointFinish.x, pointFinish.y); - - // Прорисовка окон - g.setColor(deepSkyBlueColor); - g.drawRect(startPosX.intValue() + 12, startPosY.intValue() + 9, 8, 8); - g.drawRect(startPosX.intValue() + 63, startPosY.intValue() + 9, 8, 8); - - // Прорисовка двух передних колёс - - //g.fillOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 10, 10); - //g.setColor(blackColor); - - //g.drawOval(startPosX.intValue() + 24, startPosY.intValue() + 37, 8, 8); - - // Прорисовка двух задних колёс - //g.setColor(Color.YELLOW); - //g.fillOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 10, 10); - //g.fillOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 10, 10); - g.setColor(blackColor); - //g.drawOval(startPosX.intValue() + 63, startPosY.intValue() + 37, 8, 8); - //g.drawOval(startPosX.intValue() + 53, startPosY.intValue() + 37, 8, 8); - - // Прорисовка заднего "шлюза" - g.fillRect(startPosX.intValue() + 75, startPosY.intValue() + 9, 6, 27); - - - - - - - - - if (EntityElectricLocomotive.isElectricHorns()) { - // Инициализация опорных точек для прорисовки "рогов" - Point pointHorns1 = new Point(startPosX.intValue() + 13, startPosY.intValue() + 5); - Point pointHorns2 = new Point(startPosX.intValue() + 16, startPosY.intValue() + 2); - Point pointHorns3 = new Point(startPosX.intValue() + 11, startPosY.intValue()); - - g.setColor(EntityElectricLocomotive.getAdditionalColor()); - - // Прорисовка "рогов" электровоза - g.drawLine(pointHorns1.x, pointHorns1.y, pointHorns2.x, pointHorns2.y); - g.drawLine(pointHorns2.x, pointHorns2.y, pointHorns3.x, pointHorns3.y); - - g.setColor(blackColor); - } - - if (EntityElectricLocomotive.isBatteryPlacement()) { - // Инициализация опорных точек для прорисовки молнии на хранилище батарей - Point pointLightning1 = new Point(startPosX.intValue() + 39, startPosY.intValue() + 37); - Point pointLightning2 = new Point(startPosX.intValue() + 37, startPosY.intValue() + 39); - Point pointLightning3 = new Point(startPosX.intValue() + 39, startPosY.intValue() + 40); - Point pointLightning4 = new Point(startPosX.intValue() + 37, startPosY.intValue() + 41); - - g.setColor(EntityElectricLocomotive.getAdditionalColor()); - - // Прорисовка "хранилища батарей" электровоза - - g.fillRect(startPosX.intValue() + 36, startPosY.intValue() + 37, 8, 4); - g.setColor(yellowColor); - g.drawLine(pointLightning1.x, pointLightning1.y, pointLightning2.x, pointLightning2.y); - g.drawLine(pointLightning2.x, pointLightning2.y, pointLightning3.x, pointLightning3.y); - g.drawLine(pointLightning3.x, pointLightning3.y, pointLightning4.x, pointLightning4.y); - - g.setColor(blackColor); - } - } - -} diff --git a/ProjectElectricLocomotive/src/Drawnings/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/src/Drawnings/DrawingElectricLocomotive.java new file mode 100644 index 0000000..a4c2b03 --- /dev/null +++ b/ProjectElectricLocomotive/src/Drawnings/DrawingElectricLocomotive.java @@ -0,0 +1,83 @@ +package ProjectElectricLocomotive.src.Drawnings; + +import ProjectElectricLocomotive.src.Entities.EntityElectricLocomotive; + +import java.awt.*; +import java.util.Random; + +public class DrawingElectricLocomotive extends DrawingLocomotive +{ + + public IDrawingOrnament _ornament; + + public DrawingElectricLocomotive(int speed, float weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement) + { + super(83,45); + _entityLocomotive = new EntityElectricLocomotive(speed, weight, bodyColor, additionalColor, electricHorns, batteryPlacement); + Random rnd = new Random(); + int varOrnament = rnd.nextInt(3); + + switch (varOrnament) + { + case 0: + _ornament = new DrawingWheels(); + break; + case 1: + _ornament = new DrawingOrnamentWheels(); + break; + default: + _ornament = new DrawingOrnamentSecondWheels(); + break; + } + _ornament.SetCountWheels(rnd.nextInt(1,3)); + } + @Override + public void DrawTransport(Graphics g) + { + if (!(_entityLocomotive instanceof EntityElectricLocomotive _entityElectricLocomotive) || _startPosX == null) return; + + // Создание перьев и кистей для прорисовки электровоза + Color blackColor = Color.BLACK; + Color deepSkyBlueColor = new Color(0, 191, 255); + Color yellowColor = Color.YELLOW; + + if (_entityElectricLocomotive.GetElectricHorns()) { + // Инициализация опорных точек для прорисовки "рогов" + Point pointHorns1 = new Point(_startPosX + 13, _startPosY + 5); + Point pointHorns2 = new Point(_startPosX + 16, _startPosY + 2); + Point pointHorns3 = new Point(_startPosX + 11, _startPosY); + + g.setColor(_entityElectricLocomotive.GetAdditionalColor()); + + // Прорисовка "рогов" электровоза + g.drawLine(pointHorns1.x, pointHorns1.y, pointHorns2.x, pointHorns2.y); + g.drawLine(pointHorns2.x, pointHorns2.y, pointHorns3.x, pointHorns3.y); + + g.setColor(blackColor); + } + + if (_entityElectricLocomotive.GetBatteryPlacement()) { + // Инициализация опорных точек для прорисовки молнии на хранилище батарей + Point pointLightning1 = new Point(_startPosX + 39, _startPosY + 37); + Point pointLightning2 = new Point(_startPosX + 37, _startPosY + 39); + Point pointLightning3 = new Point(_startPosX + 39, _startPosY + 40); + Point pointLightning4 = new Point(_startPosX + 37, _startPosY + 41); + + g.setColor(_entityElectricLocomotive.GetAdditionalColor()); + + // Прорисовка "хранилища батарей" электровоза + + g.fillRect(_startPosX.intValue() + 36, _startPosY + 37, 8, 4); + g.setColor(yellowColor); + g.drawLine(pointLightning1.x, pointLightning1.y, pointLightning2.x, pointLightning2.y); + g.drawLine(pointLightning2.x, pointLightning2.y, pointLightning3.x, pointLightning3.y); + g.drawLine(pointLightning3.x, pointLightning3.y, pointLightning4.x, pointLightning4.y); + + g.setColor(blackColor); + } + _ornament.DrawWheels(g, _startPosX, _startPosY); + + } + + +} diff --git a/ProjectElectricLocomotive/src/Drawnings/DrawingField.java b/ProjectElectricLocomotive/src/Drawnings/DrawingField.java new file mode 100644 index 0000000..15d1bba --- /dev/null +++ b/ProjectElectricLocomotive/src/Drawnings/DrawingField.java @@ -0,0 +1,111 @@ +package ProjectElectricLocomotive.src.Drawnings; + + +import ProjectElectricLocomotive.src.DirectionType; +import ProjectElectricLocomotive.src.Drawnings.DrawingElectricLocomotive; +import ProjectElectricLocomotive.src.FormElectricLocomotive; +import ProjectElectricLocomotive.src.MovementStrategy.*; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class DrawingField extends JPanel { + private final FormElectricLocomotive field; + DrawingLocomotive _drawingLocomotive; + private AbstractStrategy _strategy = null; + public DrawingField(FormElectricLocomotive field) { + this.field = field; + } + @Override + protected void paintComponent(Graphics g) + { + if (_drawingLocomotive == null) return; + _drawingLocomotive.DrawTransport(g); + } + public void UpButtonAction() + { + if (_drawingLocomotive == null) return; + _drawingLocomotive.MoveTransport(DirectionType.Up); + } + public void DownButtonAction() + { + if (_drawingLocomotive == null) return; + _drawingLocomotive.MoveTransport(DirectionType.Down); + } + public void RightButtonAction() + { + if (_drawingLocomotive == null) return; + _drawingLocomotive.MoveTransport(DirectionType.Right); + } + public void LeftButtonAction() + { + if (_drawingLocomotive == null) return; + _drawingLocomotive.MoveTransport(DirectionType.Left); + } + public void CreateObject(String type){ + Random rnd = new Random(); + switch (type) + { + case "DrawningLocomotive": + _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + break; + case "DrawningElectricLocomotive": + _drawingLocomotive = new DrawingElectricLocomotive(rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), + rnd.nextBoolean()); + break; + default: + return; + } + _drawingLocomotive.SetPictureSize(getWidth(),getHeight()); + _drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + } + + public void CreateButtonElectricLocomotiveAction(){ + CreateObject("DrawningElectricLocomotive"); + } + public void CreateButtonLocomotiveAction(){ + CreateObject("DrawningLocomotive"); + } + + public void ButtonStrategyStepAction() + { + if(_drawingLocomotive == null) return; + if (field.comboBoxStrategy.isEnabled()) + { + int selectedIndex = field.comboBoxStrategy.getSelectedIndex(); + switch (selectedIndex) { + case 0: + _strategy = new MoveToCenter(); + break; + case 1: + _strategy = new MoveToBorder(); + break; + default: + _strategy = null; + } + if(_strategy == null) return; + _strategy.SetData(new MoveableLocomotive(_drawingLocomotive), getWidth(), getHeight()); + } + if (_strategy == null) return; + field.comboBoxStrategy.setEnabled(false); + _strategy.MakeStep(); + + if(_strategy.getStatus() == StrategyStatus.Finish) + { + field.comboBoxStrategy.setEnabled(true); + _strategy = null; + } + } + + public void ResizeField(){ + if (_drawingLocomotive == null) return; + _drawingLocomotive.SetPictureSize(getWidth(),getHeight()); + } +} diff --git a/ProjectElectricLocomotive/src/Drawnings/DrawingLocomotive.java b/ProjectElectricLocomotive/src/Drawnings/DrawingLocomotive.java new file mode 100644 index 0000000..7828927 --- /dev/null +++ b/ProjectElectricLocomotive/src/Drawnings/DrawingLocomotive.java @@ -0,0 +1,170 @@ +package ProjectElectricLocomotive.src.Drawnings; + + +import ProjectElectricLocomotive.src.DirectionType; + +import ProjectElectricLocomotive.src.Entities.EntityLocomotive; + +import java.awt.*; + +public class DrawingLocomotive { + public EntityLocomotive _entityLocomotive; + public EntityLocomotive GetEntityLocomotive() + { + return _entityLocomotive; + } + public DrawingWheels _wheels; + protected Integer _startPosX = null; + public Integer getPosX() { + return _startPosX; + } + protected Integer _startPosY = null; + public Integer getPosY() { + return _startPosY; + } + private Integer _pictureWidth = null; + public int getWidth() { + return _drawingLocomotiveWidth; + } + private Integer _pictureHeight = null; + public int getHeight() { + return _drawingLocomotiveHeight; + } + private int _drawingLocomotiveWidth = 83; + private int _drawingLocomotiveHeight = 45; + + public DrawingLocomotive(int speed, float weight, Color bodyColor) + { + this._entityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + } + + private DrawingLocomotive() + { + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + } + protected DrawingLocomotive(int drawningLocomotiveWidth, int drawningLocomotiveHeight) + { + this._drawingLocomotiveWidth = drawningLocomotiveWidth; + this._drawingLocomotiveHeight = drawningLocomotiveHeight; + } + + public boolean SetPictureSize(int width, int height) + { + if (_drawingLocomotiveWidth > width || _drawingLocomotiveHeight > height) + { + return false; + } + _pictureWidth = width; + _pictureHeight = height; + if (_startPosX != null && _drawingLocomotiveWidth + _startPosX > width) _startPosX = _pictureWidth - _drawingLocomotiveWidth; + if (_startPosY != null && _startPosY + _drawingLocomotiveHeight > _pictureHeight) _startPosY = _pictureHeight - _drawingLocomotiveHeight; + return true; + } + public void SetPosition(int x, int y) + { + if (_pictureHeight == null || _pictureWidth == null) return; + _startPosX = x; + _startPosY = y; + if (x + _drawingLocomotiveWidth > _pictureWidth || y + _drawingLocomotiveHeight > _pictureHeight || x < 0 || y < 0) + { + if (x < 0) _startPosX = 0; + if (y < 0 ) _startPosY = 0; + if (x + _drawingLocomotiveWidth > _pictureWidth) _startPosX = _pictureWidth - _drawingLocomotiveWidth; + if (y + _drawingLocomotiveHeight > _pictureHeight) _startPosY = _pictureHeight - _drawingLocomotiveHeight; + } + } + + public boolean MoveTransport(DirectionType direction) + { + if( _startPosX == null || _startPosY == null) return false; + switch (direction) + { + // Влево + case Left: + if(_startPosX - _entityLocomotive.Step >= 0) + { + _startPosX -= (int)_entityLocomotive.Step; + } + return true; + // Вправо + case Right: + if(_startPosX + _entityLocomotive.Step + _drawingLocomotiveWidth < _pictureWidth) + { + _startPosX += (int)_entityLocomotive.Step; + } + return true; + // Вверх + case Up: + if(_startPosY - _entityLocomotive.Step >= 0) + { + _startPosY -= (int)_entityLocomotive.Step; + } + return true; + // Вниз + case Down: + if(_startPosY + _entityLocomotive.Step + _drawingLocomotiveHeight < _pictureHeight) + { + _startPosY += (int)_entityLocomotive.Step; + } + return true; + default: + return false; + } + } + public void DrawTransport(Graphics g) { + if (_entityLocomotive == null || _startPosX == null || _startPosY == null) return; + + // Создание перьев и кистей для прорисовки электровоза + Color blackColor = Color.BLACK; + Color deepSkyBlueColor = new Color(0, 191, 255); + Color yellowColor = Color.YELLOW; + + // Инициализация опорных точек для прорисовки корпуса + int pointStartX = _startPosX + 75; + int pointStartY = _startPosY + 20; + + Point pointStart = new Point(pointStartX, pointStartY); + Point point1 = new Point(pointStartX, pointStartY - 15); + Point point2 = new Point(_startPosX + 10, pointStartY - 15); + Point pointFinish = new Point(_startPosX + 5, pointStartY); + + // Инициализация опорных точек для прорисовки первой "юбки" + Point point3 = new Point(_startPosX + 7, _startPosY + 37); + Point point4 = new Point(_startPosX, _startPosY + 43); + Point point5 = new Point(_startPosX + 7, _startPosY + 43); + + // Инициализация опорных точек для прорисовки второй "юбки" + Point point6 = new Point(_startPosX + 69, _startPosY + 37); + Point point7 = new Point(_startPosX + 82, _startPosY + 43); + Point point8 = new Point(_startPosX + 69, _startPosY + 43); + + // Совокупность точек полигона уголка первой "юбки" + Point[] firstTrianglePoints = { point3, point4, point5 }; + + // Совокупность точек полигона уголка второй "юбки" + Point[] secondTrianglePoints = { point6, point7, point8 }; + + // Прорисовка уголков "юбок" + g.setColor(_entityLocomotive.GetBodyColor()); + g.fillPolygon(new Polygon(new int[]{ point3.x, point4.x, point5.x }, new int[]{ point3.y, point4.y, point5.y }, 3)); + g.fillPolygon(new Polygon(new int[]{ point6.x, point7.x, point8.x }, new int[]{ point6.y, point7.y, point8.y }, 3)); + + // Прорисовка корпуса + g.drawRect(_startPosX + 5, _startPosY + 20, 70, 17); + g.drawLine(point3.x, point3.y, point4.x, point4.y); + g.drawLine(pointStart.x, pointStart.y, point1.x, point1.y); + g.drawLine(point1.x, point1.y, point2.x, point2.y); + g.drawLine(point2.x, point2.y, pointFinish.x, pointFinish.y); + + // Прорисовка окон + g.setColor(deepSkyBlueColor); + g.drawRect(_startPosX + 12, _startPosY + 9, 8, 8); + g.drawRect(_startPosX+ 63, _startPosY + 9, 8, 8); + + _wheels.DrawWheels(g, _startPosX, _startPosY); + + } +} diff --git a/ProjectElectricLocomotive/src/Drawnings/DrawingOrnamentWheels.java b/ProjectElectricLocomotive/src/Drawnings/DrawingOrnamentWheels.java new file mode 100644 index 0000000..3cbb572 --- /dev/null +++ b/ProjectElectricLocomotive/src/Drawnings/DrawingOrnamentWheels.java @@ -0,0 +1,17 @@ +package ProjectElectricLocomotive.src.Drawnings; + +import java.awt.*; + +public class DrawingOrnamentWheels implements IDrawingOrnament +{ + + @Override + public void SetCountWheels(int count) { + + } + + @Override + public void DrawWheels(Graphics g, int _startPosX, int _startPosY) { + + } +} diff --git a/ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java b/ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java new file mode 100644 index 0000000..42c20e9 --- /dev/null +++ b/ProjectElectricLocomotive/src/Drawnings/DrawingWheels.java @@ -0,0 +1,43 @@ +package ProjectElectricLocomotive.src.Drawnings; + +import ProjectElectricLocomotive.src.CountWheels; + +import java.awt.*; +public class DrawingWheels implements IDrawingOrnament +{ + private CountWheels _wheels; + @Override + public void SetCountWheels(int count) + { + for(CountWheels temp : CountWheels.values()) + { + if(temp.GetCountWheels() >= count) + { + _wheels = temp; + return; + } + } + } + + @Override + public void DrawWheels(Graphics g,int _startPosX, int _startPosY) + { + g.setColor(Color.BLACK); + switch (_wheels.GetCountWheels()) + { + case 4: + g.fillOval(_startPosX + 58, _startPosY + 37, 10, 10); + g.drawOval(_startPosX + 58, _startPosY + 37, 8, 8); + case 3: + g.fillOval(_startPosX + 48, _startPosY + 37, 10, 10); + g.drawOval(_startPosX + 48, _startPosY + 37, 8, 8); + case 2: + g.fillOval(_startPosX + 19, _startPosY + 37, 10, 10); + g.drawOval(_startPosX + 19, _startPosY + 37, 8, 8); + case 1: + g.fillOval(_startPosX + 9, _startPosY + 37, 10, 10); + g.drawOval(_startPosX + 9, _startPosY + 37, 8, 8); + break; + } + } +} diff --git a/ProjectElectricLocomotive/src/Drawnings/IDrawingOrnament.java b/ProjectElectricLocomotive/src/Drawnings/IDrawingOrnament.java new file mode 100644 index 0000000..18ccef8 --- /dev/null +++ b/ProjectElectricLocomotive/src/Drawnings/IDrawingOrnament.java @@ -0,0 +1,9 @@ +package ProjectElectricLocomotive.src.Drawnings; + +import java.awt.*; + +public interface IDrawingOrnament +{ + void SetCountWheels(int count); + void DrawWheels(Graphics g, int _startPosX, int _startPosY); +} diff --git a/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java b/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java new file mode 100644 index 0000000..a1fc2f1 --- /dev/null +++ b/ProjectElectricLocomotive/src/Entities/EntityElectricLocomotive.java @@ -0,0 +1,47 @@ +package ProjectElectricLocomotive.src.Entities; + +import java.awt.*; +import java.util.Random; + +public class EntityElectricLocomotive extends EntityLocomotive { + private int Speed; + private float Weight; + private Color BodyColor; + public Color GetBodyColor() + { + return BodyColor; + } + private Color AdditionalColor; + public Color GetAdditionalColor() + { + return AdditionalColor; + } + private boolean ElectricHorns; + public boolean GetElectricHorns() + { + return ElectricHorns; + } + private boolean BatteryPlacement; + public boolean GetBatteryPlacement() + { + return BatteryPlacement; + } + public EntityElectricLocomotive(int speed, float weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement) + { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + ElectricHorns = electricHorns; + BatteryPlacement = batteryPlacement; + } + public void Init(int speed, float weight, Color bodyColor, Color additionalcolor, boolean electricHorns, boolean batteryPlacement) + { + Random rnd = new Random(); + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalcolor; + ElectricHorns = electricHorns; + BatteryPlacement = batteryPlacement; + Step = Speed * 100 / Weight; + } +} diff --git a/ProjectElectricLocomotive/src/Entities/EntityLocomotive.java b/ProjectElectricLocomotive/src/Entities/EntityLocomotive.java new file mode 100644 index 0000000..98ab725 --- /dev/null +++ b/ProjectElectricLocomotive/src/Entities/EntityLocomotive.java @@ -0,0 +1,36 @@ +package ProjectElectricLocomotive.src.Entities; + +import java.awt.*; +import java.util.Random; + +public class EntityLocomotive +{ + 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 float Step; + public float GetStep() + { + return Step; + } + public EntityLocomotive(int speed, float weight, Color bodyColor) + { + Random rnd = new Random(); + Speed = speed <= 0 ? rnd.nextInt(100, 300) : speed; + Weight = weight <= 0 ? rnd.nextInt(1000, 3000) : weight; + BodyColor = bodyColor; + Step = Speed * 100 / Weight; + } +} diff --git a/ProjectElectricLocomotive/src/EntityElectricLocomotive.java b/ProjectElectricLocomotive/src/EntityElectricLocomotive.java deleted file mode 100644 index 78b5150..0000000 --- a/ProjectElectricLocomotive/src/EntityElectricLocomotive.java +++ /dev/null @@ -1,117 +0,0 @@ -package ProjectElectricLocomotive.src; -import java.awt.Color; - -/** - * Класс-сущность "Тепловоз" - */ -public class EntityElectricLocomotive { - - /** - * Скорость - */ - private int speed; - - private int wheelsCount; - - /** - * Вес - */ - private double weight; - - /** - * Основной цвет - */ - private Color bodyColor; - - /** - * Дополнительный цвет (для дополнительных элементов) - */ - private Color additionalColor; - - /** - * Признак (опция) наличия "корпус" - */ - private boolean electricHorns; - - /** - * Признак (опция) наличия места под батарею - */ - private boolean batteryPlacement; - - /** - * Инициализация полей объекта-класса тепловоза - * - * @param speed Скорость - * @param weight Вес - * @param bodyColor Основной цвет - * @param additionalColor Дополнительный цвет - * @param electricHorns Наличие "корпуса" - * @param batteryPlacement Наличие места под батарею - */ - public void init(int speed, double weight, Color bodyColor, Color additionalColor, boolean electricHorns, boolean batteryPlacement) { - this.speed = speed; - this.weight = weight; - this.bodyColor = bodyColor; - this.additionalColor = additionalColor; - this.electricHorns = electricHorns; - this.batteryPlacement = batteryPlacement; - } - - /** - * Получение шага перемещения объекта - * - * @return Шаг перемещения - */ - public double getStep() { - return speed * 100 / weight; - } - - // Геттеры и сеттеры для приватных полей - public int getSpeed() { - return speed; - } - - public void setSpeed(int speed) { - this.speed = speed; - } - - public double getWeight() { - return weight; - } - - public void setWeight(double weight) { - this.weight = weight; - } - - public Color getBodyColor() { - return bodyColor; - } - - public void setBodyColor(Color bodyColor) { - this.bodyColor = bodyColor; - } - - public Color getAdditionalColor() { - return additionalColor; - } - - public void setAdditionalColor(Color additionalColor) { - this.additionalColor = additionalColor; - } - - public boolean isElectricHorns() { - return electricHorns; - } - - public void setElectricHorns(boolean electricHorns) { - this.electricHorns = electricHorns; - } - - public boolean isBatteryPlacement() { - return batteryPlacement; - } - - public void setBatteryPlacement(boolean batteryPlacement) { - this.batteryPlacement = batteryPlacement; - } -} diff --git a/ProjectElectricLocomotive/src/FormElectricLocomotive.form b/ProjectElectricLocomotive/src/FormElectricLocomotive.form deleted file mode 100644 index a4d4d5d..0000000 --- a/ProjectElectricLocomotive/src/FormElectricLocomotive.form +++ /dev/null @@ -1,62 +0,0 @@ - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/ProjectElectricLocomotive/src/FormElectricLocomotive.java b/ProjectElectricLocomotive/src/FormElectricLocomotive.java index 6e94858..580a643 100644 --- a/ProjectElectricLocomotive/src/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/src/FormElectricLocomotive.java @@ -1,124 +1,161 @@ package ProjectElectricLocomotive.src; +import ProjectElectricLocomotive.src.Drawnings.DrawingField; + import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.Random; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; -public class FormElectricLocomotive extends JFrame { +public class FormElectricLocomotive extends JFrame{ + private int Width; + private int Height; - private DrawningElectricLocomotive _drawningElectricLocomotive; + JPanel BottomPanel = new JPanel(); + JPanel CreatePanel = new JPanel(); + JPanel BottomAndCreatePanel = new JPanel(); + JPanel DimPanel = new JPanel(); + JPanel UPanel = new JPanel(); + JPanel DPanel = new JPanel(); + JPanel LRPanel = new JPanel(); - private JPanel panel; - private JButton buttonCreate; - private JButton buttonUp; - private JButton buttonDown; - private JButton buttonLeft; - private JButton buttonRight; - private JLabel pictureBoxElectricLocomotive; + JPanel CBPanel = new JPanel(); + JPanel CPBandButtonPanel = new JPanel(); - public FormElectricLocomotive() { - initComponents(); - } + DrawingField field = new DrawingField(this); - private void initComponents() { - setTitle("Electric Locomotive"); - setVisible(true); - setSize(520, 300); + JButton ButtonStep = new JButton("Шаг"); + + JButton ButtonCreateElectricLocomotive=new JButton("CreateElectricLocomotive"); + JButton ButtonCreateLocomotive=new JButton("CreateLocomotive"); + + public JComboBox comboBoxStrategy = new JComboBox<>(); + + JButton ButtonUp=new JButton("Up"); + + + JButton ButtonDown=new JButton("Down"); + + + JButton ButtonRight=new JButton("Right"); + + JButton ButtonLeft=new JButton("Left"); + public FormElectricLocomotive(){ + super("Electric Locomotive"); + setSize(900,500); + Width = getWidth(); + Height = getHeight(); + ShowWindow(); + RefreshWindow(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + } - panel = new JPanel(); - buttonCreate = new JButton("Create"); - buttonUp = new JButton("Up"); - buttonDown = new JButton("Down"); - buttonLeft = new JButton("Left"); - buttonRight = new JButton("Right"); - pictureBoxElectricLocomotive = new JLabel(); + public void ShowWindow(){ - panel.setLayout(new FlowLayout()); - panel.add(buttonCreate); - panel.add(buttonUp); - panel.add(buttonDown); - panel.add(buttonLeft); - panel.add(buttonRight); - add(panel, BorderLayout.NORTH); - add(pictureBoxElectricLocomotive, BorderLayout.CENTER); + Dimension dimen = new Dimension(30,30); - buttonCreate.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - createElectricLocomotive(); - } + ButtonUp.setPreferredSize(dimen); + ButtonUp.addActionListener(e->{ + field.UpButtonAction(); + repaint(); }); - buttonUp.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - moveTransport(DirectionType.Up); - } + ButtonDown.setPreferredSize(dimen); + ButtonDown.addActionListener(e->{ + field.DownButtonAction(); + repaint(); }); - buttonDown.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - moveTransport(DirectionType.Down); - } + ButtonRight.setPreferredSize(dimen); + ButtonRight.addActionListener(e->{ + field.RightButtonAction(); + repaint(); }); - buttonLeft.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - moveTransport(DirectionType.Left); - } + ButtonLeft.setPreferredSize(dimen); + ButtonLeft.addActionListener(e->{ + field.LeftButtonAction(); + repaint(); }); - buttonRight.addActionListener(new ActionListener() { + LRPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,0)); + LRPanel.setBackground(new Color(0,0,0,0)); + LRPanel.add(ButtonLeft); + LRPanel.add(ButtonRight); + + UPanel.setLayout(new FlowLayout()); + UPanel.setBackground(new Color(0,0,0,0)); + UPanel.add(ButtonUp); + + DPanel.setLayout(new FlowLayout()); + DPanel.setBackground(new Color(0,0,0,0)); + DPanel.add(ButtonDown); + + DimPanel.setLayout(new BoxLayout(DimPanel,BoxLayout.Y_AXIS)); + DimPanel.setBackground(new Color(0,0,0,0)); + DimPanel.add(UPanel); + DimPanel.add(LRPanel); + DimPanel.add(DPanel); + add(DimPanel); + + CreatePanel.setLayout(new FlowLayout()); + CreatePanel.setBackground(new Color(0,0,0,0)); + + CreatePanel.add(ButtonCreateLocomotive); + ButtonCreateLocomotive.addActionListener(e->{ + field.CreateButtonLocomotiveAction(); + repaint(); + }); + + CreatePanel.add(ButtonCreateElectricLocomotive); + ButtonCreateElectricLocomotive.addActionListener(e->{ + field.CreateButtonElectricLocomotiveAction(); + repaint(); + }); + comboBoxStrategy.setPreferredSize(new Dimension(80, 40)); + comboBoxStrategy.addItem("К центру"); + comboBoxStrategy.addItem("К краю"); + ButtonStep.setPreferredSize(new Dimension(80, 40)); + ButtonStep.addActionListener(e->{ + field.ButtonStrategyStepAction(); + repaint(); + }); + + CBPanel.setLayout(new FlowLayout()); + CBPanel.setBackground(new Color(0, 0, 0, 0)); + CBPanel.add(comboBoxStrategy); + CBPanel.add(ButtonStep); + CPBandButtonPanel.setLayout(new BoxLayout(CPBandButtonPanel, BoxLayout.X_AXIS)); + CPBandButtonPanel.setBackground(new Color(0, 0, 0, 0)); + CPBandButtonPanel.add(CBPanel); + add(CPBandButtonPanel); + + BottomAndCreatePanel.setLayout(new BoxLayout(BottomAndCreatePanel, BoxLayout.Y_AXIS)); + BottomAndCreatePanel.setBackground(new Color(0,0,0,0)); + BottomAndCreatePanel.add(CreatePanel); + BottomAndCreatePanel.add(BottomPanel); + + add(BottomAndCreatePanel); + add(field); + + addComponentListener(new ComponentAdapter() { @Override - public void actionPerformed(ActionEvent e) { - moveTransport(DirectionType.Right); + public void componentResized(ComponentEvent e) { + super.componentResized(e); + Width = getWidth(); + Height = getHeight(); + field.ResizeField(); + repaint(); + RefreshWindow(); + } }); } - - private void createElectricLocomotive() { - Random random = new Random(); - _drawningElectricLocomotive = new DrawningElectricLocomotive(); - _drawningElectricLocomotive.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(), - random.nextInt(0,5) - ); - _drawningElectricLocomotive.setPictureSize(pictureBoxElectricLocomotive.getWidth(), pictureBoxElectricLocomotive.getHeight()); - _drawningElectricLocomotive.setPosition(random.nextInt(pictureBoxElectricLocomotive.getWidth()), random.nextInt(pictureBoxElectricLocomotive.getHeight())); - - draw(); - } - - private void moveTransport(DirectionType direction) { - if (_drawningElectricLocomotive != null) { - if (_drawningElectricLocomotive.moveTransport(direction)) { - draw(); - } - } - } - - private void draw() { - if (_drawningElectricLocomotive != null) { - Image image = new ImageIcon(_drawningElectricLocomotive.getImage()).getImage(); - Image scaledImage = image.getScaledInstance(pictureBoxElectricLocomotive.getWidth(), pictureBoxElectricLocomotive.getHeight(), Image.SCALE_SMOOTH); - pictureBoxElectricLocomotive.setIcon(new ImageIcon(scaledImage)); - } - } - - public static void main(String[] args) { - SwingUtilities.invokeLater(() -> { - FormElectricLocomotive form = new FormElectricLocomotive(); - form.setVisible(true); - }); + public void RefreshWindow(){ + field.setLayout(new FlowLayout()); + CPBandButtonPanel.setBounds(Width - 200, 0, 190, 140); + BottomAndCreatePanel.setBounds(-330, Height - 110, Width, 180); + DimPanel.setBounds(Width - 170, Height - 170, 190, 140); } } diff --git a/ProjectElectricLocomotive/src/MovementStrategy/AbstractStrategy.java b/ProjectElectricLocomotive/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..8f6a01a --- /dev/null +++ b/ProjectElectricLocomotive/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,74 @@ +package ProjectElectricLocomotive.src.MovementStrategy; + +public abstract class AbstractStrategy +{ + private IMoveableObject _moveableObject = null; + private StrategyStatus _state = StrategyStatus.NotInit; + protected int fieldWidth; + protected int fieldHeight; + protected StrategyStatus state; + + protected ObjectParameters getObjectParameters() { + return (_moveableObject != null) ? _moveableObject.getObjectPosition() : null; + } + + protected Integer GetStep() { + if (state != StrategyStatus.InProgress) { + return null; + } + return (_moveableObject != null) ? _moveableObject.getStep() : null; + } + + public StrategyStatus getStatus() { + return state; + } + + public void SetData(IMoveableObject moveableObject, int width, int height) { + if (moveableObject == null) { + state = StrategyStatus.NotInit; + return; + } + state = StrategyStatus.InProgress; + this._moveableObject = moveableObject; + 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); + } + + private boolean moveTo(MovementDirection movementDirection) { + if (state != StrategyStatus.InProgress) { + return false; + } + return (_moveableObject != null) && _moveableObject.tryMoveObject(movementDirection); + } + + protected abstract void moveToTarget(); + + protected abstract boolean isTargetDestination(); +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/src/MovementStrategy/IMoveableObject.java b/ProjectElectricLocomotive/src/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..ce0fbe0 --- /dev/null +++ b/ProjectElectricLocomotive/src/MovementStrategy/IMoveableObject.java @@ -0,0 +1,8 @@ +package ProjectElectricLocomotive.src.MovementStrategy; + +public interface IMoveableObject +{ + ObjectParameters getObjectPosition(); + int getStep(); + boolean tryMoveObject(MovementDirection direction); +} diff --git a/ProjectElectricLocomotive/src/MovementStrategy/MoveToBorder.java b/ProjectElectricLocomotive/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..98b3dce --- /dev/null +++ b/ProjectElectricLocomotive/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,36 @@ +package ProjectElectricLocomotive.src.MovementStrategy; + +public class MoveToBorder extends AbstractStrategy +{ + @Override + protected void moveToTarget() + { + ObjectParameters objParams = getObjectParameters(); + if (objParams == null) + { + return; + } + int diffX = objParams.getRightBorder() - fieldWidth; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) MoveLeft(); + else MoveRight(); + } + int diffY = objParams.getDownBorder() - fieldHeight; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) MoveUp(); + else MoveDown(); + } + } + + @Override + protected boolean isTargetDestination() { + ObjectParameters objParams = getObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.getRightBorder() + GetStep() >= fieldWidth && objParams.getDownBorder() + GetStep() >= fieldHeight; + } +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/src/MovementStrategy/MoveToCenter.java b/ProjectElectricLocomotive/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..4ac1667 --- /dev/null +++ b/ProjectElectricLocomotive/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,37 @@ +package ProjectElectricLocomotive.src.MovementStrategy; + +public class MoveToCenter extends AbstractStrategy +{ + + @Override + protected void moveToTarget() { + ObjectParameters objParams = getObjectParameters(); + if (objParams == null) + { + return; + } + int diffX = objParams.getObjectMiddleHorizontal() - fieldWidth / 2; + if(Math.abs(diffX) > GetStep()) + { + if (diffX > 0) MoveLeft(); + else MoveRight(); + } + int diffY = objParams.getObjectMiddleVertical() - fieldHeight / 2; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) MoveUp(); + else MoveDown(); + } + } + + @Override + protected boolean isTargetDestination() { + ObjectParameters objParams = getObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.getObjectMiddleHorizontal() - GetStep() <= fieldWidth / 2 && objParams.getObjectMiddleHorizontal() + GetStep() >= fieldWidth / 2 && + objParams.getObjectMiddleVertical() - GetStep() <= fieldHeight / 2 && objParams.getObjectMiddleVertical() + GetStep() >= fieldHeight / 2; + } +} diff --git a/ProjectElectricLocomotive/src/MovementStrategy/MoveableLocomotive.java b/ProjectElectricLocomotive/src/MovementStrategy/MoveableLocomotive.java new file mode 100644 index 0000000..bf083b2 --- /dev/null +++ b/ProjectElectricLocomotive/src/MovementStrategy/MoveableLocomotive.java @@ -0,0 +1,47 @@ +package ProjectElectricLocomotive.src.MovementStrategy; + +import ProjectElectricLocomotive.src.DirectionType; +import ProjectElectricLocomotive.src.Drawnings.DrawingLocomotive; + +public class MoveableLocomotive implements IMoveableObject { + private DrawingLocomotive _locomotive = null; + public MoveableLocomotive(DrawingLocomotive drawingLocomotive) + { + _locomotive = drawingLocomotive; + } + public ObjectParameters getObjectPosition() { + if (_locomotive == null || _locomotive.GetEntityLocomotive() == null || _locomotive.getPosX() == null || _locomotive.getPosY() == null) { + return null; + } + + return new ObjectParameters(_locomotive.getPosX(), _locomotive.getPosY(), _locomotive.getWidth(), _locomotive.getHeight()); + } + public int getStep() { + if(_locomotive == null || _locomotive.GetEntityLocomotive() == null) return 0; + return (int)(_locomotive.GetEntityLocomotive().Step); + } + + public boolean tryMoveObject(MovementDirection direction) { + if (_locomotive == null || _locomotive.GetEntityLocomotive() == null) { + return false; + } + + DirectionType directionType = getDirectionType(direction); + return _locomotive.MoveTransport(directionType); + } + + private DirectionType getDirectionType(MovementDirection direction) { + switch (direction) { + case Left: + return DirectionType.Left; + case Right: + return DirectionType.Right; + case Up: + return DirectionType.Up; + case Down: + return DirectionType.Down; + default: + return DirectionType.Unknown; + } + } +} diff --git a/ProjectElectricLocomotive/src/MovementStrategy/MovementDirection.java b/ProjectElectricLocomotive/src/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..fa0d24f --- /dev/null +++ b/ProjectElectricLocomotive/src/MovementStrategy/MovementDirection.java @@ -0,0 +1,18 @@ +package ProjectElectricLocomotive.src.MovementStrategy; + +public enum MovementDirection +{ + Up(1), + Down(2), + Left(3), + Right(4); + private final int Value; + + MovementDirection(int value) { + Value = value; + } + + public int getValue() { + return Value; + } +} diff --git a/ProjectElectricLocomotive/src/MovementStrategy/ObjectParameters.java b/ProjectElectricLocomotive/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..fc38043 --- /dev/null +++ b/ProjectElectricLocomotive/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,39 @@ +package ProjectElectricLocomotive.src.MovementStrategy; + +public class ObjectParameters +{ + private final int x; + public int getLeftBorder() { + return x; + } + private final int y; + public int getTopBorder() { + return y; + } + private final int width; + + private final int height; + + public ObjectParameters(int x, int y, int width, int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + + public int getRightBorder() { + return x + width; + } + public int getDownBorder() { + return y + height; + } + + public int getObjectMiddleHorizontal() { + return x + width / 2; + } + + public int getObjectMiddleVertical() { + return y + height / 2; + } +} diff --git a/ProjectElectricLocomotive/src/MovementStrategy/StrategyStatus.java b/ProjectElectricLocomotive/src/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..8574545 --- /dev/null +++ b/ProjectElectricLocomotive/src/MovementStrategy/StrategyStatus.java @@ -0,0 +1,10 @@ +package ProjectElectricLocomotive.src.MovementStrategy; + +public enum StrategyStatus +{ + NotInit, + + InProgress, + + Finish +} \ No newline at end of file