diff --git a/ProjectMonorail/src/DirectionType.java b/ProjectMonorail/src/Drawings/DirectionType.java similarity index 78% rename from ProjectMonorail/src/DirectionType.java rename to ProjectMonorail/src/Drawings/DirectionType.java index 35657f0..e62647b 100644 --- a/ProjectMonorail/src/DirectionType.java +++ b/ProjectMonorail/src/Drawings/DirectionType.java @@ -1,3 +1,5 @@ +package Drawings; + public enum DirectionType { Up, Down, diff --git a/ProjectMonorail/src/Drawings/DrawingMonorail.java b/ProjectMonorail/src/Drawings/DrawingMonorail.java new file mode 100644 index 0000000..43a92bc --- /dev/null +++ b/ProjectMonorail/src/Drawings/DrawingMonorail.java @@ -0,0 +1,54 @@ +package Drawings; + +import Entities.*; +import java.awt.*; + +public class DrawingMonorail extends DrawingTrain{ + EntityMonorail entityMonorail; + + public DrawingMonorail(int speed, double weight, Color bodyColor, int wheelsType, Color additionalColor, boolean rail, boolean secondCarriage) { + super(speed, weight, bodyColor, wheelsType, 150, 40); + entityMonorail = new EntityMonorail(speed, weight, bodyColor, additionalColor, rail, secondCarriage); + } + + @Override + public void DrawTrain(Graphics g) { + if (entityMonorail == null || !(entityMonorail instanceof EntityMonorail) || _startPosX == null || _startPosY == null) { + return; + } + + super.DrawTrain(g); + Graphics2D g2d = (Graphics2D) g; + + if (entityMonorail.getRail()) { + g2d.setColor(entityMonorail.getAdditionalColor()); + if (entityMonorail.getSecondCarriage()){ + g2d.fillRect(_startPosX, _startPosY + 40, 150, 3); + } + else{ + g2d.fillRect(_startPosX, _startPosY + 40, 90, 3); + } + } + + if (entityMonorail.getSecondCarriage()) { + g2d.setColor(Color.BLACK); + g2d.drawRect(_startPosX + 95, _startPosY, 55, 30); + g2d.setColor(entityMonorail.getAdditionalColor()); + g2d.fillRect(_startPosX + 96, _startPosY + 1, 54, 29); + g2d.setColor(Color.BLACK); + g2d.fillRect(_startPosX + 85, _startPosY + 5, 10, 23); + g2d.drawRect(_startPosX + 105, _startPosY + 5, 35, 10); + g2d.setColor(Color.CYAN); + g2d.fillRect(_startPosX + 106, _startPosY + 6, 34, 9); + g2d.setColor(Color.BLACK); + g2d.drawRect(_startPosX + 105, _startPosY + 5, 10, 23); + g2d.setColor(Color.WHITE); + g2d.fillRect(_startPosX + 106, _startPosY + 16, 9, 12); + g2d.setColor(Color.BLACK); + g2d.drawOval(_startPosX + 100, _startPosY + 30, 10, 10); + g2d.drawOval(_startPosX + 110, _startPosY + 30, 10, 10); + g2d.drawOval(_startPosX + 130, _startPosY + 30, 10, 10); + g2d.drawOval(_startPosX + 140, _startPosY + 30, 10, 10); + } + } +} \ No newline at end of file diff --git a/ProjectMonorail/src/DrawingMonorailWheels.java b/ProjectMonorail/src/Drawings/DrawingMonorailWheels.java similarity index 94% rename from ProjectMonorail/src/DrawingMonorailWheels.java rename to ProjectMonorail/src/Drawings/DrawingMonorailWheels.java index ea594c2..e2e7c00 100644 --- a/ProjectMonorail/src/DrawingMonorailWheels.java +++ b/ProjectMonorail/src/Drawings/DrawingMonorailWheels.java @@ -1,6 +1,8 @@ +package Drawings; + import java.awt.*; -public class DrawingMonorailWheels { +public class DrawingMonorailWheels implements IDrawingWheels{ private WheelsCount _wheelsCount; public void setEnumNumber(int wheelsCount){ diff --git a/ProjectMonorail/src/Drawings/DrawingMonorailWheelsPlus.java b/ProjectMonorail/src/Drawings/DrawingMonorailWheelsPlus.java new file mode 100644 index 0000000..2f5a65d --- /dev/null +++ b/ProjectMonorail/src/Drawings/DrawingMonorailWheelsPlus.java @@ -0,0 +1,51 @@ +package Drawings; + +import java.awt.*; + +public class DrawingMonorailWheelsPlus implements IDrawingWheels{ + private WheelsCount _wheelsCount; + public void setEnumNumber(int wheelsCount){ + for (WheelsCount value : WheelsCount.values()){ + if (value.getEnumNumber() == wheelsCount){ + _wheelsCount = value; + return; + } + } + } + + public void drawWheels(Graphics2D g2d, Color color, int x, int y){ + g2d.setColor(Color.BLACK); + g2d.drawOval(x, y + 30, 10, 10); + g2d.setColor(Color.WHITE); + g2d.fillOval(x, y + 30, 10, 10); + g2d.setColor(Color.BLACK); + g2d.drawRect(x + 4, y + 30, 2, 10); + g2d.drawRect(x, y + 34, 10, 2); + g2d.setColor(color); + g2d.fillRect(x + 5, y + 31, 1, 9); + g2d.fillRect(x + 1, y + 35, 9, 1); + } + + public void drawMonorailWheels(Graphics g, Color color, int startPosX, int startPosY){ + Graphics2D g2d = (Graphics2D) g; + switch (_wheelsCount){ + case Two: + drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY); + drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY); + break; + case Three: + drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY); + drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY); + drawWheels(g2d, color, startPosX + 30, startPosY); + break; + case Four: + drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY); + drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY); + drawWheels(g2d, color, startPosX + 30, startPosY); + drawWheels(g2d, color, startPosX + 50, startPosY); + break; + default: + break; + } + } +} diff --git a/ProjectMonorail/src/Drawings/DrawingMonorailWheelsSquare.java b/ProjectMonorail/src/Drawings/DrawingMonorailWheelsSquare.java new file mode 100644 index 0000000..ee0333b --- /dev/null +++ b/ProjectMonorail/src/Drawings/DrawingMonorailWheelsSquare.java @@ -0,0 +1,49 @@ +package Drawings; + +import java.awt.*; + +public class DrawingMonorailWheelsSquare implements IDrawingWheels{ + private WheelsCount _wheelsCount; + public void setEnumNumber(int wheelsCount){ + for (WheelsCount value : WheelsCount.values()){ + if (value.getEnumNumber() == wheelsCount){ + _wheelsCount = value; + return; + } + } + } + + public void drawWheels(Graphics2D g2d, Color color, int x, int y){ + g2d.setColor(Color.BLACK); + g2d.drawOval(x, y + 30, 10, 10); + g2d.setColor(Color.WHITE); + g2d.fillOval(x, y + 30, 10, 10); + g2d.setColor(Color.BLACK); + g2d.drawRect(x + 2, y + 32, 6, 6); + g2d.setColor(color); + g2d.fillRect(x + 3, y + 33, 5, 5); + } + + public void drawMonorailWheels(Graphics g, Color color, int startPosX, int startPosY){ + Graphics2D g2d = (Graphics2D) g; + switch (_wheelsCount){ + case Two: + drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY); + drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY); + break; + case Three: + drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY); + drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY); + drawWheels(g2d, color, startPosX + 30, startPosY); + break; + case Four: + drawWheels(g2d, Color.WHITE, startPosX + 15, startPosY); + drawWheels(g2d, Color.WHITE, startPosX + 65, startPosY); + drawWheels(g2d, color, startPosX + 30, startPosY); + drawWheels(g2d, color, startPosX + 50, startPosY); + break; + default: + break; + } + } +} diff --git a/ProjectMonorail/src/DrawingMonorail.java b/ProjectMonorail/src/Drawings/DrawingTrain.java similarity index 57% rename from ProjectMonorail/src/DrawingMonorail.java rename to ProjectMonorail/src/Drawings/DrawingTrain.java index 6e7a58f..8432f3f 100644 --- a/ProjectMonorail/src/DrawingMonorail.java +++ b/ProjectMonorail/src/Drawings/DrawingTrain.java @@ -1,34 +1,53 @@ +package Drawings; +import Entities.EntityTrain; + import java.awt.*; import java.util.Random; -public class DrawingMonorail { - private EntityMonorail entityMonorail; - public EntityMonorail getEntityMonorail(){return entityMonorail;} - private DrawingMonorailWheels drawingMonorailWheels; +public class DrawingTrain { + private EntityTrain entityTrain; + public EntityTrain getEntityTrain(){return entityTrain;} + private IDrawingWheels drawingWheels; private Integer _pictureWidth; - private Integer _pictureHeight; + protected Integer _startPosX; + protected Integer _startPosY; + private int _drawingTrainWidth = 100; + private int _drawingTrainHeight = 40; + public int getPosX(){return _startPosX;} + public int getPosY(){return _startPosY;} + public int getTrainWidth(){return _drawingTrainWidth;} + public int getTrainHeight(){return _drawingTrainHeight;} - private Integer _startPosX; - - private Integer _startPosY; - - private final int _drawingTrainWidth = 150; - - private final int _drawingTrainHeight = 40; - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean rail, boolean secondCarriage){ - entityMonorail = new EntityMonorail(); - entityMonorail.Init(speed, weight, bodyColor, additionalColor, rail, secondCarriage); + public DrawingTrain(int speed, double weight, Color bodyColor, int wheelsType){ + entityTrain = new EntityTrain(speed, weight, bodyColor); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; - drawingMonorailWheels = new DrawingMonorailWheels(); + switch(wheelsType){ + case 0: + drawingWheels = new DrawingMonorailWheels(); + break; + case 1: + drawingWheels = new DrawingMonorailWheelsPlus(); + break; + case 2: + drawingWheels = new DrawingMonorailWheelsSquare(); + break; + default: + break; + } Random rand = new Random(); int wheelsCount = rand.nextInt(2, 5); - drawingMonorailWheels.setEnumNumber(wheelsCount); + drawingWheels.setEnumNumber(wheelsCount); + } + + public DrawingTrain(int speed, double weight, Color bodyColor, int wheelsType, int drawingTrainWidth, int drawingTrainHeight){ + this(speed, weight, bodyColor, wheelsType); + _drawingTrainWidth = drawingTrainWidth; + _drawingTrainHeight = drawingTrainHeight; } public boolean SetPictureSize(int width, int height){ @@ -66,41 +85,41 @@ public class DrawingMonorail { } public boolean MoveTransport(DirectionType direction){ - if (entityMonorail == null || _startPosX == null || _startPosY == null){ + if (entityTrain == null || _startPosX == null || _startPosY == null){ return false; } switch (direction){ - case Up: - if (_startPosY - entityMonorail.Step() > 0){ - _startPosY -= (int) entityMonorail.Step(); + case DirectionType.Up: + if (_startPosY - entityTrain.Step() > 0){ + _startPosY -= (int) entityTrain.Step(); } return true; - case Down: - if (_startPosY + entityMonorail.Step() + _drawingTrainHeight < _pictureHeight){ - _startPosY += (int) entityMonorail.Step(); + case DirectionType.Down: + if (_startPosY + entityTrain.Step() + _drawingTrainHeight < _pictureHeight){ + _startPosY += (int) entityTrain.Step(); } return true; - case Left: - if (_startPosX - entityMonorail.Step() > 0){ - _startPosX -= (int) entityMonorail.Step(); + case DirectionType.Left: + if (_startPosX - entityTrain.Step() > 0){ + _startPosX -= (int) entityTrain.Step(); } return true; - case Right: - if (_startPosX + entityMonorail.Step() + _drawingTrainWidth < _pictureWidth){ - _startPosX += (int) entityMonorail.Step(); + case DirectionType.Right: + if (_startPosX + entityTrain.Step() + _drawingTrainWidth < _pictureWidth){ + _startPosX += (int) entityTrain.Step(); } return true; default: return false; } } - public void DrawMonorail(Graphics g) { - if (entityMonorail == null || _startPosX == null || _startPosY == null) { + public void DrawTrain(Graphics g) { + if (entityTrain == null || _startPosX == null || _startPosY == null) { return; } - drawingMonorailWheels.drawMonorailWheels(g, entityMonorail.getAdditionalColor(), _startPosX, _startPosY); + drawingWheels.drawMonorailWheels(g, entityTrain.getBodyColor(), _startPosX, _startPosY); Graphics2D g2d = (Graphics2D) g; @@ -117,7 +136,7 @@ public class DrawingMonorail { monorailPolygon.addPoint(point.x, point.y); g2d.setColor(Color.BLACK); g2d.draw(monorailPolygon); - g2d.setColor(entityMonorail.getBodyColor()); + g2d.setColor(entityTrain.getBodyColor()); g2d.fill(monorailPolygon); Point[][] carts = new Point[][] { @@ -139,7 +158,6 @@ public class DrawingMonorail { new Point(_startPosX + 85, _startPosY + 30)} }; - g2d.setColor(Color.BLACK); for (int i = 0; i < 4; i++){ Polygon cartPolygon = new Polygon(); @@ -157,36 +175,5 @@ public class DrawingMonorail { g2d.fillRect(_startPosX + 56, _startPosY + 6, 26, 6); g2d.setColor(Color.WHITE); g2d.fillRect(_startPosX + 41, _startPosY + 8, 9, 20); - - - if (entityMonorail.getRail()) { - g2d.setColor(entityMonorail.getAdditionalColor()); - if (entityMonorail.getSecondCarriage()){ - g2d.fillRect(_startPosX, _startPosY + 40, 150, 3); - } - else{ - g2d.fillRect(_startPosX, _startPosY + 40, 90, 3); - } - } - if (entityMonorail.getSecondCarriage()) { - g2d.setColor(Color.BLACK); - g2d.drawRect(_startPosX + 95, _startPosY, 55, 30); - g2d.setColor(entityMonorail.getAdditionalColor()); - g2d.fillRect(_startPosX + 96, _startPosY + 1, 54, 29); - g2d.setColor(Color.BLACK); - g2d.fillRect(_startPosX + 85, _startPosY + 5, 10, 23); - g2d.drawRect(_startPosX + 105, _startPosY + 5, 35, 10); - g2d.setColor(Color.CYAN); - g2d.fillRect(_startPosX + 106, _startPosY + 6, 34, 9); - g2d.setColor(Color.BLACK); - g2d.drawRect(_startPosX + 105, _startPosY + 5, 10, 23); - g2d.setColor(Color.WHITE); - g2d.fillRect(_startPosX + 106, _startPosY + 16, 9, 12); - g2d.setColor(Color.BLACK); - g2d.drawOval(_startPosX + 100, _startPosY + 30, 10, 10); - g2d.drawOval(_startPosX + 110, _startPosY + 30, 10, 10); - g2d.drawOval(_startPosX + 130, _startPosY + 30, 10, 10); - g2d.drawOval(_startPosX + 140, _startPosY + 30, 10, 10); - } } -} \ No newline at end of file +} diff --git a/ProjectMonorail/src/Drawings/IDrawingWheels.java b/ProjectMonorail/src/Drawings/IDrawingWheels.java new file mode 100644 index 0000000..51817f0 --- /dev/null +++ b/ProjectMonorail/src/Drawings/IDrawingWheels.java @@ -0,0 +1,8 @@ +package Drawings; + +import java.awt.*; + +public interface IDrawingWheels { + void setEnumNumber(int x); + void drawMonorailWheels(Graphics g, Color color, int startX, int startY); +} diff --git a/ProjectMonorail/src/WheelsCount.java b/ProjectMonorail/src/Drawings/WheelsCount.java similarity index 92% rename from ProjectMonorail/src/WheelsCount.java rename to ProjectMonorail/src/Drawings/WheelsCount.java index 98a4684..3cdb7c2 100644 --- a/ProjectMonorail/src/WheelsCount.java +++ b/ProjectMonorail/src/Drawings/WheelsCount.java @@ -1,3 +1,5 @@ +package Drawings; + public enum WheelsCount { Two(2), Three(3), diff --git a/ProjectMonorail/src/Entities/EntityMonorail.java b/ProjectMonorail/src/Entities/EntityMonorail.java new file mode 100644 index 0000000..2a27217 --- /dev/null +++ b/ProjectMonorail/src/Entities/EntityMonorail.java @@ -0,0 +1,21 @@ +package Entities; + +import java.awt.*; + +public class EntityMonorail extends EntityTrain { + private Color AdditionalColor; + public Color getAdditionalColor(){return AdditionalColor;}; + + private boolean Rail; + public boolean getRail(){return Rail;} + + private boolean SecondCarriage; + public boolean getSecondCarriage(){return SecondCarriage;} + + public EntityMonorail(int speed, double weight, Color bodyColor, Color additionalColor, boolean rail, boolean secondCarriage) { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + Rail = rail; + SecondCarriage = secondCarriage; + } +} diff --git a/ProjectMonorail/src/Entities/EntityTrain.java b/ProjectMonorail/src/Entities/EntityTrain.java new file mode 100644 index 0000000..d9fb36c --- /dev/null +++ b/ProjectMonorail/src/Entities/EntityTrain.java @@ -0,0 +1,22 @@ +package Entities; + +import java.awt.*; + +public class EntityTrain { + private int Speed; + public int getSpeed(){return Speed;} + + private double Weight; + public double getWeight(){return Weight;} + + private Color BodyColor; + public Color getBodyColor(){return BodyColor;}; + + public double Step(){return Speed*100/Weight;} + + public EntityTrain(int speed, double weight, Color bodyColor){ + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectMonorail/src/EntityMonorail.java b/ProjectMonorail/src/EntityMonorail.java deleted file mode 100644 index ba3d500..0000000 --- a/ProjectMonorail/src/EntityMonorail.java +++ /dev/null @@ -1,32 +0,0 @@ -import java.awt.*; - -public class EntityMonorail { - private int Speed; - public int getSpeed(){return Speed;} - - private double Weight; - public double getWeight(){return Weight;} - - private Color BodyColor; - public Color getBodyColor(){return BodyColor;}; - - private Color AdditionalColor; - public Color getAdditionalColor(){return AdditionalColor;}; - - private boolean Rail; - public boolean getRail(){return Rail;} - - private boolean SecondCarriage; - public boolean getSecondCarriage(){return SecondCarriage;} - - public double Step(){return Speed*100/Weight;} - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean rail, boolean secondCarriage){ - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Rail = rail; - SecondCarriage = secondCarriage; - } -} diff --git a/ProjectMonorail/src/FormMonorail.form b/ProjectMonorail/src/FormMonorail.form index cb39368..8185723 100644 --- a/ProjectMonorail/src/FormMonorail.form +++ b/ProjectMonorail/src/FormMonorail.form @@ -1,33 +1,28 @@
- + - + - - + + - + - + - + - - - - - - + @@ -39,7 +34,7 @@ - + @@ -52,7 +47,7 @@ - + @@ -65,7 +60,7 @@ - + @@ -76,16 +71,40 @@ - + - + + + + + + + + + + - + - + + + + + + + + + + + + + + diff --git a/ProjectMonorail/src/FormMonorail.java b/ProjectMonorail/src/FormMonorail.java index 3133de1..8ed27df 100644 --- a/ProjectMonorail/src/FormMonorail.java +++ b/ProjectMonorail/src/FormMonorail.java @@ -1,3 +1,6 @@ +import Drawings.*; +import MovementStrategy.*; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -7,16 +10,43 @@ import java.util.Random; import java.util.List; public class FormMonorail extends JFrame { - protected DrawingMonorail _drawingMonorail = new DrawingMonorail(); + protected DrawingTrain _drawingTrain; + private AbstractStrategy _strategy; JPanel PictureBox; - private JButton buttonCreate; + private JButton buttonCreateMonorail; + private JButton buttonCreateTrain; private JButton buttonRight; private JButton buttonDown; private JButton buttonLeft; private JButton buttonUp; + private JComboBox comboBoxStrategy; + private JButton buttonStrategyStep; private List controls; + public void createObject(String obj){ + Random rand = new Random(); + switch(obj){ + case "Train": + _drawingTrain = new DrawingTrain(rand.nextInt(100, 300), rand.nextDouble(1000, 3000), + new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), rand.nextInt(0, 3)); + break; + case "Monorail": + _drawingTrain = new DrawingMonorail(rand.nextInt(100, 300), rand.nextDouble(1000, 3000), + new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), rand.nextInt(0, 3), + new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), + rand.nextBoolean(), rand.nextBoolean()); + break; + default: + return; + } + _drawingTrain.SetPictureSize(PictureBox.getWidth(), PictureBox.getHeight()); + _drawingTrain.SetPosition(rand.nextInt(50), rand.nextInt(50)); + _strategy = null; + comboBoxStrategy.setEnabled(true); + Draw(); + } + public FormMonorail() { buttonUp.setName("buttonUp"); buttonDown.setName("buttonDown"); @@ -25,64 +55,76 @@ public class FormMonorail extends JFrame { InitializeControlsRepaintList(); - buttonCreate.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - _drawingMonorail = new DrawingMonorail(); - Random random = new Random(); + buttonCreateMonorail.addActionListener(e -> createObject("Monorail")); - _drawingMonorail.Init(random.nextInt(30, 100), - random.nextInt(100, 500), - 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()); - _drawingMonorail.SetPictureSize(PictureBox.getWidth(), PictureBox.getHeight()); - _drawingMonorail.SetPosition(random.nextInt(25, 100), - random.nextInt(25, 100)); + buttonCreateTrain.addActionListener(e -> createObject("Train")); + ActionListener buttonMoveClickedListener = e -> { + String buttonName = ((JButton) e.getSource()).getName(); + boolean result = false; + switch (buttonName) { + case "buttonUp": { + result = _drawingTrain.MoveTransport(DirectionType.Up); + } + break; + case "buttonDown": { + result = _drawingTrain.MoveTransport(DirectionType.Down); + } + break; + case "buttonLeft": { + result = _drawingTrain.MoveTransport(DirectionType.Left); + } + break; + case "buttonRight": { + result = _drawingTrain.MoveTransport(DirectionType.Right); + } + break; + + } + if (result) Draw(); - } - }); - ActionListener buttonMoveClickedListener = new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - String buttonName = ((JButton) e.getSource()).getName(); - boolean result = false; - - switch (buttonName) { - case "buttonUp": { - result = _drawingMonorail.MoveTransport(DirectionType.Up); - } - break; - case "buttonDown": { - result = _drawingMonorail.MoveTransport(DirectionType.Down); - } - break; - case "buttonLeft": { - result = _drawingMonorail.MoveTransport(DirectionType.Left); - } - break; - case "buttonRight": { - result = _drawingMonorail.MoveTransport(DirectionType.Right); - } - break; - - } - if (result) - Draw(); - - } }; buttonRight.addActionListener(buttonMoveClickedListener); buttonDown.addActionListener(buttonMoveClickedListener); buttonLeft.addActionListener(buttonMoveClickedListener); buttonUp.addActionListener(buttonMoveClickedListener); + comboBoxStrategy.addItem("К Центру"); + comboBoxStrategy.addItem("К Краю"); + buttonStrategyStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_drawingTrain == null){return;} + if (comboBoxStrategy.isEnabled()){ + switch (comboBoxStrategy.getSelectedIndex()){ + case 0: + _strategy = new MoveToCenter(); + break; + case 1: + _strategy = new MoveToBorder(); + break; + default: + _strategy = null; + break; + } + if (_strategy == null) {return;} + _strategy.SetData(new MovableTrain(_drawingTrain), PictureBox.getWidth(), PictureBox.getHeight()); + } + if (_strategy == null) {return;} + _strategy.MakeStep(); + Draw(); + comboBoxStrategy.setEnabled(false); + if (_strategy.GetStatus() == StrategyStatus.Finish) { + comboBoxStrategy.setEnabled(true); + _strategy = null; + } + } + }); } private void Draw() { - if (_drawingMonorail.getEntityMonorail() == null) + if (_drawingTrain.getEntityTrain() == null) return; if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) { return; @@ -90,7 +132,7 @@ public class FormMonorail extends JFrame { Graphics g = PictureBox.getGraphics(); g.setColor(PictureBox.getBackground()); g.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); - _drawingMonorail.DrawMonorail(g); + _drawingTrain.DrawTrain(g); RepaintControls(); @@ -105,7 +147,8 @@ public class FormMonorail extends JFrame { private void InitializeControlsRepaintList() { controls = new LinkedList<>(); - controls.add(buttonCreate); + controls.add(buttonCreateMonorail); + controls.add(buttonCreateTrain); controls.add(buttonUp); controls.add(buttonDown); controls.add(buttonLeft); diff --git a/ProjectMonorail/src/MovementStrategy/AbstractStrategy.java b/ProjectMonorail/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..3d5236d --- /dev/null +++ b/ProjectMonorail/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,61 @@ +package MovementStrategy; + +import Drawings.DirectionType; + +public abstract class AbstractStrategy { + private IMoveableObject _movableObject; + private StrategyStatus _state = StrategyStatus.NotInit; + protected int FieldWidth; + protected int FieldHeight; + public StrategyStatus GetStatus() { return _state; } + + public void SetData(IMoveableObject movableObject, int width, int height) { + if (movableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _movableObject = movableObject; + FieldHeight = height; + FieldWidth = width; + } + + public void MakeStep() { + if (_state != StrategyStatus.InProgress) { + return; + } + if (IsTargetDestination()) { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + + protected boolean MoveLeft() { return MoveTo(DirectionType.Left); } + protected boolean MoveRight() { return MoveTo(DirectionType.Right); } + protected boolean MoveUp() { return MoveTo(DirectionType.Up); } + protected boolean MoveDown() { return MoveTo(DirectionType.Down); } + + protected ObjectParameters GetObjectParameters() { return _movableObject.GetObjectPosition(); } + + protected int GetStep() { + if(_state != StrategyStatus.InProgress) { + return 0; + } + return _movableObject.GetStep(); + } + + protected abstract void MoveToTarget(); + protected abstract boolean IsTargetDestination(); + private boolean MoveTo(DirectionType directionType) { + if (_state != StrategyStatus.InProgress) { + return false; + } + if (_movableObject.TryMoveObject(directionType)) { + _movableObject.MoveObject(directionType); + return true; + } + return false; + } +} diff --git a/ProjectMonorail/src/MovementStrategy/IMoveableObject.java b/ProjectMonorail/src/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..1c286dd --- /dev/null +++ b/ProjectMonorail/src/MovementStrategy/IMoveableObject.java @@ -0,0 +1,10 @@ +package MovementStrategy; + +import Drawings.DirectionType; + +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean TryMoveObject(DirectionType direction); + void MoveObject(DirectionType direction); +} diff --git a/ProjectMonorail/src/MovementStrategy/MovableTrain.java b/ProjectMonorail/src/MovementStrategy/MovableTrain.java new file mode 100644 index 0000000..c26fac7 --- /dev/null +++ b/ProjectMonorail/src/MovementStrategy/MovableTrain.java @@ -0,0 +1,30 @@ +package MovementStrategy; + +import Drawings.DirectionType; +import Drawings.DrawingTrain; + +public class MovableTrain implements IMoveableObject { + private DrawingTrain _train = null; + public MovableTrain(DrawingTrain train){ + _train = train; + } + + public ObjectParameters GetObjectPosition() { + if (_train == null || _train.getEntityTrain() == null){ + return null; + } + return new ObjectParameters(_train.getPosX(), _train.getPosY(), _train.getTrainWidth(), _train.getTrainHeight()); + } + + public int GetStep() { + return (int) _train.getEntityTrain().Step(); + } + + public boolean TryMoveObject(DirectionType direction) { + return _train.MoveTransport(direction); + } + + public void MoveObject(DirectionType direction) { + _train.MoveTransport(direction); + } +} diff --git a/ProjectMonorail/src/MovementStrategy/MoveToBorder.java b/ProjectMonorail/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..e3f2ca6 --- /dev/null +++ b/ProjectMonorail/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,27 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy { + protected boolean IsTargetDestination() { + var objParams = GetObjectParameters(); + if (objParams == null){ + return false; + } + return objParams.RightBorder() <= FieldWidth && objParams.RightBorder() + GetStep() >= FieldWidth && + objParams.DownBorder() <= FieldHeight && objParams.DownBorder() + GetStep() >= FieldHeight; + } + + protected void MoveToTarget(){ + var objParams = GetObjectParameters(); + if (objParams == null){ + return; + } + int diffX = objParams.RightBorder() - FieldWidth; + if (Math.abs(diffX) > GetStep()){ + MoveRight(); + } + int diffY = objParams.DownBorder() - FieldHeight; + if (Math.abs(diffY) > GetStep()){ + MoveDown(); + } + } +} diff --git a/ProjectMonorail/src/MovementStrategy/MoveToCenter.java b/ProjectMonorail/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..b0b6eb9 --- /dev/null +++ b/ProjectMonorail/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,39 @@ +package MovementStrategy; + +public class MoveToCenter extends AbstractStrategy { + protected boolean IsTargetDestination(){ + var objParams = GetObjectParameters(); + if (objParams == null){ + return false; + } + return objParams.ObjectMidHorizontal() - GetStep() <= FieldWidth / 2 && + objParams.ObjectMidHorizontal() + GetStep() >= FieldWidth / 2 && + objParams.ObjectMidVertical() - GetStep() <= FieldHeight / 2 && + objParams.ObjectMidVertical() + GetStep() >= FieldHeight / 2; + } + + protected void MoveToTarget(){ + var objParams = GetObjectParameters(); + if (objParams == null){ + return; + } + int diffX = objParams.ObjectMidHorizontal() - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()){ + if (diffX > 0){ + MoveLeft(); + } + else{ + MoveRight(); + } + } + int diffY = objParams.ObjectMidVertical() - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()){ + if (diffY > 0){ + MoveUp(); + } + else{ + MoveDown(); + } + } + } +} diff --git a/ProjectMonorail/src/MovementStrategy/MovementDirection.java b/ProjectMonorail/src/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..c52f124 --- /dev/null +++ b/ProjectMonorail/src/MovementStrategy/MovementDirection.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public enum MovementDirection { + Up, + Down, + Left, + Right +} diff --git a/ProjectMonorail/src/MovementStrategy/ObjectParameters.java b/ProjectMonorail/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..f6c773e --- /dev/null +++ b/ProjectMonorail/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,23 @@ +package MovementStrategy; + +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private int _height; + + public int LeftBorder(){return _x;}; + public int TopBorder(){return _y;}; + public int RightBorder(){return _x + _width;}; + public int DownBorder(){return _y + _height;}; + + public int ObjectMidHorizontal(){return _x + _width / 2;}; + public int ObjectMidVertical(){return _y + _height / 2;}; + + public ObjectParameters(int x, int y, int width, int height){ + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/ProjectMonorail/src/MovementStrategy/StrategyStatus.java b/ProjectMonorail/src/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..4f087ea --- /dev/null +++ b/ProjectMonorail/src/MovementStrategy/StrategyStatus.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum StrategyStatus { + NotInit, + InProgress, + Finish +}