diff --git a/src/DumpTruck/DrawingDumpTruck.java b/src/DumpTruck/DrawingDumpTruck.java deleted file mode 100644 index ec14ba9..0000000 --- a/src/DumpTruck/DrawingDumpTruck.java +++ /dev/null @@ -1,110 +0,0 @@ -package DumpTruck; - -import java.awt.*; - -public class DrawingDumpTruck { - - private EntityDumpTruck entityDumpTruck; - - public EntityDumpTruck getEntityDumpTruck() { - return entityDumpTruck; - } - - private DrawingWheels drawingWheels; - - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private int _truckWidth = 160; - private int _truckHeight = 90; - - public boolean Init(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber) - { - if (height < _truckHeight || width < _truckWidth) { - return false; - } - _pictureWidth = width; - _pictureHeight = height; - entityDumpTruck = new EntityDumpTruck(); - entityDumpTruck.Init(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor); - drawingWheels = new DrawingWheels(); - drawingWheels.setWheelNumber(wheelNumber); - return true; - } - - public void SetPosition(int x, int y) - { - if (x < 0 || x + _truckWidth > _pictureWidth) { x = 0; } - if (y < 0 || y + _truckHeight > _pictureHeight) { y = 0; } - _startPosX = x; - _startPosY = y; - } - - public void MoveTransport(DirectionType direction) { - if (entityDumpTruck == null) return; - - switch (direction) { - //влево - case Left: - if (_startPosX - entityDumpTruck.getStep() > 0) - { - _startPosX -= (int)entityDumpTruck.getStep(); - } - break; - //вверх - case Up: - if (_startPosY - entityDumpTruck.getStep() > 0) - { - _startPosY -= (int)entityDumpTruck.getStep(); - } - break; - //вправо - case Right: - if (_startPosX + _truckWidth + entityDumpTruck.getStep() < _pictureWidth) - { - _startPosX += (int)entityDumpTruck.getStep(); - } - break; - //вниз - case Down: - if (_startPosY + _truckHeight + entityDumpTruck.getStep() < _pictureHeight) - { - _startPosY += (int)entityDumpTruck.getStep(); - } - break; - } - } - - public void DrawTransport(Graphics2D g2D) - { - if (entityDumpTruck == null) { - return; - } - g2D.setStroke(new BasicStroke(3)); - g2D.setPaint(entityDumpTruck.getBodyColor()); - g2D.fillRect(_startPosX, _startPosY + 40, 160, 10); - g2D.fillRect(_startPosX + 120, _startPosY, 40, 40); - drawingWheels.drawWheels(g2D, Color.BLACK, _startPosX, _startPosY); - if (entityDumpTruck.getDumpBox()) - { - g2D.setPaint(entityDumpTruck.getDumpBoxColor()); - Polygon dumpBoxPolygon = new Polygon(); - dumpBoxPolygon.addPoint(_startPosX + 20, _startPosY); - dumpBoxPolygon.addPoint(_startPosX + 120, _startPosY); - dumpBoxPolygon.addPoint(_startPosX + 100, _startPosY + 39); - dumpBoxPolygon.addPoint(_startPosX, _startPosY + 39); - g2D.fillPolygon(dumpBoxPolygon); - } - if (entityDumpTruck.getDumpBox() && entityDumpTruck.getTent()) - { - g2D.setPaint(entityDumpTruck.getTentColor()); - Polygon tentPolygon = new Polygon(); - tentPolygon.addPoint(_startPosX + 15, _startPosY); - tentPolygon.addPoint(_startPosX + 120, _startPosY); - tentPolygon.addPoint(_startPosX + 115, _startPosY + 10); - tentPolygon.addPoint(_startPosX + 10, _startPosY + 10); - g2D.fillPolygon(tentPolygon); - } - } -} diff --git a/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java b/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java new file mode 100644 index 0000000..c5cc009 --- /dev/null +++ b/src/DumpTruck/DrawingObjects/DrawingDumpTruck.java @@ -0,0 +1,43 @@ +package DumpTruck.DrawingObjects; +import DumpTruck.Entities.*; + +import java.awt.*; + +public class DrawingDumpTruck extends DrawingTruck { + public DrawingDumpTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber) + { + super(speed, weight, bodyColor, width, height, 160, 90, wheelNumber); + if (getEntityTruck() != null) + { + setEntityTruck(new EntityDumpTruck(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor)); + } + } + + @Override + public void DrawTransport(Graphics2D g2D) { + if (!(getEntityTruck() instanceof EntityDumpTruck dumpTruck)) { + return; + } + super.DrawTransport(g2D); + if (dumpTruck.getDumpBox()) + { + g2D.setPaint(dumpTruck.getDumpBoxColor()); + Polygon dumpBoxPolygon = new Polygon(); + dumpBoxPolygon.addPoint(_startPosX + 20, _startPosY); + dumpBoxPolygon.addPoint(_startPosX + 120, _startPosY); + dumpBoxPolygon.addPoint(_startPosX + 100, _startPosY + 39); + dumpBoxPolygon.addPoint(_startPosX, _startPosY + 39); + g2D.fillPolygon(dumpBoxPolygon); + } + if (dumpTruck.getDumpBox() && dumpTruck.getTent()) + { + g2D.setPaint(dumpTruck.getTentColor()); + Polygon tentPolygon = new Polygon(); + tentPolygon.addPoint(_startPosX + 15, _startPosY); + tentPolygon.addPoint(_startPosX + 120, _startPosY); + tentPolygon.addPoint(_startPosX + 115, _startPosY + 10); + tentPolygon.addPoint(_startPosX + 10, _startPosY + 10); + g2D.fillPolygon(tentPolygon); + } + } +} diff --git a/src/DumpTruck/DrawingObjects/DrawingTruck.java b/src/DumpTruck/DrawingObjects/DrawingTruck.java new file mode 100644 index 0000000..aadd45f --- /dev/null +++ b/src/DumpTruck/DrawingObjects/DrawingTruck.java @@ -0,0 +1,150 @@ +package DumpTruck.DrawingObjects; + +import java.awt.*; +import java.util.Random; + +import DumpTruck.Entities.*; +import DumpTruck.DirectionType; +import DumpTruck.Wheels.*; + +public class DrawingTruck { + private EntityTruck entityTruck; + + public EntityTruck getEntityTruck() { + return entityTruck; + } + + protected void setEntityTruck(EntityTruck entityTruck) { + this.entityTruck = entityTruck; + } + + private IDrawingWheels drawingWheels; + + private int _pictureWidth; + private int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected int _truckWidth = 160; + protected int _truckHeight = 90; + + public int getPosX() { + return _startPosX; + } + + public int getPosY() { + return _startPosY; + } + + public int getWidth() { + return _truckWidth; + } + + public int getHeight() { + return _truckHeight; + } + + public DrawingTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor, int width, int height, int wheelNumber) + { + if (height < _truckHeight || width < _truckWidth) { + return; + } + _pictureWidth = width; + _pictureHeight = height; + entityTruck = new EntityTruck(speed, weight, bodyColor); + Random random = new Random(); + drawingWheels = switch (random.nextInt(0, 3)) { + case 0 -> new DrawingWheels(); + case 1 -> new DrawingWheelsStar(); + case 2 -> new DrawingWheelsCircles(); + default -> new DrawingWheels(); + }; + drawingWheels.setWheelNumber(wheelNumber); + } + + protected DrawingTruck(int speed, double weight, Color bodyColor, int width, int height, int truckWidth, int truckHeight, int wheelNumber) + { + if (width < truckWidth || height < truckHeight) return; + _pictureWidth = width; + _pictureHeight = height; + _truckWidth = truckWidth; + _truckHeight = truckHeight; + entityTruck = new EntityTruck(speed, weight, bodyColor); + Random random = new Random(); + drawingWheels = switch (random.nextInt(0, 3)) { + case 0 -> new DrawingWheels(); + case 1 -> new DrawingWheelsStar(); + case 2 -> new DrawingWheelsCircles(); + default -> new DrawingWheels(); + }; + drawingWheels.setWheelNumber(wheelNumber); + } + + public void SetPosition(int x, int y) + { + if (x < 0 || x + _truckWidth > _pictureWidth) { x = 0; } + if (y < 0 || y + _truckHeight > _pictureHeight) { y = 0; } + _startPosX = x; + _startPosY = y; + } + + public boolean CanMove(DirectionType direction) { + if (entityTruck == null) { + return false; + } + + return switch (direction) { + case Left -> _startPosX - entityTruck.getStep() > 0; + case Up -> _startPosY - entityTruck.getStep() > 0; + case Right -> _startPosX + _truckWidth + entityTruck.getStep() < _pictureWidth; + case Down -> _startPosY + _truckHeight + entityTruck.getStep() < _pictureHeight; + default -> false; + }; + } + + public void MoveTransport(DirectionType direction) { + if (!CanMove(direction) || entityTruck == null) return; + + switch (direction) { + //влево + case Left: + if (_startPosX - entityTruck.getStep() > 0) + { + _startPosX -= (int)entityTruck.getStep(); + } + break; + //вверх + case Up: + if (_startPosY - entityTruck.getStep() > 0) + { + _startPosY -= (int)entityTruck.getStep(); + } + break; + //вправо + case Right: + if (_startPosX + _truckWidth + entityTruck.getStep() < _pictureWidth) + { + _startPosX += (int)entityTruck.getStep(); + } + break; + //вниз + case Down: + if (_startPosY + _truckHeight + entityTruck.getStep() < _pictureHeight) + { + _startPosY += (int)entityTruck.getStep(); + } + break; + } + } + + public void DrawTransport(Graphics2D g2D) + { + if (entityTruck == null) { + return; + } + g2D.setStroke(new BasicStroke(3)); + g2D.setPaint(entityTruck.getBodyColor()); + g2D.fillRect(_startPosX, _startPosY + 40, 160, 10); + g2D.fillRect(_startPosX + 120, _startPosY, 40, 40); + drawingWheels.drawWheels(g2D, Color.BLACK, _startPosX, _startPosY); + } +} diff --git a/src/DumpTruck/Entities/EntityDumpTruck.java b/src/DumpTruck/Entities/EntityDumpTruck.java new file mode 100644 index 0000000..da9a3b1 --- /dev/null +++ b/src/DumpTruck/Entities/EntityDumpTruck.java @@ -0,0 +1,37 @@ +package DumpTruck.Entities; + +import java.awt.*; + +public class EntityDumpTruck extends EntityTruck { + private boolean Tent; + + public boolean getTent() { + return Tent; + } + + private boolean DumpBox; + + public boolean getDumpBox() { + return DumpBox; + } + + private Color DumpBoxColor; + + public Color getDumpBoxColor() { + return DumpBoxColor; + } + + private Color TentColor; + + public Color getTentColor() { + return TentColor; + } + + public EntityDumpTruck(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor) { + super(speed, weight, bodyColor); + Tent = tent; + DumpBox = dumpBox; + DumpBoxColor = dumpBoxColor; + TentColor = tentColor; + } +} diff --git a/src/DumpTruck/Entities/EntityTruck.java b/src/DumpTruck/Entities/EntityTruck.java new file mode 100644 index 0000000..fb1f286 --- /dev/null +++ b/src/DumpTruck/Entities/EntityTruck.java @@ -0,0 +1,34 @@ +package DumpTruck.Entities; + +import java.awt.*; + +public class EntityTruck +{ + private int Speed; + + public int getSpeed() { + return Speed; + } + + private double Weight; + + public double getWeight() { + return Weight; + } + + private Color BodyColor; + + public Color getBodyColor() { + return BodyColor; + } + + public double getStep() { + return (double)Speed * 100 / Weight; + } + + public EntityTruck(int speed, double weight, Color bodyColor) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/src/DumpTruck/EntityDumpTruck.java b/src/DumpTruck/EntityDumpTruck.java deleted file mode 100644 index cefd08a..0000000 --- a/src/DumpTruck/EntityDumpTruck.java +++ /dev/null @@ -1,61 +0,0 @@ -package DumpTruck; - -import java.awt.*; -public class EntityDumpTruck { - - 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 boolean Tent; - - public boolean getTent() { - return Tent; - } - - private boolean DumpBox; - - public boolean getDumpBox() { - return DumpBox; - } - - private Color DumpBoxColor; - - public Color getDumpBoxColor() { - return DumpBoxColor; - } - - private Color TentColor; - - public Color getTentColor() { - return TentColor; - } - - public double getStep() { - return (double)Speed * 100 / Weight; - } - - public void Init(int speed, double weight, Color bodyColor, boolean tent, boolean dumpBox, Color tentColor, Color dumpBoxColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - Tent = tent; - DumpBox = dumpBox; - DumpBoxColor = dumpBoxColor; - TentColor = tentColor; - } -} diff --git a/src/DumpTruck/MovementStrategy/AbstractStrategy.java b/src/DumpTruck/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..830036b --- /dev/null +++ b/src/DumpTruck/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,99 @@ +package DumpTruck.MovementStrategy; + +import DumpTruck.DirectionType; + +public abstract class AbstractStrategy { + + private IMoveableObject moveableObject; + + private Status state = Status.NotInit; + + private int fieldWidth; + + protected int getFieldWidth() { + return fieldWidth; + } + + private int fieldHeight; + + protected int getFieldHeight() { + return fieldHeight; + } + + public Status getStatus() { + return state; + } + + public void setData(IMoveableObject moveableObject, int width, int height) { + if (moveableObject == null) { + state = Status.NotInit; + return; + } + state = Status.InProgress; + this.moveableObject = moveableObject; + fieldWidth = width; + fieldHeight = height; + } + + public void makeStep() { + if (state != Status.InProgress) { + return; + } + if (isTargetDestination()) { + state = Status.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() { + if (moveableObject == null) { + return null; + } + return moveableObject.getObjectsPosition(); + } + + protected Integer getStep() { + if (state != Status.InProgress) { + return null; + } + if (moveableObject == null) { + return null; + } + return moveableObject.getStep(); + } + + protected abstract void moveToTarget(); + + protected abstract boolean isTargetDestination(); + + private boolean moveTo(DirectionType directionType) { + if (state != Status.InProgress) { + return false; + } + if (moveableObject == null) { + return false; + } + if (moveableObject.checkCanMove(directionType)) { + moveableObject.moveObject(directionType); + return true; + } + return false; + } +} diff --git a/src/DumpTruck/MovementStrategy/DrawingObjectTruck.java b/src/DumpTruck/MovementStrategy/DrawingObjectTruck.java new file mode 100644 index 0000000..6c49542 --- /dev/null +++ b/src/DumpTruck/MovementStrategy/DrawingObjectTruck.java @@ -0,0 +1,42 @@ +package DumpTruck.MovementStrategy; + +import DumpTruck.DirectionType; +import DumpTruck.DrawingObjects.DrawingTruck; + +public class DrawingObjectTruck implements IMoveableObject { + + private DrawingTruck drawingTruck = null; + + public DrawingObjectTruck(DrawingTruck drawingTruck) { + this.drawingTruck = drawingTruck; + } + + @Override + public ObjectParameters getObjectsPosition() { + if (drawingTruck == null || drawingTruck.getEntityTruck() == null) { + return null; + } + return new ObjectParameters(drawingTruck.getPosX(), drawingTruck.getPosY(), + drawingTruck.getWidth(), drawingTruck.getHeight()); + } + + @Override + public int getStep() { + if (drawingTruck == null) + return 0; + return (int) ((drawingTruck.getEntityTruck() != null) ? drawingTruck.getEntityTruck().getStep() : 0); + } + + @Override + public boolean checkCanMove(DirectionType direction) { + if (drawingTruck == null) + return false; + return drawingTruck.CanMove(direction); + } + + @Override + public void moveObject(DirectionType direction) { + if (drawingTruck != null) + drawingTruck.MoveTransport(direction); + } +} \ No newline at end of file diff --git a/src/DumpTruck/MovementStrategy/IMoveableObject.java b/src/DumpTruck/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..2f818f2 --- /dev/null +++ b/src/DumpTruck/MovementStrategy/IMoveableObject.java @@ -0,0 +1,13 @@ +package DumpTruck.MovementStrategy; +import DumpTruck.DirectionType; + +public interface IMoveableObject { + + ObjectParameters getObjectsPosition(); + + int getStep(); + + boolean checkCanMove(DirectionType direction); + + void moveObject(DirectionType direction); +} \ No newline at end of file diff --git a/src/DumpTruck/MovementStrategy/MoveToBorder.java b/src/DumpTruck/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..b5d90e1 --- /dev/null +++ b/src/DumpTruck/MovementStrategy/MoveToBorder.java @@ -0,0 +1,36 @@ +package DumpTruck.MovementStrategy; + +public class MoveToBorder extends AbstractStrategy { + + @Override + protected boolean isTargetDestination() { + var objParams = getObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.rightBorder() <= getFieldWidth() && + objParams.rightBorder() + getStep() >= getFieldWidth() && + objParams.downBorder() <= getFieldHeight() && + objParams.downBorder() + getStep() >= getFieldHeight(); + } + + @Override + protected void moveToTarget() { + var objParams = getObjectParameters(); + if (objParams == null) { + return; + } + var diffX = objParams.objectMiddleHorizontal() - getFieldWidth(); + if (Math.abs(diffX) > getStep()) { + if (diffX < 0) { + moveRight(); + } + } + var diffY = objParams.objectMiddleVertical() - getFieldHeight(); + if (Math.abs(diffY) > getStep()) { + if (diffY < 0) { + moveDown(); + } + } + } +} \ No newline at end of file diff --git a/src/DumpTruck/MovementStrategy/MoveToCenter.java b/src/DumpTruck/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..2905269 --- /dev/null +++ b/src/DumpTruck/MovementStrategy/MoveToCenter.java @@ -0,0 +1,40 @@ +package DumpTruck.MovementStrategy; + +public class MoveToCenter extends AbstractStrategy { + + @Override + protected boolean isTargetDestination() { + var objParams = getObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.objectMiddleHorizontal() <= getFieldWidth() / 2 && + objParams.objectMiddleHorizontal() + getStep() >= getFieldWidth() / 2 && + objParams.objectMiddleVertical() <= getFieldHeight() / 2 && + objParams.objectMiddleVertical() + getStep() >= getFieldHeight() / 2; + } + + @Override + protected void moveToTarget() { + var objParams = getObjectParameters(); + if (objParams == null) { + return; + } + var diffX = objParams.objectMiddleHorizontal() - getFieldWidth() / 2; + if (Math.abs(diffX) > getStep()) { + if (diffX > 0) { + moveLeft(); + } else { + moveRight(); + } + } + var diffY = objParams.objectMiddleVertical() - getFieldHeight() / 2; + if (Math.abs(diffY) > getStep()) { + if (diffY > 0) { + moveUp(); + } else { + moveDown(); + } + } + } +} \ No newline at end of file diff --git a/src/DumpTruck/MovementStrategy/ObjectParameters.java b/src/DumpTruck/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..f5e67ba --- /dev/null +++ b/src/DumpTruck/MovementStrategy/ObjectParameters.java @@ -0,0 +1,43 @@ +package DumpTruck.MovementStrategy; + +public class ObjectParameters { + + private final int x; + + private final int y; + + private final int width; + + private final int height; + + public int leftBorder() { + return x; + } + + public int topBorder() { + return y; + } + + public int rightBorder() { + return x + width; + } + + public int downBorder() { + return y + height; + } + + public int objectMiddleHorizontal() { + return x + width / 2; + } + + public int objectMiddleVertical() { + return y + height / 2; + } + + public ObjectParameters(int x, int y, int width, int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } +} \ No newline at end of file diff --git a/src/DumpTruck/MovementStrategy/Status.java b/src/DumpTruck/MovementStrategy/Status.java new file mode 100644 index 0000000..ba75493 --- /dev/null +++ b/src/DumpTruck/MovementStrategy/Status.java @@ -0,0 +1,9 @@ +package DumpTruck.MovementStrategy; + +public enum Status { + NotInit, + + InProgress, + + Finish +} \ No newline at end of file diff --git a/src/DumpTruck/PictureBoxDumpTruck.java b/src/DumpTruck/PictureBoxDumpTruck.java index d0d100b..1ef2a15 100644 --- a/src/DumpTruck/PictureBoxDumpTruck.java +++ b/src/DumpTruck/PictureBoxDumpTruck.java @@ -4,9 +4,13 @@ import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.util.Random; +import DumpTruck.DrawingObjects.*; +import DumpTruck.MovementStrategy.*; public class PictureBoxDumpTruck extends JPanel { - private DrawingDumpTruck drawingDumpTruck; + private DrawingTruck drawingTruck; + + private AbstractStrategy abstractStrategy; private JButton buttonLeft; @@ -17,19 +21,21 @@ public class PictureBoxDumpTruck extends JPanel { private JButton buttonDown; private JButton buttonCreateDumpTruck; + private JButton buttonCreateTruck; + private JComboBox comboBoxStrategy; + private JButton buttonStep; public PictureBoxDumpTruck() { setLayout(null); setBounds(0, 0, 800, 450); - buttonCreateDumpTruck = new JButton("Создать"); - buttonCreateDumpTruck.setFocusable(false); - buttonCreateDumpTruck.setBounds(12, 415, 100, 30); - add(buttonCreateDumpTruck); + buttonCreateTruck = new JButton("Создать грузовик"); + buttonCreateTruck.setFocusable(false); + buttonCreateTruck.setBounds(12, 415, 150, 30); + add(buttonCreateTruck); - buttonCreateDumpTruck.addActionListener(e -> { + buttonCreateTruck.addActionListener(e -> { Random random = new Random(); - drawingDumpTruck = new DrawingDumpTruck(); - drawingDumpTruck.Init(random.nextInt(200, 300), + drawingTruck = new DrawingTruck((random.nextInt(200, 300)), random.nextInt(1000, 3000), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), @@ -39,12 +45,33 @@ public class PictureBoxDumpTruck extends JPanel { new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), this.getWidth(), this.getHeight(), random.nextInt(2, 5)); - drawingDumpTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + repaint(); + }); + + buttonCreateDumpTruck = new JButton("Создать самосвал"); + buttonCreateDumpTruck.setFocusable(false); + buttonCreateDumpTruck.setBounds(180, 415, 150, 30); + add(buttonCreateDumpTruck); + + buttonCreateDumpTruck.addActionListener(e -> { + Random random = new Random(); + drawingTruck = new DrawingDumpTruck((random.nextInt(200, 300)), + random.nextInt(1000, 3000), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), + random.nextBoolean(), random.nextBoolean(), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), + new Color(random.nextInt(0, 256), random.nextInt(0, 256), + random.nextInt(0, 256)), + this.getWidth(), this.getHeight(), random.nextInt(2, 5)); + drawingTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); repaint(); }); ActionListener buttonMoveListener = e -> { - if (drawingDumpTruck == null) + if (drawingTruck == null) { return; } @@ -53,16 +80,16 @@ public class PictureBoxDumpTruck extends JPanel { switch (buttonName) { case ("buttonUp"): - drawingDumpTruck.MoveTransport(DirectionType.Up); + drawingTruck.MoveTransport(DirectionType.Up); break; case ("buttonDown"): - drawingDumpTruck.MoveTransport(DirectionType.Down); + drawingTruck.MoveTransport(DirectionType.Down); break; case ("buttonLeft"): - drawingDumpTruck.MoveTransport(DirectionType.Left); + drawingTruck.MoveTransport(DirectionType.Left); break; case ("buttonRight"): - drawingDumpTruck.MoveTransport(DirectionType.Right); + drawingTruck.MoveTransport(DirectionType.Right); break; } repaint(); @@ -106,17 +133,60 @@ public class PictureBoxDumpTruck extends JPanel { buttonUp.setBounds(722, 372, 30, 30); add(buttonUp); - setPreferredSize(new Dimension(800, 450)); + String[] items = { + "0", + "1" + }; + comboBoxStrategy = new JComboBox(items); + comboBoxStrategy.setBounds(667, 10, 120, 25); + + buttonStep = new JButton("Шаг"); + buttonStep.setFocusable(false); + buttonStep.setBounds(710, 40, 75, 30); + + buttonStep.addActionListener(e -> { + if (drawingTruck == null) { + return; + } + if (comboBoxStrategy.isEnabled()) { + abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()) { + case 0 -> new MoveToCenter(); + case 1 -> new MoveToBorder(); + default -> null; + }; + if (abstractStrategy == null) + { + return; + } + abstractStrategy.setData(new DrawingObjectTruck(drawingTruck), this.getWidth(), this.getHeight()); + } + if (abstractStrategy == null) + { + return; + } + comboBoxStrategy.setEnabled(false); + abstractStrategy.makeStep(); + repaint(); + if (abstractStrategy.getStatus() == Status.Finish) + { + comboBoxStrategy.setEnabled(true); + abstractStrategy = null; + } + }); + + add(comboBoxStrategy); + add(buttonStep); + setPreferredSize(new Dimension(800, 450)); } @Override protected void paintComponent(Graphics g) { - if (drawingDumpTruck == null) { + if (drawingTruck == null) { return; } super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; - drawingDumpTruck.DrawTransport(g2d); + drawingTruck.DrawTransport(g2d); } } diff --git a/src/DumpTruck/DrawingWheels.java b/src/DumpTruck/Wheels/DrawingWheels.java similarity index 63% rename from src/DumpTruck/DrawingWheels.java rename to src/DumpTruck/Wheels/DrawingWheels.java index fe1d640..f43850c 100644 --- a/src/DumpTruck/DrawingWheels.java +++ b/src/DumpTruck/Wheels/DrawingWheels.java @@ -1,36 +1,42 @@ -package DumpTruck; +package DumpTruck.Wheels; import java.awt.*; -public class DrawingWheels { - +public class DrawingWheels implements IDrawingWheels { private WheelNumber wheelNumber; + @Override + public WheelNumber getWheelNumber() { + return wheelNumber; + } + @Override public void setWheelNumber(int number) { switch (number) { case 2: - wheelNumber = wheelNumber.Two; + wheelNumber = WheelNumber.Two; break; case 3: - wheelNumber = wheelNumber.Three; + wheelNumber = WheelNumber.Three; break; case 4: - wheelNumber = wheelNumber.Four; + wheelNumber = WheelNumber.Four; break; - default: wheelNumber = wheelNumber.Two; + default: + wheelNumber = WheelNumber.Two; } } - public void drawWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + @Override + public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) { switch (wheelNumber) { case Two: - drawTwoWheels(g2D, color, _startPosX, _startPosY); + drawTwoWheels(g2d, color, startPosX, startPosY); break; case Three: - drawThreeWheels(g2D, color, _startPosX, _startPosY); + drawThreeWheels(g2d, color, startPosX, startPosY); break; case Four: - drawFourWheels(g2D, color, _startPosX, _startPosY); + drawFourWheels(g2d, color, startPosX, startPosY); break; } } diff --git a/src/DumpTruck/Wheels/DrawingWheelsCircles.java b/src/DumpTruck/Wheels/DrawingWheelsCircles.java new file mode 100644 index 0000000..f143925 --- /dev/null +++ b/src/DumpTruck/Wheels/DrawingWheelsCircles.java @@ -0,0 +1,72 @@ +package DumpTruck.Wheels; + +import java.awt.*; + +public class DrawingWheelsCircles implements IDrawingWheels { + private WheelNumber wheelNumber; + @Override + public WheelNumber getWheelNumber() { + return wheelNumber; + } + + @Override + public void setWheelNumber(int number) { + switch (number) { + case 2: + wheelNumber = WheelNumber.Two; + break; + case 3: + wheelNumber = WheelNumber.Three; + break; + case 4: + wheelNumber = WheelNumber.Four; + break; + default: + wheelNumber = WheelNumber.Two; + } + } + + @Override + public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) { + switch (wheelNumber) { + case Two: + drawTwoWheels(g2d, color, startPosX, startPosY); + break; + case Three: + drawThreeWheels(g2d, color, startPosX, startPosY); + break; + case Four: + drawFourWheels(g2d, color, startPosX, startPosY); + break; + } + } + + private void drawTwoWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + g2D.fillOval(_startPosX, _startPosY + 50, 40, 40); + drawPattern(g2D, _startPosX, _startPosY + 50); + g2D.fillOval(_startPosX + 120, _startPosY + 50, 40, 40); + drawPattern(g2D, _startPosX + 120, _startPosY + 50); + } + + private void drawThreeWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + drawTwoWheels(g2D, color, _startPosX, _startPosY); + g2D.fillOval(_startPosX + 40, _startPosY + 50, 40, 40); + drawPattern(g2D, _startPosX + 40, _startPosY + 50); + } + + private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + drawThreeWheels(g2D, color, _startPosX, _startPosY); + g2D.fillOval(_startPosX + 80, _startPosY + 50, 40, 40); + drawPattern(g2D, _startPosX + 80, _startPosY + 50); + } + + private void drawPattern(Graphics2D g2D, int x, int y){ + g2D.setColor(Color.WHITE); + g2D.drawOval(x + 15, y + 15, 10, 10); + g2D.drawOval(x + 5, y + 5, 30, 30); + g2D.setColor(Color.BLACK); + } +} \ No newline at end of file diff --git a/src/DumpTruck/Wheels/DrawingWheelsStar.java b/src/DumpTruck/Wheels/DrawingWheelsStar.java new file mode 100644 index 0000000..71ff68e --- /dev/null +++ b/src/DumpTruck/Wheels/DrawingWheelsStar.java @@ -0,0 +1,74 @@ +package DumpTruck.Wheels; + +import java.awt.*; + +public class DrawingWheelsStar implements IDrawingWheels { + private WheelNumber wheelNumber; + @Override + public WheelNumber getWheelNumber() { + return wheelNumber; + } + + @Override + public void setWheelNumber(int number) { + switch (number) { + case 2: + wheelNumber = WheelNumber.Two; + break; + case 3: + wheelNumber = WheelNumber.Three; + break; + case 4: + wheelNumber = WheelNumber.Four; + break; + default: + wheelNumber = WheelNumber.Two; + } + } + + @Override + public void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY) { + switch (wheelNumber) { + case Two: + drawTwoWheels(g2d, color, startPosX, startPosY); + break; + case Three: + drawThreeWheels(g2d, color, startPosX, startPosY); + break; + case Four: + drawFourWheels(g2d, color, startPosX, startPosY); + break; + } + } + + private void drawTwoWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + g2D.fillOval(_startPosX, _startPosY + 50, 40, 40); + drawPattern(g2D, _startPosX, _startPosY + 50); + g2D.fillOval(_startPosX + 120, _startPosY + 50, 40, 40); + drawPattern(g2D, _startPosX + 120, _startPosY + 50); + } + + private void drawThreeWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + drawTwoWheels(g2D, color, _startPosX, _startPosY); + g2D.fillOval(_startPosX + 40, _startPosY + 50, 40, 40); + drawPattern(g2D, _startPosX + 40, _startPosY + 50); + } + + private void drawFourWheels(Graphics2D g2D, Color color, int _startPosX, int _startPosY) { + g2D.setColor(color); + drawThreeWheels(g2D, color, _startPosX, _startPosY); + g2D.fillOval(_startPosX + 80, _startPosY + 50, 40, 40); + drawPattern(g2D, _startPosX + 80, _startPosY + 50); + } + + private void drawPattern(Graphics2D g2D, int x, int y){ + g2D.setColor(Color.WHITE); + g2D.drawLine(x + 20, y, x + 20, y + 40); + g2D.drawLine(x, y + 20, x + 40, y + 20); + g2D.drawLine(x + 10, y + 10, x + 30, y + 30); + g2D.drawLine(x + 30, y + 10, x + 10, y + 30); + g2D.setColor(Color.BLACK); + } +} \ No newline at end of file diff --git a/src/DumpTruck/Wheels/IDrawingWheels.java b/src/DumpTruck/Wheels/IDrawingWheels.java new file mode 100644 index 0000000..aa32be2 --- /dev/null +++ b/src/DumpTruck/Wheels/IDrawingWheels.java @@ -0,0 +1,11 @@ +package DumpTruck.Wheels; + +import java.awt.*; + +public interface IDrawingWheels { + void setWheelNumber(int number); + + WheelNumber getWheelNumber(); + + void drawWheels(Graphics2D g2d, Color color, int startPosX, int startPosY); +} \ No newline at end of file diff --git a/src/DumpTruck/WheelNumber.java b/src/DumpTruck/Wheels/WheelNumber.java similarity index 68% rename from src/DumpTruck/WheelNumber.java rename to src/DumpTruck/Wheels/WheelNumber.java index c3363a1..e8c9aca 100644 --- a/src/DumpTruck/WheelNumber.java +++ b/src/DumpTruck/Wheels/WheelNumber.java @@ -1,4 +1,4 @@ -package DumpTruck; +package DumpTruck.Wheels; public enum WheelNumber { Two,