From 5c0d61e232137bcdfbb479b7f9e6d0f382f741c8 Mon Sep 17 00:00:00 2001 From: urlilpolly Date: Sat, 30 Dec 2023 02:08:34 +0400 Subject: [PATCH] laba2 --- src/Drawnings/DrawingAirbus.java | 128 ++++++++----- src/Drawnings/DrawingPlane.java | 44 +++++ src/Drawnings/DrawingPortholes.java | 67 ------- src/Drawnings/DrawingPortholesCircle.java | 53 ++++++ src/Drawnings/DrawingPortholesHeart.java | 16 ++ src/Drawnings/DrawingPortholesSquare.java | 14 ++ src/Drawnings/IDrawingPortholes.java | 10 ++ src/Entities/EntityAirbus.java | 29 +-- src/Entities/EntityPlane.java | 21 +++ src/FormAirbus.java | 168 +++++++++++++++--- src/Main.java | 1 + src/MovementStrategy/AbstractStrategy.java | 76 ++++++++ src/MovementStrategy/DrawingObjectAirbus.java | 24 +++ src/MovementStrategy/IMoveableObject.java | 8 + src/MovementStrategy/MoveToBorder.java | 44 +++++ src/MovementStrategy/MoveToCenter.java | 53 ++++++ src/MovementStrategy/ObjectParameters.java | 25 +++ src/MovementStrategy/Status.java | 7 + 18 files changed, 620 insertions(+), 168 deletions(-) create mode 100644 src/Drawnings/DrawingPlane.java delete mode 100644 src/Drawnings/DrawingPortholes.java create mode 100644 src/Drawnings/DrawingPortholesCircle.java create mode 100644 src/Drawnings/DrawingPortholesHeart.java create mode 100644 src/Drawnings/DrawingPortholesSquare.java create mode 100644 src/Drawnings/IDrawingPortholes.java create mode 100644 src/Entities/EntityPlane.java create mode 100644 src/MovementStrategy/AbstractStrategy.java create mode 100644 src/MovementStrategy/DrawingObjectAirbus.java create mode 100644 src/MovementStrategy/IMoveableObject.java create mode 100644 src/MovementStrategy/MoveToBorder.java create mode 100644 src/MovementStrategy/MoveToCenter.java create mode 100644 src/MovementStrategy/ObjectParameters.java create mode 100644 src/MovementStrategy/Status.java diff --git a/src/Drawnings/DrawingAirbus.java b/src/Drawnings/DrawingAirbus.java index 415ea9a..27c0380 100644 --- a/src/Drawnings/DrawingAirbus.java +++ b/src/Drawnings/DrawingAirbus.java @@ -1,29 +1,48 @@ package Drawnings; import java.awt.*; +import java.util.Random; import Entities.*; import MovementStrategy.*; public class DrawingAirbus { public EntityAirbus entityAirbus; - public DrawingPortholes _portholes; + public IDrawingPortholes _portholes; private int _pictureWidth; private int _pictureHeight; - private int _startPosX; - private int _startPosY; + protected int _startPosX; + protected int _startPosY; private int _airbusWidth = 210; private int _airbusHeight = 100; - public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isCompartment, boolean isadditionalEngine, int countPortholes, int width, int height) { + public int GetPosX() { return _startPosX; } + public int GetPosY() { return _startPosY; } + public int GetWidth() { return _airbusWidth; } + public int GetHeight() { return _airbusHeight; } + + public DrawingAirbus(int speed, float weight, Color bodyColor, int countPortholes, int width, int height) { if (width < _airbusHeight || height < _airbusWidth) return; _pictureWidth = width; _pictureHeight = height; - entityAirbus = new EntityAirbus(); - entityAirbus.Init(speed, weight, bodyColor, additionalColor, isCompartment, isadditionalEngine); + entityAirbus = new EntityAirbus(speed, weight, bodyColor); - _portholes = new DrawingPortholes(); + Random random = new Random(); + switch (random.nextInt(0,3)) { + case 0: + _portholes = new DrawingPortholesCircle(); + break; + case 1: + _portholes = new DrawingPortholesHeart(); + break; + case 2: + _portholes = new DrawingPortholesSquare(); + break; + default: + _portholes = new DrawingPortholesCircle(); + break; + } _portholes.SetCount(countPortholes); } @@ -40,35 +59,45 @@ public class DrawingAirbus { } } + public boolean CanMove(Direction direction) + { + if (entityAirbus == null) + { + return false; + } + switch (direction) + { + case Left: + return _startPosX - entityAirbus.Step > 5; + case Right: + return _startPosX + _airbusWidth + entityAirbus.Step < _pictureWidth; + case Up: + return _startPosY - entityAirbus.Step > 0; + case Down: + return _startPosY + _airbusHeight + entityAirbus.Step < _pictureHeight; + default: + return false; + } + } + public void MoveTransport(Direction direction){ - if (entityAirbus == null) { + if (!CanMove(direction) || entityAirbus == null) + { return; } switch (direction) { case Left: - if (_startPosX - entityAirbus.Step > 5) - { - _startPosX -= entityAirbus.Step; - } + _startPosX -= entityAirbus.Step; break; case Right: - if (_startPosX + _airbusWidth + entityAirbus.Step < _pictureWidth) - { - _startPosX += entityAirbus.Step; - } + _startPosX += entityAirbus.Step; break; case Up: - if (_startPosY - entityAirbus.Step > 0) - { - _startPosY -= entityAirbus.Step; - } + _startPosY -= entityAirbus.Step; break; case Down: - if (_startPosY + _airbusHeight + entityAirbus.Step < _pictureHeight) - { - _startPosY += entityAirbus.Step; - } + _startPosY += entityAirbus.Step; break; } } @@ -78,25 +107,37 @@ public class DrawingAirbus { if (entityAirbus == null) { return; } - // иллюминаторы - _portholes.Draw(g, _startPosX+30, _startPosY+34); + g.setColor(Color.BLACK); //Тело + g.setColor(entityAirbus.getBodyColor()); + g.fillRect(_startPosX + 5, _startPosY + 50, 170, 30); + g.fillArc(_startPosX - 5, _startPosY + 50, 20, 30, 90, 180); + g.setColor(Color.BLACK); g.drawRect(_startPosX + 5, _startPosY + 50, 170, 30); g.drawArc(_startPosX - 5, _startPosY + 50, 20, 30, 90, 180); + + _portholes.Draw(g, _startPosX+30, _startPosY+34); //Заднее крыло - g.drawLine( _startPosX, _startPosY, _startPosX + 50, _startPosY + 50); - g.drawLine( _startPosX, _startPosY, _startPosX, _startPosY + 52); + g.setColor(Color.BLACK); + g.drawPolygon(new int[] {_startPosX + 50,_startPosX, _startPosX}, new int[] {_startPosY + 50, _startPosY, _startPosY + 50}, 3); + g.setColor(entityAirbus.getBodyColor()); + g.fillPolygon(new int[] {_startPosX + 50,_startPosX, _startPosX}, new int[] {_startPosY + 50, _startPosY, _startPosY + 50}, 3); //Заднее боковые крылья + g.setColor(Color.BLACK); g.drawOval(_startPosX - 7, _startPosY + 45, 30, 8); + g.setColor(entityAirbus.getBodyColor()); g.fillOval(_startPosX - 7, _startPosY + 45, 30, 8); //Нос - g.drawLine( _startPosX + 175, _startPosY + 50, _startPosX + 200, _startPosY + 65); - g.drawLine( _startPosX + 200, _startPosY + 65, _startPosX + 175, _startPosY + 80); - g.drawLine( _startPosX + 175, _startPosY + 50, _startPosX + 175, _startPosY + 80); - g.drawLine( _startPosX + 175, _startPosY + 65, _startPosX + 200, _startPosY + 65); + g.setColor(Color.BLACK); + g.drawPolygon(new int[] {_startPosX + 175,_startPosX + 200, _startPosX + 175}, new int[] {_startPosY + 50, _startPosY + 65, _startPosY + 65}, 3); + g.drawPolygon(new int[] {_startPosX + 175,_startPosX + 200, _startPosX + 175}, new int[] {_startPosY + 80, _startPosY + 65, _startPosY + 65}, 3); + g.setColor(entityAirbus.getBodyColor()); + g.fillPolygon(new int[] {_startPosX + 175,_startPosX + 200, _startPosX + 175}, new int[] {_startPosY + 50, _startPosY + 65, _startPosY + 65}, 3); + g.fillPolygon(new int[] {_startPosX + 175,_startPosX + 200, _startPosX + 175}, new int[] {_startPosY + 80, _startPosY + 65, _startPosY + 65}, 3); //Крылья + g.setColor(Color.BLACK); g.drawArc(_startPosX + 49, _startPosY + 62, 5, 5, 90, 180); g.drawLine( _startPosX + 51, _startPosY + 62, _startPosX + 140, _startPosY + 62); g.drawArc( _startPosX + 137, _startPosY + 62, 5, 5, 2790, 180); @@ -105,25 +146,14 @@ public class DrawingAirbus { g.drawLine(_startPosX + 55, _startPosY + 80, _startPosX + 55, _startPosY + 90); g.drawOval(_startPosX + 47, _startPosY + 90, 5, 5); g.drawOval( _startPosX + 57, _startPosY + 90, 5, 5); + g.setColor(entityAirbus.getBodyColor()); + g.fillOval(_startPosX + 47, _startPosY + 90, 5, 5); + g.fillOval( _startPosX + 57, _startPosY + 90, 5, 5); //Передние шасси + g.setColor(Color.BLACK); g.drawLine( _startPosX + 165, _startPosY + 80, _startPosX + 165, _startPosY + 90); g.drawOval( _startPosX + 163, _startPosY + 91, 5, 5); -//Пассажирсакий доп. отсек - if (entityAirbus.IsCompartment()) - { - g.setColor(entityAirbus.getAdditionalColor()); - g.drawArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180); - g.fillArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180); - } -// Доп. двигатели - if (entityAirbus.IsAdditionalEngine()) - { - g.drawLine(_startPosX + 95, _startPosY + 68, _startPosX + 95, _startPosY + 75); - g.setColor(entityAirbus.getAdditionalColor()); - int[] xPolygon = { _startPosX + 83, _startPosX + 103, _startPosX + 103, _startPosX + 83, _startPosX + 83}; - int[] yPolygon = {_startPosY + 78, _startPosY + 73, _startPosY + 93, _startPosY + 88, _startPosY + 78}; - g.drawPolygon(xPolygon, yPolygon, xPolygon.length); - g.fillPolygon(xPolygon, yPolygon, xPolygon.length); - } + g.setColor(entityAirbus.getBodyColor()); + g.fillOval( _startPosX + 163, _startPosY + 91, 5, 5); } } diff --git a/src/Drawnings/DrawingPlane.java b/src/Drawnings/DrawingPlane.java new file mode 100644 index 0000000..406e29e --- /dev/null +++ b/src/Drawnings/DrawingPlane.java @@ -0,0 +1,44 @@ +package Drawnings; + +import Entities.EntityPlane; + +import java.awt.*; + +public class DrawingPlane extends DrawingAirbus { + + public DrawingPlane(int speed, float weight, Color bodyColor, int countPortholes, Color additionalColor, boolean isCompartment, boolean isAdditionalEngine, int width, int height) + { + super(speed, weight, bodyColor, countPortholes, width, height); + if (entityAirbus != null) { + entityAirbus = new EntityPlane(speed, weight, bodyColor, additionalColor, isCompartment, isAdditionalEngine); + } + } + @Override + public void DrawTransport(Graphics2D g) { + + if (entityAirbus == null) + { + return; + } + + Color additionalColor = ((EntityPlane)entityAirbus).getAdditionalColor(); + //Пассажирсакий доп. отсек + if (((EntityPlane)entityAirbus).IsCompartment()) + { + g.setColor(additionalColor); + g.drawArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180); + g.fillArc(_startPosX + 60, _startPosY + 28, 115, 45, 0, 180); + } + super.DrawTransport(g); +// Доп. двигатели + if (((EntityPlane)entityAirbus).IsAdditionalEngine()) + { + g.drawLine(_startPosX + 95, _startPosY + 68, _startPosX + 95, _startPosY + 75); + g.setColor(additionalColor); + int[] xPolygon = { _startPosX + 83, _startPosX + 103, _startPosX + 103, _startPosX + 83, _startPosX + 83}; + int[] yPolygon = {_startPosY + 78, _startPosY + 73, _startPosY + 93, _startPosY + 88, _startPosY + 78}; + g.drawPolygon(xPolygon, yPolygon, xPolygon.length); + g.fillPolygon(xPolygon, yPolygon, xPolygon.length); + } + } +} diff --git a/src/Drawnings/DrawingPortholes.java b/src/Drawnings/DrawingPortholes.java deleted file mode 100644 index 9810120..0000000 --- a/src/Drawnings/DrawingPortholes.java +++ /dev/null @@ -1,67 +0,0 @@ -package Drawnings; - -import java.awt.*; -import Entities.*; - -public class DrawingPortholes { - private CountPortholes _porthole; - public CountPortholes getCount() - { - return _porthole; - } - public void SetCount (int count) { - switch (count) { - case 10: - _porthole = CountPortholes.Ten; - break; - case 20: - _porthole = CountPortholes.Twenty; - break; - case 30: - _porthole = CountPortholes.Thirty; - break; - default: - _porthole = CountPortholes.Ten; - break; - } - } - - public void Draw (Graphics2D g, int _startPosx, int _startPoxY) { - g.setColor(Color.BLACK); - if (_porthole == null) {return;} - - for (int i = 0; i < 10; ++i) - { - g.setColor(Color.cyan); - g.fillOval(_startPosx + 19 + i*8, _startPoxY+21, 3, 3); - g.setColor(Color.black); - g.drawOval(_startPosx + 19 + i*8, _startPoxY+21, 3, 3); - } - - if (_porthole != CountPortholes.Ten) { - for (int i=0; i < 5; ++i) - { - g.setColor(Color.cyan); - g.fillOval(_startPosx - 15 + i*5, _startPoxY+28, 3, 3); - g.setColor(Color.black); - g.drawOval(_startPosx - 15 + i*5, _startPoxY+28, 3, 3); - } - for (int i=0; i < 5; ++i) - { - g.setColor(Color.cyan); - g.fillOval(_startPosx + 115 + i*5, _startPoxY+28, 3, 3); - g.setColor(Color.black); - g.drawOval(_startPosx + 115 + i*5, _startPoxY+28, 3, 3); - } - } - if (_porthole == CountPortholes.Thirty){ - for (int i = 0; i < 10; ++i) - { - g.setColor(Color.cyan); - g.fillOval(_startPosx + 19 + i*8, _startPoxY+37, 3, 3); - g.setColor(Color.black); - g.drawOval(_startPosx + 19 + i*8, _startPoxY+37, 3, 3); - } - } - } -} \ No newline at end of file diff --git a/src/Drawnings/DrawingPortholesCircle.java b/src/Drawnings/DrawingPortholesCircle.java new file mode 100644 index 0000000..0413615 --- /dev/null +++ b/src/Drawnings/DrawingPortholesCircle.java @@ -0,0 +1,53 @@ +package Drawnings; + +import java.awt.*; +import Entities.*; + +public class DrawingPortholesCircle implements IDrawingPortholes { + private CountPortholes _porthole; + + public CountPortholes getCount() + { + return _porthole; + } + public void SetCount (int count) { + switch (count) { + case 10: + _porthole = CountPortholes.Ten; + break; + case 20: + _porthole = CountPortholes.Twenty; + break; + case 30: + _porthole = CountPortholes.Thirty; + break; + default: + _porthole = CountPortholes.Ten; + break; + } + } + protected void drawPortholes(Graphics2D g, int posX, int posY){ + g.setColor(Color.cyan); + g.fillOval(posX, posY, 3, 3); + g.setColor(Color.black); + g.drawOval(posX, posY, 3, 3); + } + public void Draw (Graphics2D g, int _startPosX, int _startPosY) { + if (_porthole == null) {return;} + + for (int i = 0; i < 10; ++i) { + drawPortholes(g, _startPosX + 19 + i * 8, _startPosY + 21); + } + if (_porthole != CountPortholes.Ten) { + for (int i = 0; i < 5; ++i) { + drawPortholes(g, _startPosX - 15 + i * 5, _startPosY + 28); + drawPortholes(g, _startPosX + 115 + i * 5, _startPosY + 28); + } + } + if (_porthole == CountPortholes.Thirty) { + for (int i = 0; i < 10; ++i) { + drawPortholes(g, _startPosX + 19 + i * 8, _startPosY + 37); + } + } + } +} \ No newline at end of file diff --git a/src/Drawnings/DrawingPortholesHeart.java b/src/Drawnings/DrawingPortholesHeart.java new file mode 100644 index 0000000..9b9fa72 --- /dev/null +++ b/src/Drawnings/DrawingPortholesHeart.java @@ -0,0 +1,16 @@ +package Drawnings; + +import Entities.CountPortholes; + +import java.awt.*; + +public class DrawingPortholesHeart extends DrawingPortholesCircle { + protected void drawPortholes(Graphics2D g, int posX, int posY) { + int[] HeartX = {posX + 2, posX, posX, posX + 1, posX + 2, posX + 3, posX + 5, posX + 5}; + int[] HeartY = {posY + 4, posY + 2, posY, posY, posY + 1, posY, posY, posY + 2}; + g.setColor(Color.cyan); + g.fillPolygon(HeartX, HeartY, HeartX.length); + g.setColor(Color.black); + g.drawPolygon(HeartX, HeartY, HeartX.length); + } +} \ No newline at end of file diff --git a/src/Drawnings/DrawingPortholesSquare.java b/src/Drawnings/DrawingPortholesSquare.java new file mode 100644 index 0000000..a601009 --- /dev/null +++ b/src/Drawnings/DrawingPortholesSquare.java @@ -0,0 +1,14 @@ +package Drawnings; + +import Entities.CountPortholes; + +import java.awt.*; + +public class DrawingPortholesSquare extends DrawingPortholesCircle { + protected void drawPortholes(Graphics2D g, int posX, int posY){ + g.setColor(Color.cyan); + g.fillRect(posX, posY, 3, 3); + g.setColor(Color.black); + g.drawRect(posX, posY, 3, 3); + } +} \ No newline at end of file diff --git a/src/Drawnings/IDrawingPortholes.java b/src/Drawnings/IDrawingPortholes.java new file mode 100644 index 0000000..3593865 --- /dev/null +++ b/src/Drawnings/IDrawingPortholes.java @@ -0,0 +1,10 @@ +package Drawnings; + +import java.awt.*; +import Entities.CountPortholes; + +public interface IDrawingPortholes { + public CountPortholes getCount(); + public void SetCount (int count); + public void Draw (Graphics2D g, int _startPosX, int _startPoxY); +} \ No newline at end of file diff --git a/src/Entities/EntityAirbus.java b/src/Entities/EntityAirbus.java index 1f4b8a7..0c83f72 100644 --- a/src/Entities/EntityAirbus.java +++ b/src/Entities/EntityAirbus.java @@ -6,11 +6,7 @@ public class EntityAirbus { private int Speed; private float Weight; private Color BodyColor; - private Color AdditionalColor; - private boolean IsCompartment; - private boolean IsAdditionalEngine; - - public int Step; + public float Step; public int getSpeed() { return Speed; @@ -21,27 +17,14 @@ public class EntityAirbus { public Color getBodyColor() { return BodyColor; } - public Color getAdditionalColor() { - return AdditionalColor; - } - public boolean IsCompartment() { - return IsCompartment; - } - public boolean IsAdditionalEngine() { - return IsAdditionalEngine; - } - public void Init(int speed, float weight, Color bodyColor, Color additionalColor, boolean isCompartment, boolean isAdditionalEngine) + public EntityAirbus(int speed, float weight, Color bodyColor) { + Weight = weight; + Speed = speed; + BodyColor = bodyColor; - Weight = weight; - Speed = speed; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - IsCompartment = isCompartment; - IsAdditionalEngine = isAdditionalEngine; - - Step = Speed * 100 / (int) Weight; + Step = Speed * 100 / (int) Weight; } } diff --git a/src/Entities/EntityPlane.java b/src/Entities/EntityPlane.java new file mode 100644 index 0000000..90f94f5 --- /dev/null +++ b/src/Entities/EntityPlane.java @@ -0,0 +1,21 @@ +package Entities; + +import javax.swing.text.AttributeSet; +import java.awt.*; + +public class EntityPlane extends EntityAirbus { + private Color AdditionalColor; + private boolean IsCompartment; + private boolean IsAdditionalEngine; + + public EntityPlane(int speed, float weight, Color bodyColor, Color additionalColor, boolean isCompartment, boolean isAdditionalEngine) { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + IsCompartment = isCompartment; + IsAdditionalEngine = isAdditionalEngine; + } + + public Color getAdditionalColor() { return AdditionalColor; } + public boolean IsCompartment() { return IsCompartment; } + public boolean IsAdditionalEngine() { return IsAdditionalEngine; } +} diff --git a/src/FormAirbus.java b/src/FormAirbus.java index 33a4dcf..190f3be 100644 --- a/src/FormAirbus.java +++ b/src/FormAirbus.java @@ -7,12 +7,23 @@ import MovementStrategy.*; public class FormAirbus extends JFrame { - private DrawingAirbus _drawningAirbus; - private Canvas canvas = new Canvas(); + private int width; + private int height; + private DrawingAirbus _drawingAirbus; + private AbstractStrategy _abstractStrategy; + private Canvas canvas; - JLabel labelCount = new JLabel("Введите число иллюминаторов:"); - private JTextField fieldCount = new JTextField(); - private JButton buttonCreate; + // выбор кол-ва иллюминаторов + JLabel labelCount; + private JTextField fieldCount; + + // выбор стратегии + JLabel labelStrategy; + JComboBox comboBoxStrategy; + JButton buttonStrategy; + + private JButton buttonCreateAirbus; + private JButton buttonCreatePlane; private JButton buttonUp; private JButton buttonDown; private JButton buttonRight; @@ -26,42 +37,67 @@ public class FormAirbus extends JFrame { private void InitializeComponent() { - buttonCreate = new JButton("Создать самолёт"); + canvas = new Canvas(); + + labelCount = new JLabel("Введите число иллюминаторов:"); + fieldCount = new JTextField(); + + labelStrategy = new JLabel("Шаг стратегии:"); + comboBoxStrategy = new JComboBox(new Integer[] {0, 1}); + buttonStrategy = new JButton("Выбрать стратегию"); + buttonStrategy.setMargin(new Insets(0, 0, 0, 0)); + + buttonCreateAirbus = new JButton("Создать аэробус"); + buttonCreateAirbus.setMargin(new Insets(0, 0, 0, 0)); + + buttonCreatePlane = new JButton("Создать самолёт"); + buttonCreatePlane.setMargin(new Insets(0, 0, 0, 0)); buttonUp = new JButton(); buttonUp.setName("up"); buttonUp.setIcon(new ImageIcon("images\\KeyUp.png")); - buttonUp.setSize(48, 44); buttonRight = new JButton(); buttonRight.setName("right"); buttonRight.setIcon(new ImageIcon("images\\KeyRight.png")); - buttonRight.setSize(48, 44); buttonLeft = new JButton(); buttonLeft.setName("left"); buttonLeft.setIcon(new ImageIcon("images\\KeyLeft.png")); - buttonLeft.setSize(48, 44); buttonDown = new JButton(); buttonDown.setName("down"); buttonDown.setIcon(new ImageIcon("images\\KeyDown.png")); - buttonDown.setSize(48, 44); setSize(800,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null); - buttonCreate.setBounds(12, 355, 170, 44); + buttonCreateAirbus.setBounds(12, 355, 146, 33); + buttonCreatePlane.setBounds(182, 355, 146, 33); + + labelCount.setBounds(42, 405, 240, 20); + fieldCount.setBounds(240, 407, 48, 20); + + labelStrategy.setBounds(630, 20, 146, 33); + comboBoxStrategy.setBounds(630, 50, 146, 20); + buttonStrategy.setBounds(630, 80, 146, 33); + buttonUp.setBounds(679, 313, 48, 44); buttonRight.setBounds( 728, 358, 48, 44); buttonLeft.setBounds(630, 358, 48, 44); buttonDown.setBounds( 679, 358, 48, 44); labelCount.setBounds(12, 405, 240, 20); fieldCount.setBounds(210, 407, 48, 20); - canvas.setBounds(0,0,800, 460); + canvas.setBounds(0,0,790, 460); - add(buttonCreate); + add(buttonCreateAirbus); + add(buttonCreatePlane); + add(labelCount); + add(fieldCount); + add(labelStrategy); + add(comboBoxStrategy); + add(buttonStrategy); add(buttonUp); add(buttonRight); add(buttonDown); @@ -70,14 +106,18 @@ public class FormAirbus extends JFrame { add(fieldCount); add(canvas); - buttonCreate.addActionListener(buttonCreateListener); + // логика формы + buttonCreateAirbus.addActionListener(buttonCreateAirbusListener); + buttonCreatePlane.addActionListener(buttonCreatePlaneListener); + buttonStrategy.addActionListener(buttonStrategyListener); buttonUp.addActionListener(buttonsMoveListener); buttonRight.addActionListener(buttonsMoveListener); buttonDown.addActionListener(buttonsMoveListener); buttonLeft.addActionListener(buttonsMoveListener); } - ActionListener buttonCreateListener = new ActionListener() { + ActionListener buttonCreateAirbusListener = new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { int countPortholes; try @@ -95,15 +135,86 @@ public class FormAirbus extends JFrame { } Random rand = new Random(); - _drawningAirbus = new DrawingAirbus(); - _drawningAirbus.Init(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + _drawingAirbus = new DrawingAirbus(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)), - new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), - rand.nextBoolean(), rand.nextBoolean(), countPortholes, canvas.getWidth(), canvas.getHeight()); - _drawningAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10); + _drawingAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10); + comboBoxStrategy.setEnabled(true); + canvas.repaint(); + } + }; + + ActionListener buttonCreatePlaneListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int countPortholes; + try + { + countPortholes = Integer.parseInt(fieldCount.getText()); + } + catch (Exception ex) + { + countPortholes = 0; + } + if (countPortholes != 10 && countPortholes != 20 && countPortholes != 30) + { + JOptionPane.showMessageDialog(null, "Число должно быть равно 10, 20 или 30.\nКол-во иллюминаторов приравнено к 10"); + countPortholes = 10; + } + + Random rand = new Random(); + _drawingAirbus = new DrawingPlane(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)), + countPortholes, + new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), + rand.nextBoolean(), rand.nextBoolean(), + canvas.getWidth(), canvas.getHeight()); + + _drawingAirbus.SetPosition(rand.nextInt(100) + 10, rand.nextInt(100) + 10); + comboBoxStrategy.setEnabled(true); + canvas.repaint(); + } + }; + + ActionListener buttonStrategyListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_drawingAirbus == null) + { + return; + } + if (comboBoxStrategy.isEnabled()) { + + switch (comboBoxStrategy.getSelectedIndex()) { + case 0: + _abstractStrategy = new MoveToCenter(); + break; + case 1: + _abstractStrategy = new MoveToBorder(); + break; + default: + _abstractStrategy = null; + break; + } + ; + if (_abstractStrategy == null) { + return; + } + _abstractStrategy.SetData(new DrawingObjectAirbus(_drawingAirbus), canvas.getWidth(), canvas.getHeight()); + comboBoxStrategy.setEnabled(false); + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.setEnabled(true); + _abstractStrategy = null; + } canvas.repaint(); } }; @@ -111,23 +222,23 @@ public class FormAirbus extends JFrame { ActionListener buttonsMoveListener = new ActionListener() { // реакция на нажатие public void actionPerformed(ActionEvent e) { - if (_drawningAirbus == null) + if (_drawingAirbus == null) { return; } String command = ((JButton)(e.getSource())).getName(); switch (command) { case "up": - _drawningAirbus.MoveTransport(Direction.Up); + _drawingAirbus.MoveTransport(Direction.Up); break; case "down": - _drawningAirbus.MoveTransport(Direction.Down); + _drawingAirbus.MoveTransport(Direction.Down); break; case "right": - _drawningAirbus.MoveTransport(Direction.Right); + _drawingAirbus.MoveTransport(Direction.Right); break; case "left": - _drawningAirbus.MoveTransport(Direction.Left); + _drawingAirbus.MoveTransport(Direction.Left); break; } canvas.repaint(); @@ -135,15 +246,14 @@ public class FormAirbus extends JFrame { }; class Canvas extends JComponent{ - public Canvas(){ - } + public Canvas() {} public void paintComponent (Graphics g){ - if (_drawningAirbus == null){ + if (_drawingAirbus == null){ return; } super.paintComponents (g) ; Graphics2D g2d = (Graphics2D)g; - _drawningAirbus.DrawTransport(g2d); + _drawingAirbus.DrawTransport(g2d); super.repaint(); } } diff --git a/src/Main.java b/src/Main.java index 03af442..ba66689 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,4 +1,5 @@ public class Main { + public static void main(String[] args) { new FormAirbus(); } diff --git a/src/MovementStrategy/AbstractStrategy.java b/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..742c4df --- /dev/null +++ b/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,76 @@ +package MovementStrategy; + +public abstract class AbstractStrategy { + private IMoveableObject _moveableObject; + private Status _state = Status.NotInit; + protected int FieldWidth; + protected int FieldHeight; + public Status GetStatus() { return _state; } + + // Изменить статус, установить поля + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + // сделать шаг + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestination()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + + // перемещения + protected boolean MoveLeft() { return MoveTo(Direction.Left); } + protected boolean MoveRight() { return MoveTo(Direction.Right); } + protected boolean MoveUp() { return MoveTo(Direction.Up); } + protected boolean MoveDown() { return MoveTo(Direction.Down); } + + // параметры + protected ObjectParameters GetObjectParameters() { return _moveableObject.GetObjectPosition(); } + // шаг + protected int GetStep() + { + if (_state != Status.InProgress) + { + return 0; + } + return _moveableObject.GetStep(); + } + // перемещение + protected abstract void MoveToTarget(); + + // достигнута ли цель + protected abstract boolean IsTargetDestination(); + + // попытка перемещения по направлению + private boolean MoveTo(Direction directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(directionType)) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } +} diff --git a/src/MovementStrategy/DrawingObjectAirbus.java b/src/MovementStrategy/DrawingObjectAirbus.java new file mode 100644 index 0000000..3087761 --- /dev/null +++ b/src/MovementStrategy/DrawingObjectAirbus.java @@ -0,0 +1,24 @@ +package MovementStrategy; +import Drawnings.*; + +public class DrawingObjectAirbus implements IMoveableObject { + private DrawingAirbus _drawingAirbus = null; + + public DrawingObjectAirbus(DrawingAirbus drawingAirbus) + { + _drawingAirbus = drawingAirbus; + } + + public ObjectParameters GetObjectPosition() + { + if (_drawingAirbus == null || _drawingAirbus.entityAirbus == null) + { + return null; + } + return new ObjectParameters(_drawingAirbus.GetPosX(), _drawingAirbus.GetPosY(), _drawingAirbus.GetWidth(), _drawingAirbus.GetHeight()); + } + + public int GetStep() { return (int)_drawingAirbus.entityAirbus.Step; } + public boolean CheckCanMove(Direction direction) { return _drawingAirbus.CanMove(direction); } + public void MoveObject(Direction direction) { _drawingAirbus.MoveTransport(direction); } +} diff --git a/src/MovementStrategy/IMoveableObject.java b/src/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..ffd427e --- /dev/null +++ b/src/MovementStrategy/IMoveableObject.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + boolean CheckCanMove(Direction direction); + void MoveObject(Direction direction); +} diff --git a/src/MovementStrategy/MoveToBorder.java b/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..2e74828 --- /dev/null +++ b/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,44 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy { + @Override + protected boolean IsTargetDestination() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.RightBorder() + GetStep() >= FieldWidth && objParams.DownBorder() + GetStep() >= FieldHeight; + } + + // движение к цели + @Override + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder() - FieldWidth; + var diffY = objParams.DownBorder() - FieldHeight; + if (diffX >= 0) + { + MoveDown(); + } + else if (diffY >= 0) + { + MoveRight(); + } + else if (Math.abs(diffX) > Math.abs(diffY)) + { + MoveRight(); + } + else + { + MoveDown(); + } + + } +} diff --git a/src/MovementStrategy/MoveToCenter.java b/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..1b278cb --- /dev/null +++ b/src/MovementStrategy/MoveToCenter.java @@ -0,0 +1,53 @@ +package MovementStrategy; + +public class MoveToCenter extends AbstractStrategy { + @Override + protected boolean IsTargetDestination() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal() <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal() + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical() <= FieldHeight / 2 && + objParams.ObjectMiddleVertical() + GetStep() >= FieldHeight / 2; + } + + // движение к цели + @Override + protected void MoveToTarget() + { + var objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal() - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + var diffY = objParams.ObjectMiddleVertical() - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/src/MovementStrategy/ObjectParameters.java b/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..a741f5a --- /dev/null +++ b/src/MovementStrategy/ObjectParameters.java @@ -0,0 +1,25 @@ +package 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 ObjectMiddleHorizontal () { return _x + _width / 2; } + public int ObjectMiddleVertical () { return _y + _height / 2; } + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/src/MovementStrategy/Status.java b/src/MovementStrategy/Status.java new file mode 100644 index 0000000..586b538 --- /dev/null +++ b/src/MovementStrategy/Status.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum Status { + NotInit, + InProgress, + Finish +}