From bce8865b185e9bd787fc39fafb9ff1b970e6c3d5 Mon Sep 17 00:00:00 2001 From: frog24 Date: Thu, 28 Dec 2023 20:22:33 +0400 Subject: [PATCH 1/3] code upgreat --- .idea/WarmlyShip_Hard.iml | 11 +++++++++++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ 4 files changed, 31 insertions(+) create mode 100644 .idea/WarmlyShip_Hard.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/WarmlyShip_Hard.iml b/.idea/WarmlyShip_Hard.iml new file mode 100644 index 0000000..ababd95 --- /dev/null +++ b/.idea/WarmlyShip_Hard.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0efbba1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file -- 2.25.1 From f51605506de4c6881043d007974cd1cca3575344 Mon Sep 17 00:00:00 2001 From: frog24 Date: Thu, 28 Dec 2023 20:24:24 +0400 Subject: [PATCH 2/3] test --- scr/AbstractStrategy.java | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 scr/AbstractStrategy.java diff --git a/scr/AbstractStrategy.java b/scr/AbstractStrategy.java new file mode 100644 index 0000000..f549fbe --- /dev/null +++ b/scr/AbstractStrategy.java @@ -0,0 +1,2 @@ +public class AbstractStrategy { +} -- 2.25.1 From 9b465c83f0b3abaf077cf4b7ba6b4b5fdc676fe5 Mon Sep 17 00:00:00 2001 From: frog24 Date: Thu, 28 Dec 2023 20:56:39 +0400 Subject: [PATCH 3/3] test --- .idea/misc.xml | 2 +- scr/AbstractStrategy.java | 91 ++++++++++++++++++++++- scr/DrawingDecks.java | 2 +- scr/DrawingHexagoneDecks.java | 44 ++++++++++++ scr/DrawingObjectShip.java | 43 +++++++++++ scr/DrawingRoundDecks.java | 42 +++++++++++ scr/DrawingShip.java | 131 ++++++++++++++++++++++++++++++++++ scr/DrawingWarmlyShip.java | 98 +++---------------------- scr/EntityShip.java | 35 +++++++++ scr/EntityWarmlyShip.java | 33 +-------- scr/IDrawingDecks.java | 6 ++ scr/IMoveableObject.java | 9 +++ scr/MoveToBorder.java | 31 ++++++++ scr/MoveToCenter.java | 39 ++++++++++ scr/ObjectParameters.java | 40 +++++++++++ scr/Status.java | 5 ++ scr/WarmlyShipForm.java | 100 +++++++++++++++++++++----- 17 files changed, 611 insertions(+), 140 deletions(-) create mode 100644 scr/DrawingHexagoneDecks.java create mode 100644 scr/DrawingObjectShip.java create mode 100644 scr/DrawingRoundDecks.java create mode 100644 scr/DrawingShip.java create mode 100644 scr/EntityShip.java create mode 100644 scr/IDrawingDecks.java create mode 100644 scr/IMoveableObject.java create mode 100644 scr/MoveToBorder.java create mode 100644 scr/MoveToCenter.java create mode 100644 scr/ObjectParameters.java create mode 100644 scr/Status.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 0548357..c3cdf70 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/scr/AbstractStrategy.java b/scr/AbstractStrategy.java index f549fbe..b280bd8 100644 --- a/scr/AbstractStrategy.java +++ b/scr/AbstractStrategy.java @@ -1,2 +1,91 @@ -public class AbstractStrategy { +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.GetObjectPosition(); + } + + protected Integer GetStep() { + if (state != Status.InProgress) { + 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/scr/DrawingDecks.java b/scr/DrawingDecks.java index 5e6bae3..cf1ff78 100644 --- a/scr/DrawingDecks.java +++ b/scr/DrawingDecks.java @@ -1,6 +1,6 @@ import java.awt.*; -public class DrawingDecks { +public class DrawingDecks implements IDrawingDecks{ private DeckType deckType; private int NumberDecks; public void SetNumberDecks(int value){ diff --git a/scr/DrawingHexagoneDecks.java b/scr/DrawingHexagoneDecks.java new file mode 100644 index 0000000..60909f4 --- /dev/null +++ b/scr/DrawingHexagoneDecks.java @@ -0,0 +1,44 @@ +import java.awt.*; + +public class DrawingHexagoneDecks implements IDrawingDecks{ + private DeckType deckType; + private int NumberDecks; + public void SetNumberDecks(int value){ + NumberDecks = value; + switch (value){ + case 2: + deckType = DeckType.TwoDecks; + break; + case 3: + deckType = DeckType.ThreeDecks; + break; + default: + deckType = DeckType.OneDeck; + } + }; + public void DrawDeck(int x, int y, int width, int height, Graphics g, Color MainColor){ + int[] xPoints = {x, x+5, x+width-5, x+width, x+width, x, x}; + int[] yPoints = {y+5, y, y, y+5, y+height, y+height, y+5}; + g.setColor(MainColor); + g.fillPolygon(xPoints, yPoints, xPoints.length); + g.setColor(Color.black); + g.drawPolygon(xPoints, yPoints, xPoints.length); + } + + public void DrawingDecks(int _startPosX, int _startPosY, Color MainColor, Graphics g){ + switch (deckType){ + case OneDeck: + DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor); + break; + case TwoDecks: + DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor); + DrawDeck(_startPosX + 36, _startPosY + 22, 54, 8, g, MainColor); + break; + case ThreeDecks: + DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor); + DrawDeck(_startPosX + 36, _startPosY + 22, 54, 8, g, MainColor); + DrawDeck(_startPosX + 50, _startPosY + 14, 40, 8, g, MainColor); + break; + } + } +} diff --git a/scr/DrawingObjectShip.java b/scr/DrawingObjectShip.java new file mode 100644 index 0000000..b8f4aeb --- /dev/null +++ b/scr/DrawingObjectShip.java @@ -0,0 +1,43 @@ +public class DrawingObjectShip implements IMoveableObject{ + private DrawingShip _drawingShip = null; + public DrawingObjectShip(DrawingShip drawingShip) + { + + _drawingShip = drawingShip; + } + @Override + public ObjectParameters GetObjectPosition() + { + if (_drawingShip == null || _drawingShip.entityShip == null) { + return null; + } + return new ObjectParameters(_drawingShip.GetPosX(), _drawingShip.GetPosY(), + _drawingShip.GetWidth(), _drawingShip.GetHeight()); + } + + @Override + public int GetStep() + { + if (_drawingShip != null && _drawingShip.entityShip!=null){ + return (int)(_drawingShip.entityShip.GetStep()); + } + return 0; + } + + @Override + public boolean CheckCanMove(DirectionType direction) + { + if (_drawingShip != null){ + return _drawingShip.CanMove(direction); + } + return false; + } + + @Override + public void MoveObject(DirectionType direction) + { + if (_drawingShip != null) { + _drawingShip.MoveTransport(direction); + } + } +} diff --git a/scr/DrawingRoundDecks.java b/scr/DrawingRoundDecks.java new file mode 100644 index 0000000..2456971 --- /dev/null +++ b/scr/DrawingRoundDecks.java @@ -0,0 +1,42 @@ +import java.awt.*; + +public class DrawingRoundDecks implements IDrawingDecks{ + private DeckType deckType; + private int NumberDecks; + public void SetNumberDecks(int value){ + NumberDecks = value; + switch (value){ + case 2: + deckType = DeckType.TwoDecks; + break; + case 3: + deckType = DeckType.ThreeDecks; + break; + default: + deckType = DeckType.OneDeck; + } + }; + public void DrawDeck(int x, int y, int width, int height, Graphics g, Color MainColor){ + g.setColor(MainColor); + g.fillOval(x, y, width, height); + g.setColor(Color.black); + g.drawOval(x, y, width, height); + } + + public void DrawingDecks(int _startPosX, int _startPosY, Color MainColor, Graphics g){ + switch (deckType){ + case OneDeck: + DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor); + break; + case TwoDecks: + DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor); + DrawDeck(_startPosX + 36, _startPosY + 22, 54, 8, g, MainColor); + break; + case ThreeDecks: + DrawDeck(_startPosX + 30, _startPosY + 30, 60, 10, g, MainColor); + DrawDeck(_startPosX + 36, _startPosY + 22, 54, 8, g, MainColor); + DrawDeck(_startPosX + 50, _startPosY + 14, 40, 8, g, MainColor); + break; + } + } +} diff --git a/scr/DrawingShip.java b/scr/DrawingShip.java new file mode 100644 index 0000000..79aa028 --- /dev/null +++ b/scr/DrawingShip.java @@ -0,0 +1,131 @@ +import java.awt.*; +import java.util.Random; + +public class DrawingShip { + public EntityShip entityShip; + public EntityShip GetEntityWarmlyShip(){ + return entityShip; + } + private void SetEntityWarmlyShip(EntityShip entityShip){ + this.entityShip = entityShip; + } + private IDrawingDecks drawingDecks; + private int _pictureWidth; + private int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + private int _shipWidth = 100; + private int _shipHeight = 70; + public int GetPosX() { + return _startPosX; + } + + public int GetPosY() { + return _startPosY; + } + + public int GetWidth() { + return _shipWidth; + } + + public int GetHeight() { + return _shipHeight; + } + public DrawingShip(int speed, double weight, Color mainColor, int width, int height, int numberDecks){ + if (width < _shipWidth || height <_shipHeight){ + return; + } + _pictureWidth = width; + _pictureHeight = height; + entityShip = new EntityShip(speed, weight, mainColor); + Random rnd = new Random(); + drawingDecks = switch (rnd.nextInt(3)){ + case 0 -> new DrawingDecks(); + case 1 -> new DrawingRoundDecks(); + case 2 -> new DrawingHexagoneDecks(); + default -> new DrawingDecks(); + }; + drawingDecks.SetNumberDecks(numberDecks); + } + protected DrawingShip(int speed, double weight, Color mainColor, int width, int height, int numberDecks, int shipWidth, int shipHeight){ + if (width < _shipWidth || height <_shipHeight){ + } + _pictureWidth = width; + _pictureHeight = height; + _shipWidth = shipWidth; + _shipHeight = shipHeight; + + + entityShip = new EntityShip(speed, weight, mainColor); + Random rnd = new Random(); + drawingDecks = switch (rnd.nextInt(3)){ + case 0 -> new DrawingDecks(); + case 1 -> new DrawingRoundDecks(); + case 2 -> new DrawingHexagoneDecks(); + default -> new DrawingDecks(); + }; + drawingDecks.SetNumberDecks(numberDecks); + } + public void SetPosition(int x, int y){ + if (x < 0 || x + _shipWidth > _pictureWidth){ + x = 20; + } + if (y < 0 || y + _shipHeight > _pictureHeight){ + y = 20; + } + _startPosX = x; + _startPosY = y; + } + public boolean CanMove(DirectionType direction) + { + if (entityShip == null){ + return false; + } + return switch (direction){ + case Left -> _startPosX - entityShip.GetStep() > 0; + case Up -> _startPosY - entityShip.GetStep() > 0; + case Right -> _startPosX + entityShip.GetStep() + _shipWidth <= _pictureWidth; + case Down -> _startPosY + entityShip.GetStep() + _shipHeight <= _pictureHeight; + }; + } + public void MoveTransport(DirectionType direction){ + if (!CanMove(direction) || entityShip == null) { + return; + } + switch (direction) + { + case Left: + _startPosX -= (int)entityShip.GetStep(); + break; + case Up: + _startPosY -= (int)entityShip.GetStep(); + break; + case Right: + _startPosX += (int)entityShip.GetStep(); + break; + case Down: + _startPosY += (int)entityShip.GetStep(); + break; + } + } + public void DrawTransport(Graphics g){ + if (entityShip == null || drawingDecks == null){ + System.out.println("?????????"); + return; + } + //палуба + drawingDecks.DrawingDecks(_startPosX, _startPosY, entityShip.GetMainColor(), g); + //корпус + int[] xPoints = {_startPosX, _startPosX + 100, _startPosX + 90, _startPosX + 20, _startPosX}; + int[] yPoints = {_startPosY + 40, _startPosY + 40, _startPosY + 60, _startPosY + 60, _startPosY + 40}; + int nPoints = 5; + g.setColor(entityShip.GetMainColor()); + g.fillPolygon(xPoints, yPoints, nPoints); + g.setColor(Color.black); + g.drawPolygon(xPoints, yPoints, nPoints); + //якорь + g.drawLine(_startPosX + 25, _startPosY + 45, _startPosX + 25, _startPosY + 55); + g.drawLine(_startPosX + 20, _startPosY + 50, _startPosX + 30, _startPosY + 50); + g.drawLine(_startPosX + 23, _startPosY + 55, _startPosX + 27, _startPosY + 55); + } +} diff --git a/scr/DrawingWarmlyShip.java b/scr/DrawingWarmlyShip.java index c5e0903..ef557f5 100644 --- a/scr/DrawingWarmlyShip.java +++ b/scr/DrawingWarmlyShip.java @@ -1,83 +1,18 @@ import java.awt.*; -public class DrawingWarmlyShip { - private EntityWarmlyShip entityWarmlyShip; - public EntityWarmlyShip GetEntityWarmlyShip(){ - return entityWarmlyShip; - } - private void SetEntityWarmlyShip(EntityWarmlyShip entityWarmlyShip){ - this.entityWarmlyShip = entityWarmlyShip; - } - private DrawingDecks drawingDecks; - public DrawingDecks GetDrawingDecks(){ - return drawingDecks; - } - private void SetDrawingDecks( DrawingDecks drawingDecks){ - this.drawingDecks = drawingDecks; - } - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private final int _shipWidth = 100; - private final int _shipHeight = 70; - public boolean Init(int speed, double weight, Color mainColor, Color optionalColor, boolean pipes, boolean fuelCompartment, int width, int height, int numberDecks){ - if (width < _shipWidth || height <_shipHeight){ - return false; - } - _pictureWidth = width; - _pictureHeight = height; - entityWarmlyShip = new EntityWarmlyShip(); - entityWarmlyShip.Init(speed, weight, mainColor, optionalColor, pipes, fuelCompartment); - drawingDecks = new DrawingDecks(); - drawingDecks.SetNumberDecks(numberDecks); - return true; - } - public void SetPosition(int x, int y){ - if (x < 0 || x + _shipWidth > _pictureWidth){ - x = 20; - } - if (y < 0 || y + _shipHeight > _pictureHeight){ - y = 20; - } - _startPosX = x; - _startPosY = y; - } - public void MoveTransport(DirectionType direction){ - if (entityWarmlyShip == null){ - return; - } - switch (direction){ - case Left: - System.out.println(_startPosX + " " + entityWarmlyShip.GetStep() + " " + _shipWidth + " " + (_startPosX + entityWarmlyShip.GetStep() + _shipWidth) + " " + _pictureWidth); - if (_startPosX - entityWarmlyShip.GetStep() > 0){ - _startPosX -= (int)entityWarmlyShip.GetStep(); - } - break; - case Up: - System.out.println(_startPosY + " " + entityWarmlyShip.GetStep() + " " + (_startPosY + entityWarmlyShip.GetStep()) + " " + _pictureHeight); - if (_startPosY - entityWarmlyShip.GetStep() > 0){ - _startPosY -= (int)entityWarmlyShip.GetStep(); - } - break; - case Right: - System.out.println(_startPosX + " " + entityWarmlyShip.GetStep() + " " + _shipWidth + " " + (_startPosX + entityWarmlyShip.GetStep() + _shipWidth) + " " + _pictureWidth); - if (_startPosX + entityWarmlyShip.GetStep() + _shipWidth < _pictureWidth){ - _startPosX += (int)entityWarmlyShip.GetStep(); - } - break; - case Down: - System.out.println(_startPosY + " " + entityWarmlyShip.GetStep() + " " + (_startPosY + entityWarmlyShip.GetStep()) + " " + _pictureHeight); - if (_startPosY + entityWarmlyShip.GetStep() + _shipHeight < _pictureHeight){ - _startPosY += (int)entityWarmlyShip.GetStep(); - } - break; +public class DrawingWarmlyShip extends DrawingShip{ + public DrawingWarmlyShip(int speed, double weight, Color mainColor, Color optionalColor, + boolean pipes, boolean fuelCompartment, int width, int height, int numberDecks){ + super(speed, weight, mainColor, width, height, numberDecks, 100, 60); + if (entityShip != null){ + entityShip = new EntityWarmlyShip(speed, weight, mainColor, optionalColor, pipes, fuelCompartment); } } + @Override public void DrawTransport(Graphics g){ - if (entityWarmlyShip == null || drawingDecks == null){ - System.out.println("?????????"); + if (!(entityShip instanceof EntityWarmlyShip entityWarmlyShip)) { return; } + if (entityWarmlyShip.GetPipes()){ g.setColor(entityWarmlyShip.GetOptionalColor()); g.fillRect(_startPosX + 70, _startPosY, 10, 30); @@ -93,20 +28,7 @@ public class DrawingWarmlyShip { g.setColor(Color.black); g.drawRect(_startPosX + 10, _startPosY + 30, 10, 10); } - //палуба - drawingDecks.DrawingDecks(_startPosX, _startPosY, entityWarmlyShip.GetMainColor(), g); - //корпус - int[] xPoints = {_startPosX, _startPosX + 100, _startPosX + 90, _startPosX + 20, _startPosX}; - int[] yPoints = {_startPosY + 40, _startPosY + 40, _startPosY + 60, _startPosY + 60, _startPosY + 40}; - int nPoints = 5; - g.setColor(entityWarmlyShip.GetMainColor()); - g.fillPolygon(xPoints, yPoints, nPoints); - g.setColor(Color.black); - g.drawPolygon(xPoints, yPoints, nPoints); - //якорь - g.drawLine(_startPosX + 25, _startPosY + 45, _startPosX + 25, _startPosY + 55); - g.drawLine(_startPosX + 20, _startPosY + 50, _startPosX + 30, _startPosY + 50); - g.drawLine(_startPosX + 23, _startPosY + 55, _startPosX + 27, _startPosY + 55); + super.DrawTransport(g); } } diff --git a/scr/EntityShip.java b/scr/EntityShip.java new file mode 100644 index 0000000..7940349 --- /dev/null +++ b/scr/EntityShip.java @@ -0,0 +1,35 @@ +import java.awt.*; + +public class EntityShip { + private int Speed; + public int GetSpeed(){ + return Speed; + } + private void SetSpeed(int speed){ + Speed = speed; + } + private double Weight; + public double GetWeight(){ + return Weight; + } + private void SetWeight(int weight){ + Weight = weight; + } + private Color MainColor; + public Color GetMainColor(){ + return MainColor; + } + private void SetMainColor(Color mainColor){ + MainColor = mainColor; + } + private double Step; + public double GetStep() { + return (double)Speed * 100 / Weight; + } + public EntityShip(int speed, double weight, Color mainColor) + { + Speed = speed; + Weight = weight; + MainColor = mainColor; + } +} diff --git a/scr/EntityWarmlyShip.java b/scr/EntityWarmlyShip.java index 0411129..b06dcc3 100644 --- a/scr/EntityWarmlyShip.java +++ b/scr/EntityWarmlyShip.java @@ -1,26 +1,5 @@ import java.awt.*; -public class EntityWarmlyShip { - private int Speed; - public int GetSpeed(){ - return Speed; - } - private void SetSpeed(int speed){ - Speed = speed; - } - private double Weight; - public double GetWeight(){ - return Weight; - } - private void SetWeight(int weight){ - Weight = weight; - } - private Color MainColor; - public Color GetMainColor(){ - return MainColor; - } - private void SetMainColor(Color mainColor){ - MainColor = mainColor; - } +public class EntityWarmlyShip extends EntityShip{ private Color OptionalColor; public Color GetOptionalColor(){ return OptionalColor; @@ -42,15 +21,9 @@ public class EntityWarmlyShip { private void SetFuelCompartment(boolean fuelCompartment){ FuelCompartment = fuelCompartment; } - private double Step; - public double GetStep() { - return (double)Speed * 100 / Weight; - } - public void Init(int speed, double weight, Color mainColor, Color optionalColor, boolean pipes, boolean fuelCompartment) + public EntityWarmlyShip(int speed, double weight, Color mainColor, Color optionalColor, boolean pipes, boolean fuelCompartment) { - Speed = speed; - Weight = weight; - MainColor = mainColor; + super(speed, weight, mainColor); OptionalColor = optionalColor; Pipes = pipes; FuelCompartment = fuelCompartment; diff --git a/scr/IDrawingDecks.java b/scr/IDrawingDecks.java new file mode 100644 index 0000000..7d23be6 --- /dev/null +++ b/scr/IDrawingDecks.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawingDecks { + public void SetNumberDecks(int value); + public void DrawingDecks(int _startPosX, int _startPosY, Color MainColor, Graphics g); +} diff --git a/scr/IMoveableObject.java b/scr/IMoveableObject.java new file mode 100644 index 0000000..b7bd6aa --- /dev/null +++ b/scr/IMoveableObject.java @@ -0,0 +1,9 @@ +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + + int GetStep(); + + boolean CheckCanMove(DirectionType direction); + + void MoveObject(DirectionType direction); +} diff --git a/scr/MoveToBorder.java b/scr/MoveToBorder.java new file mode 100644 index 0000000..a8de159 --- /dev/null +++ b/scr/MoveToBorder.java @@ -0,0 +1,31 @@ +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.RightBorder() - GetFieldWidth(); + if (Math.abs(diffX) > GetStep()) + { + MoveRight(); + } + var diffY = objParams.DownBorder() - GetFieldHeight(); + if (Math.abs(diffY) > GetStep()) + { + MoveDown(); + } + } +} diff --git a/scr/MoveToCenter.java b/scr/MoveToCenter.java new file mode 100644 index 0000000..ea5285d --- /dev/null +++ b/scr/MoveToCenter.java @@ -0,0 +1,39 @@ +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean isTargetDestination() { + var objParams = GetObjectParameters(); + if (objParams == null ) { + return false; + } + return + Math.abs(objParams.ObjectMiddleHorizontal() - GetFieldWidth() / 2) <= GetStep() + && Math.abs(objParams.ObjectMiddleVertical() - GetFieldHeight() / 2) <= GetStep(); + } + @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(); + } + } + } +} diff --git a/scr/ObjectParameters.java b/scr/ObjectParameters.java new file mode 100644 index 0000000..9a8fc2c --- /dev/null +++ b/scr/ObjectParameters.java @@ -0,0 +1,40 @@ +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/scr/Status.java b/scr/Status.java new file mode 100644 index 0000000..6467ed7 --- /dev/null +++ b/scr/Status.java @@ -0,0 +1,5 @@ +public enum Status { + NotInit, + InProgress, + Finish +} diff --git a/scr/WarmlyShipForm.java b/scr/WarmlyShipForm.java index 283ae78..0184984 100644 --- a/scr/WarmlyShipForm.java +++ b/scr/WarmlyShipForm.java @@ -6,16 +6,20 @@ import java.awt.event.ActionListener; import java.util.*; public class WarmlyShipForm{ - private DrawingWarmlyShip _drawingWarmlyShip; + private DrawingShip _drawingShip; + private AbstractStrategy _abstractStrategy; Canvas canv; public void Draw(){ canv.repaint(); } public WarmlyShipForm(){ JFrame frame = new JFrame("Warmly Ship"); - JButton buttonCreate = new JButton("Создать"); - buttonCreate.setFocusPainted(false); - buttonCreate.setContentAreaFilled(false); + JButton buttonCreateShip = new JButton("Создать"); + buttonCreateShip.setFocusPainted(false); + buttonCreateShip.setContentAreaFilled(false); + JButton buttonCreateWarmlyShip = new JButton("Создать Продвинутый"); + buttonCreateWarmlyShip.setFocusPainted(false); + buttonCreateWarmlyShip.setContentAreaFilled(false); JButton buttonUp = new JButton(); //buttonUp.setBorderPainted(false); //граница кнопки buttonUp.setFocusPainted(false); //контур вокруг текста @@ -37,20 +41,43 @@ public class WarmlyShipForm{ buttonRight.setContentAreaFilled(false); buttonRight.setName("right"); buttonRight.setIcon(new ImageIcon(((new ImageIcon("images/ArrowRight.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); - buttonCreate.addActionListener( + String[] items = { + "Form center", + "Form border" + }; + JComboBox comboBoxStrategy = new JComboBox(items); + JButton buttonStep = new JButton("Шаг"); + buttonStep.setFocusPainted(false); + buttonStep.setContentAreaFilled(false); + buttonCreateShip.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); Random random = new Random(); - _drawingWarmlyShip = new DrawingWarmlyShip(); - _drawingWarmlyShip.Init( + _drawingShip = new DrawingShip( + random.nextInt(200) + 100, + random.nextInt(2000) + 1000, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + 900, 460, random.nextInt(3) + 1); + _drawingShip.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + canv._drawingShip = _drawingShip; + Draw(); + } + } + ); + buttonCreateWarmlyShip.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e) { + System.out.println(e.getActionCommand()); + Random random = new Random(); + _drawingShip = new DrawingWarmlyShip( random.nextInt(200) + 100, random.nextInt(2000) + 1000, 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(), 900, 460, random.nextInt(3) + 1); - _drawingWarmlyShip.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); - canv._drawingWarmlyShip = _drawingWarmlyShip; + _drawingShip.SetPosition(random.nextInt(90) + 10, random.nextInt(90) + 10); + canv._drawingShip = _drawingShip; Draw(); } } @@ -58,26 +85,55 @@ public class WarmlyShipForm{ ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(((JButton)(e.getSource())).getName()); - if (_drawingWarmlyShip == null){ + if (_drawingShip == null){ return; } switch ((((JButton)(e.getSource())).getName())){ case "up": - _drawingWarmlyShip.MoveTransport(DirectionType.Up); + _drawingShip.MoveTransport(DirectionType.Up); break; case "down": - _drawingWarmlyShip.MoveTransport(DirectionType.Down); + _drawingShip.MoveTransport(DirectionType.Down); break; case "left": - _drawingWarmlyShip.MoveTransport(DirectionType.Left); + _drawingShip.MoveTransport(DirectionType.Left); break; case "right": - _drawingWarmlyShip.MoveTransport(DirectionType.Right); + _drawingShip.MoveTransport(DirectionType.Right); break; } Draw(); } }; + buttonStep.addActionListener(e -> { + if (_drawingShip == 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 DrawingObjectShip(_drawingShip), this.canv.getWidth(), this.canv.getHeight()-50); + } + if (_abstractStrategy == null) + { + return; + } + comboBoxStrategy.setEnabled(false); + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.setEnabled(true); + _abstractStrategy = null; + } + }); buttonUp.addActionListener(actionListener); buttonDown.addActionListener(actionListener); buttonLeft.addActionListener(actionListener); @@ -87,30 +143,36 @@ public class WarmlyShipForm{ frame.setLayout(null); canv = new Canvas(); canv.setBounds(0, 0, 900, 500); - buttonCreate.setBounds(2, 420, 100, 40); + buttonCreateShip.setBounds(2, 420, 100, 40); + buttonCreateWarmlyShip.setBounds(102, 420, 100, 40); buttonUp.setBounds(800, 380, 40, 40); buttonDown.setBounds(800, 420, 40, 40); buttonLeft.setBounds(760, 420, 40, 40); buttonRight.setBounds(840, 420, 40, 40); + comboBoxStrategy.setBounds(800,10,100,50); + buttonStep.setBounds(800,80,100,40); frame.add(canv); - frame.add(buttonCreate); + frame.add(buttonCreateShip); + frame.add(buttonCreateWarmlyShip); frame.add(buttonUp); frame.add(buttonDown); frame.add(buttonLeft); frame.add(buttonRight); + frame.add(comboBoxStrategy); + frame.add(buttonStep); frame.setVisible(true); } class Canvas extends JComponent{ - public DrawingWarmlyShip _drawingWarmlyShip; + public DrawingShip _drawingShip; public Canvas(){} public void paintComponent(Graphics g){ - if (_drawingWarmlyShip == null){ + if (_drawingShip == null){ return; } super.paintComponents(g); Graphics2D g2d = (Graphics2D)g; - _drawingWarmlyShip.DrawTransport(g2d); + _drawingShip.DrawTransport(g2d); super.repaint(); } } -- 2.25.1