From 93622be5ba62a2b6f18d734025f9bdae86bbc350 Mon Sep 17 00:00:00 2001 From: ekallin Date: Mon, 9 Oct 2023 20:56:26 +0400 Subject: [PATCH] Lab 02 hard without dopClasses --- .../AbstractStrategy.java | 84 ++++++++ .../DrawingElectricLocomotive.java | 188 +++--------------- .../DrawingLocomotive.java | 178 +++++++++++++++++ .../DrawingObjectLocomotive.java | 28 +++ ProjectElectricLocomotive/DyrectionType.java | 2 +- .../EntityElectricLocomotive.java | 18 +- .../EntityLocomotive.java | 25 +++ .../FormElectricLocomotive.form | 99 +++++---- .../FormElectricLocomotive.java | 98 ++++++--- .../IMoveableObject.java | 10 + ProjectElectricLocomotive/MoveToCenter.java | 45 +++++ .../MoveToRigthCorner.java | 23 +++ .../ObjectParameters.java | 1 + ProjectElectricLocomotive/Status.java | 7 + 14 files changed, 569 insertions(+), 237 deletions(-) create mode 100644 ProjectElectricLocomotive/AbstractStrategy.java create mode 100644 ProjectElectricLocomotive/DrawingLocomotive.java create mode 100644 ProjectElectricLocomotive/DrawingObjectLocomotive.java create mode 100644 ProjectElectricLocomotive/EntityLocomotive.java create mode 100644 ProjectElectricLocomotive/IMoveableObject.java create mode 100644 ProjectElectricLocomotive/MoveToCenter.java create mode 100644 ProjectElectricLocomotive/MoveToRigthCorner.java rename ObjectParameters.java => ProjectElectricLocomotive/ObjectParameters.java (96%) create mode 100644 ProjectElectricLocomotive/Status.java diff --git a/ProjectElectricLocomotive/AbstractStrategy.java b/ProjectElectricLocomotive/AbstractStrategy.java new file mode 100644 index 0000000..3030096 --- /dev/null +++ b/ProjectElectricLocomotive/AbstractStrategy.java @@ -0,0 +1,84 @@ +package ProjectElectricLocomotive; + +public abstract class AbstractStrategy { + private IMoveableObject _moveableObject; + + private Status _state = Status.NotInit; + protected int FieldWidth; + protected int 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; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + + protected boolean MoveLeft(){ + return MoveTo(DyrectionType.Left); + } + + protected boolean MoveRight(){ + return MoveTo(DyrectionType.Right); + } + + protected boolean MoveUp(){ + return MoveTo(DyrectionType.Up); + } + + protected boolean MoveDown(){ + return MoveTo(DyrectionType.Down); + } + + /// Параметры объекта + protected ObjectParameters/*?*/ GetObjectParameters(){ + if(_moveableObject == null) return null; + return _moveableObject.GetObjectPosition(); + } + + protected int/*?*/ GetStep() + { + if (_state != Status.InProgress) + { + return -1; + } + return _moveableObject.GetStep(); + } + protected abstract void MoveToTarget(); + + protected abstract boolean IsTargetDestinaion(); + + private boolean MoveTo(DyrectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(directionType) == false) return false; + { + _moveableObject.MoveObject(directionType); + return true; + } + } +} diff --git a/ProjectElectricLocomotive/DrawingElectricLocomotive.java b/ProjectElectricLocomotive/DrawingElectricLocomotive.java index 5a40613..b7353d1 100644 --- a/ProjectElectricLocomotive/DrawingElectricLocomotive.java +++ b/ProjectElectricLocomotive/DrawingElectricLocomotive.java @@ -1,173 +1,43 @@ package ProjectElectricLocomotive; import java.awt.*; -public class DrawingElectricLocomotive { - public EntityElectricLocomotive EntityElectricLocomotive; - private DrawingWheel _drawingWheel; - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private final int _locoWidth = 150; - private final int _locoHeight = 50; - - public boolean Init(int speed, double weight, Color bodyColor, Color additionalColor, - boolean horns, boolean seifbatteries, int width, int height) +public class DrawingElectricLocomotive extends DrawingLocomotive { + EntityElectricLocomotive electricLocomotive; + public DrawingElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, + boolean horns, boolean seifBatteries, int width, int height) { - - if (width < _locoWidth || height < _locoHeight) + super(speed, weight, bodyColor, width, height, 85, 50); + if (EntityLocomotive != null) { - return false; + EntityLocomotive = new EntityElectricLocomotive(speed, width, bodyColor, additionalColor, horns, seifBatteries); } - _pictureWidth = width; - _pictureHeight = height; - EntityElectricLocomotive = new EntityElectricLocomotive(); - _drawingWheel = new DrawingWheel(); - EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, horns, seifbatteries); - return true; } - - public void SetWheelsCount(int wheelsCount) { - _drawingWheel.SetWheelsCount(wheelsCount); - } - - public void SetPosition(int x, int y) + @Override + public void DrawTransport(Graphics g) { - if (x < 0 || x + _locoWidth > _pictureWidth) + if (EntityLocomotive instanceof EntityElectricLocomotive electricLocomotive) ///////// WARNING INSTANCEOF { - x = _pictureWidth - _locoWidth; - } - if (y < 0 || y + _locoHeight > _pictureHeight) - { - y = _pictureHeight - _locoHeight; - } - _startPosX = x; - _startPosY = y; - } + Color colorBlack = Color.BLACK; + /*Brush blackBrush = new SolidBrush(Color.Black);*/ + Color windows = Color.BLUE; + Color bodyColor = EntityLocomotive.BodyColor; + Color additionalBrush = electricLocomotive.AdditionalColor; - public void MoveTransport(DyrectionType direction){ - if(EntityElectricLocomotive == null) return; - switch(direction) - { - case Up -> { - if(_startPosY - EntityElectricLocomotive.Step() >= 0) - _startPosY -= (int) EntityElectricLocomotive.Step(); + if (electricLocomotive.Horns) { + //horns + g.setColor(colorBlack); + g.fillRect(_startPosX + 30, _startPosY + 15, 20, 5); + g.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10); + g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY); + g.drawLine(_startPosX + 45, _startPosY + 15, _startPosX + 50, _startPosY + 10); + g.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 40, _startPosY); } - case Down -> { - if(_startPosY + EntityElectricLocomotive.Step() + _locoHeight <= _pictureHeight) - _startPosY += (int) EntityElectricLocomotive.Step(); - } - case Left -> { - if(_startPosX - EntityElectricLocomotive.Step() >= 0) - _startPosX -= (int) EntityElectricLocomotive.Step(); - } - case Right -> { - if(_startPosX + EntityElectricLocomotive.Step() + _locoWidth <= _pictureWidth) - _startPosX += (int) EntityElectricLocomotive.Step(); + + if (electricLocomotive.SeifBatteries) { + g.setColor(colorBlack); + g.fillRect(_startPosX + 80, _startPosY + 30, 5, 10); } + super.DrawTransport(g); + } } - public void DrawTransport(Graphics g) { - if (EntityElectricLocomotive == null) { - return; - } - - Graphics2D g2d = (Graphics2D) g; - - Color bodyColor = EntityElectricLocomotive.BodyColor; - Color additionalColor = EntityElectricLocomotive.AdditionalColor; - Color blackBrush = Color.BLACK; - Color windowsColor = Color.BLUE; - - _drawingWheel.DrawWheels(g, additionalColor, _startPosX, _startPosY, 5, 5); - - if(EntityElectricLocomotive.Horns) - { - g2d.fillRect(_startPosX + 30, _startPosY + 15, 20, 5); - g2d.drawLine(_startPosX + 40, _startPosY + 15, _startPosX + 50, _startPosY + 10); - g2d.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 45, _startPosY); - g2d.drawLine(_startPosX + 45, _startPosY + 15, _startPosX + 50, _startPosY + 10); - g2d.drawLine(_startPosX + 50, _startPosY + 10, _startPosX + 40, _startPosY); - g2d.setColor(blackBrush); - } - - //locomotive - Polygon loco = new Polygon(); - - loco.addPoint(_startPosX, _startPosY + 40); - loco.addPoint(_startPosX, _startPosY + 30); - loco.addPoint(_startPosX + 20, _startPosY + 20); - loco.addPoint(_startPosX + 70, _startPosY + 20); - loco.addPoint(_startPosX +80, _startPosY + 30); - loco.addPoint(_startPosX +80, _startPosY + 40); - loco.addPoint(_startPosX +75, _startPosY + 45); - loco.addPoint(_startPosX +5, _startPosY + 45); - loco.addPoint(_startPosX, _startPosY + 40); - - g2d.setColor(blackBrush); - g2d.drawPolygon(loco); - g2d.setColor(bodyColor); - g2d.fillPolygon(loco); - // windows - Polygon window = new Polygon(); - window.addPoint(_startPosX + 10, _startPosY + 30); - window.addPoint(_startPosX +15, _startPosY + 25); - window.addPoint(_startPosX + 20, _startPosY + 25); - window.addPoint(_startPosX + 20, _startPosY + 30); - window.addPoint(_startPosX +10, _startPosY + 30); - - g2d.setColor(blackBrush); - g2d.drawPolygon(window); - g2d.setColor(windowsColor); - g2d.fillPolygon(window); - - g2d.fillRect(_startPosX + 25, _startPosY + 25, 10, 5); - g2d.setColor(windowsColor); - g2d.drawRect(_startPosX + 25, _startPosY + 25, 10, 5); - g2d.setColor(blackBrush); - //locomotive - - if(EntityElectricLocomotive.SeifBatteries) - { - g2d.drawRect(_startPosX + 50, _startPosY + 25, 20, 10); - g2d.setColor(blackBrush); - } - - - //обязательные колеса - //loco - g2d.fillOval(_startPosX + 10, _startPosY + 45, 5, 5); - g2d.fillOval(_startPosX + 25, _startPosY + 45, 5, 5); - g2d.fillOval(_startPosX + 50, _startPosY + 45, 5, 5); - g2d.fillOval(_startPosX + 65, _startPosY + 45, 5, 5); - - - //telega - g2d.setColor(blackBrush); - g2d.fillOval(_startPosX + 95, _startPosY + 45, 5, 5); - g2d.fillOval(_startPosX + 140, _startPosY + 45, 5, 5); - - //telejka - Polygon telega = new Polygon(); - - telega.addPoint(_startPosX + 90, _startPosY + 25); - telega.addPoint(_startPosX + 95, _startPosY + 20); - telega.addPoint(_startPosX + 145, _startPosY + 20); - telega.addPoint(_startPosX + 150, _startPosY + 25); - telega.addPoint(_startPosX + 150, _startPosY + 45); - telega.addPoint(_startPosX + 90, _startPosY + 45); - telega.addPoint(_startPosX + 90, _startPosY + 25); - - g2d.setColor(additionalColor); - g2d.fillPolygon(telega); - g2d.setColor(blackBrush); - g2d.drawPolygon(telega); - //telejka - - //телега окна - g2d.setColor(blackBrush); - g.drawLine(_startPosX + 80, _startPosY + 40, _startPosX + 90, _startPosY + 40); - g2d.setColor(windowsColor); g.fillRect(_startPosX + 95, _startPosY + 30, 10, 5); - g.fillRect(_startPosX + 115, _startPosY + 30, 10, 5); - g.fillRect(_startPosX + 135, _startPosY + 30, 10, 5); - } } diff --git a/ProjectElectricLocomotive/DrawingLocomotive.java b/ProjectElectricLocomotive/DrawingLocomotive.java new file mode 100644 index 0000000..f482883 --- /dev/null +++ b/ProjectElectricLocomotive/DrawingLocomotive.java @@ -0,0 +1,178 @@ +package ProjectElectricLocomotive; +import java.awt.*; +import java.security.cert.PolicyNode; + +public class DrawingLocomotive { + public EntityLocomotive EntityLocomotive; + private DrawingWheel _drawingWheel; + private int _pictureWidth; + private int _pictureHeight; + public int _startPosX; + public int _startPosY; + private int _locoWidth = 150; + private int _locoHeight = 50; + + public int GetPosX(){ + return _startPosX; + } + public int GetPosY(){ + return _startPosY; + } + public int GetWidth(){ + return _locoWidth; + } + public int GetHeight(){ + return _locoHeight; + } + + public DrawingLocomotive(int speed, double weight, Color bodyColor, int width, int heigth) + { + if (width < _locoWidth || heigth < _locoHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = heigth; + EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + } + + protected DrawingLocomotive(int speed, double weight, Color bodyColor, int width, + int height, int locoWidth, int locoHeight) + { + if (width < _locoWidth || height < _locoHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _locoWidth = locoWidth; + _locoHeight = locoHeight; + EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); + } + + //Установка позиции + public void SetPosition(int x, int y) + { + if (x < 0 || x + _locoWidth > _pictureWidth) + { + x = _pictureWidth - _locoWidth; + } + if (y < 0 || y + _locoHeight > _pictureHeight) + { + y = _pictureHeight - _locoHeight; + } + _startPosX = x; + _startPosY = y; + } + + public void MoveTransport(DyrectionType direction) { + if (EntityLocomotive == null) { + return; + } + switch (direction) { + case Left: + if (_startPosX - EntityLocomotive.Step() > 0) { + _startPosX -= (int) EntityLocomotive.Step(); + } + break; + case Up: + if (_startPosY - EntityLocomotive.Step() > 0) { + _startPosY -= (int) EntityLocomotive.Step(); + } + break; + case Right: + if (_startPosX + EntityLocomotive.Step() + _locoWidth < _pictureWidth) { + _startPosX += (int) EntityLocomotive.Step(); + } + break; + case Down: + if (_startPosY + EntityLocomotive.Step() + _locoHeight < _pictureHeight) { + _startPosY += (int) EntityLocomotive.Step(); + } + break; + } + } + public void DrawTransport(Graphics g) + { + { + if (EntityLocomotive == null) return; + } + + Color colorBlack = Color.BLACK; + /*Brush blackBrush = new SolidBrush(Color.Black);*/ + Color windows = Color.BLUE; + Color bodyColor = EntityLocomotive.BodyColor; + + //локомотив + g.setColor(bodyColor); + Polygon locoP = new Polygon(); + locoP.addPoint(_startPosX, _startPosY + 40); + locoP.addPoint(_startPosX, _startPosY + 30); + locoP.addPoint(_startPosX + 20, _startPosY + 20); + locoP.addPoint(_startPosX + 70, _startPosY + 20); + locoP.addPoint(_startPosX +80, _startPosY + 30); + locoP.addPoint(_startPosX +80, _startPosY + 40); + locoP.addPoint(_startPosX +75, _startPosY + 45); + locoP.addPoint(_startPosX +5, _startPosY + 45); + locoP.addPoint(_startPosX, _startPosY + 40); + g.fillPolygon(locoP); + + g.setColor(colorBlack); + /*locoP.addPoint(_startPosX, _startPosY + 40); + locoP.addPoint(_startPosX, _startPosY + 30); + locoP.addPoint(_startPosX + 20, _startPosY + 20); + locoP.addPoint(_startPosX + 70, _startPosY + 20); + locoP.addPoint(_startPosX +80, _startPosY + 30); + locoP.addPoint(_startPosX +80, _startPosY + 40); + locoP.addPoint(_startPosX +75, _startPosY + 45); + locoP.addPoint(_startPosX +5, _startPosY + 45); + locoP.addPoint(_startPosX, _startPosY + 40);*/ + g.drawPolygon(locoP); + + + //окошки + Polygon window = new Polygon(); + window.addPoint(_startPosX + 10, _startPosY + 30); + window.addPoint(_startPosX +15, _startPosY + 25); + window.addPoint(_startPosX + 20, _startPosY + 25); + window.addPoint(_startPosX + 20, _startPosY + 30); + window.addPoint(_startPosX +10, _startPosY + 30); + g.setColor(windows); + g.fillPolygon(window); + g.fillRect(_startPosX + 25, _startPosY + 25, 10, 5); + g.setColor(Color.black); + g.drawPolygon(window); + g.drawRect(_startPosX + 25, _startPosY + 25, 10, 5); + + //обязательные колеса + //loco + g.fillOval(_startPosX + 10, _startPosY + 45, 5, 5); + g.fillOval(_startPosX + 25, _startPosY + 45, 5, 5); + g.fillOval(_startPosX + 50, _startPosY + 45, 5, 5); + g.fillOval(_startPosX + 65, _startPosY + 45, 5, 5); + } + + public boolean CanMove(DyrectionType direction) + { + if (EntityLocomotive == null) + { + return false; + } + switch(direction) { + //влево + case Left: + if (_startPosX - EntityLocomotive.Step() > 0) return true; + break; + case Up: + if (_startPosY - EntityLocomotive.Step() > 0) return true; + break; + case Right: + if (_startPosX + EntityLocomotive.Step() < _pictureWidth) return true; + break; + case Down: + if (_startPosY + EntityLocomotive.Step() < _pictureHeight) return true; + break; + } + return false; + } +} diff --git a/ProjectElectricLocomotive/DrawingObjectLocomotive.java b/ProjectElectricLocomotive/DrawingObjectLocomotive.java new file mode 100644 index 0000000..8f32e6a --- /dev/null +++ b/ProjectElectricLocomotive/DrawingObjectLocomotive.java @@ -0,0 +1,28 @@ +package ProjectElectricLocomotive; + +public class DrawingObjectLocomotive implements IMoveableObject { + private DrawingLocomotive _drawningLocomotive = null; + + public DrawingObjectLocomotive(DrawingLocomotive drawningLocomotive) + { + _drawningLocomotive = drawningLocomotive; + } + public ObjectParameters GetObjectPosition() + { + if (_drawningLocomotive == null || _drawningLocomotive.EntityLocomotive == null) + { + return null; + } + return new ObjectParameters(_drawningLocomotive.GetPosX(), _drawningLocomotive.GetPosY(), + _drawningLocomotive.GetWidth(), _drawningLocomotive.GetHeight()); + } + public int GetStep(){ + return (int)(_drawningLocomotive.EntityLocomotive.Step()); + } + public boolean CheckCanMove(DyrectionType direction){ + return _drawningLocomotive.CanMove(direction); + } + public void MoveObject(DyrectionType direction){ + _drawningLocomotive.MoveTransport(direction); + } +} diff --git a/ProjectElectricLocomotive/DyrectionType.java b/ProjectElectricLocomotive/DyrectionType.java index d833f54..f08a494 100644 --- a/ProjectElectricLocomotive/DyrectionType.java +++ b/ProjectElectricLocomotive/DyrectionType.java @@ -1,5 +1,5 @@ package ProjectElectricLocomotive; public enum DyrectionType { - Up, Down, Left, Right + Up, Down, Left, Right; } diff --git a/ProjectElectricLocomotive/EntityElectricLocomotive.java b/ProjectElectricLocomotive/EntityElectricLocomotive.java index 492585f..2be3ca3 100644 --- a/ProjectElectricLocomotive/EntityElectricLocomotive.java +++ b/ProjectElectricLocomotive/EntityElectricLocomotive.java @@ -2,25 +2,17 @@ package ProjectElectricLocomotive; import java.awt.*; -public class EntityElectricLocomotive { - public int Speed; - public double Weight; - public Color BodyColor; +public class EntityElectricLocomotive extends EntityLocomotive { public Color AdditionalColor; public boolean Horns; + public boolean SeifBatteries; - public double Step() + public EntityElectricLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifBatteries) { - return (double) Speed * 100 / Weight; - } - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, - boolean horns, boolean seifBatteries) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; + super(speed, weight, bodyColor); AdditionalColor = additionalColor; Horns = horns; SeifBatteries = seifBatteries; } + } diff --git a/ProjectElectricLocomotive/EntityLocomotive.java b/ProjectElectricLocomotive/EntityLocomotive.java new file mode 100644 index 0000000..aaf6deb --- /dev/null +++ b/ProjectElectricLocomotive/EntityLocomotive.java @@ -0,0 +1,25 @@ +package ProjectElectricLocomotive; + +import java.awt.*; + +public class EntityLocomotive { + public int Speed; + public double Weight; + public Color BodyColor; + + public EntityLocomotive(int speed, double weight, Color bodyColor) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + + public double Step() + { + return (double) Speed * 100 / Weight; + } + public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean horns, boolean seifbatteries) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.form b/ProjectElectricLocomotive/FormElectricLocomotive.form index 24ba903..9b1c126 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.form +++ b/ProjectElectricLocomotive/FormElectricLocomotive.form @@ -1,64 +1,62 @@
- + - + - - + + - - - - - - + - + - - - - - + - - - - + - + - - - - - + - - - - + + + + + + + + + + + + + + + + + + + + + - - - - + @@ -71,9 +69,24 @@ + + + + + + + + + + + + + + + - + @@ -90,13 +103,23 @@ - + - + + + + + - + + + + + + + diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index 49f6cf2..ee0b9fa 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -1,17 +1,23 @@ package ProjectElectricLocomotive; import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class FormElectricLocomotive { - DrawingElectricLocomotive _drawingElectricLocomotive = new DrawingElectricLocomotive(); - private JButton buttonCreate; + //DrawingElectricLocomotive _drawingElectricLocomotive = new DrawingElectricLocomotive(); + private JButton buttonCreateElectricLocomotive; private JPanel pictureBox; private JButton buttonUp; private JButton buttonDown; private JButton buttonLeft; private JButton buttonRight; + public JComboBox comboBoxStrategy; + private JButton buttonStep; + private JButton buttonCreateLocomotive; + private DrawingLocomotive _drawingLocomotive; + private AbstractStrategy _abstractStrategy; public JPanel getPictureBox() { return pictureBox; @@ -23,41 +29,82 @@ public class FormElectricLocomotive { buttonLeft.setName("buttonLeft"); buttonRight.setName("buttonRight"); - buttonCreate.addActionListener(e -> { - _drawingElectricLocomotive = new DrawingElectricLocomotive(); - Random random = new Random(); - - _drawingElectricLocomotive.Init( - random.nextInt(100, 300), - random.nextInt(1000, 3000), - 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(), - pictureBox.getWidth(), - pictureBox.getHeight() - ); - - _drawingElectricLocomotive.SetWheelsCount(random.nextInt(2, 5)); - _drawingElectricLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + buttonCreateLocomotive.addActionListener(e -> { + Random rnd = new Random(); + _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), rnd.nextInt(1000, 3000), + new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)), + pictureBox.getWidth(), pictureBox.getHeight()); + _drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); Draw(); }); + buttonCreateElectricLocomotive.addActionListener(e -> { + Random random = new Random(); + _drawingLocomotive = new DrawingElectricLocomotive(random.nextInt(100, 300),random.nextInt(1000, 3000), + 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)), + random.nextBoolean(), + random.nextBoolean(), + pictureBox.getWidth(), pictureBox.getHeight()); + _drawingLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + Draw(); + }); + + buttonStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_drawingLocomotive == null) { + return; + } + if (comboBoxStrategy.isEnabled()) { + switch(comboBoxStrategy.getSelectedIndex()) + { + case 0: + _abstractStrategy = new MoveToCenter(); + break; + case 1: + _abstractStrategy = new MoveToRigthCorner(); + break; + default: + _abstractStrategy = null; + break; + } ; + if (_abstractStrategy == null) { + return; + } + _abstractStrategy.SetData(new + DrawingObjectLocomotive(_drawingLocomotive), pictureBox.getWidth(), + pictureBox.getHeight()); + comboBoxStrategy.setEnabled(false); + } + if (_abstractStrategy == null) { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) { + comboBoxStrategy.setEnabled(true); + _abstractStrategy = null; + } + } + }); + + ActionListener buttonMoveClickedListener = e -> { String buttonName = ((JButton) e.getSource()).getName(); switch (buttonName) { case ("buttonUp") -> { - _drawingElectricLocomotive.MoveTransport(DyrectionType.Up); + _drawingLocomotive.MoveTransport(DyrectionType.Up); } case ("buttonDown") -> { - _drawingElectricLocomotive.MoveTransport(DyrectionType.Down); + _drawingLocomotive.MoveTransport(DyrectionType.Down); } case ("buttonLeft") -> { - _drawingElectricLocomotive.MoveTransport(DyrectionType.Left); + _drawingLocomotive.MoveTransport(DyrectionType.Left); } case ("buttonRight") -> { - _drawingElectricLocomotive.MoveTransport(DyrectionType.Right); + _drawingLocomotive.MoveTransport(DyrectionType.Right); } } Draw(); @@ -68,12 +115,11 @@ public class FormElectricLocomotive { buttonRight.addActionListener(buttonMoveClickedListener); } public void Draw() { - if (_drawingElectricLocomotive.EntityElectricLocomotive == null) { + if (_drawingLocomotive.EntityLocomotive == null) { return; } Graphics g = pictureBox.getGraphics(); pictureBox.paint(g); - _drawingElectricLocomotive.DrawTransport(g); + _drawingLocomotive.DrawTransport(g); } - } diff --git a/ProjectElectricLocomotive/IMoveableObject.java b/ProjectElectricLocomotive/IMoveableObject.java new file mode 100644 index 0000000..c102340 --- /dev/null +++ b/ProjectElectricLocomotive/IMoveableObject.java @@ -0,0 +1,10 @@ +package ProjectElectricLocomotive; +import java.awt.*; + +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + + boolean CheckCanMove(DyrectionType direction); + void MoveObject(DyrectionType direction); +} diff --git a/ProjectElectricLocomotive/MoveToCenter.java b/ProjectElectricLocomotive/MoveToCenter.java new file mode 100644 index 0000000..405efed --- /dev/null +++ b/ProjectElectricLocomotive/MoveToCenter.java @@ -0,0 +1,45 @@ +package ProjectElectricLocomotive; + +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() + { + var objParams = GetObjectParameters(); + if (objParams == null) return false; + + return objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal() + GetStep() + >= FieldWidth / 2 && objParams.ObjectMiddleVertical() <= FieldHeight / 2 && objParams.ObjectMiddleVertical() + + GetStep() >= FieldHeight / 2; + } + + @Override + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) return; + var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectElectricLocomotive/MoveToRigthCorner.java b/ProjectElectricLocomotive/MoveToRigthCorner.java new file mode 100644 index 0000000..67b4ce5 --- /dev/null +++ b/ProjectElectricLocomotive/MoveToRigthCorner.java @@ -0,0 +1,23 @@ +package ProjectElectricLocomotive; + +public class MoveToRigthCorner extends AbstractStrategy { + @Override + protected boolean IsTargetDestinaion() + { + var objParams = GetObjectParameters(); + if (objParams == null) return false; + + return objParams.RightBorder() >= FieldWidth - GetStep() && objParams.DownBorder() >= FieldHeight - GetStep(); + } + + @Override + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) return; + + if (objParams.RightBorder() < FieldWidth - GetStep()) MoveRight(); + if (objParams.DownBorder() < FieldHeight - GetStep()) MoveDown(); + + } +} diff --git a/ObjectParameters.java b/ProjectElectricLocomotive/ObjectParameters.java similarity index 96% rename from ObjectParameters.java rename to ProjectElectricLocomotive/ObjectParameters.java index 0573525..ffa5140 100644 --- a/ObjectParameters.java +++ b/ProjectElectricLocomotive/ObjectParameters.java @@ -1,3 +1,4 @@ +package ProjectElectricLocomotive; public class ObjectParameters { private final int _x; private final int _y; diff --git a/ProjectElectricLocomotive/Status.java b/ProjectElectricLocomotive/Status.java new file mode 100644 index 0000000..54bdeaf --- /dev/null +++ b/ProjectElectricLocomotive/Status.java @@ -0,0 +1,7 @@ +package ProjectElectricLocomotive; + +public enum Status { + NotInit, + InProgress, + Finish +}