diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/src/ChoiseAndDrawingPortholes/CountPortholes.java b/src/src/ChoiseAndDrawingPortholes/CountPortholes.java new file mode 100644 index 0000000..627b4c1 --- /dev/null +++ b/src/src/ChoiseAndDrawingPortholes/CountPortholes.java @@ -0,0 +1,12 @@ +package ChoiseAndDrawingPortholes; + +public enum CountPortholes { + TenPortholes(10), + TwentyPortholes(20), + ThirtyPortholes(30); + private int numberofportholes; + CountPortholes(int numberofportholes) { + this.numberofportholes = numberofportholes; + } + public int getNumportholes() {return numberofportholes;} +} \ No newline at end of file diff --git a/src/src/ChoiseAndDrawingPortholes/DrawingPortholesCircle.java b/src/src/ChoiseAndDrawingPortholes/DrawingPortholesCircle.java new file mode 100644 index 0000000..66601c0 --- /dev/null +++ b/src/src/ChoiseAndDrawingPortholes/DrawingPortholesCircle.java @@ -0,0 +1,57 @@ +package ChoiseAndDrawingPortholes; + +import java.awt.*; +import Entities.*; + +public class DrawingPortholesCircle implements IDifferentPortholes { + private CountPortholes _porthole; + + public CountPortholes getCount() { + return _porthole; + } + + public void SetCount(int count) { + switch (count) { + case 10: + _porthole = CountPortholes.TenPortholes; + break; + case 20: + _porthole = CountPortholes.TwentyPortholes; + break; + case 30: + _porthole = CountPortholes.ThirtyPortholes; + break; + default: + _porthole = CountPortholes.TenPortholes; + 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.TenPortholes) { + 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.ThirtyPortholes) { + 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/src/ChoiseAndDrawingPortholes/DrawingPortholesHeart.java b/src/src/ChoiseAndDrawingPortholes/DrawingPortholesHeart.java new file mode 100644 index 0000000..556f244 --- /dev/null +++ b/src/src/ChoiseAndDrawingPortholes/DrawingPortholesHeart.java @@ -0,0 +1,14 @@ +package ChoiseAndDrawingPortholes; + +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); + } +} diff --git a/src/src/ChoiseAndDrawingPortholes/DrawingPortholesSquare.java b/src/src/ChoiseAndDrawingPortholes/DrawingPortholesSquare.java new file mode 100644 index 0000000..60b3e97 --- /dev/null +++ b/src/src/ChoiseAndDrawingPortholes/DrawingPortholesSquare.java @@ -0,0 +1,12 @@ +package ChoiseAndDrawingPortholes; + +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); + } +} diff --git a/src/src/ChoiseAndDrawingPortholes/IDifferentPortholes.java b/src/src/ChoiseAndDrawingPortholes/IDifferentPortholes.java new file mode 100644 index 0000000..3161db1 --- /dev/null +++ b/src/src/ChoiseAndDrawingPortholes/IDifferentPortholes.java @@ -0,0 +1,9 @@ +package ChoiseAndDrawingPortholes; + +import java.awt.*; + +public interface IDifferentPortholes { + public CountPortholes getCount(); + public void SetCount (int count); + public void Draw (Graphics2D g, int _startPosX, int _startPoxY); +} diff --git a/src/src/CountPortholes.java b/src/src/CountPortholes.java deleted file mode 100644 index a07155b..0000000 --- a/src/src/CountPortholes.java +++ /dev/null @@ -1,5 +0,0 @@ -public enum CountPortholes { - Ten, - Twenty, - Thirty; -} \ No newline at end of file diff --git a/src/src/DirectionType.java b/src/src/DirectionType.java deleted file mode 100644 index d3a2058..0000000 --- a/src/src/DirectionType.java +++ /dev/null @@ -1,6 +0,0 @@ -public enum DirectionType { - Up, - Down, - Left, - Right -} \ No newline at end of file diff --git a/src/src/DrawingAirbus.java b/src/src/DrawingAirbus.java deleted file mode 100644 index ad0031f..0000000 --- a/src/src/DrawingAirbus.java +++ /dev/null @@ -1,144 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.util.Random; - -public class DrawingAirbus extends JPanel { - public EntityAirbus EntityAirbus; - public DrawingPortholes _portholes = null; - private Integer picture_width; - private Integer picture_height; - private Integer _StartPosX; - private Integer _StartPosY; - private int drawingAirbusWidth = 185; - private int drawingAirbusHeight = 100; - public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean sheeppipes, boolean fueltank) { - EntityAirbus = new EntityAirbus(); - EntityAirbus.Init(speed, weight, bodycolor, additionalcolor, sheeppipes, fueltank); - picture_width = null; - picture_height = null; - _StartPosX = null; - _StartPosY = null; - _portholes = new DrawingPortholes(); - Random rand = new Random(); - int randomNum = rand.nextInt(4); - _portholes.SetCount(randomNum); - } - public boolean SetPictureSize(int width, int height) { - if (width < drawingAirbusWidth || height < drawingAirbusHeight) return false; - picture_width = width; - picture_height = height; - if (_StartPosX != null || _StartPosY != null) { - if (_StartPosX + drawingAirbusWidth > picture_width) - { - _StartPosX = _StartPosX - (_StartPosX + drawingAirbusWidth - picture_width); - } - else if (_StartPosX < 0) _StartPosX = 0; - if (_StartPosY + drawingAirbusHeight > picture_height) - { - _StartPosY = _StartPosY - (_StartPosY + drawingAirbusHeight - picture_height); - } - else if (_StartPosY < 0) _StartPosY = 0; - } - return true; - } - public void SetPosition(int x, int y) { - if (!(picture_width != null && picture_height != null)) return; - if (x + drawingAirbusWidth > picture_width) - { - _StartPosX = x - (x + drawingAirbusWidth - picture_width); - } - else if (x < 0) _StartPosX = 0; - else _StartPosX = x; - if (y + drawingAirbusHeight > picture_height) - { - _StartPosY = y - (y + drawingAirbusHeight - picture_height); - } - else if (y < 0) _StartPosY = 0; - else _StartPosY = y; - } - - public boolean MoveTransport(DirectionType direction) { - if (EntityAirbus == null || _StartPosX == null || _StartPosY == null) return false; - switch (direction) { - case Left: - if (_StartPosX - EntityAirbus.Step > 0) { - _StartPosX -= (int)EntityAirbus.Step; - } - return true; - case Up: - if (_StartPosY - EntityAirbus.Step > 0) - { - _StartPosY -= (int)EntityAirbus.Step; - } - return true; - case Right: - if (_StartPosX + drawingAirbusWidth + (int)EntityAirbus.Step < picture_width - EntityAirbus.Step) - { - _StartPosX += (int)EntityAirbus.Step; - } - return true; - case Down: - if (_StartPosY + drawingAirbusHeight + (int)EntityAirbus.Step < picture_height - EntityAirbus.Step) - { - _StartPosY += (int)EntityAirbus.Step; - } - return true; - default: - return false; - } - } - - public void DrawTransport(Graphics2D g) { - - if (EntityAirbus == null) { - return; - } - // иллюминаторы - _portholes.Draw(g, _StartPosX+30, _StartPosY+34); - - g.setColor(Color.BLACK); -//Тело - g.drawRect(_StartPosX + 5, _StartPosY + 50, 170, 30); - g.drawArc(_StartPosX - 5, _StartPosY + 50, 20, 30, 90, 180); -//Заднее крыло - g.drawLine( _StartPosX, _StartPosY, _StartPosX + 50, _StartPosY + 50); - g.drawLine( _StartPosX, _StartPosY, _StartPosX, _StartPosY + 52); -//Заднее боковые крылья - g.drawOval(_StartPosX - 7, _StartPosY + 45, 30, 8); - 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.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); - g.drawLine( _StartPosX + 51, _StartPosY + 67, _StartPosX + 140, _StartPosY + 67); -//Задние шасси - 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.drawLine( _StartPosX + 165, _StartPosY + 80, _StartPosX + 165, _StartPosY + 90); - g.drawOval( _StartPosX + 163, _StartPosY + 91, 5, 5); -//Пассажирсакий доп. отсек - if (EntityAirbus.getIsCompartmen()) - { - 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.getIsAdditionalEngine()) - { - 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); - } - } -} diff --git a/src/src/DrawingPortholes.java b/src/src/DrawingPortholes.java deleted file mode 100644 index 81fd49d..0000000 --- a/src/src/DrawingPortholes.java +++ /dev/null @@ -1,63 +0,0 @@ -import java.awt.*; - -public class DrawingPortholes { - private CountPortholes _porthole; - - public CountPortholes getCount() { - return _porthole; - } - - public void SetCount(int count) { - switch (count) { - case 1: // 10 - _porthole = CountPortholes.Ten; - break; - case 2: // 20 - _porthole = CountPortholes.Twenty; - break; - case 3: // 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/src/CanvasAirbus.java b/src/src/Drawings/CanvasAirbus.java similarity index 62% rename from src/src/CanvasAirbus.java rename to src/src/Drawings/CanvasAirbus.java index 7768d8a..e41b039 100644 --- a/src/src/CanvasAirbus.java +++ b/src/src/Drawings/CanvasAirbus.java @@ -1,16 +1,20 @@ +package Drawings; + +import Drawings.DrawingAirbus; + import javax.swing.*; import java.awt.*; public class CanvasAirbus extends JComponent { - public DrawingAirbus _drawingAirbus; + public DrawingAirplan _drawingAirplan; public CanvasAirbus(){} public void paintComponent(Graphics g) { - if (_drawingAirbus == null) { + if (_drawingAirplan == null) { return; } super.paintComponents(g); Graphics2D g2d = (Graphics2D) g; - _drawingAirbus.DrawTransport(g2d); + _drawingAirplan.DrawTransport(g2d); super.repaint(); } } \ No newline at end of file diff --git a/src/src/Drawings/DrawingAirbus.java b/src/src/Drawings/DrawingAirbus.java new file mode 100644 index 0000000..251ce29 --- /dev/null +++ b/src/src/Drawings/DrawingAirbus.java @@ -0,0 +1,42 @@ +package Drawings; + +import Drawings.DrawingAirplan; +import Entities.EntityAirbus; + +import java.awt.*; + +public class DrawingAirbus extends DrawingAirplan { + + public DrawingAirbus(int speed, int weight, Color bodyColor, int countPortholes, Color additionalColor, boolean isCompartment, boolean isAdditionalEngine, int width, int height) { + super(speed, weight, bodyColor, countPortholes, width, height); + if (entityAirplan != null) { + entityAirplan = new EntityAirbus(speed, weight, bodyColor, additionalColor, isCompartment, isAdditionalEngine); + } + } + + @Override + public void DrawTransport(Graphics2D g) { + + if (entityAirplan == null) { + return; + } + + Color additionalColor = ((EntityAirbus) entityAirplan).getAdditionalColor(); + //Пассажирсакий доп. отсек + if (((EntityAirbus) entityAirplan).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 (((EntityAirbus) entityAirplan).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); + } + } +} \ No newline at end of file diff --git a/src/src/Drawings/DrawingAirplan.java b/src/src/Drawings/DrawingAirplan.java new file mode 100644 index 0000000..2bd8b1b --- /dev/null +++ b/src/src/Drawings/DrawingAirplan.java @@ -0,0 +1,167 @@ +package Drawings; + +import Entities.*; +import ChoiseAndDrawingPortholes.*; +import MovementStrategy.Direction; + +import javax.swing.*; +import java.awt.*; +import java.util.Random; + +public class DrawingAirplan { + + public EntityAirplan entityAirplan; + public IDifferentPortholes _portholes; + private int _pictureWidth; + private int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + private int _airbusWidth = 210; + private int _airbusHeight = 100; + + public int GetPosX() { + return _startPosX; + } + + public int GetPosY() { + return _startPosY; + } + + public int GetWidth() { + return _airbusWidth; + } + + public int GetHeight() { + return _airbusHeight; + } + + public DrawingAirplan(int speed, float weight, Color bodyColor, int countPortholes, int width, int height) { + if (width < _airbusHeight || height < _airbusWidth) + return; + _pictureWidth = width; + _pictureHeight = height; + entityAirplan = new EntityAirplan(speed, weight, bodyColor); + + 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); + } + + public void SetPosition(int x, int y) { + if (x > _pictureWidth || y > _pictureHeight || x < 0 || y < 0) { + _startPosX = 0; + _startPosY = 0; + } else { + _startPosX = x; + _startPosY = y; + } + } + + public boolean CanMove(Direction direction) { + if (entityAirplan == null) { + return false; + } + switch (direction) { + case Left: + return _startPosX - entityAirplan.Step > 5; + case Right: + return _startPosX + _airbusWidth + entityAirplan.Step < _pictureWidth; + case Up: + return _startPosY - entityAirplan.Step > 0; + case Down: + return _startPosY + _airbusHeight + entityAirplan.Step < _pictureHeight; + default: + return false; + } + } + + public void MoveTransport(Direction direction) { + if (!CanMove(direction) || entityAirplan == null) { + return; + } + + switch (direction) { + case Left: + _startPosX -= entityAirplan.Step; + break; + case Right: + _startPosX += entityAirplan.Step; + break; + case Up: + _startPosY -= entityAirplan.Step; + break; + case Down: + _startPosY += entityAirplan.Step; + break; + } + } + + public void DrawTransport(Graphics2D g) { + + if (entityAirplan == null) { + return; + } + + + g.setColor(Color.BLACK); +//Тело + g.setColor(entityAirplan.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.setColor(Color.BLACK); + g.drawPolygon(new int[]{_startPosX + 50, _startPosX, _startPosX}, new int[]{_startPosY + 50, _startPosY, _startPosY + 50}, 3); + g.setColor(entityAirplan.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(entityAirplan.getBodyColor()); + g.fillOval(_startPosX - 7, _startPosY + 45, 30, 8); +//Нос + 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(entityAirplan.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); + g.drawLine(_startPosX + 51, _startPosY + 67, _startPosX + 140, _startPosY + 67); +//Задние шасси + 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(entityAirplan.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); + g.setColor(entityAirplan.getBodyColor()); + g.fillOval(_startPosX + 163, _startPosY + 91, 5, 5); + } +} + diff --git a/src/src/Entities/EntityAirbus.java b/src/src/Entities/EntityAirbus.java new file mode 100644 index 0000000..cb9c48c --- /dev/null +++ b/src/src/Entities/EntityAirbus.java @@ -0,0 +1,19 @@ +package Entities; + +import java.awt.*; +public class EntityAirbus extends EntityAirplan { + private Color AdditionalColor; + private boolean IsCompartment; + private boolean IsAdditionalEngine; + + public EntityAirbus(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/src/Entities/EntityAirplan.java b/src/src/Entities/EntityAirplan.java new file mode 100644 index 0000000..4207fce --- /dev/null +++ b/src/src/Entities/EntityAirplan.java @@ -0,0 +1,29 @@ +package Entities; + +import java.awt.*; +public class EntityAirplan { + private int Speed; + private float Weight; + private Color BodyColor; + public float Step; + + public int getSpeed() { + return Speed; + } + public float getWeight() { + return Weight; + } + public Color getBodyColor() { + return BodyColor; + } + + + public EntityAirplan(int speed, float weight, Color bodyColor) + { + Weight = weight; + Speed = speed; + BodyColor = bodyColor; + Step = Speed * 200 / (int) Weight; + } +} + diff --git a/src/src/EntityAirbus.java b/src/src/EntityAirbus.java deleted file mode 100644 index de7f62a..0000000 --- a/src/src/EntityAirbus.java +++ /dev/null @@ -1,24 +0,0 @@ -import java.awt.*; -public class EntityAirbus { - private int Speed; - private double Weight; - private Color BodyColor; - public Color getBodyColor() {return BodyColor;} - private Color AdditionalColor; - public Color getAdditionalColor() {return AdditionalColor;} - private boolean IsCompartment; - public boolean getIsCompartmen() {return IsCompartment;} - private boolean IsAdditionalEngine; - public boolean getIsAdditionalEngine() {return IsAdditionalEngine;} - public double Step; - public void Init(int speed, double weight, Color bodycolor, Color additionalcolor, boolean isCompartment, boolean isAdditionalEngine) - { - Speed = speed; - Weight = weight; - BodyColor = bodycolor; - AdditionalColor = additionalcolor; - IsCompartment = isCompartment; - IsAdditionalEngine = isAdditionalEngine; - Step = Speed * 100 / Weight; - } -} diff --git a/src/src/FormAirbus.java b/src/src/FormAirbus.java index e35cfe6..8006d4b 100644 --- a/src/src/FormAirbus.java +++ b/src/src/FormAirbus.java @@ -1,123 +1,250 @@ +import Drawings.DrawingAirbus; +import Drawings.DrawingAirplan; +import MovementStrategy.AbstractStrategy; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; +import java.awt.event.*; import java.util.Random; +import MovementStrategy.*; public class FormAirbus extends JFrame { - private String title; - private Dimension dimension; - private int Width, Height; - private CanvasAirbus canvasAirbus = new CanvasAirbus(); - private JButton CreateButton = new JButton("Create");; - private JButton UpButton = new JButton(); - private JButton DownButton = new JButton();; - private JButton LeftButton = new JButton();; - private JButton RightButton = new JButton(); - public FormAirbus(String title, Dimension dimension) { - this.title = title; - this.dimension = dimension; - } - public void Init() { - setTitle(title); - setMinimumSize(dimension); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - Width = getWidth() - 15; - Height = getHeight() - 35; + private int width; + private int height; + private DrawingAirplan _drawingAirbus; + private AbstractStrategy _abstractStrategy; + private Canvas canvas; - CreateButton.setName("CREATE"); - Icon iconUp = new ImageIcon("src\\image\\up.jpg"); - UpButton.setIcon(iconUp); - UpButton.setName("UP"); - DownButton.setName("DOWN"); - Icon iconDown = new ImageIcon("src\\image\\down.jpg"); - DownButton.setIcon(iconDown); - LeftButton.setName("LEFT"); - Icon iconLeft = new ImageIcon("src\\image\\left.jpg"); - LeftButton.setIcon(iconLeft); - RightButton.setName("RIGHT"); - Icon iconRight = new ImageIcon("src\\image\\right.jpg"); - RightButton.setIcon(iconRight); + // выбор кол-ва иллюминаторов + JLabel labelCount; + private JTextField fieldCount; - 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();; - canvasAirbus._drawingAirbus = new DrawingAirbus(); - canvasAirbus._drawingAirbus.Init(speed, weight, bodyColor, additionalColor, sheepPipes, fuelTank); - canvasAirbus._drawingAirbus.SetPictureSize(Width, Height); - canvasAirbus._drawingAirbus.SetPosition( StartPositionX, StartPositionY); - canvasAirbus.repaint(); - } - }); + // выбор стратегии + JLabel labelStrategy; + JComboBox comboBoxStrategy; + JButton buttonStrategy; - ActionListener actionListener = new ActionListener() { - @Override - public void actionPerformed(ActionEvent event) { - if (canvasAirbus._drawingAirbus == null) return; - boolean result = false; - switch ((((JButton)(event.getSource())).getName())) { - case "UP": - result = canvasAirbus._drawingAirbus.MoveTransport(DirectionType.Up); - break; - case "DOWN": - result = canvasAirbus._drawingAirbus.MoveTransport(DirectionType.Down); - break; - case "LEFT": - result = canvasAirbus._drawingAirbus.MoveTransport(DirectionType.Left); - break; - case "RIGHT": - result = canvasAirbus._drawingAirbus.MoveTransport(DirectionType.Right); - break; - } - if (result) { - canvasAirbus.repaint(); - } - } - }; - UpButton.addActionListener(actionListener); - DownButton.addActionListener(actionListener); - LeftButton.addActionListener(actionListener); - RightButton.addActionListener(actionListener); + private JButton buttonCreateAirbus; + private JButton buttonCreatePlane; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonRight; + private JButton buttonLeft; - setSize(dimension.width,dimension.height); - setLayout(null); - canvasAirbus.setBounds(0,0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 100, 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); - add(CreateButton); - add(UpButton); - add(DownButton); - add(RightButton); - add(LeftButton); - add(canvasAirbus); + public FormAirbus() { + super("Создание самолёта"); + InitializeComponent(); setVisible(true); - //обработка события изменения размеров окна - addComponentListener(new ComponentAdapter() { - public void componentResized(ComponentEvent e) { - Width = getWidth() - 15; - Height = getHeight() - 35; - if (canvasAirbus._drawingAirbus != null) - canvasAirbus._drawingAirbus.SetPictureSize(Width, Height); - canvasAirbus.setBounds(0,0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 100, 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); - } - }); } -} + + private void InitializeComponent() { + canvas = new Canvas(); + + labelCount = new JLabel("Введите число иллюминаторов:"); + fieldCount = new JTextField(); + + labelStrategy = new JLabel("Шаг стратегии:"); + comboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"}); + 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")); + + buttonRight = new JButton(); + buttonRight.setName("right"); + buttonRight.setIcon(new ImageIcon("images\\KeyRight.png")); + + buttonLeft = new JButton(); + buttonLeft.setName("left"); + buttonLeft.setIcon(new ImageIcon("images\\KeyLeft.png")); + + buttonDown = new JButton(); + buttonDown.setName("down"); + buttonDown.setIcon(new ImageIcon("images\\KeyDown.png")); + + setSize(800, 500); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(null); + + 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, 790, 460); + + add(buttonCreateAirbus); + add(buttonCreatePlane); + add(labelCount); + add(fieldCount); + add(labelStrategy); + add(comboBoxStrategy); + add(buttonStrategy); + add(buttonUp); + add(buttonRight); + add(buttonDown); + add(buttonLeft); + add(labelCount); + add(fieldCount); + add(canvas); + + // логика формы + buttonCreateAirbus.addActionListener(buttonCreateAirbusListener); + buttonCreatePlane.addActionListener(buttonCreatePlaneListener); + buttonStrategy.addActionListener(buttonStrategyListener); + buttonUp.addActionListener(buttonsMoveListener); + buttonRight.addActionListener(buttonsMoveListener); + buttonDown.addActionListener(buttonsMoveListener); + buttonLeft.addActionListener(buttonsMoveListener); + } + + ActionListener buttonCreateAirbusListener = 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 DrawingAirplan(rand.nextInt(200) + 100, rand.nextInt(2000) + 1000, + new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)), + countPortholes, + canvas.getWidth(), canvas.getHeight()); + + _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 DrawingAirbus(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 MoveableAirplan(_drawingAirbus), canvas.getWidth(), canvas.getHeight()); + comboBoxStrategy.setEnabled(false); + } + if (_abstractStrategy == null) { + return; + } + _abstractStrategy.MakeStep(); + if (_abstractStrategy.GetStatus() == StrategyStatus.Finish) { + comboBoxStrategy.setEnabled(true); + _abstractStrategy = null; + } + canvas.repaint(); + } + }; + + ActionListener buttonsMoveListener = new ActionListener() { + // реакция на нажатие + public void actionPerformed(ActionEvent e) { + if (_drawingAirbus == null) { + return; + } + String command = ((JButton) (e.getSource())).getName(); + switch (command) { + case "up": + _drawingAirbus.MoveTransport(Direction.Up); + break; + case "down": + _drawingAirbus.MoveTransport(Direction.Down); + break; + case "right": + _drawingAirbus.MoveTransport(Direction.Right); + break; + case "left": + _drawingAirbus.MoveTransport(Direction.Left); + break; + } + canvas.repaint(); + } + }; + + class Canvas extends JComponent { + public Canvas() { + } + + public void paintComponent(Graphics g) { + if (_drawingAirbus == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + _drawingAirbus.DrawTransport(g2d); + super.repaint(); + } + } +} \ No newline at end of file diff --git a/src/src/Main.java b/src/src/Main.java index 2871a6b..c61bce0 100644 --- a/src/src/Main.java +++ b/src/src/Main.java @@ -2,7 +2,6 @@ import java.awt.*; public class Main { public static void main(String[] args) { - FormAirbus form = new FormAirbus("Самолёт", new Dimension(500, 500)); - form.Init(); + new FormAirbus(); } } \ No newline at end of file diff --git a/src/src/MovementStrategy/AbstractStrategy.java b/src/src/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..c9f62a0 --- /dev/null +++ b/src/src/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,76 @@ +package MovementStrategy; + +public abstract class AbstractStrategy { + private IMoveableObject _moveableObject; + private StrategyStatus _state = StrategyStatus.NotInit; + protected int FieldWidth; + protected int FieldHeight; + public StrategyStatus GetStatus() { return _state; } + + // Изменить статус, установить поля + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + // сделать шаг + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + if (IsTargetDestination()) + { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + + // перемещения + protected boolean MoveLeft() { return MoveTo(Direction.Up); } + 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 != StrategyStatus.InProgress) + { + return 0; + } + return _moveableObject.GetStep(); + } + // перемещение + protected abstract void MoveToTarget(); + + // достигнута ли цель + protected abstract boolean IsTargetDestination(); + + // попытка перемещения по направлению + private boolean MoveTo(Direction directionType) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + if (_moveableObject.CheckCanMove(directionType)) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } +} diff --git a/src/src/MovementStrategy/Direction.java b/src/src/MovementStrategy/Direction.java new file mode 100644 index 0000000..9a4a107 --- /dev/null +++ b/src/src/MovementStrategy/Direction.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public enum Direction { + Up, + Down, + Left, + Right; +} diff --git a/src/src/MovementStrategy/IMoveableObject.java b/src/src/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..ffd427e --- /dev/null +++ b/src/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/src/MovementStrategy/MoveToBorder.java b/src/src/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..3741cb8 --- /dev/null +++ b/src/src/MovementStrategy/MoveToBorder.java @@ -0,0 +1,45 @@ +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/src/MovementStrategy/MoveToCenter.java b/src/src/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..82726ac --- /dev/null +++ b/src/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(); + } + } + } +} \ No newline at end of file diff --git a/src/src/MovementStrategy/MoveableAirplan.java b/src/src/MovementStrategy/MoveableAirplan.java new file mode 100644 index 0000000..ddc103b --- /dev/null +++ b/src/src/MovementStrategy/MoveableAirplan.java @@ -0,0 +1,33 @@ +package MovementStrategy; + +import Drawings.DrawingAirplan; + +public class MoveableAirplan implements IMoveableObject { + private DrawingAirplan _drawingAirplan = null; + + public MoveableAirplan(DrawingAirplan drawingAirplan) + { + _drawingAirplan = drawingAirplan; + } + + public ObjectParameters GetObjectPosition() + { + if (_drawingAirplan == null || _drawingAirplan.entityAirplan == null) + { + return null; + } + return new ObjectParameters(_drawingAirplan.GetPosX(), _drawingAirplan.GetPosY(), _drawingAirplan.GetWidth(), _drawingAirplan.GetHeight()); + } + + public int GetStep() { return (int)_drawingAirplan.entityAirplan.Step; } + + @Override + public boolean CheckCanMove(Direction direction) { + return _drawingAirplan.CanMove(direction); + } + + @Override + public void MoveObject(Direction direction) { + _drawingAirplan.MoveTransport(direction); + } +} \ No newline at end of file diff --git a/src/src/MovementStrategy/ObjectParameters.java b/src/src/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..a741f5a --- /dev/null +++ b/src/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/src/MovementStrategy/StrategyStatus.java b/src/src/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..4f087ea --- /dev/null +++ b/src/src/MovementStrategy/StrategyStatus.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum StrategyStatus { + NotInit, + InProgress, + Finish +}