diff --git a/ProjectContainerShip/src/DeckCount.java b/ProjectContainerShip/src/Drawings/DeckCount.java similarity index 84% rename from ProjectContainerShip/src/DeckCount.java rename to ProjectContainerShip/src/Drawings/DeckCount.java index d8ceff9..45d67ae 100644 --- a/ProjectContainerShip/src/DeckCount.java +++ b/ProjectContainerShip/src/Drawings/DeckCount.java @@ -1,4 +1,4 @@ -package ProjectContainerShip.src; +package ProjectContainerShip.src.Drawings; public enum DeckCount { One(1), diff --git a/ProjectContainerShip/src/DirectionType.java b/ProjectContainerShip/src/Drawings/DirectionType.java similarity index 61% rename from ProjectContainerShip/src/DirectionType.java rename to ProjectContainerShip/src/Drawings/DirectionType.java index 523ac1b..87f9677 100644 --- a/ProjectContainerShip/src/DirectionType.java +++ b/ProjectContainerShip/src/Drawings/DirectionType.java @@ -1,4 +1,4 @@ -package ProjectContainerShip.src; +package ProjectContainerShip.src.Drawings; public enum DirectionType { Up, diff --git a/ProjectContainerShip/src/Drawings/DrawningContainerShip.java b/ProjectContainerShip/src/Drawings/DrawningContainerShip.java new file mode 100644 index 0000000..232a566 --- /dev/null +++ b/ProjectContainerShip/src/Drawings/DrawningContainerShip.java @@ -0,0 +1,88 @@ +package ProjectContainerShip.src.Drawings; + +import ProjectContainerShip.src.Entities.EntityContainerShip; + +import java.awt.*; + +public class DrawningContainerShip extends DrawningShip { + EntityContainerShip entityContainerShip; + + public DrawningContainerShip(int speed, double weight, Color bodyColor, int deckType, Color additionalColor, boolean crane, boolean container) { + super(speed, weight, bodyColor, deckType, 130, 60); + entityContainerShip = new EntityContainerShip(speed, weight, bodyColor, additionalColor, crane, container); + } + + // тут рисовашки + + @Override + public void DrawShip(Graphics g) { + if (entityContainerShip == null || _startPosX == null || _startPosY == null) { + return; + } + super.DrawShip(g); + Graphics2D g2d = (Graphics2D) g; + + if (entityContainerShip.getCrane()) { + drawCrane( entityContainerShip.getAdditionalColor(), g2d); + + } + if (entityContainerShip.getContainer()) { + drawContainer( entityContainerShip.getAdditionalColor(), g2d); + } + } + + private void drawCrane(Color additionalColor, Graphics g2d) + { + g2d.setColor(additionalColor); + Point[] crane = new Point[] + { + new Point(_startPosX + 75, _startPosY + 50), + new Point(_startPosX + 85, _startPosY+ 50), + new Point(_startPosX + 85, _startPosY + 20), + new Point(_startPosX + 105, _startPosY + 20), + new Point(_startPosX + 105, _startPosY + 15), + new Point(_startPosX + 75, _startPosY + 15), + + }; + Polygon cranePolygon = new Polygon(); + for (Point point: crane) { + cranePolygon.addPoint(point.x, point.y); + } + g2d.fillPolygon(cranePolygon); + g2d.drawPolygon(cranePolygon); + } + + private void drawContainer(Color additionalColor, Graphics g2d) + { + g2d.setColor(Color.BLACK); + + Point[] container1 = new Point[] { + new Point(_startPosX + 90, _startPosY + 50), + new Point(_startPosX + 120, _startPosY + 50), + new Point(_startPosX + 120, _startPosY + 40), + new Point(_startPosX + 90, _startPosY + 40) + }; + Polygon container1Polygon = new Polygon(); + for(Point point: container1) { + container1Polygon.addPoint(point.x, point.y); + } + + Point[] container2 = new Point[] { + new Point(_startPosX + 90, _startPosY + 40), + new Point(_startPosX + 90, _startPosY + 30), + new Point(_startPosX + 120, _startPosY + 30), + new Point(_startPosX + 120, _startPosY + 40) + }; + Polygon container2Polygon = new Polygon(); + for(Point point: container1) { + container2Polygon.addPoint(point.x, point.y); + } + + g2d.drawPolygon(container1Polygon); + g2d.drawPolygon(container2Polygon); + g2d.setColor(entityContainerShip.getAdditionalColor()); + g2d.fillPolygon(container1Polygon); + g2d.fillPolygon(container2Polygon); + } + +} diff --git a/ProjectContainerShip/src/DrawningContainerShipDeck.java b/ProjectContainerShip/src/Drawings/DrawningContainerShipDeck.java similarity index 82% rename from ProjectContainerShip/src/DrawningContainerShipDeck.java rename to ProjectContainerShip/src/Drawings/DrawningContainerShipDeck.java index ced31af..7268026 100644 --- a/ProjectContainerShip/src/DrawningContainerShipDeck.java +++ b/ProjectContainerShip/src/Drawings/DrawningContainerShipDeck.java @@ -1,7 +1,7 @@ -package ProjectContainerShip.src; +package ProjectContainerShip.src.Drawings; import java.awt.*; -public class DrawningContainerShipDeck { +public class DrawningContainerShipDeck implements IDrawningDeck { private DeckCount _deckCount; public void setEnumNumber(int deckCount) { @@ -13,7 +13,7 @@ public class DrawningContainerShipDeck { } } - public void drawContainerShipDeck(Graphics g, Color color, float startPosX, float startPosY) + public void drawContainerShipDeck(Graphics g, Color color, int startPosX, int startPosY) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(color); diff --git a/ProjectContainerShip/src/Drawings/DrawningContainerShipDeckFull.java b/ProjectContainerShip/src/Drawings/DrawningContainerShipDeckFull.java new file mode 100644 index 0000000..46bd8f7 --- /dev/null +++ b/ProjectContainerShip/src/Drawings/DrawningContainerShipDeckFull.java @@ -0,0 +1,32 @@ +package ProjectContainerShip.src.Drawings; +import java.awt.*; +public class DrawningContainerShipDeckFull implements IDrawningDeck { + private DeckCount _deckCount; + public void setEnumNumber(int wheelsCount){ + for (DeckCount value : DeckCount.values()){ + if (value.getEnumNumber() == wheelsCount){ + _deckCount = value; + return; + } + } + } + public void drawContainerShipDeck(Graphics g, Color color, int startPosX, int startPosY) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(color); + g2d.setStroke(new BasicStroke(4)); + for (int i = 0; i < _deckCount.getEnumNumber(); i++) { + drawDeck(g2d, (int) startPosX, (int)startPosY); + startPosY-=10; + } + } + private void drawDeck(Graphics2D g2d, int posX, int posY) { + g2d.drawLine(posX + 20, posY + 50, posX + 50, posY + 50); + g2d.drawLine(posX + 50, posY + 50, posX + 55, posY + 45); + g2d.drawLine(posX + 55, posY + 45, posX + 50, posY + 40); + g2d.drawLine(posX + 50, posY + 40, posX + 20, posY + 40); + g2d.drawLine(posX + 20, posY + 40, posX + 15, posY + 45); + g2d.drawLine(posX + 15, posY + 45, posX + 20, posY + 50); + + } +} diff --git a/ProjectContainerShip/src/Drawings/DrawningContainerShipDeckTrapez.java b/ProjectContainerShip/src/Drawings/DrawningContainerShipDeckTrapez.java new file mode 100644 index 0000000..2faa8f8 --- /dev/null +++ b/ProjectContainerShip/src/Drawings/DrawningContainerShipDeckTrapez.java @@ -0,0 +1,30 @@ +package ProjectContainerShip.src.Drawings; +import java.awt.*; +public class DrawningContainerShipDeckTrapez implements IDrawningDeck { + private DeckCount _deckCount; + public void setEnumNumber(int wheelsCount){ + for (DeckCount value : DeckCount.values()){ + if (value.getEnumNumber() == wheelsCount){ + _deckCount = value; + return; + } + } + } + // поправить + private void drawDeck(Graphics2D g2d, int posX, int posY) { + g2d.drawLine(posX + 20, posY + 50, posX + 50, posY + 50); + g2d.drawLine(posX + 50, posY + 50, posX + 60, posY + 40); + g2d.drawLine(posX + 60, posY + 40, posX + 10, posY + 40); + g2d.drawLine(posX + 10, posY + 40, posX + 20, posY + 50); + } + public void drawContainerShipDeck(Graphics g, Color color, int startPosX, int startPosY) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(color); + g2d.setStroke(new BasicStroke(4)); + for (int i = 0; i < _deckCount.getEnumNumber(); i++) { + drawDeck(g2d, (int) startPosX, (int)startPosY); + startPosY-=10; + } + } +} diff --git a/ProjectContainerShip/src/Drawings/DrawningShip.java b/ProjectContainerShip/src/Drawings/DrawningShip.java new file mode 100644 index 0000000..8b98a56 --- /dev/null +++ b/ProjectContainerShip/src/Drawings/DrawningShip.java @@ -0,0 +1,144 @@ +package ProjectContainerShip.src.Drawings; + +import ProjectContainerShip.src.Entities.EntityShip; + +import java.awt.*; +import java.util.Random; + +public class DrawningShip { + private EntityShip entityShip; + public EntityShip getEntityShip() { + return entityShip; + } + private IDrawningDeck drawningDeck; + private Integer _pictureWidth; + private Integer _pictureHeight; + protected Integer _startPosX; + protected Integer _startPosY; + private int _drawningShipWidth = 130; + private int _drawningShipHeight = 60; + public int getPosX() { + return _startPosX; + } + public int getPosY() { + return _startPosY; + } + public int getTrainWidth() { + return _drawningShipWidth; + } + public int getTrainHeight() { + return _drawningShipHeight; + } + public DrawningShip(int speed, double weight, Color bodyColor, int deckType){ + entityShip = new EntityShip(speed, weight, bodyColor); + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + switch(deckType){ + case 0: + drawningDeck = new DrawningContainerShipDeck(); + break; + case 1: + drawningDeck = new DrawningContainerShipDeckFull(); + break; + case 2: + drawningDeck = new DrawningContainerShipDeckTrapez(); + break; + default: + break; + } + Random rand = new Random(); + int deckCount = rand.nextInt(1, 4); + drawningDeck.setEnumNumber(deckCount); + } + public DrawningShip(int speed, double weight, Color bodyColor, int deckType, int drawningShipWidth, int drawningShipHeight){ + this(speed, weight, bodyColor, deckType); + _drawningShipWidth = drawningShipWidth; + _drawningShipHeight = drawningShipHeight; + } + public boolean SetPictureSize(int width, int height){ + if (_drawningShipWidth <= width && _drawningShipHeight <= height){ + _pictureWidth = width; + _pictureHeight = height; + if (_startPosX != null && _startPosY != null){ + if (_startPosX + _drawningShipWidth > _pictureWidth) { + _startPosX = _pictureWidth - _drawningShipWidth; + } + if (_startPosY + _drawningShipHeight > _pictureHeight) { + _startPosY = _pictureHeight - _drawningShipHeight; + } + } + return true; + } + return false; + } + + public void SetPosition(int x, int y) + { + if (_pictureHeight == null || _pictureWidth == null) { + return; + } + if (x + _drawningShipWidth > _pictureWidth || x < 0) + { + x = 0; + } + if (y + _drawningShipHeight > _pictureHeight || y < 0) + { + y = 0; + } + _startPosX = x; + _startPosY = y; + } + + public boolean MoveTransport(DirectionType direction){ + if (entityShip == null || _startPosX == null || _startPosY == null){ + return false; + } + switch (direction){ + case DirectionType.Up: + if (_startPosY - entityShip.Step() > 0){ + _startPosY -= (int) entityShip.Step(); + } + return true; + case DirectionType.Down: + if (_startPosY + entityShip.Step() + _drawningShipHeight < _pictureHeight){ + _startPosY += (int) entityShip.Step(); + } + return true; + case DirectionType.Left: + if (_startPosX - entityShip.Step() > 0){ + _startPosX -= (int) entityShip.Step(); + } + return true; + case DirectionType.Right: + if (_startPosX + entityShip.Step() + _drawningShipWidth < _pictureWidth){ + _startPosX += (int) entityShip.Step(); + } + return true; + default: return false; + } + } + public void DrawShip(Graphics g) { + if (entityShip == null || _startPosX == null || _startPosY == null) { + return; + } + + Graphics2D g2d = (Graphics2D) g; + Point[] containerShipBorders = new Point[] { + new Point(_startPosX, _startPosY + 50), + new Point(_startPosX + 20, _startPosY + 70), + new Point(_startPosX + 110, _startPosY + 70), + new Point(_startPosX + 130, _startPosY + 50) + }; + Polygon containerShipPolygon = new Polygon(); + for(Point point : containerShipBorders) + containerShipPolygon.addPoint(point.x, point.y); + g2d.setColor(Color.BLACK); + g2d.draw(containerShipPolygon); + g2d.setColor(entityShip.getBodyColor()); + g2d.fill(containerShipPolygon); + drawningDeck.drawContainerShipDeck(g, entityShip.getBodyColor(), _startPosX, _startPosY); + } + +} diff --git a/ProjectContainerShip/src/Drawings/IDrawningDeck.java b/ProjectContainerShip/src/Drawings/IDrawningDeck.java new file mode 100644 index 0000000..a29c422 --- /dev/null +++ b/ProjectContainerShip/src/Drawings/IDrawningDeck.java @@ -0,0 +1,8 @@ +package ProjectContainerShip.src.Drawings; + +import java.awt.*; + +public interface IDrawningDeck { + void setEnumNumber(int x); + void drawContainerShipDeck(Graphics g, Color color, int startX, int startY); +} diff --git a/ProjectContainerShip/src/DrawningContainerShip.java b/ProjectContainerShip/src/DrawningContainerShip.java deleted file mode 100644 index 6a712f0..0000000 --- a/ProjectContainerShip/src/DrawningContainerShip.java +++ /dev/null @@ -1,188 +0,0 @@ -package ProjectContainerShip.src; - -import java.awt.*; -import java.util.Random; - -public class DrawningContainerShip { - private EntityContainerShip entityContainerShip; - public EntityContainerShip getEntityContainerShip() { - return entityContainerShip; - } - private Integer _pictureWidth; - private Integer _pictureHeight; - private Integer _startPosX; - private Integer _startPosY; - private final int _drawingContainerShipWidth = 130; - private final int _drawingContainerShipHeight = 60; - public DrawningContainerShipDeck _drawingContainerShipDeck; - - public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean crane, boolean container) { - entityContainerShip = new EntityContainerShip(); - entityContainerShip.Init(speed, weight, bodyColor, additionalColor, crane, container); - _startPosY = null; - _startPosX = null; - _pictureWidth = null; - _pictureHeight = null; - - _drawingContainerShipDeck = new DrawningContainerShipDeck(); - Random random = new Random(); - int deckCount = random.nextInt(1,4); - _drawingContainerShipDeck.setEnumNumber(deckCount); - - } - - - public void setPosition(int x, int y) { - if (_pictureHeight == null || _pictureWidth == null) - return; - _startPosX = x; - _startPosY = y; - - if (_drawingContainerShipWidth + x > _pictureWidth || x < 0) { - _startPosX = 0; - } - if (_drawingContainerShipHeight + y > _pictureHeight || y < 0) { - _startPosY = 0; - } - } - public boolean setPictureSize(int width, int height) { - - if (_drawingContainerShipHeight > height || _drawingContainerShipWidth > width) - return false; - _pictureHeight = height; - _pictureWidth = width; - - if (_startPosX != null && _startPosY != null) - { - if (_startPosX + _drawingContainerShipWidth > width) - _startPosX = width - _drawingContainerShipWidth; - if (_startPosY + _drawingContainerShipHeight > height) - _startPosY = height - _drawingContainerShipHeight; - } - - return true; - - - } - - public boolean moveTransport(DirectionType direction) { - if (entityContainerShip == null || _pictureWidth == null || _pictureHeight == null) - return false; - switch (direction) { - case Left: - if (_startPosX - entityContainerShip.Step() > 0) - _startPosX -= (int) entityContainerShip.Step(); - return true; - case Up: - if (_startPosY - entityContainerShip.Step() > 0) - _startPosY -= (int) entityContainerShip.Step(); - return true; - case Right: - if (_startPosX + entityContainerShip.Step() < _pictureWidth - _drawingContainerShipWidth) - _startPosX += (int) entityContainerShip.Step(); - return true; - case Down: - if (_startPosY + entityContainerShip.Step() < _pictureHeight - _drawingContainerShipHeight) - _startPosY += (int) entityContainerShip.Step(); - return true; - default: - return false; - - - } - } - - - // тут рисовашки - - private void drawCrane(Color additionalColor, Graphics g2d) - { - g2d.setColor(additionalColor); - Point[] crane = new Point[] - { - new Point(_startPosX + 75, _startPosY + 50), - new Point(_startPosX + 85, _startPosY+ 50), - new Point(_startPosX + 85, _startPosY + 20), - new Point(_startPosX + 105, _startPosY + 20), - new Point(_startPosX + 105, _startPosY + 15), - new Point(_startPosX + 75, _startPosY + 15), - - }; - Polygon cranePolygon = new Polygon(); - for (Point point: crane) { - cranePolygon.addPoint(point.x, point.y); - } - g2d.fillPolygon(cranePolygon); - g2d.drawPolygon(cranePolygon); - } - - private void drawContainer(Color additionalColor, Graphics g2d) - { - g2d.setColor(Color.BLACK); - - Point[] container1 = new Point[] { - new Point(_startPosX + 90, _startPosY + 50), - new Point(_startPosX + 120, _startPosY + 50), - new Point(_startPosX + 120, _startPosY + 40), - new Point(_startPosX + 90, _startPosY + 40) - }; - Polygon container1Polygon = new Polygon(); - for(Point point: container1) { - container1Polygon.addPoint(point.x, point.y); - } - - Point[] container2 = new Point[] { - new Point(_startPosX + 90, _startPosY + 40), - new Point(_startPosX + 90, _startPosY + 30), - new Point(_startPosX + 120, _startPosY + 30), - new Point(_startPosX + 120, _startPosY + 40) - }; - Polygon container2Polygon = new Polygon(); - for(Point point: container1) { - container2Polygon.addPoint(point.x, point.y); - } - - g2d.drawPolygon(container1Polygon); - g2d.drawPolygon(container2Polygon); - g2d.setColor(entityContainerShip.getAdditionalColor()); - g2d.fillPolygon(container1Polygon); - g2d.fillPolygon(container2Polygon); - } - - public void drawContainerShip(Graphics g) { - if (entityContainerShip == null || _startPosX == null || _startPosY == null) { - return; - } - - Graphics2D g2d = (Graphics2D) g; - - Point[] containerShipBorders = new Point[] { - new Point(_startPosX, _startPosY + 50), - new Point(_startPosX + 20, _startPosY + 70), - new Point(_startPosX + 110, _startPosY + 70), - new Point(_startPosX + 130, _startPosY + 50) - }; - Polygon containerShipPolygon = new Polygon(); - for(Point point : containerShipBorders) - containerShipPolygon.addPoint(point.x, point.y); - g2d.setColor(Color.BLACK); - g2d.draw(containerShipPolygon); - g2d.setColor(entityContainerShip.getBodyColor()); - g2d.fill(containerShipPolygon); - - if (entityContainerShip.getCrane()) { - drawCrane( entityContainerShip.getAdditionalColor(), g2d); - - } - if (entityContainerShip.getContainer()) { - drawContainer( entityContainerShip.getAdditionalColor(), g2d); - } - - _drawingContainerShipDeck.drawContainerShipDeck(g, entityContainerShip.getBodyColor(), _startPosX, _startPosY); - - - } - - - -} \ No newline at end of file diff --git a/ProjectContainerShip/src/Entities/EntityContainerShip.java b/ProjectContainerShip/src/Entities/EntityContainerShip.java new file mode 100644 index 0000000..aa90b42 --- /dev/null +++ b/ProjectContainerShip/src/Entities/EntityContainerShip.java @@ -0,0 +1,32 @@ +package ProjectContainerShip.src.Entities; + +import java.awt.*; +public class EntityContainerShip extends EntityShip { + + // доп цвет + private Color AdditionalColor; + + public Color getAdditionalColor() { + return AdditionalColor; + } + + // доп опция кран + private boolean Crane; + public boolean getCrane() { + return Crane; + } + + // доп опция контейнер + private boolean Container; + public boolean getContainer() { + return Container; + } + + // инициализация полей обьекта + public EntityContainerShip(int speed, double weight, Color bodyColor, Color additionalColor, boolean crane, boolean container) { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + Crane = crane; + Container = container; + } +} diff --git a/ProjectContainerShip/src/Entities/EntityShip.java b/ProjectContainerShip/src/Entities/EntityShip.java new file mode 100644 index 0000000..3981c58 --- /dev/null +++ b/ProjectContainerShip/src/Entities/EntityShip.java @@ -0,0 +1,29 @@ +package ProjectContainerShip.src.Entities; + +import java.awt.*; +public class EntityShip { + private int Speed; + public int getSpeed() { + return Speed; + } + + private double Weight; + public double getWeight() { + return Weight; + } + + private Color BodyColor; + public Color getBodyColor() { + return BodyColor; + } + + public double Step() { + return Speed*100/Weight; + } + + public EntityShip(int speed, double weight, Color bodyColor) { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectContainerShip/src/EntityContainerShip.java b/ProjectContainerShip/src/EntityContainerShip.java deleted file mode 100644 index ba315a3..0000000 --- a/ProjectContainerShip/src/EntityContainerShip.java +++ /dev/null @@ -1,55 +0,0 @@ -package ProjectContainerShip.src; -import java.awt.*; -public class EntityContainerShip { - - // скорость - private int Speed; - public int getSpeed() { - return Speed; - } - - // вес - private double Weight; - public double getWeight() { - return Weight; - } - - // основной цвет - private Color BodyColor; - public Color getBodyColor() { - return BodyColor; - } - - // доп цвет - private Color AdditionalColor; - public Color getAdditionalColor() { - return AdditionalColor; - } - - // шаг - public double Step() { - return Speed*100/Weight; - } - - // доп опция кран - private boolean Crane; - public boolean getCrane() { - return Crane; - } - - // доп опция контейнер - private boolean Container; - public boolean getContainer() { - return Container; - } - - // инициализация полей обьекта - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean crane, boolean container) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Crane = crane; - Container = container; - } -} diff --git a/ProjectContainerShip/src/FormContainerShip.form b/ProjectContainerShip/src/FormContainerShip.form index 9eeb0b1..6cb73a2 100644 --- a/ProjectContainerShip/src/FormContainerShip.form +++ b/ProjectContainerShip/src/FormContainerShip.form @@ -3,12 +3,12 @@ - + - + @@ -16,22 +16,9 @@ - - - - - - - - - - - - - - + @@ -44,12 +31,12 @@ - + - + @@ -62,7 +49,7 @@ - + @@ -75,7 +62,7 @@ - + @@ -86,6 +73,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ProjectContainerShip/src/FormContainerShip.java b/ProjectContainerShip/src/FormContainerShip.java index 2d7b289..2a4f49a 100644 --- a/ProjectContainerShip/src/FormContainerShip.java +++ b/ProjectContainerShip/src/FormContainerShip.java @@ -1,5 +1,8 @@ package ProjectContainerShip.src; +import ProjectContainerShip.src.Drawings.*; +import ProjectContainerShip.src.MovementStrategy.*; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -9,16 +12,44 @@ import java.util.Random; import java.util.List; public class FormContainerShip extends JFrame { - protected DrawningContainerShip _drawningContainerShip = new DrawningContainerShip(); + protected DrawningShip _drawningShip; + private AbstractStrategy _strategy; JPanel PanelWrapper; private JPanel PictureBox; - private JButton buttonCreate; + private JButton buttonCreateContainerShip; + private JButton buttonCreateShip; private JButton buttonRight; private JButton buttonDown; private JButton buttonLeft; private JButton buttonUp; + private JButton buttonStrategyStep; + private JComboBox comboBoxStrategy; private List controls; + + public void createObject(String obj){ + Random rand = new Random(); + switch(obj){ + case "Ship": + _drawningShip = new DrawningShip(rand.nextInt(100, 300), rand.nextDouble(1000, 3000), + new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), rand.nextInt(0, 3)); + break; + case "ContainerShip": + _drawningShip = new DrawningContainerShip(rand.nextInt(100, 300), rand.nextDouble(1000, 3000), + new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), rand.nextInt(0, 3), + new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), + rand.nextBoolean(), rand.nextBoolean()); + break; + default: + return; + } + _drawningShip.SetPictureSize(PictureBox.getWidth(), PictureBox.getHeight()); + _drawningShip.SetPosition(rand.nextInt(50), rand.nextInt(50)); + _strategy = null; + comboBoxStrategy.setEnabled(true); + Draw(); + } + public FormContainerShip() { buttonUp.setName("buttonUp"); buttonDown.setName("buttonDown"); @@ -27,75 +58,89 @@ public class FormContainerShip extends JFrame { InitializeControlsRepaintList(); - buttonCreate.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - _drawningContainerShip = new DrawningContainerShip(); - Random random = new Random(); + buttonCreateContainerShip.addActionListener(e -> createObject("ContainerShip")); - _drawningContainerShip.Init(random.nextInt(30, 100), - random.nextInt(100, 500), - 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() ); - _drawningContainerShip.setPictureSize(PictureBox.getWidth(), PictureBox.getHeight()); - _drawningContainerShip.setPosition(random.nextInt(25, 100), - random.nextInt(25, 100)); + buttonCreateShip.addActionListener(e -> createObject("Ship")); + ActionListener buttonMoveClickedListener = e -> { + String buttonName = ((JButton) e.getSource()).getName(); + boolean result = false; + switch (buttonName) { + case "buttonUp": { + result = _drawningShip.MoveTransport(DirectionType.Up); + } + break; + case "buttonDown": { + result = _drawningShip.MoveTransport(DirectionType.Down); + } + break; + case "buttonLeft": { + result = _drawningShip.MoveTransport(DirectionType.Left); + } + break; + case "buttonRight": { + result = _drawningShip.MoveTransport(DirectionType.Right); + } + break; + + } + if (result) Draw(); - } - }); - ActionListener buttonMoveClickedListener = new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - String buttonName = ((JButton) e.getSource()).getName(); - boolean result = false; - - switch (buttonName) { - case "buttonUp": { - result = _drawningContainerShip.moveTransport(DirectionType.Up); - } - break; - case "buttonDown": { - result = _drawningContainerShip.moveTransport(DirectionType.Down); - } - break; - case "buttonLeft": { - result = _drawningContainerShip.moveTransport(DirectionType.Left); - } - break; - case "buttonRight": { - result = _drawningContainerShip.moveTransport(DirectionType.Right); - } - break; - - } - if (result) - Draw(); - - } }; buttonRight.addActionListener(buttonMoveClickedListener); buttonDown.addActionListener(buttonMoveClickedListener); buttonLeft.addActionListener(buttonMoveClickedListener); buttonUp.addActionListener(buttonMoveClickedListener); + + comboBoxStrategy.addItem("К Центру"); + comboBoxStrategy.addItem("К Краю"); + buttonStrategyStep.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_drawningShip == null){return;} + if (comboBoxStrategy.isEnabled()){ + switch (comboBoxStrategy.getSelectedIndex()){ + case 0: + _strategy = new MoveToCenter(); + break; + case 1: + _strategy = new MoveToBorder(); + break; + default: + _strategy = null; + break; + } + if (_strategy == null) {return;} + _strategy.SetData(new MovableShip(_drawningShip), PictureBox.getWidth(), PictureBox.getHeight()); + } + if (_strategy == null) {return;} + _strategy.MakeStep(); + Draw(); + comboBoxStrategy.setEnabled(false); + if (_strategy.GetStatus() == StrategyStatus.Finish) { + comboBoxStrategy.setEnabled(true); + _strategy = null; + } + } + }); } private void Draw() { - if (_drawningContainerShip.getEntityContainerShip() == null) + if (_drawningShip.getEntityShip() == null) return; if (PictureBox.getWidth() == 0 || PictureBox.getHeight() == 0) { return; } Graphics g = PictureBox.getGraphics(); g.setColor(PictureBox.getBackground()); - g.fillRect(0,0, PictureBox.getWidth(), PictureBox.getHeight()); - _drawningContainerShip.drawContainerShip(g); + g.fillRect(0, 0, PictureBox.getWidth(), PictureBox.getHeight()); + _drawningShip.DrawShip(g); RepaintControls(); } + private void RepaintControls() { for (JComponent control : controls) { control.repaint(); @@ -105,12 +150,11 @@ public class FormContainerShip extends JFrame { private void InitializeControlsRepaintList() { controls = new LinkedList<>(); - controls.add(buttonCreate); + controls.add(buttonCreateContainerShip); + controls.add(buttonCreateShip); controls.add(buttonUp); controls.add(buttonDown); controls.add(buttonLeft); controls.add(buttonRight); } - - } diff --git a/ProjectContainerShip/src/MovementStrategy/AbstractStrategy.java b/ProjectContainerShip/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..1f1cf7b --- /dev/null +++ b/ProjectContainerShip/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,60 @@ +package ProjectContainerShip.src.MovementStrategy; + +import ProjectContainerShip.src.Drawings.DirectionType; +public abstract class AbstractStrategy { + private IMoveableObject _movableObject; + private StrategyStatus _state = StrategyStatus.NotInit; + protected int FieldWidth; + protected int FieldHeight; + public StrategyStatus GetStatus() { return _state; } + + public void SetData(IMoveableObject movableObject, int width, int height) { + if (movableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _movableObject = movableObject; + FieldHeight = height; + FieldWidth = width; + } + + public void MakeStep() { + if (_state != StrategyStatus.InProgress) { + return; + } + if (IsTargetDestination()) { + _state = StrategyStatus.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() { return _movableObject.GetObjectPosition(); } + + protected int GetStep() { + if(_state != StrategyStatus.InProgress) { + return 0; + } + return _movableObject.GetStep(); + } + + protected abstract void MoveToTarget(); + protected abstract boolean IsTargetDestination(); + private boolean MoveTo(DirectionType directionType) { + if (_state != StrategyStatus.InProgress) { + return false; + } + if (_movableObject.TryMoveObject(directionType)) { + _movableObject.MoveObject(directionType); + return true; + } + return false; + } +} diff --git a/ProjectContainerShip/src/MovementStrategy/IMoveableObject.java b/ProjectContainerShip/src/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..0b6d705 --- /dev/null +++ b/ProjectContainerShip/src/MovementStrategy/IMoveableObject.java @@ -0,0 +1,9 @@ +package ProjectContainerShip.src.MovementStrategy; + +import ProjectContainerShip.src.Drawings.DirectionType; +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean TryMoveObject(DirectionType direction); + void MoveObject(DirectionType direction); +} diff --git a/ProjectContainerShip/src/MovementStrategy/MovableShip.java b/ProjectContainerShip/src/MovementStrategy/MovableShip.java new file mode 100644 index 0000000..f132907 --- /dev/null +++ b/ProjectContainerShip/src/MovementStrategy/MovableShip.java @@ -0,0 +1,30 @@ +package ProjectContainerShip.src.MovementStrategy; + +import ProjectContainerShip.src.Drawings.DirectionType; +import ProjectContainerShip.src.Drawings.DrawningShip; + +public class MovableShip implements IMoveableObject { + private DrawningShip _ship = null; + public MovableShip(DrawningShip train){ + _ship = train; + } + + public ObjectParameters GetObjectPosition() { + if (_ship == null || _ship.getEntityShip() == null){ + return null; + } + return new ObjectParameters(_ship.getPosX(), _ship.getPosY(), _ship.getTrainWidth(), _ship.getTrainHeight()); + } + + public int GetStep() { + return (int) _ship.getEntityShip().Step(); + } + + public boolean TryMoveObject(DirectionType direction) { + return _ship.MoveTransport(direction); + } + + public void MoveObject(DirectionType direction) { + _ship.MoveTransport(direction); + } +} diff --git a/ProjectContainerShip/src/MovementStrategy/MoveToBorder.java b/ProjectContainerShip/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..efc7da9 --- /dev/null +++ b/ProjectContainerShip/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,27 @@ +package ProjectContainerShip.src.MovementStrategy; + +public class MoveToBorder extends AbstractStrategy { + protected boolean IsTargetDestination() { + var objParams = GetObjectParameters(); + if (objParams == null){ + return false; + } + return objParams.RightBorder() <= FieldWidth && objParams.RightBorder() + GetStep() >= FieldWidth && + objParams.DownBorder() <= FieldHeight && objParams.DownBorder() + GetStep() >= FieldHeight; + } + + protected void MoveToTarget(){ + var objParams = GetObjectParameters(); + if (objParams == null){ + return; + } + int diffX = objParams.RightBorder() - FieldWidth; + if (Math.abs(diffX) > GetStep()){ + MoveRight(); + } + int diffY = objParams.DownBorder() - FieldHeight; + if (Math.abs(diffY) > GetStep()){ + MoveDown(); + } + } +} diff --git a/ProjectContainerShip/src/MovementStrategy/MoveToCenter.java b/ProjectContainerShip/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..a730ca9 --- /dev/null +++ b/ProjectContainerShip/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,39 @@ +package ProjectContainerShip.src.MovementStrategy; + +public class MoveToCenter extends AbstractStrategy { + protected boolean IsTargetDestination(){ + var objParams = GetObjectParameters(); + if (objParams == null){ + return false; + } + return objParams.ObjectMidHorizontal() - GetStep() <= FieldWidth / 2 && + objParams.ObjectMidHorizontal() + GetStep() >= FieldWidth / 2 && + objParams.ObjectMidVertical() - GetStep() <= FieldHeight / 2 && + objParams.ObjectMidVertical() + GetStep() >= FieldHeight / 2; + } + + protected void MoveToTarget(){ + var objParams = GetObjectParameters(); + if (objParams == null){ + return; + } + int diffX = objParams.ObjectMidHorizontal() - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()){ + if (diffX > 0){ + MoveLeft(); + } + else{ + MoveRight(); + } + } + int diffY = objParams.ObjectMidVertical() - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()){ + if (diffY > 0){ + MoveUp(); + } + else{ + MoveDown(); + } + } + } +} diff --git a/ProjectContainerShip/src/MovementStrategy/MovementDirection.java b/ProjectContainerShip/src/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..367f818 --- /dev/null +++ b/ProjectContainerShip/src/MovementStrategy/MovementDirection.java @@ -0,0 +1,8 @@ +package ProjectContainerShip.src.MovementStrategy; + +public enum MovementDirection { + Up, + Down, + Left, + Right +} diff --git a/ProjectContainerShip/src/MovementStrategy/ObjectParameters.java b/ProjectContainerShip/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..4c45c8e --- /dev/null +++ b/ProjectContainerShip/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,23 @@ +package ProjectContainerShip.src.MovementStrategy; + +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private 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 ObjectMidHorizontal(){return _x + _width / 2;}; + public int ObjectMidVertical(){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/ProjectContainerShip/src/MovementStrategy/StrategyStatus.java b/ProjectContainerShip/src/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..de0f114 --- /dev/null +++ b/ProjectContainerShip/src/MovementStrategy/StrategyStatus.java @@ -0,0 +1,7 @@ +package ProjectContainerShip.src.MovementStrategy; + +public enum StrategyStatus { + NotInit, + InProgress, + Finish +}