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 @@
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