diff --git a/DifferenceOfWheels/DrawningOrnamentRectangle.java b/DifferenceOfWheels/DrawningOrnamentRectangle.java new file mode 100644 index 0000000..9bf32db --- /dev/null +++ b/DifferenceOfWheels/DrawningOrnamentRectangle.java @@ -0,0 +1,47 @@ +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); + } + + @Override + public void DrawWeels(Graphics2D g, int x, int y) { + switch (CountWeel){ + case 2: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 3: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 38, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 4: + DrawOrnament(g, x + 5, y + 67); + DrawOrnament(g, x + 32, y + 67); + DrawOrnament(g, x + 61, y + 67); + DrawOrnament(g, x + 88, y + 67); + break; + } + } +} diff --git a/DifferenceOfWheels/DrawningOrnamentStar.java b/DifferenceOfWheels/DrawningOrnamentStar.java new file mode 100644 index 0000000..0765524 --- /dev/null +++ b/DifferenceOfWheels/DrawningOrnamentStar.java @@ -0,0 +1,57 @@ +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); + } + + @Override + public void DrawWeels(Graphics2D g, int x, int y) { + switch (CountWeel){ + case 2: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 3: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 38, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 4: + DrawOrnament(g, x + 5, y + 67); + DrawOrnament(g, x + 32, y + 67); + DrawOrnament(g, x + 61, y + 67); + DrawOrnament(g, x + 88, y + 67); + break; + } + } +} diff --git a/DifferenceOfWheels/DrawningOrnamentTriangle.java b/DifferenceOfWheels/DrawningOrnamentTriangle.java new file mode 100644 index 0000000..58bc5a9 --- /dev/null +++ b/DifferenceOfWheels/DrawningOrnamentTriangle.java @@ -0,0 +1,51 @@ +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); + } + + @Override + public void DrawWeels(Graphics2D g, int x, int y) { + switch (CountWeel){ + case 2: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 3: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 38, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 4: + DrawOrnament(g, x + 5, y + 67); + DrawOrnament(g, x + 32, y + 67); + DrawOrnament(g, x + 61, y + 67); + DrawOrnament(g, x + 88, y + 67); + break; + } + } + +} diff --git a/DifferenceOfWheels/DrawningWheels.java b/DifferenceOfWheels/DrawningWheels.java new file mode 100644 index 0000000..b6ea6b4 --- /dev/null +++ b/DifferenceOfWheels/DrawningWheels.java @@ -0,0 +1,53 @@ +package DifferenceOfWheels; + +import java.awt.*; +import java.util.Random; + +public class DrawningWheels implements IOrnaments { + int CountWeel; + 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); + + } + + @Override + public void DrawWeels(Graphics2D g, int x, int y) { + switch (CountWeel){ + case 2: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 3: + DrawOrnament(g, x + 10, y + 67); + DrawOrnament(g, x + 38, y + 67); + DrawOrnament(g, x + 85, y + 67); + break; + case 4: + DrawOrnament(g, x + 5, y + 67); + DrawOrnament(g, x + 32, y + 67); + DrawOrnament(g, x + 61, y + 67); + DrawOrnament(g, x + 88, y + 67); + break; + } + } +} diff --git a/DopEnum.java b/DifferenceOfWheels/EnumWheels.java similarity index 65% rename from DopEnum.java rename to DifferenceOfWheels/EnumWheels.java index 22f1a64..eeb6295 100644 --- a/DopEnum.java +++ b/DifferenceOfWheels/EnumWheels.java @@ -1,11 +1,13 @@ -public enum DopEnum { +package DifferenceOfWheels; + +public enum EnumWheels { Two (2), Three (3), Four (4); private int number; - DopEnum(int n){ + EnumWheels(int n){ this.number = n; } diff --git a/DifferenceOfWheels/IOrnaments.java b/DifferenceOfWheels/IOrnaments.java new file mode 100644 index 0000000..89731b8 --- /dev/null +++ b/DifferenceOfWheels/IOrnaments.java @@ -0,0 +1,10 @@ +package DifferenceOfWheels; +import java.awt.*; + +public interface IOrnaments { + void SetCount(int n); + int get_count_weels(); + void DrawOrnament(Graphics2D g, int x, int y); + + void DrawWeels(Graphics2D g, int x, int y); +} diff --git a/DrawningDumpTruck.java b/DrawningDumpTruck.java deleted file mode 100644 index 5937cc0..0000000 --- a/DrawningDumpTruck.java +++ /dev/null @@ -1,191 +0,0 @@ -import java.awt.*; -import java.util.Random; -//import javax.swing.*; - -public class DrawningDumpTruck { - public EntityDumpTruck EntityDumpTruck; - - private EntityDumpTruck get_EntityDumpTruck() { return EntityDumpTruck; } - public DrawningWeels _drawingWeels = null; - - // Ширина окна - private Integer _pictureWidth; - - // Высота окна - private Integer _pictureHeight; - - // Левая координата прорисовки автомобиля - private Integer _startPosX; - - // Верхняя кооридната прорисовки автомобиля - private Integer _startPosY; - - // Ширина прорисовки самосвала - private final int _drawningDumpTruckWidth = 116; - - // Высота прорисовки самосвала - private final int _drawingDumpTruckHeight = 100; - Random random = new Random(); - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent) - { - EntityDumpTruck = new EntityDumpTruck(); - EntityDumpTruck.Init(speed, weight, bodyColor, additionalColor, body, tent); - _pictureWidth = null; - _pictureHeight = null; - _startPosX = null; - _startPosY = null; - _drawingWeels = new DrawningWeels(random.nextInt(2, 5)); - - } - - public Boolean SetPictureSize(int width, int height) - { - if (_drawningDumpTruckWidth > width || _drawingDumpTruckHeight > height) - { - return false; - } - - if (_pictureWidth != null && width != _pictureWidth || _pictureHeight != null && height != _pictureHeight) - { - if (_startPosX + _drawningDumpTruckWidth > width) - { - _startPosX -= _drawningDumpTruckWidth; - } - - if (_startPosY + _drawingDumpTruckHeight > height) - { - _startPosY -= _drawingDumpTruckHeight; - } - } - - _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 + _drawningDumpTruckWidth > _pictureWidth) - { - x = _pictureWidth - _drawningDumpTruckWidth; - } - if (y < 0) - { - y = 0; - } - if (y + _drawingDumpTruckHeight > _pictureHeight) - { - y = _pictureHeight - _drawingDumpTruckHeight; - - } - - _startPosX = x; - _startPosY = y; - - } - - public Boolean MoveTransport(DirectionType direction) - { - if (EntityDumpTruck == null || _startPosX == null || _startPosY == null) - { - return false; - } - - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX - _drawningDumpTruckWidth > 0) - { - _startPosX -= (int)EntityDumpTruck.Step; - } - return true; - //вверх - case DirectionType.Up: - if (_startPosY - _drawingDumpTruckHeight > 0) - { - _startPosY -= (int)EntityDumpTruck.Step; - } - return true; - // вправо - case DirectionType.Right: - if (_startPosX + _drawningDumpTruckWidth + (int)EntityDumpTruck.Step < _pictureWidth) - { - _startPosX += (int)EntityDumpTruck.Step; - } - return true; - //вниз - case DirectionType.Down: - if (_startPosY + _drawingDumpTruckHeight + (int)EntityDumpTruck.Step < _pictureHeight) - { - _startPosY += (int)EntityDumpTruck.Step; - } - return true; - default: - return false; - } - } - - public void DrawTransport(Graphics2D g) - { - if (EntityDumpTruck == null || _startPosX == null || _startPosY == null) - { - return; - } - - g.setPaint(Color.BLACK); - - if (EntityDumpTruck.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(EntityDumpTruck.AdditionalColor); - g.fillPolygon(x_b, y_b, 4); - } - - if (EntityDumpTruck.Tent && EntityDumpTruck.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(EntityDumpTruck.AdditionalColor); - g.fillPolygon(x_t, y_t, 3); - } - - // Колёса - g.setPaint(Color.BLACK); - if (_drawingWeels != null){ - _drawingWeels.DrawningWeel(g, _startPosX, _startPosY); - } - - //Границы самосвала - 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(EntityDumpTruck.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/DrawningWeels.java b/DrawningWeels.java index ddd4baf..d4ab789 100644 --- a/DrawningWeels.java +++ b/DrawningWeels.java @@ -1,7 +1,6 @@ import java.awt.*; public class DrawningWeels { - private int count; DrawningWeels (int n){ count = n; @@ -12,7 +11,6 @@ public class DrawningWeels { public void DrawningWeel (Graphics g, int x, int y){ g.setColor(Color.BLACK); - //g.fillOval(x, y, 25, 25); switch (get_countWheels()){ case 2: diff --git a/Drawnings/CanvasDumpTruck.java b/Drawnings/CanvasDumpTruck.java new file mode 100644 index 0000000..635a2ba --- /dev/null +++ b/Drawnings/CanvasDumpTruck.java @@ -0,0 +1,18 @@ +package Drawnings; +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/DirectionType.java b/Drawnings/DirectionType.java similarity index 69% rename from DirectionType.java rename to Drawnings/DirectionType.java index 95f37e8..9c1c629 100644 --- a/DirectionType.java +++ b/Drawnings/DirectionType.java @@ -1,6 +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..37c2187 --- /dev/null +++ b/Drawnings/DrawningDumpTruck.java @@ -0,0 +1,58 @@ +package Drawnings; +import Entities.EntityDumpTruck; + +import java.awt.*; + +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); + } + + public void DrawTransport(Graphics2D g) + { + if (EntityTruck == null || !(EntityTruck instanceof EntityDumpTruck entityTruck) || _startPosX == null || _startPosY == null) + { + return; + } + + g.setPaint(Color.BLACK); + + _startPosX += 5; + super.DrawTransport(g); + _startPosX -= 5; + + 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); + } + + } +} diff --git a/Drawnings/DrawningTruck.java b/Drawnings/DrawningTruck.java new file mode 100644 index 0000000..d24177e --- /dev/null +++ b/Drawnings/DrawningTruck.java @@ -0,0 +1,208 @@ +package Drawnings; + +import java.awt.*; +import java.util.Random; +import DifferenceOfWheels.*; +import Entities.EntityTruck; + + +public class DrawningTruck { + public EntityTruck EntityTruck; + protected 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){ + _ornament.DrawWeels(g, _startPosX - 5, _startPosY); + } + + //Границы самосвала + 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); + + } + +} diff --git a/Entities/EntityDumpTruck.java b/Entities/EntityDumpTruck.java new file mode 100644 index 0000000..80afee1 --- /dev/null +++ b/Entities/EntityDumpTruck.java @@ -0,0 +1,26 @@ +package Entities; + +import java.awt.*; + +public class EntityDumpTruck extends EntityTruck { + + public Color AdditionalColor; + private Color get_AdditionalColor(){ return AdditionalColor; } + + public Boolean Body; + private Boolean get_Body() { return Body; } + + public Boolean Tent; + private Boolean get_Tent() { return Tent; } + + public double Step; + + 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/EntityDumpTruck.java b/EntityDumpTruck.java deleted file mode 100644 index 0edb60f..0000000 --- a/EntityDumpTruck.java +++ /dev/null @@ -1,35 +0,0 @@ -import java.awt.*; - -public class EntityDumpTruck { - 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 Color AdditionalColor; - private Color get_AdditionalColor(){ return AdditionalColor; } - - public Boolean Body; - private Boolean get_Body() { return Body; } - - public Boolean Tent; - private Boolean get_Tent() { return Tent; } - - public double Step; - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, Boolean body, Boolean tent) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Body = body; - Tent = tent; - Step = speed * 100 / weight; - } - -} diff --git a/FormDumpTruck.java b/FormDumpTruck.java index 3028df0..ffc2f07 100644 --- a/FormDumpTruck.java +++ b/FormDumpTruck.java @@ -1,3 +1,8 @@ +import Drawnings.CanvasDumpTruck; +import Drawnings.DirectionType; +import Drawnings.DrawningDumpTruck; +import MovementStrategy.*; + import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -10,24 +15,58 @@ public class FormDumpTruck extends JFrame{ private CanvasDumpTruck _canvasDumpTruck = new CanvasDumpTruck(); + private AbstractStrategy _strategy; + // Размер формы private Dimension dimension; // Название формы private String title; - private JButton CreateButton = new JButton("Создать самосвал"); + 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() { - CreateButton.setName("CREATE"); + CreateDumpTruckButton.setName("Создать самосвал"); + CreateTruckButton.setName("Создать грузовик"); setMinimumSize(dimension); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -47,40 +86,76 @@ public class FormDumpTruck extends JFrame{ Icon iconRight = new ImageIcon("C:\\Users\\Antonovs\\Downloads\\ArrowRight.png"); RightButton.setIcon(iconRight); - CreateButton.addActionListener(new ActionListener() { + CreateTruckButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - Random random = new Random(); - _canvasDumpTruck._drawningDumpTruck = new DrawningDumpTruck(); - Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); - Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)); - _canvasDumpTruck._drawningDumpTruck.Init(random.nextInt(1000, 3000), random.nextInt(1000, 3000), - bodyColor, additionalColor, - random.nextBoolean(), random.nextBoolean()); + CreateObject("DrawningTruck"); + } + }); - _canvasDumpTruck._drawningDumpTruck.SetPictureSize(getWidth(), getHeight()); - _canvasDumpTruck._drawningDumpTruck.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); - _canvasDumpTruck.repaint(); + CreateDumpTruckButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + CreateObject("DrawningDumpTruck"); + } + }); + + 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._drawningDumpTruck == null) return; + if (_canvasDumpTruck._drawningTruck == null) return; boolean result = false; switch ((((JButton) (event.getSource())).getName())) { case "UP": - result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Up); + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Up); break; case "DOWN": - result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Down); + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Down); break; case "LEFT": - result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Left); + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Left); break; case "RIGHT": - result = _canvasDumpTruck._drawningDumpTruck.MoveTransport(DirectionType.Right); + result = _canvasDumpTruck._drawningTruck.MoveTransport(DirectionType.Right); break; } if (result) { @@ -97,17 +172,27 @@ public class FormDumpTruck extends JFrame{ setSize(dimension.width, dimension.height); setLayout(null); _canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 160, 40); + // кнопки создаиния самосвала и грузовика + CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40); + CreateTruckButton.setBounds(160, getHeight() - 90, 160, 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); - add(CreateButton); + // кнокпи для стратегий + 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(_canvasDumpTruck); + add(ComboBoxStrategy); + add(ButtonStrategy); pack(); setVisible(true); @@ -115,14 +200,17 @@ public class FormDumpTruck extends JFrame{ public void componentResized(ComponentEvent e) { int Width = getWidth(); int Height = getHeight(); - if (_canvasDumpTruck._drawningDumpTruck != null) - _canvasDumpTruck._drawningDumpTruck.SetPictureSize(Width, Height); + if (_canvasDumpTruck._drawningTruck != null) + _canvasDumpTruck._drawningTruck.SetPictureSize(Width, Height); _canvasDumpTruck.setBounds(0, 0, getWidth(), getHeight()); - CreateButton.setBounds(10, getHeight() - 90, 160, 40); + CreateDumpTruckButton.setBounds(10, getHeight() - 90, 160, 40); + CreateTruckButton.setBounds(180, getHeight() - 90, 160, 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/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; + } + +} diff --git a/MovementStrategy/StrategyStatus.java b/MovementStrategy/StrategyStatus.java new file mode 100644 index 0000000..4f087ea --- /dev/null +++ b/MovementStrategy/StrategyStatus.java @@ -0,0 +1,7 @@ +package MovementStrategy; + +public enum StrategyStatus { + NotInit, + InProgress, + Finish +}