From 984fbdc7da0cfb3c6300835fa96065314331db37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D0=BE=D0=BD=D0=B8=D0=B4?= Date: Fri, 24 Nov 2023 16:43:36 +0400 Subject: [PATCH] lab2 --- .idea/uiDesigner.xml | 124 ++++++++++++ src/Block.java | 22 --- src/CruiserHard/Complication/Block.java | 8 + .../Complication}/DrawBlock.java | 36 ++-- .../Complication/DrawBlockOval.java | 50 +++++ .../Complication/DrawBlockTriangle.java | 61 ++++++ src/CruiserHard/Complication/IDrawBlock.java | 7 + .../Direction}/Direction.java | 2 + .../Drawing}/DrawingCruiser.java | 65 ++++--- .../Drawing/DrawingHelipadCruiser.java | 52 ++++++ src/CruiserHard/Entity/EntityCruiser.java | 26 +++ .../Entity/EntityHelipadCruiser.java | 23 +++ src/CruiserHard/FormCruiser.java | 176 ++++++++++++++++++ src/{ => CruiserHard}/Images/todown.png | Bin src/{ => CruiserHard}/Images/toleft.png | Bin src/{ => CruiserHard}/Images/toright.png | Bin src/{ => CruiserHard}/Images/totop.png | Bin src/{ => CruiserHard}/Main.java | 1 + .../MovementStrategy/AbstractStrategy.java | 91 +++++++++ .../DrawingObjectCruiser.java | 39 ++++ .../MovementStrategy/IMoveableObject.java | 12 ++ .../MovementStrategy/MoveToBorder.java | 34 ++++ .../MovementStrategy/MoveToCenter.java | 38 ++++ .../MovementStrategy/ObjectParameters.java | 33 ++++ src/CruiserHard/MovementStrategy/Status.java | 6 + src/EntityCruiser.java | 31 --- src/FormCruiser.java | 111 ----------- 27 files changed, 848 insertions(+), 200 deletions(-) create mode 100644 .idea/uiDesigner.xml delete mode 100644 src/Block.java create mode 100644 src/CruiserHard/Complication/Block.java rename src/{ => CruiserHard/Complication}/DrawBlock.java (53%) create mode 100644 src/CruiserHard/Complication/DrawBlockOval.java create mode 100644 src/CruiserHard/Complication/DrawBlockTriangle.java create mode 100644 src/CruiserHard/Complication/IDrawBlock.java rename src/{ => CruiserHard/Direction}/Direction.java (87%) rename src/{ => CruiserHard/Drawing}/DrawingCruiser.java (73%) create mode 100644 src/CruiserHard/Drawing/DrawingHelipadCruiser.java create mode 100644 src/CruiserHard/Entity/EntityCruiser.java create mode 100644 src/CruiserHard/Entity/EntityHelipadCruiser.java create mode 100644 src/CruiserHard/FormCruiser.java rename src/{ => CruiserHard}/Images/todown.png (100%) rename src/{ => CruiserHard}/Images/toleft.png (100%) rename src/{ => CruiserHard}/Images/toright.png (100%) rename src/{ => CruiserHard}/Images/totop.png (100%) rename src/{ => CruiserHard}/Main.java (83%) create mode 100644 src/CruiserHard/MovementStrategy/AbstractStrategy.java create mode 100644 src/CruiserHard/MovementStrategy/DrawingObjectCruiser.java create mode 100644 src/CruiserHard/MovementStrategy/IMoveableObject.java create mode 100644 src/CruiserHard/MovementStrategy/MoveToBorder.java create mode 100644 src/CruiserHard/MovementStrategy/MoveToCenter.java create mode 100644 src/CruiserHard/MovementStrategy/ObjectParameters.java create mode 100644 src/CruiserHard/MovementStrategy/Status.java delete mode 100644 src/EntityCruiser.java delete mode 100644 src/FormCruiser.java 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/Block.java b/src/Block.java deleted file mode 100644 index 3ee5968..0000000 --- a/src/Block.java +++ /dev/null @@ -1,22 +0,0 @@ -public enum Block { - One(1), - Two(2), - Three(3); - - Block(int i) { - } - - public static Block GetBlock(int i){ - switch (i) - { - case 1: - return One; - case 2: - return Two; - case 3: - return Three; - } - - return null; - } -} \ No newline at end of file diff --git a/src/CruiserHard/Complication/Block.java b/src/CruiserHard/Complication/Block.java new file mode 100644 index 0000000..97a3e2f --- /dev/null +++ b/src/CruiserHard/Complication/Block.java @@ -0,0 +1,8 @@ +package CruiserHard.Complication; + +public enum Block { + One(1), + Two(2), + Three(3); + Block(int i) {} +} diff --git a/src/DrawBlock.java b/src/CruiserHard/Complication/DrawBlock.java similarity index 53% rename from src/DrawBlock.java rename to src/CruiserHard/Complication/DrawBlock.java index 8fe9ed0..9dcdd8f 100644 --- a/src/DrawBlock.java +++ b/src/CruiserHard/Complication/DrawBlock.java @@ -1,28 +1,40 @@ +package CruiserHard.Complication; import java.awt.*; - -public class DrawBlock { +public class DrawBlock implements IDrawBlock{ private Block blockCount; - - public void SetBlockCount(int count){ - blockCount = Block.GetBlock(count); + @Override + public void SetBlockCount(int num) { + switch (num){ + case 1: + blockCount = Block.One; + break; + case 2: + blockCount = Block.Two; + break; + default: + blockCount = Block.Three; + } } - - public void DrawningBlock(int _startPosX, int _startPosY, int _cruiserWidth, Graphics2D g2d, Color bodyColor){ + @Override + public Block GetBlockCount() { + return blockCount; + } + public void DrawBlocks(int _startPosX, int _startPosY,Graphics g2d){ switch (blockCount) { case One: break; case Two: - DrawFirstDop(_startPosX,_startPosY,_cruiserWidth,g2d,bodyColor); + DrawFirstDop(_startPosX,_startPosY,g2d); break; case Three: - DrawSecondDop(_startPosX,_startPosY,_cruiserWidth,g2d,bodyColor); + DrawSecondDop(_startPosX,_startPosY,g2d); break; } } - public void DrawFirstDop(int _startPosX, int _startPosY, int _cruiserWidth, Graphics2D g2d, Color bodyColor) { + public void DrawFirstDop(int _startPosX, int _startPosY, Graphics g2d) { Polygon elementsSec = new Polygon(); elementsSec.addPoint(_startPosX + 55,_startPosY + 25); elementsSec.addPoint(_startPosX + 65,_startPosY + 25); @@ -31,8 +43,8 @@ public class DrawBlock { g2d.setColor(Color.WHITE); g2d.fillPolygon(elementsSec); } - public void DrawSecondDop(int _startPosX, int _startPosY, int _cruiserWidth, Graphics2D g2d, Color bodyColor) { - DrawFirstDop(_startPosX,_startPosY,_cruiserWidth,g2d,bodyColor); + public void DrawSecondDop(int _startPosX, int _startPosY, Graphics g2d) { + DrawFirstDop(_startPosX,_startPosY,g2d); Polygon elementsThree = new Polygon(); elementsThree.addPoint(_startPosX + 75,_startPosY + 15); elementsThree.addPoint(_startPosX + 85,_startPosY + 15); diff --git a/src/CruiserHard/Complication/DrawBlockOval.java b/src/CruiserHard/Complication/DrawBlockOval.java new file mode 100644 index 0000000..97a979f --- /dev/null +++ b/src/CruiserHard/Complication/DrawBlockOval.java @@ -0,0 +1,50 @@ +package CruiserHard.Complication; +import java.awt.*; + +public class DrawBlockOval implements IDrawBlock{ + private Block blockCount; + @Override + public void SetBlockCount(int num) { + switch (num){ + case 1: + blockCount = Block.One; + break; + case 2: + blockCount = Block.Two; + break; + default: + blockCount = Block.Three; + } + } + + @Override + public Block GetBlockCount() { + return blockCount; + } + + @Override + public void DrawBlocks(int _startPosX, int _startPosY,Graphics g2d) { + switch (blockCount) + { + case One: + break; + + case Two: + DrawFirstDopOval(_startPosX,_startPosY,g2d); + break; + + case Three: + DrawSecondDopOval(_startPosX,_startPosY,g2d); + break; + } + } + public void DrawFirstDopOval(int _startPosX, int _startPosY, Graphics g2d) { + g2d.setColor(Color.WHITE); + g2d.fillOval(_startPosX + 55,_startPosY + 25,10,10); + } + public void DrawSecondDopOval(int _startPosX, int _startPosY, Graphics g2d) { + DrawFirstDopOval(_startPosX,_startPosY,g2d); + g2d.setColor(Color.WHITE); + g2d.fillOval(_startPosX + 75,_startPosY + 15, 10, 30); + } +} \ No newline at end of file diff --git a/src/CruiserHard/Complication/DrawBlockTriangle.java b/src/CruiserHard/Complication/DrawBlockTriangle.java new file mode 100644 index 0000000..5a09aa7 --- /dev/null +++ b/src/CruiserHard/Complication/DrawBlockTriangle.java @@ -0,0 +1,61 @@ +package CruiserHard.Complication; + +import java.awt.*; + +public class DrawBlockTriangle implements IDrawBlock { + private Block blockCount; + + @Override + public void SetBlockCount(int num) { + switch (num) { + case 1: + blockCount = Block.One; + break; + case 2: + blockCount = Block.Two; + break; + default: + blockCount = Block.Three; + } + } + + @Override + public Block GetBlockCount() { + return blockCount; + } + + @Override + public void DrawBlocks(int _startPosX, int _startPosY, Graphics g2d) { + switch (blockCount) { + case One: + break; + + case Two: + DrawFirstDopTriangle(_startPosX, _startPosY, g2d); + break; + + case Three: + DrawSecondDopTriangle(_startPosX, _startPosY, g2d); + break; + } + } + + public void DrawFirstDopTriangle(int _startPosX, int _startPosY, Graphics g2d) { + Polygon elementsSec = new Polygon(); + elementsSec.addPoint(_startPosX + 55, _startPosY + 25); + elementsSec.addPoint(_startPosX + 65, _startPosY + 30); + elementsSec.addPoint(_startPosX + 55, _startPosY + 35); + g2d.setColor(Color.WHITE); + g2d.fillPolygon(elementsSec); + } + + public void DrawSecondDopTriangle(int _startPosX, int _startPosY, Graphics g2d) { + DrawFirstDopTriangle(_startPosX, _startPosY, g2d); + Polygon elementsThree = new Polygon(); + elementsThree.addPoint(_startPosX + 75, _startPosY + 15); + elementsThree.addPoint(_startPosX + 85, _startPosY + 30); + elementsThree.addPoint(_startPosX + 75, _startPosY + 45); + g2d.setColor(Color.WHITE); + g2d.fillPolygon(elementsThree); + } +} \ No newline at end of file diff --git a/src/CruiserHard/Complication/IDrawBlock.java b/src/CruiserHard/Complication/IDrawBlock.java new file mode 100644 index 0000000..0d31b6c --- /dev/null +++ b/src/CruiserHard/Complication/IDrawBlock.java @@ -0,0 +1,7 @@ +package CruiserHard.Complication; +import java.awt.*; +public interface IDrawBlock { + void SetBlockCount(int number); + Block GetBlockCount(); + void DrawBlocks(int _startPosX, int _startPosY,Graphics g2d); +} \ No newline at end of file diff --git a/src/Direction.java b/src/CruiserHard/Direction/Direction.java similarity index 87% rename from src/Direction.java rename to src/CruiserHard/Direction/Direction.java index ac5e0ba..8bfa4db 100644 --- a/src/Direction.java +++ b/src/CruiserHard/Direction/Direction.java @@ -1,3 +1,5 @@ +package CruiserHard.Direction; + public enum Direction { Up(1), Down(2), diff --git a/src/DrawingCruiser.java b/src/CruiserHard/Drawing/DrawingCruiser.java similarity index 73% rename from src/DrawingCruiser.java rename to src/CruiserHard/Drawing/DrawingCruiser.java index 2e69ea4..7b162b5 100644 --- a/src/DrawingCruiser.java +++ b/src/CruiserHard/Drawing/DrawingCruiser.java @@ -1,26 +1,41 @@ +package CruiserHard.Drawing; + +import CruiserHard.Direction.Direction; +import CruiserHard.Entity.EntityCruiser; + import javax.swing.*; import java.awt.*; import java.awt.geom.Ellipse2D; import java.util.*; -public class DrawingCruiser { - private EntityCruiser cruiser; //Класс-сущность - public EntityCruiser GetCruiser(){return cruiser;} +public class DrawingCruiser extends JPanel { + public EntityCruiser cruiser; //Класс-сущность public int _startPosX; //Координаты отрисовки по оси x + public EntityCruiser GetCruiser(){return cruiser;} + public int GetStartX() { + return _startPosX; + } public int _startPosY; //Координаты отрисовки по оси y + public int GetStartY() { + return _startPosY; + } private Integer _pictureWidth = null; //Ширина окна private Integer _pictureHeight = null; //Высота окна private final int _cruiserWidth = 150; //Ширина отрисовки крейсера + public int GetWidth() { + return _cruiserWidth; + } private final int _cruiserHeight = 60; //Высота отрисовки крейсера - private DrawBlock drawBlock = new DrawBlock(); + public int GetHeight() { + return _cruiserHeight; + } //Инициализация - public void Init(int speed, float weight, Color bodyColor, boolean rocketMines, boolean helipad) - { - cruiser = new EntityCruiser(); - cruiser.Init(speed,weight,bodyColor, rocketMines, helipad); - Random random = new Random(); - drawBlock.SetBlockCount(random.nextInt(1, 4)); + public DrawingCruiser(int speed, float weight, Color bodyColor, int width, int height, int blockCount) { + cruiser = new EntityCruiser(speed,weight,bodyColor); + _pictureWidth = width; + _pictureHeight = height; + } //Начальные коордитанты @@ -56,7 +71,7 @@ public class DrawingCruiser { } //Отрисовка транспорта - public void DrawTransport(Graphics g) + public void DrawTransport(Graphics gr) { if (GetCruiser() == null) return; @@ -64,7 +79,8 @@ public class DrawingCruiser { { return; } - Graphics2D g2d = (Graphics2D) g; + super.paintComponent(gr); + Graphics2D g2d = (Graphics2D) gr; g2d.setColor(cruiser.GetBodyColor()); // палуба Polygon paluba = new Polygon(); @@ -87,22 +103,23 @@ public class DrawingCruiser { g2d.setColor(Color.BLACK); g2d.fillPolygon(elements); g2d.fill(new Ellipse2D.Double(_startPosX + 100, _startPosY + 20, 20, 20)); - // турбины g2d.setColor(Color.BLACK); g2d.fillRect(_startPosX, _startPosY + 10, 10, 20); g2d.fillRect(_startPosX, _startPosY + 35, 10, 20); - // шахты - if (cruiser.GetRocketMines()) { - g2d.setColor(Color.green); - g2d.fillRect(_startPosX + 15, _startPosY + 10, 10, 15); - g2d.fillRect(_startPosX + 30, _startPosY + 10, 10, 15); - } - // верт площадка - if (cruiser.GetHelipad()) { - g2d.setColor(Color.ORANGE); - g2d.fill(new Ellipse2D.Double(_startPosX + 15, _startPosY + 25, 25, 25)); - drawBlock.DrawningBlock(_startPosX, _startPosY, _cruiserWidth, g2d, cruiser.GetBodyColor()); + + } + + //Изменение границ отрисовки + public boolean CanMove(Direction direction) { + if (cruiser == null) { + return false; } + return switch (direction) { + case Left -> _startPosX - cruiser.GetStep() > 0; + case Up -> _startPosY - cruiser.GetStep() > 0; + case Right -> _startPosX + _cruiserWidth + cruiser.GetStep() < _pictureWidth; + case Down -> _startPosY + _cruiserHeight + cruiser.GetStep() < _pictureHeight; + }; } } \ No newline at end of file diff --git a/src/CruiserHard/Drawing/DrawingHelipadCruiser.java b/src/CruiserHard/Drawing/DrawingHelipadCruiser.java new file mode 100644 index 0000000..932bb33 --- /dev/null +++ b/src/CruiserHard/Drawing/DrawingHelipadCruiser.java @@ -0,0 +1,52 @@ +package CruiserHard.Drawing; +import java.awt.*; +import java.util.Random; + +import CruiserHard.Complication.DrawBlock; +import CruiserHard.Complication.DrawBlockOval; +import CruiserHard.Complication.DrawBlockTriangle; +import CruiserHard.Complication.IDrawBlock; + +import CruiserHard.Entity.*; + +public class DrawingHelipadCruiser extends DrawingCruiser { + private IDrawBlock drawingBlocks; + public DrawingHelipadCruiser(int speed, float weight, Color bodyColor, Color + elementsColor, boolean rocketMines, boolean helipad, int width, int height, int blockCount) + { + super(speed, weight, bodyColor, width, height, blockCount); + if (cruiser != null) + { + cruiser = new EntityHelipadCruiser(speed, weight, bodyColor, + elementsColor, rocketMines, helipad); + } + Random rand = new Random(); + drawingBlocks = switch (rand.nextInt(3)) { + case 0 -> new DrawBlock(); + case 1 -> new DrawBlockOval(); + case 2 -> new DrawBlockTriangle(); + default -> new DrawBlock(); + }; + + drawingBlocks.SetBlockCount(blockCount); + } + @Override + public void DrawTransport(Graphics g) { + if (!(cruiser instanceof EntityHelipadCruiser helipadCrusier)) { + return; + } + super.DrawTransport(g); + drawingBlocks.DrawBlocks(_startPosX, _startPosY,g); + if (helipadCrusier.GetRocketMines()){ + g.setColor(Color.green); + g.fillRect(_startPosX + 15, _startPosY + 10, 10, 15); + g.fillRect(_startPosX + 30, _startPosY + 10, 10, 15); + } + if (helipadCrusier.GetHelipad()) + { + // верт площадка + g.setColor(Color.ORANGE); + g.fillOval(_startPosX + 15, _startPosY + 25, 25, 25); + } + } +} diff --git a/src/CruiserHard/Entity/EntityCruiser.java b/src/CruiserHard/Entity/EntityCruiser.java new file mode 100644 index 0000000..ecbe065 --- /dev/null +++ b/src/CruiserHard/Entity/EntityCruiser.java @@ -0,0 +1,26 @@ +package CruiserHard.Entity; + +import java.awt.*; +import java.util.*; + +public class EntityCruiser { + private int Speed; //Скорость + private float Weight; //Вес + private Color BodyColor; //Цвет + private float Step; //Шаг при перемещении + + //Инициализация + public EntityCruiser(int speed, float weight, Color bodyColor) { + Random random = new Random(); + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + Step = Speed * 100 / Weight; + } + public Color GetBodyColor(){ + return BodyColor; + } + public float GetStep(){ + return Step; + } +} diff --git a/src/CruiserHard/Entity/EntityHelipadCruiser.java b/src/CruiserHard/Entity/EntityHelipadCruiser.java new file mode 100644 index 0000000..9bd2149 --- /dev/null +++ b/src/CruiserHard/Entity/EntityHelipadCruiser.java @@ -0,0 +1,23 @@ +package CruiserHard.Entity; +import java.awt.*; + +public class EntityHelipadCruiser extends EntityCruiser { + private Color ElementsColor; + public Color GetElementsColor() { return ElementsColor; } + private boolean RocketMines; + public boolean GetRocketMines(){ + return RocketMines; + } + private boolean Helipad; + public boolean GetHelipad(){ + return Helipad; + } + + public EntityHelipadCruiser(int speed, float weight, Color bodyColor, Color + elementsColor, boolean rocketMines, boolean helipad) { + super(speed, weight, bodyColor); + ElementsColor = elementsColor; + RocketMines = rocketMines; + Helipad = helipad; + } +} diff --git a/src/CruiserHard/FormCruiser.java b/src/CruiserHard/FormCruiser.java new file mode 100644 index 0000000..02ccfc3 --- /dev/null +++ b/src/CruiserHard/FormCruiser.java @@ -0,0 +1,176 @@ +package CruiserHard; +import javax.swing.*; +import java.awt.*; +import java.awt.Graphics; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.*; +import CruiserHard.Drawing.*; +import CruiserHard.Direction.*; +import CruiserHard.MovementStrategy.*; + +public class FormCruiser{ + private DrawingCruiser _drawingCruiser; + private AbstractStrategy _abstractStrategy; + Canvas canv; + public void Draw(){ + canv.repaint(); + } + public FormCruiser(){ + JFrame frame = new JFrame("RoadTrain"); + JButton buttonCreateCruiser = new JButton("Крейсер"); + buttonCreateCruiser.setFocusPainted(false); + buttonCreateCruiser.setContentAreaFilled(false); + JButton buttonCreateHelipadCruiser = new JButton("СуперКрейсер"); + buttonCreateHelipadCruiser.setFocusPainted(false); + buttonCreateHelipadCruiser.setContentAreaFilled(false); + String[] items = { + "ToCenter", + "ToBorder" + }; + JComboBox comboBoxStrategy = new JComboBox(items); + JButton buttonStep = new JButton("Шаг"); + buttonStep.setFocusPainted(false); + buttonStep.setContentAreaFilled(false); + JButton buttonUp = new JButton(); + buttonUp.setFocusPainted(false); + buttonUp.setContentAreaFilled(false); + buttonUp.setName("up"); + buttonUp.setIcon(new ImageIcon(((new ImageIcon("src/CruiserHard/Images/totop.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); + JButton buttonDown = new JButton(); + buttonDown.setFocusPainted(false); + buttonDown.setContentAreaFilled(false); + buttonDown.setName("down"); + buttonDown.setIcon(new ImageIcon(((new ImageIcon("src/CruiserHard/Images/todown.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); + JButton buttonLeft = new JButton(); + buttonLeft.setFocusPainted(false); + buttonLeft.setContentAreaFilled(false); + buttonLeft.setName("left"); + buttonLeft.setIcon(new ImageIcon(((new ImageIcon("src/CruiserHard/Images/toleft.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); + JButton buttonRight = new JButton(); + buttonRight.setFocusPainted(false); + buttonRight.setContentAreaFilled(false); + buttonRight.setName("right"); + buttonRight.setIcon(new ImageIcon(((new ImageIcon("src/CruiserHard/Images/toright.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); + buttonCreateCruiser.addActionListener( + e -> { + Random random = new Random(); + _drawingCruiser = new DrawingCruiser(random.nextInt(200) + 100, + random.nextInt(2000) + 1000, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3-1+1)+1); + _drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома + random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома + ,this.canv.getWidth(), this.canv.getHeight()); + canv._drawingCruiser = _drawingCruiser; + Draw(); + } + ); + buttonCreateHelipadCruiser.addActionListener( + e -> { + Random random = new Random(); + _drawingCruiser = new DrawingHelipadCruiser(random.nextInt(100) + 50, + random.nextInt(1000) + 500, + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), + random.nextBoolean(), random.nextBoolean(), + this.canv.getWidth(), this.canv.getHeight(), random.nextInt(3-1+1)+1); + _drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома + random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома + ,this.canv.getWidth(), this.canv.getHeight()); + canv._drawingCruiser = _drawingCruiser; + Draw(); + } + ); + ActionListener actionListener = e -> { + if (_drawingCruiser == null){ + return; + } + switch ((((JButton)(e.getSource())).getName())){ + case "up": + _drawingCruiser.MoveTransport(Direction.Up); + break; + case "down": + _drawingCruiser.MoveTransport(Direction.Down); + break; + case "left": + _drawingCruiser.MoveTransport(Direction.Left); + break; + case "right": + _drawingCruiser.MoveTransport(Direction.Right); + break; + } + Draw(); + }; + buttonUp.addActionListener(actionListener); + buttonDown.addActionListener(actionListener); + buttonLeft.addActionListener(actionListener); + buttonRight.addActionListener(actionListener); + buttonStep.addActionListener(e -> { + if (_drawingCruiser == null) { + return; + } + if (comboBoxStrategy.isEnabled()) { + _abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()) { + case 0 -> new MoveToCenter(); + case 1 -> new MoveToBorder(); + default -> null; + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawingObjectCruiser(_drawingCruiser), this.canv.getWidth(), this.canv.getHeight()); + } + if (_abstractStrategy == null) + { + return; + } + comboBoxStrategy.setEnabled(false); + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.setEnabled(true); + _abstractStrategy = null; + } + }); + frame.setSize(910, 500); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLayout(null); + canv = new Canvas(); + canv.setBounds(0, 0, 895, 500 - 40); // 40 - const, высота панели сверху + buttonCreateCruiser.setBounds(20, 420, 100, 40); + buttonCreateHelipadCruiser.setBounds(140, 420, 100, 40); + buttonUp.setBounds(800, 380, 40, 40); + buttonDown.setBounds(800, 420, 40, 40); + buttonLeft.setBounds(760, 420, 40, 40); + buttonRight.setBounds(840, 420, 40, 40); + comboBoxStrategy.setBounds(800,10,100,50); + buttonStep.setBounds(800,80,100,40); + frame.add(canv); + frame.add(buttonCreateCruiser); + frame.add(buttonCreateHelipadCruiser); + frame.add(buttonUp); + frame.add(buttonDown); + frame.add(buttonLeft); + frame.add(buttonRight); + frame.add(comboBoxStrategy); + frame.add(buttonStep); + frame.setVisible(true); + } + class Canvas extends JComponent{ + public DrawingCruiser _drawingCruiser; + public Canvas(){} + + public void paintComponent(Graphics g){ + if (_drawingCruiser == null){ + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D)g; + _drawingCruiser.DrawTransport(g2d); + super.repaint(); + } + } +} \ No newline at end of file diff --git a/src/Images/todown.png b/src/CruiserHard/Images/todown.png similarity index 100% rename from src/Images/todown.png rename to src/CruiserHard/Images/todown.png diff --git a/src/Images/toleft.png b/src/CruiserHard/Images/toleft.png similarity index 100% rename from src/Images/toleft.png rename to src/CruiserHard/Images/toleft.png diff --git a/src/Images/toright.png b/src/CruiserHard/Images/toright.png similarity index 100% rename from src/Images/toright.png rename to src/CruiserHard/Images/toright.png diff --git a/src/Images/totop.png b/src/CruiserHard/Images/totop.png similarity index 100% rename from src/Images/totop.png rename to src/CruiserHard/Images/totop.png diff --git a/src/Main.java b/src/CruiserHard/Main.java similarity index 83% rename from src/Main.java rename to src/CruiserHard/Main.java index 91a9371..b3d901b 100644 --- a/src/Main.java +++ b/src/CruiserHard/Main.java @@ -1,3 +1,4 @@ +package CruiserHard; public class Main { public static void main(String[] args) { FormCruiser fm = new FormCruiser(); diff --git a/src/CruiserHard/MovementStrategy/AbstractStrategy.java b/src/CruiserHard/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..ed5aa2f --- /dev/null +++ b/src/CruiserHard/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,91 @@ +package CruiserHard.MovementStrategy;; +import CruiserHard.Direction.*; +public abstract class AbstractStrategy { + private IMoveableObject moveableObject; + + private Status state = Status.NotInit; + + private int fieldWidth; + + protected int GetFieldWidth() { + return fieldWidth; + } + + private int fieldHeight; + + protected int GetFieldHeight() { + return fieldHeight; + } + + public Status GetStatus() { + return state; + } + + public void SetData(IMoveableObject moveableObject, int width, int height) { + if (moveableObject == null) { + state = Status.NotInit; + return; + } + state = Status.InProgress; + this.moveableObject = moveableObject; + fieldWidth = width; + fieldHeight = height; + } + + public void MakeStep() { + if (state != Status.InProgress) { + return; + } + if (isTargetDestination()) { + state = Status.Finish; + return; + } + MoveToTarget(); + } + + protected boolean MoveLeft() { + return MoveTo(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() { + if (moveableObject == null) { + return null; + } + return moveableObject.GetObjectPosition(); + } + + protected Integer GetStep() { + if (state != Status.InProgress) { + return null; + } + return moveableObject.GetStep(); + } + + protected abstract void MoveToTarget(); + + protected abstract boolean isTargetDestination(); + + private boolean MoveTo(Direction direction) { + if (state != Status.InProgress) { + return false; + } + if (moveableObject == null) { + return false; + } + if (moveableObject.CheckCanMove(direction)) { + moveableObject.MoveObject(direction); + return true; + } + return false; + } +} diff --git a/src/CruiserHard/MovementStrategy/DrawingObjectCruiser.java b/src/CruiserHard/MovementStrategy/DrawingObjectCruiser.java new file mode 100644 index 0000000..faed2c8 --- /dev/null +++ b/src/CruiserHard/MovementStrategy/DrawingObjectCruiser.java @@ -0,0 +1,39 @@ +package CruiserHard.MovementStrategy; +import CruiserHard.Direction.*; +import CruiserHard.Drawing.*; + +public class DrawingObjectCruiser implements IMoveableObject{ + private DrawingCruiser _drawingCruiser = null; + public DrawingObjectCruiser (DrawingCruiser drawingCruiser) + { + _drawingCruiser = drawingCruiser; + } + @Override + public ObjectParameters GetObjectPosition() { + if (_drawingCruiser == null || _drawingCruiser.cruiser == null) { + return null; + } + return new ObjectParameters(_drawingCruiser.GetStartX(), _drawingCruiser.GetStartY(), + _drawingCruiser.GetWidth(), _drawingCruiser.GetHeight()); + } + + @Override + public int GetStep() { + if (_drawingCruiser != null && _drawingCruiser.cruiser!=null) + return (int)(_drawingCruiser.cruiser.GetStep()); + return 0; + } + + @Override + public boolean CheckCanMove(Direction direction) { + if (_drawingCruiser != null) + return _drawingCruiser.CanMove(direction); + return false; + } + + @Override + public void MoveObject(Direction direction) { + if (_drawingCruiser != null) + _drawingCruiser.MoveTransport(direction); + } +} diff --git a/src/CruiserHard/MovementStrategy/IMoveableObject.java b/src/CruiserHard/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..d458dc8 --- /dev/null +++ b/src/CruiserHard/MovementStrategy/IMoveableObject.java @@ -0,0 +1,12 @@ +package CruiserHard.MovementStrategy; +import CruiserHard.Direction.*; + +public interface IMoveableObject{ + ObjectParameters GetObjectPosition(); + + int GetStep(); + + boolean CheckCanMove(Direction direction); + + void MoveObject(Direction direction); +} diff --git a/src/CruiserHard/MovementStrategy/MoveToBorder.java b/src/CruiserHard/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..c89b1b2 --- /dev/null +++ b/src/CruiserHard/MovementStrategy/MoveToBorder.java @@ -0,0 +1,34 @@ +package CruiserHard.MovementStrategy; +public class MoveToBorder extends AbstractStrategy{ + @Override + protected boolean isTargetDestination() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.RightBorder() <= GetFieldWidth() && + objParams.RightBorder() + GetStep() >= GetFieldWidth() && + objParams.DownBorder() <= GetFieldHeight() && + objParams.DownBorder() + GetStep() >= GetFieldHeight(); + } + + @Override + protected void MoveToTarget() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + var diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth(); + if (Math.abs(diffX) > GetStep()) { + if (diffX < 0) { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical() - GetFieldHeight(); + if (Math.abs(diffY) > GetStep()) { + if (diffY < 0) { + MoveDown(); + } + } + } +} diff --git a/src/CruiserHard/MovementStrategy/MoveToCenter.java b/src/CruiserHard/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..256a463 --- /dev/null +++ b/src/CruiserHard/MovementStrategy/MoveToCenter.java @@ -0,0 +1,38 @@ +package CruiserHard.MovementStrategy; +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean isTargetDestination() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.ObjectMiddleHorizontal() <= GetFieldWidth() / 2 && + objParams.ObjectMiddleHorizontal() + GetStep() >= GetFieldWidth() / 2 && + objParams.ObjectMiddleVertical() <= GetFieldHeight() / 2 && + objParams.ObjectMiddleVertical() + GetStep() >= GetFieldHeight() / 2; + } + + @Override + protected void MoveToTarget() { + var objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + var diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth() / 2; + if (Math.abs(diffX) > GetStep()) { + if (diffX > 0) { + MoveLeft(); + } else { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical() - GetFieldHeight() / 2; + if (Math.abs(diffY) > GetStep()) { + if (diffY > 0) { + MoveUp(); + } else { + MoveDown(); + } + } + } +} diff --git a/src/CruiserHard/MovementStrategy/ObjectParameters.java b/src/CruiserHard/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..450b838 --- /dev/null +++ b/src/CruiserHard/MovementStrategy/ObjectParameters.java @@ -0,0 +1,33 @@ +package CruiserHard.MovementStrategy; +public class ObjectParameters { + private final int _x; + + private final int _y; + + private final int _width; + + private final int _height; + + 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/CruiserHard/MovementStrategy/Status.java b/src/CruiserHard/MovementStrategy/Status.java new file mode 100644 index 0000000..1e56f08 --- /dev/null +++ b/src/CruiserHard/MovementStrategy/Status.java @@ -0,0 +1,6 @@ +package CruiserHard.MovementStrategy; +public enum Status { + NotInit, + InProgress, + Finish +} diff --git a/src/EntityCruiser.java b/src/EntityCruiser.java deleted file mode 100644 index f7de679..0000000 --- a/src/EntityCruiser.java +++ /dev/null @@ -1,31 +0,0 @@ -import java.awt.*; -import java.util.*; - -public class EntityCruiser { - private int Speed; //Скорость - private float Weight; //Вес - private Color BodyColor; //Цвет - private float Step; //Шаг при перемещении - private boolean Helipad; //Наличие верт. площадки - private boolean RocketMines; //Наличие шахт - - //Инициализация - public void Init(int speed, float weight, Color bodyColor, boolean rocketMines, boolean helipad) - { - Random random = new Random(); - Speed = speed <= 0 ? random.nextInt(50, 150) : speed; - Weight = weight <= 0 ? random.nextInt(50, 150) : weight; - BodyColor = bodyColor; - RocketMines = rocketMines; - Helipad = helipad; - Step = Speed * 100 / Weight; - } - public boolean GetHelipad() {return Helipad;} - public boolean GetRocketMines() {return RocketMines;} - public Color GetBodyColor(){ - return BodyColor; - } - public float GetStep(){ - return Step; - } -} diff --git a/src/FormCruiser.java b/src/FormCruiser.java deleted file mode 100644 index 302e95f..0000000 --- a/src/FormCruiser.java +++ /dev/null @@ -1,111 +0,0 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.Graphics; -import java.awt.event.ActionListener; -import java.util.*; - -public class FormCruiser{ - private DrawingCruiser _drawingCruiser; - Canvas canv; - public void Draw(){ - canv.repaint(); - } - public FormCruiser(){ - JFrame frame = new JFrame("Cruiser"); - JButton buttonCreate = new JButton("Создать"); - buttonCreate.setFocusPainted(false); - buttonCreate.setContentAreaFilled(false); - JButton buttonUp = new JButton(); - buttonUp.setFocusPainted(false); - buttonUp.setContentAreaFilled(false); - buttonUp.setName("up"); - buttonUp.setIcon(new ImageIcon(((new ImageIcon("src/Images/totop.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); - JButton buttonDown = new JButton(); - buttonDown.setFocusPainted(false); - buttonDown.setContentAreaFilled(false); - buttonDown.setName("down"); - buttonDown.setIcon(new ImageIcon(((new ImageIcon("src/Images/todown.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); - JButton buttonLeft = new JButton(); - buttonLeft.setFocusPainted(false); - buttonLeft.setContentAreaFilled(false); - buttonLeft.setName("left"); - buttonLeft.setIcon(new ImageIcon(((new ImageIcon("src/Images/toleft.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); - JButton buttonRight = new JButton(); - buttonRight.setFocusPainted(false); - buttonRight.setContentAreaFilled(false); - buttonRight.setName("right"); - buttonRight.setIcon(new ImageIcon(((new ImageIcon("src/Images/toright.png")).getImage()).getScaledInstance(35, 35, java.awt.Image.SCALE_SMOOTH))); - buttonCreate.addActionListener( - e -> { - System.out.println(e.getActionCommand()); - Random random = new Random(); - _drawingCruiser = new DrawingCruiser(); - _drawingCruiser.Init( - random.nextInt(200) + 100, - random.nextInt(2000) + 1000, - new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), - random.nextBoolean(), random.nextBoolean()); - _drawingCruiser.SetPosition(random.nextInt(100-30+1)+30, //начальные и конечные значения для рандома - random.nextInt(90-20+1)+20 //начальные и конечные значения для рандома - ,this.canv.getWidth(), this.canv.getHeight()); - canv._drawingCruiser = _drawingCruiser; - Draw(); - } - ); - ActionListener actionListener = e -> { - if (_drawingCruiser == null){ - return; - } - switch ((((JButton)(e.getSource())).getName())){ - case "up": - _drawingCruiser.MoveTransport(Direction.Up); - break; - case "down": - _drawingCruiser.MoveTransport(Direction.Down); - break; - case "left": - _drawingCruiser.MoveTransport(Direction.Left); - break; - case "right": - _drawingCruiser.MoveTransport(Direction.Right); - break; - } - Draw(); - }; - buttonUp.addActionListener(actionListener); - buttonDown.addActionListener(actionListener); - buttonLeft.addActionListener(actionListener); - buttonRight.addActionListener(actionListener); - frame.setSize(910, 500); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setLayout(null); - canv = new Canvas(); - canv.setBounds(0, 0, 895, 500 - 40); // 40 - const, высота панели сверху - buttonCreate.setBounds(20, 420, 100, 40); - buttonUp.setBounds(800, 380, 40, 40); - buttonDown.setBounds(800, 420, 40, 40); - buttonLeft.setBounds(760, 420, 40, 40); - buttonRight.setBounds(840, 420, 40, 40); - frame.add(canv); - frame.add(buttonCreate); - frame.add(buttonUp); - frame.add(buttonDown); - frame.add(buttonLeft); - frame.add(buttonRight); - frame.setVisible(true); - } - class Canvas extends JComponent{ - public DrawingCruiser _drawingCruiser; - public Canvas(){} - - public void paintComponent(Graphics g){ - if (_drawingCruiser == null){ - return; - } - super.paintComponents(g); - Graphics2D g2d = (Graphics2D)g; - _drawingCruiser.DrawTransport(g2d); - super.repaint(); - } - } -} \ No newline at end of file