From 40b7fc09c2c6ef6cc61c167ae2734d65bf352ade Mon Sep 17 00:00:00 2001 From: asakky Date: Sat, 8 Jun 2024 08:00:59 +0400 Subject: [PATCH] LabWork02 --- ProjectMotorBoat/.idea/uiDesigner.xml | 124 +++++++++++++ .../DrawingPaddlesType1.java} | 10 +- .../DrawingPaddlesType2.java | 43 +++++ .../DrawingPaddlesType3.java | 42 +++++ .../IDifferentPaddles.java | 10 ++ .../NumberOfPaddles.java | 2 + .../{ => DrawingBoat}/CanvasMotorBoat.java | 9 +- .../src/{ => DrawingBoat}/DirectionType.java | 4 + .../src/DrawingBoat/DrawingBoat.java | 166 ++++++++++++++++++ .../src/DrawingBoat/DrawingMotorBoat.java | 49 ++++++ ProjectMotorBoat/src/DrawingMotorBoat.java | 145 --------------- ProjectMotorBoat/src/Entities/EntityBoat.java | 19 ++ .../src/Entities/EntityMotorBoat.java | 18 ++ ProjectMotorBoat/src/EntityMotorBoat.java | 25 --- ProjectMotorBoat/src/FormMotorBoat.java | 140 ++++++++++++--- ProjectMotorBoat/src/Main.java | 2 +- .../MovementStrategy/AbstractStrategy.java | 57 ++++++ .../MovementStrategy/IMoveableObjects.java | 8 + .../src/MovementStrategy/MoveToBorder.java | 28 +++ .../src/MovementStrategy/MoveToCenter.java | 49 ++++++ .../src/MovementStrategy/MoveableBoat.java | 46 +++++ .../MovementStrategy/MovementDirection.java | 9 + .../MovementStrategy/ObjectParameters.java | 28 +++ .../src/MovementStrategy/StrategyStatus.java | 8 + 24 files changed, 836 insertions(+), 205 deletions(-) create mode 100644 ProjectMotorBoat/.idea/uiDesigner.xml rename ProjectMotorBoat/src/{DrawingPaddles.java => DifferentDrawingPaddles/DrawingPaddlesType1.java} (83%) create mode 100644 ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType2.java create mode 100644 ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType3.java create mode 100644 ProjectMotorBoat/src/DifferentDrawingPaddles/IDifferentPaddles.java rename ProjectMotorBoat/src/{ => DifferentDrawingPaddles}/NumberOfPaddles.java (89%) rename ProjectMotorBoat/src/{ => DrawingBoat}/CanvasMotorBoat.java (69%) rename ProjectMotorBoat/src/{ => DrawingBoat}/DirectionType.java (65%) create mode 100644 ProjectMotorBoat/src/DrawingBoat/DrawingBoat.java create mode 100644 ProjectMotorBoat/src/DrawingBoat/DrawingMotorBoat.java delete mode 100644 ProjectMotorBoat/src/DrawingMotorBoat.java create mode 100644 ProjectMotorBoat/src/Entities/EntityBoat.java create mode 100644 ProjectMotorBoat/src/Entities/EntityMotorBoat.java delete mode 100644 ProjectMotorBoat/src/EntityMotorBoat.java create mode 100644 ProjectMotorBoat/src/MovementStrategy/AbstractStrategy.java create mode 100644 ProjectMotorBoat/src/MovementStrategy/IMoveableObjects.java create mode 100644 ProjectMotorBoat/src/MovementStrategy/MoveToBorder.java create mode 100644 ProjectMotorBoat/src/MovementStrategy/MoveToCenter.java create mode 100644 ProjectMotorBoat/src/MovementStrategy/MoveableBoat.java create mode 100644 ProjectMotorBoat/src/MovementStrategy/MovementDirection.java create mode 100644 ProjectMotorBoat/src/MovementStrategy/ObjectParameters.java create mode 100644 ProjectMotorBoat/src/MovementStrategy/StrategyStatus.java diff --git a/ProjectMotorBoat/.idea/uiDesigner.xml b/ProjectMotorBoat/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/ProjectMotorBoat/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ProjectMotorBoat/src/DrawingPaddles.java b/ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType1.java similarity index 83% rename from ProjectMotorBoat/src/DrawingPaddles.java rename to ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType1.java index b79efda..6f20fe2 100644 --- a/ProjectMotorBoat/src/DrawingPaddles.java +++ b/ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType1.java @@ -1,7 +1,10 @@ +package DifferentDrawingPaddles; + import java.awt.*; -public class DrawingPaddles { +public class DrawingPaddlesType1 implements IDifferentPaddles { private NumberOfPaddles numberOfPaddles; + @Override public void setNumberOfPaddles(int numberofpaddle) { for (NumberOfPaddles numofenum : NumberOfPaddles.values()) { if (numofenum.getNumpaddles() == numberofpaddle) { @@ -10,11 +13,12 @@ public class DrawingPaddles { } } } + @Override public NumberOfPaddles getNumberOfPaddles() { return numberOfPaddles; } - public void DrawPaddles(Graphics g, int x, int y, int width, int height, Color bodyColor) { - + @Override + public void DrawPaddles(Graphics2D g, int x, int y, int width, int height, Color bodyColor) { g.setColor(Color.BLACK); // Весло слева int paddleLeftX1 = x + 35; diff --git a/ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType2.java b/ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType2.java new file mode 100644 index 0000000..acaeaec --- /dev/null +++ b/ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType2.java @@ -0,0 +1,43 @@ +package DifferentDrawingPaddles; + +import java.awt.*; + +public class DrawingPaddlesType2 implements IDifferentPaddles { + private NumberOfPaddles numberOfPaddles; + @Override + public void setNumberOfPaddles(int numberofpaddle) { + for (NumberOfPaddles numofenum : NumberOfPaddles.values()) { + if (numofenum.getNumpaddles() == numberofpaddle) { + numberOfPaddles = numofenum; + return; + } + } + } + @Override + public NumberOfPaddles getNumberOfPaddles() { + return numberOfPaddles; + } + @Override + public void DrawPaddles(Graphics2D g, int x, int y, int width, int height, Color bodyColor) { + g.setColor(Color.BLACK); + // Весло слева + int paddleLeftX1 = x + 35; + int paddleLeftY1 = y + 10; + int paddleLeftX2 = x + 25; + int paddleLeftY2 = y - 10; + g.drawLine(paddleLeftX1, paddleLeftY1, paddleLeftX2, paddleLeftY2); + + // Весло справа + int paddleRightX1 = x + 35; + int paddleRightY1 = y + 60; + int paddleRightX2 = x + 25; + int paddleRightY2 = y + 80; + g.drawLine(paddleRightX1, paddleRightY1, paddleRightX2, paddleRightY2); + + // Лопасти весел + g.setColor(Color.BLACK); + g.fillOval(paddleLeftX2 - 5, paddleLeftY2 - 5, 10, 10); + g.fillOval(paddleRightX2 - 5, paddleRightY2 - 5, 10, 10); + } +} + diff --git a/ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType3.java b/ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType3.java new file mode 100644 index 0000000..b97cbf8 --- /dev/null +++ b/ProjectMotorBoat/src/DifferentDrawingPaddles/DrawingPaddlesType3.java @@ -0,0 +1,42 @@ +package DifferentDrawingPaddles; + +import java.awt.*; + +public class DrawingPaddlesType3 implements IDifferentPaddles { + private NumberOfPaddles numberOfPaddles; + @Override + public void setNumberOfPaddles(int numberofpaddle) { + for (NumberOfPaddles numofenum : NumberOfPaddles.values()) { + if (numofenum.getNumpaddles() == numberofpaddle) { + numberOfPaddles = numofenum; + return; + } + } + } + @Override + public NumberOfPaddles getNumberOfPaddles() { + return numberOfPaddles; + } + @Override + public void DrawPaddles(Graphics2D g, int x, int y, int width, int height, Color bodyColor) { + g.setColor(Color.BLACK); + // Весло слева + int paddleLeftX1 = x + 35; + int paddleLeftY1 = y + 10; + int paddleLeftX2 = x + 45; + int paddleLeftY2 = y - 10; + g.drawLine(paddleLeftX1, paddleLeftY1, paddleLeftX2, paddleLeftY2); + + // Весло справа + int paddleRightX1 = x + 35; + int paddleRightY1 = y + 60; + int paddleRightX2 = x + 45; + int paddleRightY2 = y + 80; + g.drawLine(paddleRightX1, paddleRightY1, paddleRightX2, paddleRightY2); + + // Лопасти весел + g.setColor(Color.BLACK); + g.fillOval(paddleLeftX2 - 5, paddleLeftY2 - 5, 10, 10); + g.fillOval(paddleRightX2 - 5, paddleRightY2 - 5, 10, 10); + } +} diff --git a/ProjectMotorBoat/src/DifferentDrawingPaddles/IDifferentPaddles.java b/ProjectMotorBoat/src/DifferentDrawingPaddles/IDifferentPaddles.java new file mode 100644 index 0000000..bbd8019 --- /dev/null +++ b/ProjectMotorBoat/src/DifferentDrawingPaddles/IDifferentPaddles.java @@ -0,0 +1,10 @@ +package DifferentDrawingPaddles; + +import java.awt.*; + +public interface IDifferentPaddles { + void setNumberOfPaddles(int numberofpaddle); + NumberOfPaddles getNumberOfPaddles(); + void DrawPaddles(Graphics2D g, int x, int y, int width, int height, Color bodyColor); +} + diff --git a/ProjectMotorBoat/src/NumberOfPaddles.java b/ProjectMotorBoat/src/DifferentDrawingPaddles/NumberOfPaddles.java similarity index 89% rename from ProjectMotorBoat/src/NumberOfPaddles.java rename to ProjectMotorBoat/src/DifferentDrawingPaddles/NumberOfPaddles.java index ed81e78..a6d0e78 100644 --- a/ProjectMotorBoat/src/NumberOfPaddles.java +++ b/ProjectMotorBoat/src/DifferentDrawingPaddles/NumberOfPaddles.java @@ -1,3 +1,5 @@ +package DifferentDrawingPaddles; + public enum NumberOfPaddles { OnePaddle(1), TwoPaddles(2), diff --git a/ProjectMotorBoat/src/CanvasMotorBoat.java b/ProjectMotorBoat/src/DrawingBoat/CanvasMotorBoat.java similarity index 69% rename from ProjectMotorBoat/src/CanvasMotorBoat.java rename to ProjectMotorBoat/src/DrawingBoat/CanvasMotorBoat.java index 02088e5..982519c 100644 --- a/ProjectMotorBoat/src/CanvasMotorBoat.java +++ b/ProjectMotorBoat/src/DrawingBoat/CanvasMotorBoat.java @@ -1,17 +1,20 @@ +package DrawingBoat; + import javax.swing.*; import java.awt.*; public class CanvasMotorBoat extends JComponent { - public DrawingMotorBoat _drawingMotorBoat; + public DrawingBoat _drawingBoat; public CanvasMotorBoat(){} public void paintComponent(Graphics g) { - if (_drawingMotorBoat == null) { + if (_drawingBoat == null) { return; } super.paintComponents(g); Graphics2D g2d = (Graphics2D) g; - _drawingMotorBoat.DrawTransport(g2d); + _drawingBoat.DrawTransport(g2d); super.repaint(); } } + diff --git a/ProjectMotorBoat/src/DirectionType.java b/ProjectMotorBoat/src/DrawingBoat/DirectionType.java similarity index 65% rename from ProjectMotorBoat/src/DirectionType.java rename to ProjectMotorBoat/src/DrawingBoat/DirectionType.java index 35657f0..48de010 100644 --- a/ProjectMotorBoat/src/DirectionType.java +++ b/ProjectMotorBoat/src/DrawingBoat/DirectionType.java @@ -1,6 +1,10 @@ +package DrawingBoat; + public enum DirectionType { + Unknown, Up, Down, Left, Right } + diff --git a/ProjectMotorBoat/src/DrawingBoat/DrawingBoat.java b/ProjectMotorBoat/src/DrawingBoat/DrawingBoat.java new file mode 100644 index 0000000..cf9528a --- /dev/null +++ b/ProjectMotorBoat/src/DrawingBoat/DrawingBoat.java @@ -0,0 +1,166 @@ +package DrawingBoat; + +import DifferentDrawingPaddles.DrawingPaddlesType1; +import DifferentDrawingPaddles.DrawingPaddlesType2; +import DifferentDrawingPaddles.DrawingPaddlesType3; +import DifferentDrawingPaddles.IDifferentPaddles; +import Entities.EntityBoat; + +import javax.swing.*; +import java.awt.*; + +public class DrawingBoat extends JPanel { + public Entities.EntityBoat EntityBoat; + public IDifferentPaddles drawingPaddles; + private Integer picture_width; + private Integer picture_height; + public Integer _StartPosX; + public Integer _StartPosY; + private int drawingBoatWidth = 140; + protected int drawingBoatHeight = 90; + + public Integer GetPosX() { + return _StartPosX; + } + + public Integer GetPosY() { + return _StartPosY; + } + + public Integer GetWidth() { + return drawingBoatWidth; + } + + public Integer GetHeight() { + return drawingBoatHeight; + } + + protected DrawingBoat() { + picture_width = null; + picture_height = null; + _StartPosX = null; + _StartPosY = null; + } + + protected void SetAmountandTypePaddles() { + int numberOfPaddles = (int) (Math.random() * 4 + 0); + switch ((int) (Math.random() * 3 + 1)) { + case 1: + drawingPaddles = new DrawingPaddlesType1(); + break; + case 2: + drawingPaddles = new DrawingPaddlesType2(); + break; + case 3: + drawingPaddles = new DrawingPaddlesType3(); + break; + default: + numberOfPaddles = 0; + break; + } + drawingPaddles.setNumberOfPaddles(numberOfPaddles); + } + + public DrawingBoat(int speed, double weight, Color bodycolor) { + super(); + EntityBoat = new EntityBoat(speed, weight, bodycolor); + SetAmountandTypePaddles(); + } + + public boolean SetPictureSize(int width, int height) { + if (width < drawingBoatWidth || height < drawingBoatHeight) return false; + picture_width = width; + picture_height = height; + if (_StartPosX != null || _StartPosY != null) { + if (_StartPosX + drawingBoatWidth > picture_width) { + _StartPosX = _StartPosX - (_StartPosX + drawingBoatWidth - picture_width); + } else if (_StartPosX < 10) _StartPosX = 10; + if (_StartPosY + drawingBoatHeight + 15 > picture_height) { + _StartPosY = 15 + _StartPosY - (_StartPosY + drawingBoatHeight - picture_height); + } else if (_StartPosY < 10) _StartPosY = 10; + } + return true; + } + + public void SetPosition(int x, int y) { + if (!(picture_width != null && picture_height != null)) return; + if (x + drawingBoatWidth > picture_width) { + _StartPosX = x - (x + drawingBoatWidth - picture_width); + } else if (x < 10) _StartPosX = 10; + else _StartPosX = x; + if (y + drawingBoatHeight + 15 > picture_height) { + _StartPosY = 15 + y - (y + drawingBoatHeight - picture_height); + } else if (y < 10) _StartPosY = 15; + else _StartPosY = y; + } + + public boolean MoveTransport(DirectionType direction) { + if (EntityBoat == null || _StartPosX == null || _StartPosY == null) return false; + switch (direction) { + case Left: + if (_StartPosX - EntityBoat.Step > 0) { + _StartPosX -= (int) EntityBoat.Step; + } + return true; + case Up: + if (_StartPosY - (EntityBoat.Step + 15) > 0) { + _StartPosY -= (int) EntityBoat.Step; + } + return true; + case Right: + if (_StartPosX + drawingBoatWidth + (int) EntityBoat.Step < picture_width - EntityBoat.Step) { + _StartPosX += (int) EntityBoat.Step; + } + return true; + case Down: + if (_StartPosY + drawingBoatHeight + (int) EntityBoat.Step < picture_height - EntityBoat.Step) { + _StartPosY += (int) EntityBoat.Step; + } + return true; + default: + return false; + } + } + + public void DrawTransport(Graphics2D g) { + if (EntityBoat == null || _StartPosX == null || _StartPosY == null) return; + int y = _StartPosY; + + g.setColor(EntityBoat.getBodyColor()); + if (drawingPaddles.getNumberOfPaddles() != null) { + switch (drawingPaddles.getNumberOfPaddles()) { + case OnePaddle: + drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityBoat.getBodyColor()); + + break; + case TwoPaddles: + drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityBoat.getBodyColor()); + drawingPaddles.DrawPaddles(g, _StartPosX + 45, y, 100, 15, EntityBoat.getBodyColor()); + + break; + case ThreePaddles: + drawingPaddles.DrawPaddles(g, _StartPosX + 15, y, 100, 15, EntityBoat.getBodyColor()); + drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityBoat.getBodyColor()); + drawingPaddles.DrawPaddles(g, _StartPosX + 45, y, 100, 15, EntityBoat.getBodyColor()); + break; + } + } + + Point[] hull = new Point[]{ + new Point(_StartPosX + 5, _StartPosY), + new Point(_StartPosX + 100, _StartPosY), + new Point(_StartPosX + 140, _StartPosY + 35), + new Point(_StartPosX + 100, _StartPosY + 70), + new Point(_StartPosX + 5, _StartPosY + 70), + }; + + Polygon hullPolygon = new Polygon(); + for (Point point : hull) { + hullPolygon.addPoint(point.x, point.y); + } + g.setColor(EntityBoat.getBodyColor()); + g.fillPolygon(hullPolygon); + g.setColor(Color.BLACK); + g.drawPolygon(hullPolygon); + } +} diff --git a/ProjectMotorBoat/src/DrawingBoat/DrawingMotorBoat.java b/ProjectMotorBoat/src/DrawingBoat/DrawingMotorBoat.java new file mode 100644 index 0000000..448f7ab --- /dev/null +++ b/ProjectMotorBoat/src/DrawingBoat/DrawingMotorBoat.java @@ -0,0 +1,49 @@ +package DrawingBoat; + +import Entities.EntityMotorBoat; + +import java.awt.*; + +public class DrawingMotorBoat extends DrawingBoat { + public DrawingMotorBoat(int speed, double weight, Color bodycolor, Color additionalcolor, boolean glass, boolean engine) { + EntityBoat = new EntityMotorBoat(speed, weight, bodycolor, additionalcolor, glass, engine); + SetAmountandTypePaddles(); + } + @Override + public void DrawTransport(Graphics2D g) { + if (EntityBoat == null || !(EntityBoat instanceof EntityMotorBoat motorboat) || _StartPosX == null || _StartPosY == null) + return; + int y = _StartPosY; + super.DrawTransport(g); + int count_paddles = 0; + if (drawingPaddles.getNumberOfPaddles() != null) { + switch (drawingPaddles.getNumberOfPaddles()) { + case OnePaddle: + count_paddles = 1; + break; + case TwoPaddles: + count_paddles = 2; + break; + case ThreePaddles: + count_paddles = 3; + break; + } + } + + if (motorboat.Glass) { + //стекло + g.setColor(Color.blue); + g.fillRect(_StartPosX + 20, _StartPosY + 15, 80, 40); + g.setColor(Color.BLACK); + g.drawRect(_StartPosX + 20, _StartPosY + 15, 80, 40); + } + if (motorboat.Engine) { + g.setColor(motorboat.getAdditionalColor()); + g.fillRect(_StartPosX, _StartPosY + 10, 5, 50); + g.setColor(Color.BLACK); + g.drawRect(_StartPosX, _StartPosY + 10, 5, 50); + + } + } +} + diff --git a/ProjectMotorBoat/src/DrawingMotorBoat.java b/ProjectMotorBoat/src/DrawingMotorBoat.java deleted file mode 100644 index 25a58c7..0000000 --- a/ProjectMotorBoat/src/DrawingMotorBoat.java +++ /dev/null @@ -1,145 +0,0 @@ -import javax.swing.*; -import java.awt.*; - -public class DrawingMotorBoat extends JPanel { - public EntityMotorBoat EntityMotorBoat; - public DrawingPaddles drawingPaddles = null; - private Integer picture_width; - private Integer picture_height; - private Integer _StartPosX; - private Integer _StartPosY; - private int drawingBoatWidth = 140; - private int drawingBoatHeight = 110; - public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean glass, boolean engine) { - EntityMotorBoat = new EntityMotorBoat(); - EntityMotorBoat.Init(speed, weight, bodycolor, additionalcolor, glass, engine); - picture_width = null; - picture_height = null; - _StartPosX = null; - _StartPosY = null; - drawingPaddles = new DrawingPaddles(); - drawingPaddles.setNumberOfPaddles((int)(Math.random() * 4 + 0)); - } - public boolean SetPictureSize(int width, int height) { - if (width < drawingBoatWidth || height < drawingBoatHeight) return false; - picture_width = width; - picture_height = height; - if (_StartPosX != null || _StartPosY != null) { - if (_StartPosX + drawingBoatWidth > picture_width) - { - _StartPosX = _StartPosX - (_StartPosX + drawingBoatWidth - picture_width); - } - else if (_StartPosX < 10) _StartPosX = 10; - if (_StartPosY + drawingBoatHeight + 15 > picture_height) - { - _StartPosY = 15+_StartPosY - (_StartPosY + drawingBoatHeight - picture_height); - } - else if (_StartPosY < 10) _StartPosY = 10; - } - return true; - } - public void SetPosition(int x, int y) { - if (!(picture_width != null && picture_height != null)) return; - if (x + drawingBoatWidth > picture_width) - { - _StartPosX = x - (x + drawingBoatWidth - picture_width); - } - else if (x < 10) _StartPosX = 10; - else _StartPosX = x; - if (y + drawingBoatHeight + 15 > picture_height) - { - _StartPosY = 15 + y - (y + drawingBoatHeight - picture_height); - } - else if (y < 10) _StartPosY = 15; - else _StartPosY = y; - } - public boolean MoveTransport(DirectionType direction) { - if (EntityMotorBoat == null || _StartPosX == null || _StartPosY == null) return false; - switch (direction) { - case Left: - if (_StartPosX - EntityMotorBoat.Step > 0) { - _StartPosX -= (int)EntityMotorBoat.Step; - } - return true; - case Up: - if (_StartPosY - (EntityMotorBoat.Step+15) > 0) - { - _StartPosY -= (int)EntityMotorBoat.Step; - } - return true; - case Right: - if (_StartPosX + drawingBoatWidth + (int)EntityMotorBoat.Step < picture_width - EntityMotorBoat.Step) - { - _StartPosX += (int)EntityMotorBoat.Step; - } - return true; - case Down: - if (_StartPosY + drawingBoatHeight + (int)EntityMotorBoat.Step < picture_height - EntityMotorBoat.Step) - { - _StartPosY += (int)EntityMotorBoat.Step; - } - return true; - default: - return false; - } - } - public void DrawTransport(Graphics2D g) { - if (EntityMotorBoat == null || _StartPosX == null || _StartPosY == null) return; - int y = _StartPosY; - - g.setColor(EntityMotorBoat.getBodyColor()); - if (drawingPaddles.getNumberOfPaddles() != null) { - switch (drawingPaddles.getNumberOfPaddles()) { - case OnePaddle: - drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityMotorBoat.getBodyColor()); - - break; - case TwoPaddles: - drawingPaddles.DrawPaddles(g, _StartPosX + 30, y, 100, 15, EntityMotorBoat.getBodyColor()); - drawingPaddles.DrawPaddles(g, _StartPosX + 45, y , 100, 15, EntityMotorBoat.getBodyColor()); - - break; - case ThreePaddles: - drawingPaddles.DrawPaddles(g, _StartPosX + 15, y, 100, 15, EntityMotorBoat.getBodyColor()); - drawingPaddles.DrawPaddles(g, _StartPosX + 30, y , 100, 15, EntityMotorBoat.getBodyColor()); - drawingPaddles.DrawPaddles(g, _StartPosX + 45, y , 100, 15, EntityMotorBoat.getBodyColor()); - - break; - } - } - - Point[] hull = new Point[] { - new Point(_StartPosX + 5, _StartPosY), - new Point(_StartPosX + 100, _StartPosY), - new Point(_StartPosX + 140, _StartPosY + 35), - new Point(_StartPosX + 100, _StartPosY + 70), - new Point(_StartPosX + 5, _StartPosY + 70), - }; - - Polygon hullPolygon = new Polygon(); - for (Point point : hull) { - hullPolygon.addPoint(point.x, point.y); - } - g.setColor(EntityMotorBoat.getBodyColor()); - g.fillPolygon(hullPolygon); - g.setColor(Color.BLACK); - g.drawPolygon(hullPolygon); - - - if (EntityMotorBoat.getGlass()) { - //стекло - g.setColor(Color.blue); - g.fillRect(_StartPosX + 20, _StartPosY + 15, 80, 40); - g.setColor(Color.BLACK); - g.drawRect(_StartPosX + 20, _StartPosY + 15, 80, 40); - } - - // Двигатель - if (EntityMotorBoat.getEngine()) { - g.setColor(EntityMotorBoat.getAdditionalColor()); - g.fillRect(_StartPosX, _StartPosY + 10, 5, 50); - g.setColor(Color.BLACK); - g.drawRect(_StartPosX, _StartPosY + 10, 5, 50); - } - } -} diff --git a/ProjectMotorBoat/src/Entities/EntityBoat.java b/ProjectMotorBoat/src/Entities/EntityBoat.java new file mode 100644 index 0000000..2c45ab9 --- /dev/null +++ b/ProjectMotorBoat/src/Entities/EntityBoat.java @@ -0,0 +1,19 @@ +package Entities; + +import java.awt.*; + +public class EntityBoat { + private int Speed; + private double Weight; + private Color BodyColor; + public Color getBodyColor() {return BodyColor;} + public double Step; + public EntityBoat(int speed, double weight, Color bodycolor) + { + Speed = speed; + Weight = weight; + BodyColor = bodycolor; + Step = Speed * 100 / Weight; + } +} + diff --git a/ProjectMotorBoat/src/Entities/EntityMotorBoat.java b/ProjectMotorBoat/src/Entities/EntityMotorBoat.java new file mode 100644 index 0000000..73259d5 --- /dev/null +++ b/ProjectMotorBoat/src/Entities/EntityMotorBoat.java @@ -0,0 +1,18 @@ +package Entities; + +import java.awt.*; +public class EntityMotorBoat extends EntityBoat{ + public Color AdditionalColor; + public Color getAdditionalColor() {return AdditionalColor;} + public boolean Glass; + public boolean Engine; + public EntityMotorBoat(int speed, double weight, Color bodyColor, Color additionalcolor, boolean engine, boolean glass) + { + super(speed, weight, bodyColor); + AdditionalColor = additionalcolor; + Glass = glass; + Engine = engine; + } +} + + diff --git a/ProjectMotorBoat/src/EntityMotorBoat.java b/ProjectMotorBoat/src/EntityMotorBoat.java deleted file mode 100644 index 7150968..0000000 --- a/ProjectMotorBoat/src/EntityMotorBoat.java +++ /dev/null @@ -1,25 +0,0 @@ -import java.awt.*; -public class EntityMotorBoat { - private int Speed; - private double Weight; - private Color BodyColor; - public Color getBodyColor() {return BodyColor;} - private Color AdditionalColor; - public Color getAdditionalColor() {return AdditionalColor;} - private boolean Glass; - public boolean getGlass() {return Glass;} - private boolean Engine; - public boolean getEngine() {return Engine;} - public double Step; - public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean glass, boolean engine) - { - Speed = speed; - Weight = weight; - BodyColor = bodycolor; - AdditionalColor = additionalcolor; - Glass = glass; - Engine = engine; - Step = Speed * 100 / Weight; - } -} - diff --git a/ProjectMotorBoat/src/FormMotorBoat.java b/ProjectMotorBoat/src/FormMotorBoat.java index ef5a983..3c79a52 100644 --- a/ProjectMotorBoat/src/FormMotorBoat.java +++ b/ProjectMotorBoat/src/FormMotorBoat.java @@ -1,3 +1,9 @@ +import DrawingBoat.CanvasMotorBoat; +import DrawingBoat.DirectionType; +import DrawingBoat.DrawingBoat; +import DrawingBoat.DrawingMotorBoat; +import MovementStrategy.*; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -10,24 +16,58 @@ public class FormMotorBoat extends JFrame { private String title; private Dimension dimension; private int Width, Height; - private CanvasMotorBoat canvasMotorBoat = new CanvasMotorBoat(); - private JButton CreateButton = new JButton("Create");; + public CanvasMotorBoat canvasMotorBoat = new CanvasMotorBoat(); + private JButton CreateButton = new JButton("Create MotorBoat");; + private JButton CreateShipButton = new JButton("Create Boat"); private JButton UpButton = new JButton(); private JButton DownButton = new JButton();; private JButton LeftButton = new JButton();; private JButton RightButton = new JButton(); + private AbstractStrategy _strategy; + private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"}); + private JButton ButtonStrategy = new JButton("Шаг"); public FormMotorBoat(String title, Dimension dimension) { this.title = title; this.dimension = dimension; } + private void CreateObject(String typeOfClass) { + int StartPositionX = (int)(Math.random() * 90 + 10); + int StartPositionY = (int)(Math.random() * 90 + 10); + int speed = (int)(Math.random() * 300 + 100); + double weight = (double)(Math.random() * 3000 + 1000); + Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); + switch (typeOfClass) { + case "DrawingBoat": + canvasMotorBoat._drawingBoat = new DrawingBoat(speed, weight, bodyColor); + canvasMotorBoat._drawingBoat.SetPictureSize(Width, Height); + canvasMotorBoat._drawingBoat.SetPosition(StartPositionX, StartPositionY); + canvasMotorBoat.repaint(); + break; + case "DrawingMotorBoat": + Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; + boolean glass = new Random().nextBoolean(); + boolean engine = new Random().nextBoolean();; + canvasMotorBoat._drawingBoat = new DrawingMotorBoat(speed, weight, bodyColor, additionalColor, glass, engine); + canvasMotorBoat._drawingBoat.SetPictureSize(Width, Height); + canvasMotorBoat._drawingBoat.SetPosition(StartPositionX, StartPositionY); + canvasMotorBoat.repaint(); + break; + default: return; + } + _strategy = null; + ComboBoxStrategy.setEnabled(true); + } public void Init() { setTitle(title); setMinimumSize(dimension); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - Width = getWidth() - 15; - Height = getHeight() - 35; + + Width = getWidth() - 10; + Height = getHeight() - 34; + _strategy = null; CreateButton.setName("CREATE"); + CreateShipButton.setName("CREATEBOATBUTTON"); Icon iconUp = new ImageIcon("src\\images\\up.png"); UpButton.setIcon(iconUp); UpButton.setName("UP"); @@ -44,39 +84,73 @@ public class FormMotorBoat extends JFrame { CreateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - int StartPositionX = (int)(Math.random() * 90 + 10); - int StartPositionY = (int)(Math.random() * 90 + 10); - int speed = (int)(Math.random() * 300 + 100); - double weight = (double)(Math.random() * 3000 + 1000); - Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); - Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; - boolean engine = new Random().nextBoolean(); - boolean glass = new Random().nextBoolean();; - canvasMotorBoat._drawingMotorBoat = new DrawingMotorBoat(); - canvasMotorBoat._drawingMotorBoat.Init(speed, weight, bodyColor, additionalColor, glass, engine); - canvasMotorBoat._drawingMotorBoat.SetPictureSize(Width, Height); - canvasMotorBoat._drawingMotorBoat.SetPosition( StartPositionX, StartPositionY); - canvasMotorBoat.repaint(); + CreateObject("DrawingMotorBoat"); + } + }); + + CreateShipButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawingBoat"); + } + }); + + ButtonStrategy.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (canvasMotorBoat._drawingBoat == null) return; + if (ComboBoxStrategy.isEnabled()) + { + int index = ComboBoxStrategy.getSelectedIndex(); + switch(index) + { + case 0: + _strategy = new MoveToCenter(); + break; + case 1: + _strategy = new MoveToBorder(); + break; + default: + _strategy = null; + break; + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableBoat(canvasMotorBoat._drawingBoat), Width, Height); + } + if (_strategy == null) + { + return; + } + ComboBoxStrategy.setEnabled(false); + _strategy.MakeStep(); + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + ComboBoxStrategy.setEnabled(true); + _strategy = null; + } } }); ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { - if (canvasMotorBoat._drawingMotorBoat == null) return; + if (canvasMotorBoat._drawingBoat == null) return; boolean result = false; switch ((((JButton)(event.getSource())).getName())) { case "UP": - result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Up); + result = canvasMotorBoat._drawingBoat.MoveTransport(DirectionType.Up); break; case "DOWN": - result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Down); + result = canvasMotorBoat._drawingBoat.MoveTransport(DirectionType.Down); break; case "LEFT": - result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Left); + result = canvasMotorBoat._drawingBoat.MoveTransport(DirectionType.Left); break; case "RIGHT": - result = canvasMotorBoat._drawingMotorBoat.MoveTransport(DirectionType.Right); + result = canvasMotorBoat._drawingBoat.MoveTransport(DirectionType.Right); break; } if (result) { @@ -92,32 +166,42 @@ public class FormMotorBoat extends JFrame { setSize(dimension.width,dimension.height); setLayout(null); canvasMotorBoat.setBounds(0,0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 100, 40); + CreateButton.setBounds(10, getHeight() - 90, 140, 40); + CreateShipButton.setBounds(160, getHeight() - 90, 140, 40); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 35, 34); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 35, 34); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 35, 34); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 35, 34); + ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); + ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25); add(CreateButton); + add(CreateShipButton); add(UpButton); add(DownButton); add(RightButton); add(LeftButton); + add(ComboBoxStrategy); + add(ButtonStrategy); add(canvasMotorBoat); setVisible(true); //обработка события изменения размеров окна addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { - Width = getWidth() - 15; - Height = getHeight() - 35; - if (canvasMotorBoat._drawingMotorBoat != null) - canvasMotorBoat._drawingMotorBoat.SetPictureSize(Width, Height); + Width = getWidth() - 10; + Height = getHeight() - 34; + if (canvasMotorBoat._drawingBoat != null) + canvasMotorBoat._drawingBoat.SetPictureSize(Width, Height); canvasMotorBoat.setBounds(0,0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 100, 40); + CreateButton.setBounds(10, getHeight() - 90, 140, 40); + CreateShipButton.setBounds(160, getHeight() - 90, 140, 40); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 35, 34); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 35, 34); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 35, 34); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 35, 34); + ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); + ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25); } }); } } + diff --git a/ProjectMotorBoat/src/Main.java b/ProjectMotorBoat/src/Main.java index 0389743..fe74b43 100644 --- a/ProjectMotorBoat/src/Main.java +++ b/ProjectMotorBoat/src/Main.java @@ -2,7 +2,7 @@ import java.awt.*; public class Main { public static void main(String[] args) { - FormMotorBoat form = new FormMotorBoat("Моторная лодка", new Dimension(500,500)); + FormMotorBoat form = new FormMotorBoat("Моторная лодка", new Dimension(700,500)); form.Init(); } } \ No newline at end of file diff --git a/ProjectMotorBoat/src/MovementStrategy/AbstractStrategy.java b/ProjectMotorBoat/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..340b060 --- /dev/null +++ b/ProjectMotorBoat/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,57 @@ +package MovementStrategy; + +public abstract class AbstractStrategy { + private IMoveableObjects _moveableObject; + private StrategyStatus _state = StrategyStatus.NotInit; + public int FieldWidth; + public int 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 (IsTargetDestinaion()) + { + _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 Integer GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject.GetStep(); + } + protected abstract void MoveToTarget(); + protected abstract boolean IsTargetDestinaion(); + private boolean MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + boolean stateTryMoveObject = _moveableObject.TryMoveObject(movementDirection); + if (stateTryMoveObject) return stateTryMoveObject; + return false; + } +} + diff --git a/ProjectMotorBoat/src/MovementStrategy/IMoveableObjects.java b/ProjectMotorBoat/src/MovementStrategy/IMoveableObjects.java new file mode 100644 index 0000000..ee79330 --- /dev/null +++ b/ProjectMotorBoat/src/MovementStrategy/IMoveableObjects.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public interface IMoveableObjects { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean TryMoveObject(MovementDirection direction); +} + diff --git a/ProjectMotorBoat/src/MovementStrategy/MoveToBorder.java b/ProjectMotorBoat/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..b136def --- /dev/null +++ b/ProjectMotorBoat/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,28 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth-GetStep() && + objParams.DownBorder + GetStep() >= FieldHeight-GetStep(); + } + @Override + protected void MoveToTarget() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + //реализация в правый нижний угол + int x = objParams.RightBorder; + if (x + GetStep() < FieldWidth) MoveRight(); + int y = objParams.DownBorder; + if (y + GetStep() < FieldHeight) MoveDown(); + } +} + diff --git a/ProjectMotorBoat/src/MovementStrategy/MoveToCenter.java b/ProjectMotorBoat/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..9db34d8 --- /dev/null +++ b/ProjectMotorBoat/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,49 @@ +package MovementStrategy; + +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight /2; + } + @Override + protected void MoveToTarget() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} + diff --git a/ProjectMotorBoat/src/MovementStrategy/MoveableBoat.java b/ProjectMotorBoat/src/MovementStrategy/MoveableBoat.java new file mode 100644 index 0000000..4817daf --- /dev/null +++ b/ProjectMotorBoat/src/MovementStrategy/MoveableBoat.java @@ -0,0 +1,46 @@ +package MovementStrategy; + +import DrawingBoat.CanvasMotorBoat; +import DrawingBoat.DirectionType; +import DrawingBoat.DrawingBoat; + +public class MoveableBoat implements IMoveableObjects{ + private CanvasMotorBoat canvas = new CanvasMotorBoat(); + public MoveableBoat(DrawingBoat drawingboat) + { + canvas._drawingBoat = drawingboat; + } + @Override + public ObjectParameters GetObjectPosition() { + if (canvas._drawingBoat == null || canvas._drawingBoat.EntityBoat == null || + canvas._drawingBoat.GetPosX() == null || canvas._drawingBoat.GetPosY() == null) + { + return null; + } + return new ObjectParameters(canvas._drawingBoat.GetPosX(), canvas._drawingBoat.GetPosY(), + canvas._drawingBoat.GetWidth(), canvas._drawingBoat.GetHeight()); + } + @Override + public int GetStep() { + return (int)(canvas._drawingBoat.EntityBoat.Step); + } + @Override + public boolean TryMoveObject(MovementDirection direction) { + if (canvas._drawingBoat == null || canvas._drawingBoat.EntityBoat == null) + { + return false; + } + return canvas._drawingBoat.MoveTransport(GetDirectionType(direction)); + } + private static DirectionType GetDirectionType(MovementDirection direction) + { + switch (direction) { + case MovementDirection.Left: return DirectionType.Left; + case MovementDirection.Right: return DirectionType.Right; + case MovementDirection.Up: return DirectionType.Up; + case MovementDirection.Down: return DirectionType.Down; + default: return DirectionType.Unknown; + } + } +} + diff --git a/ProjectMotorBoat/src/MovementStrategy/MovementDirection.java b/ProjectMotorBoat/src/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..1716b6d --- /dev/null +++ b/ProjectMotorBoat/src/MovementStrategy/MovementDirection.java @@ -0,0 +1,9 @@ +package MovementStrategy; + +public enum MovementDirection { + Up, + Down, + Left, + Right +} + diff --git a/ProjectMotorBoat/src/MovementStrategy/ObjectParameters.java b/ProjectMotorBoat/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..748de9d --- /dev/null +++ b/ProjectMotorBoat/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,28 @@ +package MovementStrategy; + +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private int _height; + public int LeftBorder = _x; + public int TopBorder = _y; + public int RightBorder = _x + _width; + public int DownBorder = _y + _height; + public int ObjectMiddleHorizontal = _x + _width / 2; + public int ObjectMiddleVertical = _y + _height / 2; + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + LeftBorder = _x; + TopBorder = _y; + RightBorder = _x + _width; + DownBorder = _y + _height; + ObjectMiddleHorizontal = _x + _width / 2; + ObjectMiddleVertical = _y + _height / 2; + } +} + diff --git a/ProjectMotorBoat/src/MovementStrategy/StrategyStatus.java b/ProjectMotorBoat/src/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..16d60de --- /dev/null +++ b/ProjectMotorBoat/src/MovementStrategy/StrategyStatus.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public enum StrategyStatus { + NotInit, + InProgress, + Finish +} +