From 23b457e9c15254bd40bac3ac500479ae19b8b2bf Mon Sep 17 00:00:00 2001 From: sardq Date: Mon, 4 Dec 2023 20:50:24 +0400 Subject: [PATCH] =?UTF-8?q?2=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AbstractStrategy.java | 119 ++++++++++++++++++ src/DrawingCrane.java | 84 ++++++++----- src/DrawingHoistingCrane.java | 31 +++++ src/DrawingWheelDouble.java | 31 +++++ ...rawingWheel.java => DrawingWheelOval.java} | 2 +- src/DrawingWheelRectangle.java | 32 +++++ src/DrawningObjectCrane.java | 29 +++++ src/EntityCrane.java | 21 +--- src/EntityHoistingCrane.java | 19 +++ src/Frame.java | 96 +++++++++++--- src/IMoveableObject.java | 7 ++ src/IWheel.java | 5 + src/MoveToBorder.java | 47 +++++++ src/MoveToCenter.java | 45 +++++++ src/ObjectParameters.java | 25 ++++ src/Status.java | 5 + 16 files changed, 529 insertions(+), 69 deletions(-) create mode 100644 src/AbstractStrategy.java create mode 100644 src/DrawingHoistingCrane.java create mode 100644 src/DrawingWheelDouble.java rename src/{DrawingWheel.java => DrawingWheelOval.java} (95%) create mode 100644 src/DrawingWheelRectangle.java create mode 100644 src/DrawningObjectCrane.java create mode 100644 src/EntityHoistingCrane.java create mode 100644 src/IMoveableObject.java create mode 100644 src/IWheel.java create mode 100644 src/MoveToBorder.java create mode 100644 src/MoveToCenter.java create mode 100644 src/ObjectParameters.java create mode 100644 src/Status.java diff --git a/src/AbstractStrategy.java b/src/AbstractStrategy.java new file mode 100644 index 0000000..b5f3b33 --- /dev/null +++ b/src/AbstractStrategy.java @@ -0,0 +1,119 @@ +public abstract class AbstractStrategy +{ + // Перемещаемый объект + private IMoveableObject _moveableObject; + // Статус перемещения + private Status _state = Status.NotInit; + // Ширина поля + protected int FieldWidth; + protected int GetFieldWidth() + { + return FieldWidth; + } + private void SetFieldWidth(int fieldWidth) { + FieldWidth = fieldWidth; + } + // Высота поля + protected int FieldHeight ; + protected int GetFieldHeigth() + { + return FieldHeight; + } + private void SetFieldHeigth(int fieldHeight) { + FieldHeight = 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(); + } + // Перемещение влево + /// Результат перемещения (true - удалось переместиться, false -неудача) + protected boolean MoveLeft() {return MoveTo(DirectionType.Left);} + // Перемещение вправо false - неудача) + protected boolean MoveRight() {return MoveTo(DirectionType.Right);} + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected boolean MoveUp(){return MoveTo(DirectionType.Up);} + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected boolean MoveDown() {return MoveTo(DirectionType.Down);} + /// + /// Параметры объекта + /// + protected ObjectParameters GetObjectParameters() + { + return _moveableObject.GetObjectPosition(); + } + + /// + /// Шаг объекта + /// + /// + protected Integer GetStep() +{ + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject.GetStep(); +} + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract boolean IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private boolean MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(directionType)) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } +} diff --git a/src/DrawingCrane.java b/src/DrawingCrane.java index 48df5c8..dbef7f9 100644 --- a/src/DrawingCrane.java +++ b/src/DrawingCrane.java @@ -1,31 +1,41 @@ import java.awt.*; - public class DrawingCrane { - private EntityCrane entityCrane; - private DrawingWheel drawingWheel; + protected EntityCrane entityCrane; + public EntityCrane GetEntityCrane(){return entityCrane;} + protected void SetEntityCrane(EntityCrane entityCraneTemp){entityCrane=entityCraneTemp;} + private IWheel drawingWheel; private int _pictureWidth; private int _pictureHeight; - private int _startPositionX; - private int _startPositionY; - + protected int _startPositionX; + protected int _startPositionY; private final int _craneWidth = 200; - private final int _craneHeight = 150; - public void Init(int speed, double weight, boolean counterWeight, boolean crane, Color bodyColor, Color additionalColor, int width, int height,int wheelCount) + public int GetPosX() {return _startPositionX;} + public int GetPosY() {return _startPositionY;} + public int GetWidth() {return _craneWidth;} + public int GetHeight() {return _craneHeight;} + + + protected DrawingCrane(int speed, double weight, Color bodyColor, int width, int height, int WheelCount, int wheelMode) { - if ((_craneWidth > width) || (_craneHeight > height)) return; _pictureHeight = height; _pictureWidth = width; - entityCrane = new EntityCrane(); - entityCrane.Init(speed, weight, counterWeight,crane, bodyColor, additionalColor); - drawingWheel = new DrawingWheel(); - drawingWheel.SetWheelCounter(wheelCount); + int mode = wheelMode % 3; + entityCrane = new EntityCrane(speed, weight, bodyColor); + switch (mode) + { + case 0 -> {drawingWheel = new DrawingWheelOval();} + case 1 -> {drawingWheel = new DrawingWheelDouble();} + case 2 -> {drawingWheel = new DrawingWheelRectangle();} + } + + drawingWheel.SetWheelCounter(WheelCount); + } public void SetPosition(int x, int y) { - if(x+_craneWidth>_pictureWidth) { while(x + _craneWidth > _pictureWidth) @@ -49,39 +59,56 @@ public class DrawingCrane if (entityCrane == null) return; switch (direction) { case Left -> { - if (_startPositionX - entityCrane.Step > 0) { _startPositionX -= (int) entityCrane.Step; } } case Right->{ - if (_startPositionX + entityCrane.Step + _craneWidth < _pictureWidth) { _startPositionX += (int) entityCrane.Step; } } case Up-> { - if (_startPositionY - entityCrane.Step > 0) { _startPositionY -= (int) entityCrane.Step; } } - case Down ->{ if (_startPositionY + entityCrane.Step + _craneHeight < _pictureHeight) { - _startPositionY += (int) entityCrane.Step; } } - } - + } + public boolean CanMove(DirectionType direction) + { + if (entityCrane == null) + { + return false; + } + switch(direction) + { + //влево + case Left -> { + return _startPositionX - entityCrane.Step > 0;} + //вверх + case Up -> { + return _startPositionY - entityCrane.Step > 0;} + // вправо + case Right -> { + return _startPositionX + entityCrane.Step - _craneWidth < _pictureWidth;} + //вниз + case Down -> { + return _startPositionY + entityCrane.Step - _craneHeight < _pictureHeight;} + default ->{ + return false; + } + } } public void DrawTransport(Graphics g) { var g2d = (Graphics2D)g; if (entityCrane == null) return; // Гусеницы - g2d.drawLine( _startPositionX + 5, _startPositionY + 110, _startPositionX + 195, _startPositionY + 110); g2d.drawLine( _startPositionX, _startPositionY + 115, _startPositionX, _startPositionY + 145); g2d.drawLine( _startPositionX + 5, _startPositionY + 150, _startPositionX + 195, _startPositionY + 150); @@ -100,16 +127,5 @@ public class DrawingCrane //кабинка и выхлоп g2d.drawRect( _startPositionX + 60, _startPositionY + 10, 20, 55); g2d.drawRect( _startPositionX + 110, _startPositionY, 75, 65); - if (entityCrane.getCounterWeight()) { - g2d.setColor(entityCrane.getAdditionalColor()); - g2d.drawRect( _startPositionX + 185, _startPositionY + 20, 15, 45); - - } - if (entityCrane.getCrane()) { - g2d.drawRect( _startPositionX + 20, _startPositionY, 30, 65); - g2d.drawRect( _startPositionX, _startPositionY, 20, 30); - g2d.fillRect( _startPositionX + 20, _startPositionY, 30, 65); - g2d.fillRect( _startPositionX, _startPositionY, 20, 30); - } } -} \ No newline at end of file +} diff --git a/src/DrawingHoistingCrane.java b/src/DrawingHoistingCrane.java new file mode 100644 index 0000000..7810239 --- /dev/null +++ b/src/DrawingHoistingCrane.java @@ -0,0 +1,31 @@ +import java.awt.*; +public class DrawingHoistingCrane extends DrawingCrane { + public DrawingHoistingCrane(int speed, double weight, Color bodyColor, Color additionalColor, boolean counterWeight, boolean crane,int width, int height, int WheelCount, int wheelMode) + { + super(speed, weight, bodyColor, width, height, WheelCount,wheelMode); + if (entityCrane != null) + { + entityCrane = new EntityHoistingCrane(speed, weight, bodyColor, additionalColor, counterWeight, crane); + } + + } + @Override + public void DrawTransport(Graphics g) { + if (!(entityCrane instanceof EntityHoistingCrane)) { + return; + } + super.DrawTransport(g); + if (((EntityHoistingCrane) entityCrane).getCounterWeight()) { + g.setColor(((EntityHoistingCrane) entityCrane).getAdditionalColor()); + g.drawRect(_startPositionX + 185, _startPositionY + 20, 15, 45); + + } + if (((EntityHoistingCrane) entityCrane).getCrane()) { + g.drawRect(_startPositionX + 20, _startPositionY, 30, 65); + g.drawRect(_startPositionX, _startPositionY, 20, 30); + g.setColor(((EntityHoistingCrane) entityCrane).getAdditionalColor()); + g.fillRect(_startPositionX + 20, _startPositionY, 30, 65); + g.fillRect(_startPositionX, _startPositionY, 20, 30); + } + } +} diff --git a/src/DrawingWheelDouble.java b/src/DrawingWheelDouble.java new file mode 100644 index 0000000..3ad8325 --- /dev/null +++ b/src/DrawingWheelDouble.java @@ -0,0 +1,31 @@ +import java.awt.*; + +public class DrawingWheelDouble implements IWheel{ + private WheelCounter wheelCounter; + public void SetWheelCounter(int count) + { + if (count % 3 == 0) + wheelCounter = WheelCounter.SIX; + else if (count % 3 == 1) + wheelCounter = WheelCounter.FOUR; + else if (count % 3 == 2) + wheelCounter = WheelCounter.FIVE; + } + + public void DrawWheels(int _startPositionX, int _startPositionY, Graphics2D g2d) + { + g2d.drawOval( _startPositionX + 2, _startPositionY + 112, 36, 36); + g2d.drawOval( _startPositionX + 7, _startPositionY + 117, 25, 25); + g2d.drawOval( _startPositionX + 160, _startPositionY + 112, 36, 36); + g2d.drawOval( _startPositionX + 165, _startPositionY + 117, 25, 25); + g2d.drawOval( _startPositionX + 74, _startPositionY + 112, 10, 10); + g2d.drawOval( _startPositionX + 76, _startPositionY + 114, 6, 6); + g2d.drawOval( _startPositionX + 111, _startPositionY + 112, 10, 10); + g2d.drawOval( _startPositionX + 113, _startPositionY + 114, 6, 6); + switch (wheelCounter) + { + case FIVE -> {g2d.drawOval( _startPositionX + 133, _startPositionY + 128, 20, 20);g2d.drawOval( _startPositionX + 136, _startPositionY + 131, 14, 14);} + case SIX -> {g2d.drawOval( _startPositionX + 45, _startPositionY + 128, 20, 20);g2d.drawOval( _startPositionX + 48, _startPositionY + 131, 14, 14);g2d.drawOval( _startPositionX + 136, _startPositionY + 131, 14, 14); g2d.drawOval( _startPositionX + 133, _startPositionY + 128, 20, 20);} + } + } +} diff --git a/src/DrawingWheel.java b/src/DrawingWheelOval.java similarity index 95% rename from src/DrawingWheel.java rename to src/DrawingWheelOval.java index adae0c1..c298939 100644 --- a/src/DrawingWheel.java +++ b/src/DrawingWheelOval.java @@ -1,6 +1,6 @@ import java.awt.*; -public class DrawingWheel { +public class DrawingWheelOval implements IWheel { private WheelCounter wheelCounter; public void SetWheelCounter(int count) { diff --git a/src/DrawingWheelRectangle.java b/src/DrawingWheelRectangle.java new file mode 100644 index 0000000..e9e4e16 --- /dev/null +++ b/src/DrawingWheelRectangle.java @@ -0,0 +1,32 @@ +import java.awt.*; + +public class DrawingWheelRectangle implements IWheel { + private WheelCounter wheelCounter; + public WheelCounter getWheelCount() + { + return wheelCounter; + } + + public void SetWheelCounter(int count) + { + if (count % 3 == 0) + wheelCounter = WheelCounter.SIX; + else if (count % 3 == 1) + wheelCounter = WheelCounter.FOUR; + else if (count % 3 == 2) + wheelCounter = WheelCounter.FIVE; + } + + public void DrawWheels(int _startPositionX, int _startPositionY, Graphics2D g2d) + { + g2d.drawRect( _startPositionX + 2, _startPositionY + 112, 36, 36); + g2d.drawRect( _startPositionX + 160, _startPositionY + 112, 36, 36); + g2d.drawRect( _startPositionX + 74, _startPositionY + 112, 10, 10); + g2d.drawRect( _startPositionX + 111, _startPositionY + 112, 10, 10); + switch (wheelCounter) + { + case FIVE -> {g2d.drawRect( _startPositionX + 133, _startPositionY + 128, 20, 20);} + case SIX -> {g2d.drawRect( _startPositionX + 45, _startPositionY + 128, 20, 20); g2d.drawRect( _startPositionX + 133, _startPositionY + 128, 20, 20);} + } + } +} diff --git a/src/DrawningObjectCrane.java b/src/DrawningObjectCrane.java new file mode 100644 index 0000000..828b689 --- /dev/null +++ b/src/DrawningObjectCrane.java @@ -0,0 +1,29 @@ +public class DrawningObjectCrane implements IMoveableObject { + private final DrawingCrane _drawingCrane; + public DrawningObjectCrane(DrawingCrane drawingCrane) + { + _drawingCrane = drawingCrane; + } + public ObjectParameters GetObjectPosition() + { + if (_drawingCrane == null || _drawingCrane.entityCrane == null) + { + return null; + } + return new ObjectParameters(_drawingCrane.GetPosX(), _drawingCrane.GetPosY(), _drawingCrane.GetWidth(), _drawingCrane.GetHeight()); + + } + public int GetStep() { + int step = (int) (_drawingCrane.entityCrane.Step); + if (step != 0) + return step; + return 0; + } + public boolean CheckCanMove(DirectionType direction) { + boolean canMove = _drawingCrane.CanMove(direction); + if(canMove) + return true; + return false; + } + public void MoveObject(DirectionType direction) { _drawingCrane.MoveCrane(direction);} +} diff --git a/src/EntityCrane.java b/src/EntityCrane.java index c47e360..2b76535 100644 --- a/src/EntityCrane.java +++ b/src/EntityCrane.java @@ -6,8 +6,6 @@ public class EntityCrane private int Speed ; public int getSpeed() {return Speed;} private void setSpeed(int value) {Speed = value;} - - //вес private double Weight ; public double getWeight() {return Weight;} @@ -16,29 +14,14 @@ public class EntityCrane private Color BodyColor ; public Color getBodyColor() {return BodyColor;} private void setBodyColor(Color value) {BodyColor = value;} - //дополнительный - private Color AdditionalColor ; - public Color getAdditionalColor() {return AdditionalColor;} - private void setAdditionalColor(Color value) {AdditionalColor = value;} - - private boolean CounterWeight ; - public boolean getCounterWeight() {return CounterWeight;} - private void setCounterWeight(boolean value) {CounterWeight = value;} - - private boolean Crane; - public boolean getCrane() {return Crane;} - private void setCrane(boolean value) {Crane = value;} //шаг перемещения public double Step; - public void Init(int speed, double weight, boolean counterWeight,boolean crane, Color bodyColor, Color additionalColor) + public EntityCrane(int speed, double weight, Color bodyColor) { Speed = speed; Weight = weight; - CounterWeight = counterWeight; - Crane = crane; BodyColor = bodyColor; - AdditionalColor = additionalColor; Step = (double)Speed * 100 / Weight; } -} +} \ No newline at end of file diff --git a/src/EntityHoistingCrane.java b/src/EntityHoistingCrane.java new file mode 100644 index 0000000..3724e5f --- /dev/null +++ b/src/EntityHoistingCrane.java @@ -0,0 +1,19 @@ +import java.awt.*; + +public class EntityHoistingCrane extends EntityCrane +{ + private Color AdditionalColor ; + public Color getAdditionalColor() {return AdditionalColor;} + private boolean CounterWeight ; + public boolean getCounterWeight() {return CounterWeight;} + private boolean Crane; + public boolean getCrane() {return Crane;} + private void setCrane(boolean value) {Crane = value;} + public EntityHoistingCrane(int speed, double weight, Color bodyColor, Color additionalColor, boolean counterWeight, boolean crane) + { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + CounterWeight = counterWeight; + Crane = crane; + } +} diff --git a/src/Frame.java b/src/Frame.java index b76a4ff..fa719de 100644 --- a/src/Frame.java +++ b/src/Frame.java @@ -1,14 +1,11 @@ -import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.image.*; -import java.io.IOException; import java.util.Random; - class DrawCrane extends JComponent { private DrawingCrane _drawingCrane; + private AbstractStrategy _abstractStrategy; public void paintComponent(Graphics g) { super.paintComponent(g); @@ -20,15 +17,59 @@ class DrawCrane extends JComponent { protected void CreateCraneButton_Click() { Random rnd = new Random(); - _drawingCrane = new DrawingCrane(); - Color addColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt( 256)); Color bodyColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt(256)); - _drawingCrane.Init(rnd.nextInt(100)+100, rnd.nextInt(2000)+1000, - ConvertToBoolean(rnd.nextInt( 2)), ConvertToBoolean(rnd.nextInt( 2)), bodyColor, addColor, - Frame.Width-10, Frame.Height-38, rnd.nextInt( 3)+4); + _drawingCrane = new DrawingCrane(rnd.nextInt(100)+100, rnd.nextInt(2000)+1000, + bodyColor, + Frame.Width-10, Frame.Height-38, rnd.nextInt( 3),rnd.nextInt( 3)); _drawingCrane.SetPosition(rnd.nextInt( 100), rnd.nextInt(90)+10); super.repaint(); } + protected void CreateHoistingCraneButton_Click() + { + Random rnd = new Random(); + Color addColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt( 256)); + Color bodyColor = new Color(rnd.nextInt( 256), rnd.nextInt( 256), rnd.nextInt(256)); + + _drawingCrane = new DrawingHoistingCrane(rnd.nextInt(100)+100, rnd.nextInt(2000)+1000, + bodyColor, + addColor,ConvertToBoolean(rnd.nextInt( 2)), ConvertToBoolean(rnd.nextInt( 2)),Frame.Width-10, Frame.Height-38, rnd.nextInt( 3),rnd.nextInt( 3)); + _drawingCrane.SetPosition(rnd.nextInt( 100), rnd.nextInt(90)+10); + super.repaint(); + } + protected void buttonStep_Click(JComboBox comboBox,int width,int height) + { + if (_drawingCrane == null) + { + return; + } + if (comboBox.isEnabled()) + { + String strategy = String.valueOf(comboBox.getSelectedItem()); + switch (strategy) + { + case "0"-> {_abstractStrategy =new MoveToCenter();} + case "1"-> {_abstractStrategy =new MoveToBorder();} + default -> {_abstractStrategy = null;} + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawningObjectCrane(_drawingCrane), width, height); + comboBox.setEnabled(false); + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + super.repaint(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBox.setEnabled(true); + _abstractStrategy = null; + } + } protected void ButtonMove_Click(String tag) { if (_drawingCrane == null) @@ -55,7 +96,6 @@ public class Frame extends JFrame { } protected static final int Width = 900; protected static final int Height = 500; - DrawCrane Crane; private void InitUI() { setSize(Width, Height); @@ -91,6 +131,7 @@ public class Frame extends JFrame { down.setLayout(null); JButton CreateButton = new JButton("Создать"); + JButton CreateHoistingButton = new JButton("Создать улучшенный кран"); right.setIcon(new ImageIcon("src/crane/right.png")); left.setIcon(new ImageIcon("src/crane/left.png")); down.setIcon(new ImageIcon("src/crane/down.png")); @@ -115,16 +156,28 @@ public class Frame extends JFrame { constraints.gridx = 2; panelMoveButtons.add(right, constraints); - + String[] items = { + "0", + "1" + }; + JComboBox comboBoxStrategy = new JComboBox(items); + comboBoxStrategy.setBounds(697,16,182,33); JPanel panelCreateButton = new JPanel(); panelCreateButton.setLayout(new BorderLayout()); - panelCreateButton.setBounds(0, 420, 112, 34); + panelCreateButton.setBounds(0, 420, 200, 34); panelCreateButton.add(CreateButton); + JPanel panelCreateHoistingButton = new JPanel(); + panelCreateHoistingButton.setLayout(new BorderLayout()); + panelCreateHoistingButton.setBounds(200, 420, 200, 34); + panelCreateHoistingButton.add(CreateHoistingButton); + JButton buttonStep =new JButton("Шаг"); + buttonStep.setBounds(798,55,80,35); + add(buttonStep); add(panelMoveButtons); add(panelCreateButton); - - + add(panelCreateHoistingButton); + add(comboBoxStrategy); setVisible(true); CreateButton.addActionListener(new ActionListener() { @Override @@ -132,11 +185,24 @@ public class Frame extends JFrame { Crane.CreateCraneButton_Click(); } }); + CreateHoistingButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Crane.CreateHoistingCraneButton_Click(); + } + }); + buttonStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Crane.buttonStep_Click(comboBoxStrategy,getWidth(),getHeight()); + } + }); } + public class MoveAL implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Crane.ButtonMove_Click(e.getActionCommand()); } } -} +} \ No newline at end of file diff --git a/src/IMoveableObject.java b/src/IMoveableObject.java new file mode 100644 index 0000000..9e50e7b --- /dev/null +++ b/src/IMoveableObject.java @@ -0,0 +1,7 @@ +public interface IMoveableObject +{ + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean CheckCanMove(DirectionType direction); + void MoveObject(DirectionType direction); +} diff --git a/src/IWheel.java b/src/IWheel.java new file mode 100644 index 0000000..c5e9941 --- /dev/null +++ b/src/IWheel.java @@ -0,0 +1,5 @@ +import java.awt.*; +public interface IWheel { + void DrawWheels(int _startPositionX, int _startPositionY, Graphics2D g2d); + void SetWheelCounter(int wheelCount); +} diff --git a/src/MoveToBorder.java b/src/MoveToBorder.java new file mode 100644 index 0000000..69b3524 --- /dev/null +++ b/src/MoveToBorder.java @@ -0,0 +1,47 @@ +public class MoveToBorder extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal() <= FieldWidth && objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth && + objParams.ObjectMiddleHorizontal() <= FieldHeight && + objParams.ObjectMiddleHorizontal() + GetStep() >= FieldHeight; + } + @Override + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder() - FieldWidth; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.DownBorder() - FieldHeight; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/src/MoveToCenter.java b/src/MoveToCenter.java new file mode 100644 index 0000000..8c0209a --- /dev/null +++ b/src/MoveToCenter.java @@ -0,0 +1,45 @@ +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/src/ObjectParameters.java b/src/ObjectParameters.java new file mode 100644 index 0000000..33b9931 --- /dev/null +++ b/src/ObjectParameters.java @@ -0,0 +1,25 @@ +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) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/src/Status.java b/src/Status.java new file mode 100644 index 0000000..6467ed7 --- /dev/null +++ b/src/Status.java @@ -0,0 +1,5 @@ +public enum Status { + NotInit, + InProgress, + Finish +}