diff --git a/WarmlyShip/.idea/.name b/WarmlyShip/.idea/.name new file mode 100644 index 0000000..002da1d --- /dev/null +++ b/WarmlyShip/.idea/.name @@ -0,0 +1 @@ +Main.java \ No newline at end of file diff --git a/WarmlyShip/src/DrawingDecks.java b/WarmlyShip/src/DiffetentsDrawingDecks/DrawingDecksType1.java similarity index 72% rename from WarmlyShip/src/DrawingDecks.java rename to WarmlyShip/src/DiffetentsDrawingDecks/DrawingDecksType1.java index f7afbdc..14d4f7b 100644 --- a/WarmlyShip/src/DrawingDecks.java +++ b/WarmlyShip/src/DiffetentsDrawingDecks/DrawingDecksType1.java @@ -1,7 +1,10 @@ +package DiffetentsDrawingDecks; + import java.awt.*; -public class DrawingDecks { +public class DrawingDecksType1 implements IDifferentDecks { private NumberOfDecks numberOfDecks; + @Override public void setNumberOfDecks(int numberofdeck) { for (NumberOfDecks numofenum : NumberOfDecks.values()) { if (numofenum.getNumdecks() == numberofdeck) { @@ -10,10 +13,12 @@ public class DrawingDecks { } } } + @Override public NumberOfDecks getNumberOfDecks() { return numberOfDecks; } - public void DrawDecks(Graphics g, int x, int y, int width, int height, Color bodyColor) { + @Override + public void DrawDecks(Graphics2D g, int x, int y, int width, int height, Color bodyColor) { g.setColor(bodyColor); g.fillRect(x, y, width, height); g.setColor(Color.BLACK); diff --git a/WarmlyShip/src/DiffetentsDrawingDecks/DrawingDecksType2.java b/WarmlyShip/src/DiffetentsDrawingDecks/DrawingDecksType2.java new file mode 100644 index 0000000..d5e1e36 --- /dev/null +++ b/WarmlyShip/src/DiffetentsDrawingDecks/DrawingDecksType2.java @@ -0,0 +1,34 @@ +package DiffetentsDrawingDecks; + +import java.awt.*; + +public class DrawingDecksType2 implements IDifferentDecks { + private NumberOfDecks numberOfDecks; + @Override + public void setNumberOfDecks(int numberofdeck) { + for (NumberOfDecks numofenum : NumberOfDecks.values()) { + if (numofenum.getNumdecks() == numberofdeck) { + numberOfDecks = numofenum; + return; + } + } + } + @Override + public NumberOfDecks getNumberOfDecks() { + return numberOfDecks; + } + @Override + public void DrawDecks(Graphics2D g, int x, int y, int width, int height, Color bodyColor) { + g.setStroke(new BasicStroke(3.0F)); + g.setColor(bodyColor); + //верхняя часть полубы + g.drawLine(x, y, x + width, y); + //опорные балки + int countBalks = 4; + int wayBetweenBalks = width / countBalks; + for (int i = 0; i <= countBalks; i++) { + int nowX = x + wayBetweenBalks * i; + g.drawLine(nowX, y, nowX, y + height); + } + } +} diff --git a/WarmlyShip/src/DiffetentsDrawingDecks/DrawingDecksType3.java b/WarmlyShip/src/DiffetentsDrawingDecks/DrawingDecksType3.java new file mode 100644 index 0000000..89c15e0 --- /dev/null +++ b/WarmlyShip/src/DiffetentsDrawingDecks/DrawingDecksType3.java @@ -0,0 +1,28 @@ +package DiffetentsDrawingDecks; + +import java.awt.*; + +public class DrawingDecksType3 implements IDifferentDecks{ + private NumberOfDecks numberOfDecks; + @Override + public void setNumberOfDecks(int numberofdeck) { + for (NumberOfDecks numofenum : NumberOfDecks.values()) { + if (numofenum.getNumdecks() == numberofdeck) { + numberOfDecks = numofenum; + return; + } + } + } + @Override + public NumberOfDecks getNumberOfDecks() { + return numberOfDecks; + } + @Override + public void DrawDecks(Graphics2D g, int x, int y, int width, int height, Color bodyColor) { + g.setColor(bodyColor); + g.fillRect(x, y, width, height); + g.setColor(Color.BLACK); + g.fillRect(x+ 10, y+5, width - 20, height-5); + g.setColor(bodyColor); + } +} diff --git a/WarmlyShip/src/DiffetentsDrawingDecks/IDifferentDecks.java b/WarmlyShip/src/DiffetentsDrawingDecks/IDifferentDecks.java new file mode 100644 index 0000000..12fe36c --- /dev/null +++ b/WarmlyShip/src/DiffetentsDrawingDecks/IDifferentDecks.java @@ -0,0 +1,9 @@ +package DiffetentsDrawingDecks; + +import java.awt.*; + +public interface IDifferentDecks { + void setNumberOfDecks(int numberofdeck); + NumberOfDecks getNumberOfDecks(); + void DrawDecks(Graphics2D g, int x, int y, int width, int height, Color bodyColor); +} diff --git a/WarmlyShip/src/NumberOfDecks.java b/WarmlyShip/src/DiffetentsDrawingDecks/NumberOfDecks.java similarity index 88% rename from WarmlyShip/src/NumberOfDecks.java rename to WarmlyShip/src/DiffetentsDrawingDecks/NumberOfDecks.java index d1526e0..01d3246 100644 --- a/WarmlyShip/src/NumberOfDecks.java +++ b/WarmlyShip/src/DiffetentsDrawingDecks/NumberOfDecks.java @@ -1,3 +1,5 @@ +package DiffetentsDrawingDecks; + public enum NumberOfDecks { OneDeck(1), TwoDecks(2), diff --git a/WarmlyShip/src/CanvasWarmlyShip.java b/WarmlyShip/src/DrawingShip/CanvasWarmlyShip.java similarity index 69% rename from WarmlyShip/src/CanvasWarmlyShip.java rename to WarmlyShip/src/DrawingShip/CanvasWarmlyShip.java index 2e1b6a2..11725eb 100644 --- a/WarmlyShip/src/CanvasWarmlyShip.java +++ b/WarmlyShip/src/DrawingShip/CanvasWarmlyShip.java @@ -1,16 +1,18 @@ +package DrawingShip; + import javax.swing.*; import java.awt.*; public class CanvasWarmlyShip extends JComponent { - public DrawingWarmlyShip _drawingWarmlyShip; + public DrawingShip _drawingShip; public CanvasWarmlyShip(){} 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(); } } diff --git a/WarmlyShip/src/DirectionType.java b/WarmlyShip/src/DrawingShip/DirectionType.java similarity index 66% rename from WarmlyShip/src/DirectionType.java rename to WarmlyShip/src/DrawingShip/DirectionType.java index 35657f0..055aabb 100644 --- a/WarmlyShip/src/DirectionType.java +++ b/WarmlyShip/src/DrawingShip/DirectionType.java @@ -1,4 +1,7 @@ +package DrawingShip; + public enum DirectionType { + Unknow, Up, Down, Left, diff --git a/WarmlyShip/src/DrawingWarmlyShip.java b/WarmlyShip/src/DrawingShip/DrawingShip.java similarity index 55% rename from WarmlyShip/src/DrawingWarmlyShip.java rename to WarmlyShip/src/DrawingShip/DrawingShip.java index 2a3a221..b52faa1 100644 --- a/WarmlyShip/src/DrawingWarmlyShip.java +++ b/WarmlyShip/src/DrawingShip/DrawingShip.java @@ -1,24 +1,55 @@ +package DrawingShip; +import DiffetentsDrawingDecks.DrawingDecksType2; +import DiffetentsDrawingDecks.DrawingDecksType3; +import DiffetentsDrawingDecks.IDifferentDecks; +import Entities.EntityShip; + +import DiffetentsDrawingDecks.DrawingDecksType1; + import javax.swing.*; import java.awt.*; -public class DrawingWarmlyShip extends JPanel { - public EntityWarmlyShip EntityWarmlyShip; - public DrawingDecks drawingDecks = null; +public class DrawingShip extends JPanel { + public Entities.EntityShip EntityShip; + public IDifferentDecks drawingDecks; private Integer picture_width; private Integer picture_height; - private Integer _StartPosX; - private Integer _StartPosY; + public Integer _StartPosX; + public Integer _StartPosY; private int drawingShipWidth = 150; - private int drawingShipHeight = 140; - public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean sheeppipes, boolean fueltank) { - EntityWarmlyShip = new EntityWarmlyShip(); - EntityWarmlyShip.Init(speed, weight, bodycolor, additionalcolor, sheeppipes, fueltank); + protected int drawingShipHeight = 50; + public Integer GetPosX() {return _StartPosX;} + public Integer GetPosY() {return _StartPosY;} + public Integer GetWidth() {return drawingShipWidth;} + public Integer GetHeight() {return drawingShipHeight;} + protected DrawingShip() { picture_width = null; picture_height = null; _StartPosX = null; _StartPosY = null; - drawingDecks = new DrawingDecks(); - drawingDecks.setNumberOfDecks((int)(Math.random() * 4 + 0)); + } + protected void SetAmountandTypeDecks() { + int numberOfDecks = (int)(Math.random() * 4 + 0); + switch ((int)(Math.random() * 3 + 1)) { + case 1: + drawingDecks = new DrawingDecksType1(); + break; + case 2: + drawingDecks = new DrawingDecksType2(); + break; + case 3: + drawingDecks = new DrawingDecksType3(); + break; + default: + numberOfDecks = 0; + break; + } + drawingDecks.setNumberOfDecks(numberOfDecks); + } + public DrawingShip(int speed, double weight, Color bodycolor) { + super(); + EntityShip = new EntityShip(speed, weight, bodycolor); + SetAmountandTypeDecks(); } public boolean SetPictureSize(int width, int height) { if (width < drawingShipWidth || height < drawingShipHeight) return false; @@ -54,29 +85,29 @@ public class DrawingWarmlyShip extends JPanel { else _StartPosY = y; } public boolean MoveTransport(DirectionType direction) { - if (EntityWarmlyShip == null || _StartPosX == null || _StartPosY == null) return false; + if (EntityShip == null || _StartPosX == null || _StartPosY == null) return false; switch (direction) { - case Left: - if (_StartPosX - EntityWarmlyShip.Step > 0) { - _StartPosX -= (int)EntityWarmlyShip.Step; + case DirectionType.Left: + if (_StartPosX - EntityShip.Step > 0) { + _StartPosX -= (int)EntityShip.Step; } return true; - case Up: - if (_StartPosY - EntityWarmlyShip.Step > 0) + case DirectionType.Up: + if (_StartPosY - EntityShip.Step > 0) { - _StartPosY -= (int)EntityWarmlyShip.Step; + _StartPosY -= (int)EntityShip.Step; } return true; - case Right: - if (_StartPosX + drawingShipWidth + (int)EntityWarmlyShip.Step < picture_width - EntityWarmlyShip.Step) + case DirectionType.Right: + if (_StartPosX + drawingShipWidth + (int)EntityShip.Step < picture_width - EntityShip.Step) { - _StartPosX += (int)EntityWarmlyShip.Step; + _StartPosX += (int)EntityShip.Step; } return true; - case Down: - if (_StartPosY + drawingShipHeight + (int)EntityWarmlyShip.Step < picture_height - EntityWarmlyShip.Step) + case DirectionType.Down: + if (_StartPosY + drawingShipHeight + (int)EntityShip.Step < picture_height - EntityShip.Step) { - _StartPosY += (int)EntityWarmlyShip.Step; + _StartPosY += (int)EntityShip.Step; } return true; default: @@ -84,31 +115,24 @@ public class DrawingWarmlyShip extends JPanel { } } public void DrawTransport(Graphics2D g) { - if (EntityWarmlyShip == null || _StartPosX == null || _StartPosY == null) return; + if (EntityShip == null || _StartPosX == null || _StartPosY == null) return; int y = _StartPosY; - if (EntityWarmlyShip.getShipPipes()) { - //трубы - g.setColor(EntityWarmlyShip.getAdditionalColor()); - g.fillRect(_StartPosX + 70, _StartPosY, 12, 30); - g.fillRect(_StartPosX + 90, _StartPosY, 12, 30); - y += 30; - } - g.setColor(EntityWarmlyShip.getBodyColor()); + g.setColor(EntityShip.getBodyColor()); if (drawingDecks.getNumberOfDecks() != null) { switch (drawingDecks.getNumberOfDecks()) { case OneDeck: - drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityWarmlyShip.getBodyColor()); + drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityShip.getBodyColor()); y += 15; break; case TwoDecks: - drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityWarmlyShip.getBodyColor()); - drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityWarmlyShip.getBodyColor()); + drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityShip.getBodyColor()); + drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityShip.getBodyColor()); y += 30; break; case ThreeDecks: - drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityWarmlyShip.getBodyColor()); - drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityWarmlyShip.getBodyColor()); - drawingDecks.DrawDecks(g, _StartPosX + 30, y + 30, 100, 15, EntityWarmlyShip.getBodyColor()); + drawingDecks.DrawDecks(g, _StartPosX + 30, y, 100, 15, EntityShip.getBodyColor()); + drawingDecks.DrawDecks(g, _StartPosX + 30, y + 15, 100, 15, EntityShip.getBodyColor()); + drawingDecks.DrawDecks(g, _StartPosX + 30, y + 30, 100, 15, EntityShip.getBodyColor()); y += 45; break; } @@ -117,11 +141,6 @@ public class DrawingWarmlyShip extends JPanel { int[] arrayY = {y, y, y, y + 50, y + 50, y + 50, y + 50, y}; Polygon poly = new Polygon(arrayX, arrayY, 8); g.fillPolygon(poly); - g.setColor(EntityWarmlyShip.getAdditionalColor()); - if (EntityWarmlyShip.getFuelTank()) { - g.setColor(EntityWarmlyShip.getAdditionalColor()); - g.fillRect(_StartPosX + 40, y + 30, 70, 10); - } drawingShipHeight = y + 50 - _StartPosY; } -} +} \ No newline at end of file diff --git a/WarmlyShip/src/DrawingShip/DrawingWarmlyShip.java b/WarmlyShip/src/DrawingShip/DrawingWarmlyShip.java new file mode 100644 index 0000000..a3d7ec7 --- /dev/null +++ b/WarmlyShip/src/DrawingShip/DrawingWarmlyShip.java @@ -0,0 +1,47 @@ +package DrawingShip; +import Entities.EntityWarmlyShip; + +import java.awt.*; + +public class DrawingWarmlyShip extends DrawingShip { + public DrawingWarmlyShip(int speed, double weight, Color bodycolor, Color additionalcolor, boolean sheeppipes, boolean fueltank) { + EntityShip = new EntityWarmlyShip(speed, weight, bodycolor, additionalcolor, sheeppipes, fueltank); + SetAmountandTypeDecks(); + } + @Override + public void DrawTransport(Graphics2D g) { + if (EntityShip == null || !(EntityShip instanceof EntityWarmlyShip warmlyship) || _StartPosX == null || _StartPosY == null) + return; + int y = _StartPosY; + if (warmlyship.ShipPipes) { + //трубы + g.setColor(warmlyship.getAdditionalColor()); + g.fillRect(_StartPosX + 70, _StartPosY, 12, 30); + g.fillRect(_StartPosX + 90, _StartPosY, 12, 30); + _StartPosY += 30; + } + super.DrawTransport(g); + int count_decks = 0; + if (drawingDecks.getNumberOfDecks() != null) { + switch (drawingDecks.getNumberOfDecks()) { + case OneDeck: + count_decks = 1; + break; + case TwoDecks: + count_decks = 2; + break; + case ThreeDecks: + count_decks = 3; + break; + } + } + if (warmlyship.FuelTank) { + g.setColor(warmlyship.getAdditionalColor()); + g.fillRect(_StartPosX + 40, _StartPosY + count_decks * 15 + 30, 70, 10); + } + if (warmlyship.ShipPipes) { + _StartPosY -= 30; + drawingShipHeight += 30; + } + } +} diff --git a/WarmlyShip/src/Entities/EntityShip.java b/WarmlyShip/src/Entities/EntityShip.java new file mode 100644 index 0000000..ba40143 --- /dev/null +++ b/WarmlyShip/src/Entities/EntityShip.java @@ -0,0 +1,18 @@ +package Entities; + +import java.awt.*; + +public class EntityShip { + private int Speed; + private double Weight; + private Color BodyColor; + public Color getBodyColor() {return BodyColor;} + public double Step; + public EntityShip(int speed, double weight, Color bodycolor) + { + Speed = speed; + Weight = weight; + BodyColor = bodycolor; + Step = Speed * 100 / Weight; + } +} diff --git a/WarmlyShip/src/Entities/EntityWarmlyShip.java b/WarmlyShip/src/Entities/EntityWarmlyShip.java new file mode 100644 index 0000000..8863dcc --- /dev/null +++ b/WarmlyShip/src/Entities/EntityWarmlyShip.java @@ -0,0 +1,16 @@ +package Entities; + +import java.awt.*; +public class EntityWarmlyShip extends EntityShip{ + public Color AdditionalColor; + public Color getAdditionalColor() {return AdditionalColor;} + public boolean ShipPipes; + public boolean FuelTank; + public EntityWarmlyShip(int speed, double weight, Color bodyColor, Color additionalcolor, boolean sheeppipes, boolean fueltank) + { + super(speed, weight, bodyColor); + AdditionalColor = additionalcolor; + ShipPipes = sheeppipes; + FuelTank = fueltank; + } +} diff --git a/WarmlyShip/src/EntityWarmlyShip.java b/WarmlyShip/src/EntityWarmlyShip.java deleted file mode 100644 index 6fd0aed..0000000 --- a/WarmlyShip/src/EntityWarmlyShip.java +++ /dev/null @@ -1,24 +0,0 @@ -import java.awt.*; -public class EntityWarmlyShip { - private int Speed; - private double Weight; - private Color BodyColor; - public Color getBodyColor() {return BodyColor;} - private Color AdditionalColor; - public Color getAdditionalColor() {return AdditionalColor;} - private boolean ShipPipes; - public boolean getShipPipes() {return ShipPipes;} - private boolean FuelTank; - public boolean getFuelTank() {return FuelTank;} - public double Step; - public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean shippipes, boolean fueltank) - { - Speed = speed; - Weight = weight; - BodyColor = bodycolor; - AdditionalColor = additionalcolor; - ShipPipes = shippipes; - FuelTank = fueltank; - Step = Speed * 100 / Weight; - } -} diff --git a/WarmlyShip/src/FormWarmlyShip.java b/WarmlyShip/src/FormWarmlyShip.java index 760d3fa..b712300 100644 --- a/WarmlyShip/src/FormWarmlyShip.java +++ b/WarmlyShip/src/FormWarmlyShip.java @@ -1,3 +1,9 @@ +import DrawingShip.CanvasWarmlyShip; +import DrawingShip.DirectionType; +import DrawingShip.DrawingShip; +import DrawingShip.DrawingWarmlyShip; +import MovementStrategy.*; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -10,24 +16,58 @@ public class FormWarmlyShip extends JFrame { private String title; private Dimension dimension; private int Width, Height; - private CanvasWarmlyShip canvasWarmlyShip = new CanvasWarmlyShip(); - private JButton CreateButton = new JButton("Create");; + public CanvasWarmlyShip canvasWarmlyShip = new CanvasWarmlyShip(); + private JButton CreateButton = new JButton("Create warmlyship");; + private JButton CreateShipButton = new JButton("Create ship"); private JButton UpButton = new JButton(); private JButton DownButton = new JButton();; private JButton LeftButton = new JButton();; private JButton RightButton = new JButton(); + private AbstractStrategy _strategy; + private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"}); + private JButton ButtonStrategy = new JButton("Шаг"); public FormWarmlyShip(String title, Dimension dimension) { this.title = title; this.dimension = dimension; } + private void CreateObject(String typeOfClass) { + int StartPositionX = (int)(Math.random() * 90 + 10); + int StartPositionY = (int)(Math.random() * 90 + 10); + int speed = (int)(Math.random() * 300 + 100); + double weight = (double)(Math.random() * 3000 + 1000); + Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); + switch (typeOfClass) { + case "DrawingShip": + canvasWarmlyShip._drawingShip = new DrawingShip(speed, weight, bodyColor); + canvasWarmlyShip._drawingShip.SetPictureSize(Width, Height); + canvasWarmlyShip._drawingShip.SetPosition(StartPositionX, StartPositionY); + canvasWarmlyShip.repaint(); + break; + case "DrawingWarmlyShip": + Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; + boolean sheepPipes = new Random().nextBoolean(); + boolean fuelTank = new Random().nextBoolean();; + canvasWarmlyShip._drawingShip = new DrawingWarmlyShip(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank); + canvasWarmlyShip._drawingShip.SetPictureSize(Width, Height); + canvasWarmlyShip._drawingShip.SetPosition(StartPositionX, StartPositionY); + canvasWarmlyShip.repaint(); + break; + default: return; + } + _strategy = null; + ComboBoxStrategy.setEnabled(true); + } public void Init() { setTitle(title); setMinimumSize(dimension); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - Width = getWidth() - 15; - Height = getHeight() - 35; + + Width = getWidth() - 10; + Height = getHeight() - 34; + _strategy = null; CreateButton.setName("CREATE"); + CreateShipButton.setName("CREATESHIPBUTTON"); Icon iconUp = new ImageIcon("src\\images\\up.jpg"); UpButton.setIcon(iconUp); UpButton.setName("UP"); @@ -44,39 +84,74 @@ public class FormWarmlyShip extends JFrame { CreateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - int StartPositionX = (int)(Math.random() * 90 + 10); - int StartPositionY = (int)(Math.random() * 90 + 10); - int speed = (int)(Math.random() * 300 + 100); - double weight = (double)(Math.random() * 3000 + 1000); - Color bodyColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0)); - Color additionalColor = new Color((int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0),(int)(Math.random() * 255 + 0));; - boolean sheepPipes = new Random().nextBoolean(); - boolean fuelTank = new Random().nextBoolean();; - canvasWarmlyShip._drawingWarmlyShip = new DrawingWarmlyShip(); - canvasWarmlyShip._drawingWarmlyShip.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank); - canvasWarmlyShip._drawingWarmlyShip.SetPictureSize(Width, Height); - canvasWarmlyShip._drawingWarmlyShip.SetPosition( StartPositionX, StartPositionY); - canvasWarmlyShip.repaint(); + CreateObject("DrawingWarmlyShip"); + } + }); + + CreateShipButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawingShip"); + } + }); + + ButtonStrategy.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (canvasWarmlyShip._drawingShip == null) return; + if (ComboBoxStrategy.isEnabled()) + { + int index = ComboBoxStrategy.getSelectedIndex(); + switch(index) + { + case 0: + _strategy = new MoveToCenter(); + break; + case 1: + _strategy = new MoveToBorder(); + break; + default: + _strategy = null; + break; + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableShip(canvasWarmlyShip._drawingShip), Width, Height); + } + if (_strategy == null) + { + return; + } + ComboBoxStrategy.setEnabled(false); + _strategy.MakeStep(); + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + ComboBoxStrategy.setEnabled(true); + _strategy = null; + } } }); ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { - if (canvasWarmlyShip._drawingWarmlyShip == null) return; + if (canvasWarmlyShip._drawingShip == null) return; boolean result = false; switch ((((JButton)(event.getSource())).getName())) { case "UP": - result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Up); + System.out.println("Кнопка UP нажата"); + result = canvasWarmlyShip._drawingShip.MoveTransport(DirectionType.Up); break; case "DOWN": - result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Down); + result = canvasWarmlyShip._drawingShip.MoveTransport(DirectionType.Down); break; case "LEFT": - result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Left); + result = canvasWarmlyShip._drawingShip.MoveTransport(DirectionType.Left); break; case "RIGHT": - result = canvasWarmlyShip._drawingWarmlyShip.MoveTransport(DirectionType.Right); + result = canvasWarmlyShip._drawingShip.MoveTransport(DirectionType.Right); break; } if (result) { @@ -92,31 +167,40 @@ public class FormWarmlyShip extends JFrame { setSize(dimension.width,dimension.height); setLayout(null); canvasWarmlyShip.setBounds(0,0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 100, 40); + CreateButton.setBounds(10, getHeight() - 90, 140, 40); + CreateShipButton.setBounds(160, getHeight() - 90, 140, 40); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); + ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); + ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25); add(CreateButton); + add(CreateShipButton); add(UpButton); add(DownButton); add(RightButton); add(LeftButton); + add(ComboBoxStrategy); + add(ButtonStrategy); add(canvasWarmlyShip); setVisible(true); //обработка события изменения размеров окна addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { - Width = getWidth() - 15; - Height = getHeight() - 35; - if (canvasWarmlyShip._drawingWarmlyShip != null) - canvasWarmlyShip._drawingWarmlyShip.SetPictureSize(Width, Height); + Width = getWidth() - 10; + Height = getHeight() - 34; + if (canvasWarmlyShip._drawingShip != null) + canvasWarmlyShip._drawingShip.SetPictureSize(Width, Height); canvasWarmlyShip.setBounds(0,0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 100, 40); + CreateButton.setBounds(10, getHeight() - 90, 140, 40); + CreateShipButton.setBounds(160, getHeight() - 90, 140, 40); UpButton.setBounds(getWidth() - 140, getHeight() - 160, 50, 50); DownButton.setBounds(getWidth() - 140, getHeight() - 100, 50, 50); RightButton.setBounds(getWidth() - 80, getHeight() - 100, 50, 50); LeftButton.setBounds(getWidth() - 200, getHeight() - 100, 50, 50); + ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); + ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25); } }); } diff --git a/WarmlyShip/src/Main.java b/WarmlyShip/src/Main.java index c5fcc85..a785b53 100644 --- a/WarmlyShip/src/Main.java +++ b/WarmlyShip/src/Main.java @@ -1,8 +1,7 @@ import java.awt.*; - public class Main { public static void main(String[] args) { - FormWarmlyShip form = new FormWarmlyShip("Теплоход", new Dimension(500,500)); + FormWarmlyShip form = new FormWarmlyShip("Теплоход", new Dimension(700,500)); form.Init(); } } \ No newline at end of file diff --git a/WarmlyShip/src/MovementStrategy/AbstractStrategy.java b/WarmlyShip/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..c491f92 --- /dev/null +++ b/WarmlyShip/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,56 @@ +package MovementStrategy; + +public abstract class AbstractStrategy { + private IMoveableObjects _moveableObject; + private StrategyStatus _state = StrategyStatus.NotInit; + public int FieldWidth; + public int FieldHeight; + public StrategyStatus GetStatus() {return _state;} + public void SetData(IMoveableObjects moveableObjects, int width, int height) + { + if (moveableObjects == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObjects; + FieldWidth = width; + FieldHeight = height; + } + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) return; + if (IsTargetDestinaion()) + { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + protected boolean MoveLeft() {return MoveTo(MovementDirection.Left);}; + protected boolean MoveRight() {return MoveTo(MovementDirection.Right);}; + protected boolean MoveUp() {return MoveTo(MovementDirection.Up);}; + protected boolean MoveDown() {return MoveTo(MovementDirection.Down);}; + protected ObjectParameters GetObjectParameters() {return _moveableObject.GetObjectPosition();}; + protected Integer GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject.GetStep(); + } + protected abstract void MoveToTarget(); + protected abstract boolean IsTargetDestinaion(); + private boolean MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + boolean stateTryMoveObject = _moveableObject.TryMoveObject(movementDirection); + if (stateTryMoveObject) return stateTryMoveObject; + return false; + } +} diff --git a/WarmlyShip/src/MovementStrategy/IMoveableObjects.java b/WarmlyShip/src/MovementStrategy/IMoveableObjects.java new file mode 100644 index 0000000..4ecb751 --- /dev/null +++ b/WarmlyShip/src/MovementStrategy/IMoveableObjects.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public interface IMoveableObjects { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean TryMoveObject(MovementDirection direction); +} diff --git a/WarmlyShip/src/MovementStrategy/MoveToBorder.java b/WarmlyShip/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..c85b39f --- /dev/null +++ b/WarmlyShip/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,27 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth-GetStep() && + objParams.DownBorder + GetStep() >= FieldHeight-GetStep(); + } + @Override + protected void MoveToTarget() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + //реализация в правый нижний угол + int x = objParams.RightBorder; + if (x + GetStep() < FieldWidth) MoveRight(); + int y = objParams.DownBorder; + if (y + GetStep() < FieldHeight) MoveDown(); + } +} diff --git a/WarmlyShip/src/MovementStrategy/MoveToCenter.java b/WarmlyShip/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..2669284 --- /dev/null +++ b/WarmlyShip/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,48 @@ +package MovementStrategy; + +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight /2; + } + @Override + protected void MoveToTarget() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/WarmlyShip/src/MovementStrategy/MoveableShip.java b/WarmlyShip/src/MovementStrategy/MoveableShip.java new file mode 100644 index 0000000..c6e4de3 --- /dev/null +++ b/WarmlyShip/src/MovementStrategy/MoveableShip.java @@ -0,0 +1,45 @@ +package MovementStrategy; + +import DrawingShip.CanvasWarmlyShip; +import DrawingShip.DirectionType; +import DrawingShip.DrawingShip; + +public class MoveableShip implements IMoveableObjects{ + private CanvasWarmlyShip canvas = new CanvasWarmlyShip(); + public MoveableShip(DrawingShip drawningship) + { + canvas._drawingShip = drawningship; + } + @Override + public ObjectParameters GetObjectPosition() { + if (canvas._drawingShip == null || canvas._drawingShip.EntityShip == null || + canvas._drawingShip.GetPosX() == null || canvas._drawingShip.GetPosY() == null) + { + return null; + } + return new ObjectParameters(canvas._drawingShip.GetPosX(), canvas._drawingShip.GetPosY(), + canvas._drawingShip.GetWidth(), canvas._drawingShip.GetHeight()); + } + @Override + public int GetStep() { + return (int)(canvas._drawingShip.EntityShip.Step); + } + @Override + public boolean TryMoveObject(MovementDirection direction) { + if (canvas._drawingShip == null || canvas._drawingShip.EntityShip == null) + { + return false; + } + return canvas._drawingShip.MoveTransport(GetDirectionType(direction)); + } + private static DirectionType GetDirectionType(MovementDirection direction) + { + switch (direction) { + case MovementDirection.Left: return DirectionType.Left; + case MovementDirection.Right: return DirectionType.Right; + case MovementDirection.Up: return DirectionType.Up; + case MovementDirection.Down: return DirectionType.Down; + default: return DirectionType.Unknow; + } + } +} diff --git a/WarmlyShip/src/MovementStrategy/MovementDirection.java b/WarmlyShip/src/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..c52f124 --- /dev/null +++ b/WarmlyShip/src/MovementStrategy/MovementDirection.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public enum MovementDirection { + Up, + Down, + Left, + Right +} diff --git a/WarmlyShip/src/MovementStrategy/ObjectParameters.java b/WarmlyShip/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..b68fe75 --- /dev/null +++ b/WarmlyShip/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,27 @@ +package MovementStrategy; + +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private int _height; + public int LeftBorder = _x; + public int TopBorder = _y; + public int RightBorder = _x + _width; + public int DownBorder = _y + _height; + public int ObjectMiddleHorizontal = _x + _width / 2; + public int ObjectMiddleVertical = _y + _height / 2; + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + LeftBorder = _x; + TopBorder = _y; + RightBorder = _x + _width; + DownBorder = _y + _height; + ObjectMiddleHorizontal = _x + _width / 2; + ObjectMiddleVertical = _y + _height / 2; + } +} diff --git a/WarmlyShip/src/MovementStrategy/StrategyStatus.java b/WarmlyShip/src/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..4f087ea --- /dev/null +++ b/WarmlyShip/src/MovementStrategy/StrategyStatus.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum StrategyStatus { + NotInit, + InProgress, + Finish +}