From c9f315463b159d1dd3504f7ef6befcb3138b5992 Mon Sep 17 00:00:00 2001 From: Anitonchik Date: Thu, 9 May 2024 20:09:37 +0400 Subject: [PATCH] LabWork02 --- .../DrawningOrnamentRectangle.java | 26 ++ DifferenceOfWheels/DrawningOrnamentStar.java | 36 +++ .../DrawningOrnamentTriangle.java | 30 +++ DifferenceOfWheels/DrawningWheels.java | 32 +++ DifferenceOfWheels/EnumWheels.java | 15 ++ DifferenceOfWheels/IOrnaments.java | 8 + Drawnings/CanvasDumpTruck.java | 21 ++ Drawnings/DirectionType.java | 9 + Drawnings/DrawningDumpTruck.java | 95 ++++++++ Drawnings/DrawningTruck.java | 224 ++++++++++++++++++ Entities/EntityDumpTruck.java | 25 ++ Entities/EntityTruck.java | 24 ++ FormDumpTruck.java | 220 +++++++++++++++++ Main.java | 8 + MovementStrategy/AbstractStrategy.java | 60 +++++ MovementStrategy/IMoveableObject.java | 7 + MovementStrategy/MoveToBorder.java | 36 +++ MovementStrategy/MoveToCenter.java | 38 +++ MovementStrategy/MoveableTruck.java | 48 ++++ MovementStrategy/MovementDirection.java | 8 + MovementStrategy/ObjectParameters.java | 27 +++ 21 files changed, 997 insertions(+) create mode 100644 DifferenceOfWheels/DrawningOrnamentRectangle.java create mode 100644 DifferenceOfWheels/DrawningOrnamentStar.java create mode 100644 DifferenceOfWheels/DrawningOrnamentTriangle.java create mode 100644 DifferenceOfWheels/DrawningWheels.java create mode 100644 DifferenceOfWheels/EnumWheels.java create mode 100644 DifferenceOfWheels/IOrnaments.java create mode 100644 Drawnings/CanvasDumpTruck.java create mode 100644 Drawnings/DirectionType.java create mode 100644 Drawnings/DrawningDumpTruck.java create mode 100644 Drawnings/DrawningTruck.java create mode 100644 Entities/EntityDumpTruck.java create mode 100644 Entities/EntityTruck.java create mode 100644 FormDumpTruck.java create mode 100644 Main.java create mode 100644 MovementStrategy/AbstractStrategy.java create mode 100644 MovementStrategy/IMoveableObject.java create mode 100644 MovementStrategy/MoveToBorder.java create mode 100644 MovementStrategy/MoveToCenter.java create mode 100644 MovementStrategy/MoveableTruck.java create mode 100644 MovementStrategy/MovementDirection.java create mode 100644 MovementStrategy/ObjectParameters.java diff --git a/DifferenceOfWheels/DrawningOrnamentRectangle.java b/DifferenceOfWheels/DrawningOrnamentRectangle.java new file mode 100644 index 0000000..235c211 --- /dev/null +++ b/DifferenceOfWheels/DrawningOrnamentRectangle.java @@ -0,0 +1,26 @@ +package DifferenceOfWheels; + +import java.awt.*; + +public class DrawningOrnamentRectangle implements IOrnaments{ + int CountWeel; + + @Override + public void SetCount(int n) { + CountWeel = n; + } + + @Override + public int get_count_weels() { + return CountWeel; + } + + @Override + public void DrawOrnament(Graphics2D g, int x, int y) { + g.setColor(Color.BLACK); + g.fillOval(x, y, 25, 25); + + g.setColor(Color.PINK); + g.fillRect(x + 5, y + 5, 15, 15); + } +} diff --git a/DifferenceOfWheels/DrawningOrnamentStar.java b/DifferenceOfWheels/DrawningOrnamentStar.java new file mode 100644 index 0000000..3a015f1 --- /dev/null +++ b/DifferenceOfWheels/DrawningOrnamentStar.java @@ -0,0 +1,36 @@ +package DifferenceOfWheels; + +import java.awt.*; + +public class DrawningOrnamentStar implements IOrnaments{ + int CountWeel; + @Override + public void SetCount(int n) { + CountWeel = n; + } + + @Override + public int get_count_weels() { + return CountWeel; + } + + @Override + public void DrawOrnament(Graphics2D g, int x, int y) { + g.setColor(Color.BLACK); + g.fillOval(x, y, 25, 25); + + g.setColor(Color.CYAN); + Polygon elements = new Polygon(); + elements.addPoint(x + 12, y + 2); + elements.addPoint(x + 15, y + 8); + elements.addPoint(x + 21, y + 10); + elements.addPoint(x + 15, y + 13); + elements.addPoint(x + 19, y + 19); + elements.addPoint(x + 12, y + 15); + elements.addPoint(x + 5, y + 19); + elements.addPoint(x + 9, y + 13); + elements.addPoint(x + 3, y + 10); + elements.addPoint(x + 10, y + 8); + g.fillPolygon(elements); + } +} diff --git a/DifferenceOfWheels/DrawningOrnamentTriangle.java b/DifferenceOfWheels/DrawningOrnamentTriangle.java new file mode 100644 index 0000000..965059d --- /dev/null +++ b/DifferenceOfWheels/DrawningOrnamentTriangle.java @@ -0,0 +1,30 @@ +package DifferenceOfWheels; + +import java.awt.*; + +public class DrawningOrnamentTriangle implements IOrnaments{ + int CountWeel; + @Override + public void SetCount(int n) { + CountWeel = n; + } + + @Override + public int get_count_weels() { + return CountWeel; + } + + @Override + public void DrawOrnament(Graphics2D g, int x, int y) { + g.setColor(Color.BLACK); + g.fillOval(x, y, 25, 25); + + g.setColor(Color.MAGENTA); + Polygon elements = new Polygon(); + elements.addPoint(x + 7, y + 6); + elements.addPoint(x + 20, y + 6); + elements.addPoint(x + 7, y + 18); + g.fillPolygon(elements); + } + +} diff --git a/DifferenceOfWheels/DrawningWheels.java b/DifferenceOfWheels/DrawningWheels.java new file mode 100644 index 0000000..0779884 --- /dev/null +++ b/DifferenceOfWheels/DrawningWheels.java @@ -0,0 +1,32 @@ +package DifferenceOfWheels; + +import java.util.Random; +import java.awt.*; + +public class DrawningWheels implements IOrnaments{ + int CountWeel; + private int count = 0; + Random rnd = new Random(); + int ranOrnament = rnd.nextInt(0, 3); + + @Override + public void SetCount(int n) { + for (EnumWheels count: EnumWheels.values()) { + if (count.get_number() == n) { + CountWeel = n; + return; + } + } + } + + @Override + public int get_count_weels () { + return CountWeel; + } + + @Override + public void DrawOrnament (Graphics2D g,int x, int y){ + g.setColor(Color.BLACK); + g.fillOval(x, y, 25, 25); + } +} diff --git a/DifferenceOfWheels/EnumWheels.java b/DifferenceOfWheels/EnumWheels.java new file mode 100644 index 0000000..eeb6295 --- /dev/null +++ b/DifferenceOfWheels/EnumWheels.java @@ -0,0 +1,15 @@ +package DifferenceOfWheels; + +public enum EnumWheels { + Two (2), + Three (3), + Four (4); + + private int number; + + EnumWheels(int n){ + this.number = n; + } + + public int get_number() { return number; } +} diff --git a/DifferenceOfWheels/IOrnaments.java b/DifferenceOfWheels/IOrnaments.java new file mode 100644 index 0000000..5be0c52 --- /dev/null +++ b/DifferenceOfWheels/IOrnaments.java @@ -0,0 +1,8 @@ +package DifferenceOfWheels; +import java.awt.*; + +public interface IOrnaments { + void SetCount(int n); + int get_count_weels(); + void DrawOrnament(Graphics2D g, int x, int y); +} diff --git a/Drawnings/CanvasDumpTruck.java b/Drawnings/CanvasDumpTruck.java new file mode 100644 index 0000000..0f59856 --- /dev/null +++ b/Drawnings/CanvasDumpTruck.java @@ -0,0 +1,21 @@ +package Drawnings; + +import Drawnings.DrawningDumpTruck; + +import javax.swing.*; +import java.awt.*; + +public class CanvasDumpTruck extends JComponent { + public DrawningTruck _drawningTruck; + public CanvasDumpTruck() {} + + public void paintComponent(Graphics g) { + if (_drawningTruck == null) { + return; + } + super.paintComponents(g); + Graphics2D g2d = (Graphics2D) g; + _drawningTruck.DrawTransport(g2d); + super.repaint(); + } +} diff --git a/Drawnings/DirectionType.java b/Drawnings/DirectionType.java new file mode 100644 index 0000000..9c1c629 --- /dev/null +++ b/Drawnings/DirectionType.java @@ -0,0 +1,9 @@ +package Drawnings; + +public enum DirectionType { + Up, + Down, + Left, + Right, + Unknow +} diff --git a/Drawnings/DrawningDumpTruck.java b/Drawnings/DrawningDumpTruck.java new file mode 100644 index 0000000..11a6d95 --- /dev/null +++ b/Drawnings/DrawningDumpTruck.java @@ -0,0 +1,95 @@ +package Drawnings; +import Entities.EntityDumpTruck; +import java.awt.*; +import java.util.Random; + +public class DrawningDumpTruck extends DrawningTruck { + + public DrawningDumpTruck(int speed, double weight, Color bodyColor, + Color additionalColor, Boolean body, Boolean tent) + { + super(speed, weight, bodyColor); + //super(110, 100); + EntityTruck = new EntityDumpTruck(speed, weight, bodyColor, additionalColor, body, tent); + _ornament.SetCount(random.nextInt(2, 5)); + + } + + public DrawningDumpTruck(){ + super(110, 100); + } + + @Override + public void DrawTransport(Graphics2D g) + { + if (EntityTruck == null || !(EntityTruck instanceof EntityDumpTruck entityTruck) || _startPosX == null || _startPosY == null) + { + return; + } + + _startPosX += 5; + super.DrawTransport(g); + _startPosX -= 5; + + g.setPaint(Color.BLACK); + + if (entityTruck.Body) + { + int[] x_b = {_startPosX, _startPosX + 77, _startPosX + 77, _startPosX + 20}; + int[] y_b = {_startPosY + 15, _startPosY + 15, _startPosY + 50, _startPosY + 50}; + + g.setPaint(Color.BLACK); + g.drawPolygon(x_b, y_b, 4); + g.setPaint(entityTruck.AdditionalColor); + g.fillPolygon(x_b, y_b, 4); + } + + if (entityTruck.Tent && entityTruck.Body) + { + int[] x_t = {_startPosX, _startPosX + 39, _startPosX + 77}; + int[] y_t = {_startPosY + 13, _startPosY, _startPosY + 13}; + + g.setPaint(Color.BLACK); + g.drawPolygon(x_t, y_t, 3); + g.setPaint(entityTruck.AdditionalColor); + g.fillPolygon(x_t, y_t, 3); + } + + // Колёса + g.setPaint(Color.BLACK); + if (_ornament != null){ + switch (_ornament.get_count_weels()){ + case 2: + _ornament.DrawOrnament(g, _startPosX + 10, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 85, _startPosY + 67); + break; + case 3: + _ornament.DrawOrnament(g, _startPosX + 10, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 38, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 85, _startPosY + 67); + break; + case 4: + _ornament.DrawOrnament(g, _startPosX + 5, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 32, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 61, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 88, _startPosY + 67); + break; + } + } + + //Границы самосвала + g.drawRect( _startPosX + 10, _startPosY + 53, 100, 13); + g.drawRect( _startPosX + 80, _startPosY + 15, 30, 36); + g.drawRect(_startPosX + 85, _startPosY + 20, 20, 20); + + // Кузов + g.setPaint(entityTruck.BodyColor); + g.fillRect(_startPosX + 10, _startPosY + 53, 100, 13); + g.fillRect(_startPosX + 80, _startPosY + 15, 30, 36); + + //Окно + g.setPaint(Color.CYAN); + g.fillRect(_startPosX + 85, _startPosY + 20, 20, 20); + + } +} diff --git a/Drawnings/DrawningTruck.java b/Drawnings/DrawningTruck.java new file mode 100644 index 0000000..3295039 --- /dev/null +++ b/Drawnings/DrawningTruck.java @@ -0,0 +1,224 @@ +package Drawnings; + +import DifferenceOfWheels.*; +import Entities.EntityDumpTruck; +import Entities.EntityTruck; + +import java.awt.*; +import java.util.Random; + +public class DrawningTruck{ + public Entities.EntityTruck EntityTruck; + protected Entities.EntityTruck get_EntityTruck() { return EntityTruck; } + public IOrnaments _ornament = new DrawningWheels(); + int CountOfWheels = 0; + int ornament_type; + + // Ширина окна + private Integer _pictureWidth; + + // Высота окна + private Integer _pictureHeight; + + // Левая координата прорисовки автомобиля + protected Integer _startPosX; + + // Верхняя кооридната прорисовки автомобиля + protected Integer _startPosY; + + // Ширина прорисовки самосвала + private int _drawningTruckWidth = 110; //100 + + // Высота прорисовки самосвала + private int _drawningTruckHeight = 110; //92 + + public Integer GetPosX() { return _startPosX;} + public Integer GetPosY() { return _startPosY;} + public Integer GetWidth() { return _drawningTruckWidth;} + public Integer GetHeight() { return _drawningTruckHeight;} + Random random = new Random(); + + private DrawningTruck() + { + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + + } + + public DrawningTruck(int speed, double weight, Color bodyColor) + { + EntityTruck = new EntityTruck(speed, weight, bodyColor); + CountOfWheels = random.nextInt(2, 5); + ornament_type = random.nextInt(0, 3); + } + + protected DrawningTruck(int drawningTruckWidth, int drawningTruckHeight) + { + _drawningTruckWidth = drawningTruckWidth; + _drawningTruckHeight = drawningTruckHeight; + } + + protected void SetOrnamentWheel(){ + switch (ornament_type){ + case 0: + _ornament = new DrawningOrnamentStar(); + _ornament.SetCount(CountOfWheels); + break; + case 1: + _ornament = new DrawningOrnamentTriangle(); + _ornament.SetCount(CountOfWheels); + break; + case 2: + _ornament = new DrawningOrnamentRectangle(); + _ornament.SetCount(CountOfWheels); + break; + } + } + + public Boolean SetPictureSize(int width, int height) + { + if (_drawningTruckWidth > width || _drawningTruckHeight > height) + { + return false; + } + + if (_pictureWidth != null && width != _pictureWidth || _pictureHeight != null && height != _pictureHeight) + { + if (_startPosX + _drawningTruckWidth > width) + { + _startPosX -= _drawningTruckWidth; + } + + if (_startPosY + _drawningTruckHeight > height) + { + _startPosY -= _drawningTruckHeight; + } + } + + _pictureWidth = width; + _pictureHeight = height; + return true; + } + + public void SetPosition(int x, int y) + { + if (_pictureHeight == null|| _pictureWidth == null) + { + return; + } + + if (x < 0) + { + x = 0; + } + if (x + _drawningTruckWidth > _pictureWidth) + { + x = (int)_pictureWidth - _drawningTruckWidth; + } + if (y < 0) + { + y = 0; + } + if (y + _drawningTruckHeight > _pictureHeight) + { + y = (int)_pictureHeight - _drawningTruckHeight; + + } + + _startPosX = x; + _startPosY = y; + + } + + public Boolean MoveTransport(DirectionType direction) + { + if (EntityTruck == null || _startPosX == null || _startPosY == null) + { + return false; + } + + switch (direction) + { + //влево + case DirectionType.Left: + if (_startPosX - _drawningTruckWidth > 0) + { + _startPosX -= (int)EntityTruck.Step; + } + return true; + //вверх + case DirectionType.Up: + if (_startPosY - _drawningTruckHeight > 0) + { + _startPosY -= (int)EntityTruck.Step; + } + return true; + // вправо + case DirectionType.Right: + if (_startPosX + _drawningTruckWidth + (int)EntityTruck.Step < _pictureWidth) + { + _startPosX += (int)EntityTruck.Step; + } + return true; + //вниз + case DirectionType.Down: + if (_startPosY + _drawningTruckHeight + (int)EntityTruck.Step < _pictureHeight) + { + _startPosY += (int)EntityTruck.Step; + } + return true; + default: + return false; + } + } + + public void DrawTransport(Graphics2D g) + { + if (EntityTruck == null || _startPosX == null || _startPosY == null) + { + return; + } + + g.setPaint(Color.BLACK); + + // Колёса + g.setPaint(Color.BLACK); + SetOrnamentWheel(); + if (_ornament != null){ + switch (_ornament.get_count_weels()){ + case 2: + _ornament.DrawOrnament(g, _startPosX + 5, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 80, _startPosY + 67); + break; + case 3: + _ornament.DrawOrnament(g, _startPosX + 5, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 33, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 80, _startPosY + 67); + break; + case 4: + _ornament.DrawOrnament(g, _startPosX , _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 27, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 56, _startPosY + 67); + _ornament.DrawOrnament(g, _startPosX + 83, _startPosY + 67); + break; + } + } + + //Границы самосвала + g.drawRect( _startPosX + 5, _startPosY + 53, 100, 13); + g.drawRect( _startPosX + 75, _startPosY + 15, 30, 36); + g.drawRect(_startPosX + 80, _startPosY + 20, 20, 20); + + // Кузов + g.setPaint(EntityTruck.BodyColor); + g.fillRect(_startPosX + 5, _startPosY + 53, 100, 13); + g.fillRect(_startPosX + 75, _startPosY + 15, 30, 36); + + //Окно + g.setPaint(Color.CYAN); + g.fillRect(_startPosX + 80, _startPosY + 20, 20, 20); + + } +} \ No newline at end of file diff --git a/Entities/EntityDumpTruck.java b/Entities/EntityDumpTruck.java new file mode 100644 index 0000000..f4826e2 --- /dev/null +++ b/Entities/EntityDumpTruck.java @@ -0,0 +1,25 @@ +package Entities; + +import java.awt.*; + +public class EntityDumpTruck extends EntityTruck { + + public Color AdditionalColor; + public Color get_AdditionalColor(){ return AdditionalColor; } + + public Boolean Body; + private Boolean get_Body() { return Body; } + + public Boolean Tent; + private Boolean get_Tent() { return Tent; } + + + public EntityDumpTruck(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent) + { + super (speed, weight, bodyColor); + AdditionalColor = additionalColor; + Body = body; + Tent = tent; + } + +} diff --git a/Entities/EntityTruck.java b/Entities/EntityTruck.java new file mode 100644 index 0000000..b59af98 --- /dev/null +++ b/Entities/EntityTruck.java @@ -0,0 +1,24 @@ +package Entities; + +import java.awt.*; + +public class EntityTruck { + public int Speed; + private int get_Speed(){ return Speed; } + + public double Weight; + private double get_Weight(){return Weight; } + + public Color BodyColor; + private Color get_BodyColor(){ return BodyColor; } + + public double Step; + + public EntityTruck(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + Step = (speed * 100) / weight; + } +} diff --git a/FormDumpTruck.java b/FormDumpTruck.java new file mode 100644 index 0000000..59fe9c2 --- /dev/null +++ b/FormDumpTruck.java @@ -0,0 +1,220 @@ +import Drawnings.CanvasDumpTruck; +import Drawnings.DirectionType; +import Drawnings.DrawningDumpTruck; +import Drawnings.DrawningTruck; +import MovementStrategy.*; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import javax.swing.*; +import java.util.Random; + +public class FormDumpTruck extends JFrame{ + + private CanvasDumpTruck _canvasDumpTruck = new CanvasDumpTruck(); + + private AbstractStrategy _strategy; + + // Размер формы + private Dimension dimension; + // Название формы + private String title; + + private JButton CreateDumpTruckButton = new JButton("Создать грузовик"); + private JButton CreateTruckButton = new JButton("Создать самосвал"); + private JButton UpButton = new JButton(); + private JButton DownButton = new JButton();; + private JButton LeftButton = new JButton();; + private JButton RightButton = new JButton(); + private JComboBox ComboBoxStrategy = new JComboBox(new String[]{"К центру", "К краю"}); + private JButton ButtonStrategy = new JButton("Шаг"); + + public FormDumpTruck(String title, Dimension dimension) { + this.title = title; + this.dimension = dimension; + } + + private void CreateObject(String type){ + Random random = new Random(); + Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + switch(type){ + case "DrawningTruck": + _canvasDumpTruck._drawningTruck = new Drawnings.DrawningTruck(random.nextInt(100, 300), random.nextInt(1000, 3000), + bodyColor); + _canvasDumpTruck._drawningTruck.SetPictureSize(getWidth(), getHeight()); + _canvasDumpTruck._drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + _canvasDumpTruck.repaint(); + break; + case "DrawningDumpTruck": + Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); + _canvasDumpTruck._drawningTruck = new DrawningDumpTruck(); //--------------------------------- + _canvasDumpTruck._drawningTruck = new DrawningDumpTruck(random.nextInt(100, 300), random.nextInt(1000, 3000), + bodyColor, additionalColor, + random.nextBoolean(), random.nextBoolean()); + _canvasDumpTruck._drawningTruck.SetPictureSize(getWidth(), getHeight()); + _canvasDumpTruck._drawningTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); + _canvasDumpTruck.repaint(); + break; + default: + return; + } + _strategy = null; + ComboBoxStrategy.setEnabled(true); + } + + public void Form() { + CreateDumpTruckButton.setName("Создать самосвал"); + CreateTruckButton.setName("Создать грузовик"); + setMinimumSize(dimension); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + UpButton.setName("UP"); + Icon iconUp = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowUp.png"); + UpButton.setIcon(iconUp); + + DownButton.setName("DOWN"); + Icon iconDown = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowDown.png"); + DownButton.setIcon(iconDown); + + LeftButton.setName("LEFT"); + Icon iconLeft = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowLeft.png"); + LeftButton.setIcon(iconLeft); + + RightButton.setName("RIGHT"); + Icon iconRight = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowRight.png"); + RightButton.setIcon(iconRight); + + CreateDumpTruckButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawningDumpTruck"); + } + }); + + CreateTruckButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawningTruck"); + } + }); + + ButtonStrategy.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (_canvasDumpTruck._drawningTruck == null) return; + if (ComboBoxStrategy.isEnabled()) + { + int index = ComboBoxStrategy.getSelectedIndex(); + switch(index) + { + case 0: + _strategy = new MoveToCenter(); + break; + case 1: + _strategy = new MoveToBorder(); + break; + default: + _strategy = null; + break; + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableTruck(_canvasDumpTruck._drawningTruck), getWidth() - 23, getHeight()-23); + } + if (_strategy == null) + { + return; + } + ComboBoxStrategy.setEnabled(false); + _strategy.MakeStep(); + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + ComboBoxStrategy.setEnabled(true); + _strategy = null; + } + } + }); + + + + ActionListener actionListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent event) { + if (_canvasDumpTruck._drawningTruck == null) return; + boolean result = false; + switch ((((JButton) (event.getSource())).getName())) { + case "UP": + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Up); + break; + case "DOWN": + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Down); + break; + case "LEFT": + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Left); + break; + case "RIGHT": + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Right); + break; + } + if (result) { + _canvasDumpTruck.repaint(); + } + } + }; + + UpButton.addActionListener(actionListener); + DownButton.addActionListener(actionListener); + LeftButton.addActionListener(actionListener); + RightButton.addActionListener(actionListener); + + setSize(dimension.width, dimension.height); + setLayout(null); + _canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight()); + // кнопки создаиния самосвала и грузовика + CreateDumpTruckButton.setBounds(10, getHeight() - 90, 140, 40); + CreateTruckButton.setBounds(160, getHeight() - 90, 140, 40); + // кнопки направлений + UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60); + DownButton.setBounds(getWidth() - 140, getHeight() - 100, 60, 60); + RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60); + LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60); + // кнокпи для стратегий + ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); + ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25); + // добавление кнопок + add(CreateDumpTruckButton); + add(CreateTruckButton); + add(UpButton); + add(DownButton); + add(RightButton); + add(LeftButton); + add(ComboBoxStrategy); + add(ButtonStrategy); + add(_canvasDumpTruck); + pack(); + setVisible(true); + + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent e) { + int Width = getWidth(); + int Height = getHeight(); + if (_canvasDumpTruck._drawningTruck != null) + _canvasDumpTruck._drawningTruck.SetPictureSize(Width, Height); + _canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight()); + CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40); + CreateTruckButton.setBounds(180, getHeight() - 90, 140, 40); + UpButton.setBounds(getWidth() - 150, getHeight() - 170, 60, 60); + DownButton.setBounds(getWidth() - 150, getHeight() - 100, 60, 60); + RightButton.setBounds(getWidth() - 85, getHeight() - 100, 60, 60); + LeftButton.setBounds(getWidth() - 215, getHeight() - 100, 60, 60); + ComboBoxStrategy.setBounds(getWidth() - 170, 10, 140, 35); + ButtonStrategy.setBounds(getWidth() - 130, 55, 100, 25); + } + }); + } +} diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..6b93ce6 --- /dev/null +++ b/Main.java @@ -0,0 +1,8 @@ +import java.awt.*; + +public class Main { + public static void main(String[] args) { + FormDumpTruck formDumpTruck = new FormDumpTruck("Самосвал", new Dimension(700, 700)); + formDumpTruck.Form(); + } +} \ No newline at end of file diff --git a/MovementStrategy/AbstractStrategy.java b/MovementStrategy/AbstractStrategy.java new file mode 100644 index 0000000..08703d0 --- /dev/null +++ b/MovementStrategy/AbstractStrategy.java @@ -0,0 +1,60 @@ +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 (IsTargetDestinaion()) + { + _state = StrategyStatus.Finish; + return; + } + MoveToTarget(); + } + protected boolean MoveLeft() { return MoveTo(MovementDirection.Left);} + protected boolean MoveRight() { return MoveTo(MovementDirection.Right);} + protected boolean MoveUp() { return MoveTo(MovementDirection.Up);} + protected boolean MoveDown() { return MoveTo(MovementDirection.Down);} + protected ObjectParameters GetObjectParameters(){ return _moveableObject.GetObjectPosition();} + protected Integer GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject.GetStep(); + } + protected abstract void MoveToTarget(); + protected abstract boolean IsTargetDestinaion(); + private boolean MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + if (_moveableObject.TryMoveObject(movementDirection)) + return true; + else + return false; + } +} diff --git a/MovementStrategy/IMoveableObject.java b/MovementStrategy/IMoveableObject.java new file mode 100644 index 0000000..a067173 --- /dev/null +++ b/MovementStrategy/IMoveableObject.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public interface IMoveableObject { + ObjectParameters GetObjectPosition(); + int GetStep(); + Boolean TryMoveObject(MovementDirection direction); +} diff --git a/MovementStrategy/MoveToBorder.java b/MovementStrategy/MoveToBorder.java new file mode 100644 index 0000000..b716856 --- /dev/null +++ b/MovementStrategy/MoveToBorder.java @@ -0,0 +1,36 @@ +package MovementStrategy; + +public class MoveToBorder extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth - GetStep() && + objParams.DownBorder + GetStep() >= FieldHeight - GetStep(); + } + + @Override + protected void MoveToTarget() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) + { + return; + } + + int diffX = FieldWidth - objParams.RightBorder; + if (Math.abs(diffX) > GetStep()) + { + MoveRight(); + } + + int diffY = FieldHeight - objParams.DownBorder; + if (Math.abs(diffY) > GetStep()) + { + MoveDown(); + } + } + +} diff --git a/MovementStrategy/MoveToCenter.java b/MovementStrategy/MoveToCenter.java new file mode 100644 index 0000000..3f2b69f --- /dev/null +++ b/MovementStrategy/MoveToCenter.java @@ -0,0 +1,38 @@ +package MovementStrategy; + +public class MoveToCenter extends AbstractStrategy{ + @Override + protected boolean IsTargetDestinaion() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 - GetStep() && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 - GetStep() && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 - GetStep() && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2 - GetStep() ; + } + @Override + protected void MoveToTarget() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.abs(diffX) > GetStep()) { + if (diffX > 0) { + MoveLeft(); + } else { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.abs(diffY) > GetStep()) { + if (diffY > 0) { + MoveUp(); + } else { + MoveDown(); + } + } + } +} diff --git a/MovementStrategy/MoveableTruck.java b/MovementStrategy/MoveableTruck.java new file mode 100644 index 0000000..c4f627a --- /dev/null +++ b/MovementStrategy/MoveableTruck.java @@ -0,0 +1,48 @@ +package MovementStrategy; + +import Drawnings.CanvasDumpTruck; +import Drawnings.DirectionType; +import Drawnings.DrawningTruck; + +public class MoveableTruck implements IMoveableObject { + private CanvasDumpTruck canvas = new CanvasDumpTruck(); + public MoveableTruck(DrawningTruck drawningTruck) + { + canvas._drawningTruck = drawningTruck; + } + @Override + public ObjectParameters GetObjectPosition() { + if (canvas._drawningTruck == null || canvas._drawningTruck.EntityTruck == null || + canvas._drawningTruck.GetPosX() == null || canvas._drawningTruck.GetPosY() == null) + { + return null; + } + return new ObjectParameters(canvas._drawningTruck.GetPosX(), canvas._drawningTruck.GetPosY(), + canvas._drawningTruck.GetWidth(), canvas._drawningTruck.GetHeight()); + } + + @Override + public int GetStep() { + return (int)(canvas._drawningTruck.EntityTruck.Step); + } + + @Override + public Boolean TryMoveObject(MovementDirection direction) { + if (canvas._drawningTruck == null || canvas._drawningTruck.EntityTruck == null) + { + return false; + } + return canvas._drawningTruck.MoveTransport(GetDirectionType(direction)); + } + + private static DirectionType GetDirectionType(MovementDirection direction) + { + switch (direction) { + case MovementDirection.Left: return DirectionType.Left; + case MovementDirection.Right: return DirectionType.Right; + case MovementDirection.Up: return DirectionType.Up; + case MovementDirection.Down: return DirectionType.Down; + default: return DirectionType.Unknow; + } + } +} diff --git a/MovementStrategy/MovementDirection.java b/MovementStrategy/MovementDirection.java new file mode 100644 index 0000000..c52f124 --- /dev/null +++ b/MovementStrategy/MovementDirection.java @@ -0,0 +1,8 @@ +package MovementStrategy; + +public enum MovementDirection { + Up, + Down, + Left, + Right +} diff --git a/MovementStrategy/ObjectParameters.java b/MovementStrategy/ObjectParameters.java new file mode 100644 index 0000000..3ff2428 --- /dev/null +++ b/MovementStrategy/ObjectParameters.java @@ -0,0 +1,27 @@ +package MovementStrategy; + +public class ObjectParameters { + private int _x; + private int _y; + private int _width; + private int _height; + public int LeftBorder = _x; + public int TopBorder = _y; + public int RightBorder = _x + _width; + public int DownBorder = _y + _height; + public int ObjectMiddleHorizontal = _x + _width / 2; + public int ObjectMiddleVertical = _y + _height / 2; + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + RightBorder = _x + _width; + DownBorder = _y + _height; + ObjectMiddleHorizontal = _x + _width / 2; + ObjectMiddleVertical = _y + _height / 2; + } + +}