From 02566d79573fbecc209a7a59b7433214ad7b6cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 14:43:17 +0400 Subject: [PATCH 01/15] Constructors have been added --- DrawingArtillery.java | 8 +++----- DrawingRollers.java | 2 +- EntityArtillery.java | 2 +- FormArtillery.java | 17 ++++++++--------- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/DrawingArtillery.java b/DrawingArtillery.java index f765894..472a1e2 100644 --- a/DrawingArtillery.java +++ b/DrawingArtillery.java @@ -14,11 +14,9 @@ public class DrawingArtillery { return artillery; } - public void Init(int speed, float weight, Color bodyColor, int rollersCount) { - artillery = new EntityArtillery(); - artillery.Init(speed, weight, bodyColor); - drawingRollers = new DrawingRollers(); - drawingRollers.Init(rollersCount, bodyColor); + public DrawingArtillery(int speed, float weight, Color bodyColor, int rollersCount) { + artillery = new EntityArtillery(speed, weight, bodyColor); + drawingRollers = new DrawingRollers(rollersCount, bodyColor); } public void SetPosition(int x, int y, int width, int height) { diff --git a/DrawingRollers.java b/DrawingRollers.java index 0b2b1fc..d12eafd 100644 --- a/DrawingRollers.java +++ b/DrawingRollers.java @@ -4,7 +4,7 @@ public class DrawingRollers { private RollersCount rollersCount; private Color color; - public void Init(int rollersCount, Color bodyColor) { + public DrawingRollers(int rollersCount, Color bodyColor) { setRollersCount(rollersCount); color = bodyColor; } diff --git a/EntityArtillery.java b/EntityArtillery.java index 4367dae..ef15317 100644 --- a/EntityArtillery.java +++ b/EntityArtillery.java @@ -6,7 +6,7 @@ public class EntityArtillery { private float weight; private Color bodyColor; - public void Init(int speed, float weight, Color bodyColor) { + public EntityArtillery(int speed, float weight, Color bodyColor) { Random rnd = new Random(); this.speed = speed <= 0 ? rnd.nextInt(50, 150) : speed; this.weight = weight <= 0 ? rnd.nextInt(40, 70) : weight; diff --git a/FormArtillery.java b/FormArtillery.java index 8e75a9a..97533a5 100644 --- a/FormArtillery.java +++ b/FormArtillery.java @@ -23,15 +23,14 @@ public class FormArtillery extends JFrame { this.setContentPane(artilleryPane); createButton.addActionListener(e -> { Random rnd = new Random(); - _artillery = new DrawingArtillery(); - _artillery.Init( - rnd.nextInt(100, 300), - rnd.nextInt(1000, 2000), - new Color( - rnd.nextInt(0, 256), - rnd.nextInt(0, 256), - rnd.nextInt(0, 256)), - rnd.nextInt(4, 7) + _artillery = new DrawingArtillery( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color( + rnd.nextInt(0, 256), + rnd.nextInt(0, 256), + rnd.nextInt(0, 256)), + rnd.nextInt(4, 7) ); _artillery.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); speedLabel.setText(String.format("Скорость: %s", _artillery.getArtillery().getSpeed())); -- 2.25.1 From 0e85f721778f6133d9d255eebb826920fbf84e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 14:55:39 +0400 Subject: [PATCH 02/15] Changed some private fields to protected in DrawingArtillery. Added method getCurrentPosition --- DrawingArtillery.java | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/DrawingArtillery.java b/DrawingArtillery.java index 472a1e2..1dd0216 100644 --- a/DrawingArtillery.java +++ b/DrawingArtillery.java @@ -1,14 +1,14 @@ import java.awt.*; public class DrawingArtillery { - private EntityArtillery artillery; - private DrawingRollers drawingRollers; - private float _startPosX; - private float _startPosY; + protected EntityArtillery artillery; + protected DrawingRollers drawingRollers; + protected float _startPosX; + protected float _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; - private final int _artilleryWidth = 80; - private final int _artilleryHeight = 50; + protected int _artilleryWidth = 80; + protected int _artilleryHeight = 50; public EntityArtillery getArtillery() { return artillery; @@ -19,6 +19,12 @@ public class DrawingArtillery { drawingRollers = new DrawingRollers(rollersCount, bodyColor); } + protected DrawingArtillery(int speed, float weight, Color bodyColor, int rollersCount, int artilleryWidth, int artilleryHeight) { + this(speed, weight, bodyColor, rollersCount); + _artilleryWidth = artilleryWidth; + _artilleryHeight = artilleryHeight; + } + public void SetPosition(int x, int y, int width, int height) { if (x < 0 || x + _artilleryWidth >= width) { @@ -80,7 +86,7 @@ public class DrawingArtillery { return; } g.setColor(artillery.getBodyColor() != null ? artillery.getBodyColor() : Color.BLACK); - g.fillRect((int) (_startPosX + _artilleryWidth / 8 * 2), (int) _startPosY, _artilleryWidth / 8 * 4, _artilleryWidth / 5); + g.fillRect((int) (_startPosX + _artilleryWidth / 8 * 2), (int) _startPosY, _artilleryWidth / 8 * 4, _artilleryHeight / 5); g.fillRect((int) _startPosX, (int) (_startPosY + _artilleryHeight / 5), _artilleryWidth, _artilleryHeight / 3); @@ -108,4 +114,8 @@ public class DrawingArtillery { _startPosY = _pictureHeight - _artilleryHeight; } } + + public float[] getCurrentPosition() { + return new float[] { _startPosX, _startPosX + _artilleryWidth - 1, _startPosY, _startPosY + _artilleryHeight -1 }; + } } -- 2.25.1 From e890502041298395b34f1cca33255de2acc9b82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 14:56:34 +0400 Subject: [PATCH 03/15] Added new option to Direction enum --- Direction.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Direction.java b/Direction.java index a641b80..3b7253a 100644 --- a/Direction.java +++ b/Direction.java @@ -2,5 +2,6 @@ public enum Direction { Up, Down, Left, - Right + Right, + None } -- 2.25.1 From 145d4dac45488829b2a833e5c264550123a9556e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 15:03:57 +0400 Subject: [PATCH 04/15] Added class EntityAdvancedArtillery --- EntityAdvancedArtillery.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 EntityAdvancedArtillery.java diff --git a/EntityAdvancedArtillery.java b/EntityAdvancedArtillery.java new file mode 100644 index 0000000..746d15d --- /dev/null +++ b/EntityAdvancedArtillery.java @@ -0,0 +1,26 @@ +import java.awt.*; + +public class EntityAdvancedArtillery extends EntityArtillery { + private Color dopColor; + private boolean weapon; + private boolean salvoBattery; + + public EntityAdvancedArtillery(int speed, float weight, Color bodyColor, Color dopColor, boolean weapon, boolean salvoBattery) { + super(speed, weight, bodyColor); + this.dopColor = dopColor; + this.weapon = weapon; + this.salvoBattery = salvoBattery; + } + + public Color getDopColor() { + return dopColor; + } + + public boolean getWeapon() { + return weapon; + } + + public boolean getSalvoBattery() { + return salvoBattery; + } +} -- 2.25.1 From 4e7db63b49d88662bfdeba25e6db9929f53d4a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 15:13:24 +0400 Subject: [PATCH 05/15] Added class DrawingAdvancedArtillery --- DrawingAdvancedArtillery.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 DrawingAdvancedArtillery.java diff --git a/DrawingAdvancedArtillery.java b/DrawingAdvancedArtillery.java new file mode 100644 index 0000000..d7d8c79 --- /dev/null +++ b/DrawingAdvancedArtillery.java @@ -0,0 +1,28 @@ +import java.awt.*; + +public class DrawingAdvancedArtillery extends DrawingArtillery { + public DrawingAdvancedArtillery(int speed, float weight, Color bodyColor, int rollersCount, Color dopColor, boolean weapon, boolean salvoBattery) { + super(speed, weight, bodyColor, rollersCount, 80, 50); + artillery = new EntityAdvancedArtillery(speed, weight, bodyColor, dopColor, weapon, salvoBattery); + } + + @Override + public void drawTransport(Graphics2D g) { + if (!(artillery instanceof EntityAdvancedArtillery advancedArtillery)) { + return; + } + + g.setColor(advancedArtillery.getDopColor()); + if (advancedArtillery.getWeapon()) { + g.setStroke(new BasicStroke(8)); + g.drawLine((int) _startPosX + _artilleryWidth / 2, (int) _startPosY + _artilleryHeight / 10, (int) _startPosX + _artilleryWidth, (int) _startPosY); + } + g.setStroke(new BasicStroke(6)); + if (advancedArtillery.getSalvoBattery()) { + g.drawLine((int) _startPosX + _artilleryWidth / 4, (int) _startPosY + _artilleryHeight / 4, (int) _startPosX + _artilleryWidth / 4 + _artilleryHeight / 4, (int) _startPosY - 5); + g.drawLine((int) _startPosX + _artilleryWidth / 4 - _artilleryHeight / 4, (int) _startPosY + _artilleryHeight / 4, (int) _startPosX + _artilleryWidth / 4, (int) _startPosY - 5); + g.drawLine((int) _startPosX + _artilleryWidth / 4 - _artilleryHeight / 2, (int) _startPosY + _artilleryHeight / 4, (int) _startPosX + _artilleryWidth / 4 - _artilleryHeight / 4, (int) _startPosY - 5); + } + super.drawTransport(g); + } +} -- 2.25.1 From f9908821273622afac07bbe55f3e62fd8a1e15d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 15:16:47 +0400 Subject: [PATCH 06/15] Added interface IDrawingObject --- IDrawingObject.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 IDrawingObject.java diff --git a/IDrawingObject.java b/IDrawingObject.java new file mode 100644 index 0000000..f1effa3 --- /dev/null +++ b/IDrawingObject.java @@ -0,0 +1,9 @@ +import java.awt.*; + +public interface IDrawingObject { + float getStep(); + void setObject(int x, int y, int width, int height); + void moveObject(Direction direction); + void drawingObject(Graphics2D g); + float[] getCurrentPosition(); +} -- 2.25.1 From 6840f8220ec58ebf2e98b16e781b5166387e1eb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 15:25:07 +0400 Subject: [PATCH 07/15] Added class DrawingObjectArtillery --- DrawingArtillery.java | 2 +- DrawingObjectArtillery.java | 37 +++++++++++++++++++++++++++++++++++++ FormArtillery.java | 2 +- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 DrawingObjectArtillery.java diff --git a/DrawingArtillery.java b/DrawingArtillery.java index 1dd0216..2b64328 100644 --- a/DrawingArtillery.java +++ b/DrawingArtillery.java @@ -25,7 +25,7 @@ public class DrawingArtillery { _artilleryHeight = artilleryHeight; } - public void SetPosition(int x, int y, int width, int height) { + public void setPosition(int x, int y, int width, int height) { if (x < 0 || x + _artilleryWidth >= width) { return; diff --git a/DrawingObjectArtillery.java b/DrawingObjectArtillery.java new file mode 100644 index 0000000..95a71e4 --- /dev/null +++ b/DrawingObjectArtillery.java @@ -0,0 +1,37 @@ +import java.awt.*; + +public class DrawingObjectArtillery implements IDrawingObject { + private DrawingArtillery _artillery = null; + + public DrawingObjectArtillery(DrawingArtillery artillery) { + _artillery = artillery; + } + + public float getStep() { + if (_artillery != null && _artillery.artillery != null) { + return _artillery.artillery.getStep(); + } + return 0; + } + + public float[] getCurrentPosition() { + if (_artillery != null) { + return _artillery.getCurrentPosition(); + } + return new float[] { 0, 0, 0, 0 }; + } + + public void moveObject(Direction direction) { + if (_artillery != null) { + _artillery.moveTransport(direction); + } + } + + public void setObject(int x, int y, int width, int height) { + _artillery.setPosition(x, y, width, height); + } + + public void drawingObject(Graphics2D g) { + _artillery.drawTransport(g); + } +} diff --git a/FormArtillery.java b/FormArtillery.java index 97533a5..6b22958 100644 --- a/FormArtillery.java +++ b/FormArtillery.java @@ -32,7 +32,7 @@ public class FormArtillery extends JFrame { rnd.nextInt(0, 256)), rnd.nextInt(4, 7) ); - _artillery.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); + _artillery.setPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); speedLabel.setText(String.format("Скорость: %s", _artillery.getArtillery().getSpeed())); weightLabel.setText(String.format("Вес: %s", _artillery.getArtillery().getWeight())); colorLabel.setText(String.format("Цвет: %x", _artillery.getArtillery().getBodyColor().getRGB())); -- 2.25.1 From ed422215f0fa30eb1545be59ec64f79e36c0a283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 15:40:01 +0400 Subject: [PATCH 08/15] Added class AbstractMap --- AbstractMap.java | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 AbstractMap.java diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..ce5d4b5 --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,102 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public abstract class AbstractMap { + private IDrawingObject _drawingObject = null; + protected int[][] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected final Random _random = new Random(); + protected final int _freeRoad = 0; + protected final int _barrier = 1; + + public Image createMap(int width, int height, IDrawingObject drawingObject) { + _width = width; + _height = height; + _drawingObject = drawingObject; + do { + generateMap(); + } while (!setObjectOnMap()); + return drawMapWithObject(); + } + + public Image moveObject(Direction direction) { + _drawingObject.moveObject(direction); + if (objectIntersects()) { + switch (direction) { + case Left -> _drawingObject.moveObject(Direction.Right); + case Right -> _drawingObject.moveObject(Direction.Left); + case Up -> _drawingObject.moveObject(Direction.Down); + case Down -> _drawingObject.moveObject(Direction.Up); + } + } + return drawMapWithObject(); + } + + private boolean setObjectOnMap() { + if (_drawingObject == null || _map == null) + { + return false; + } + + for (int i = 2; i < _map.length; i++) + { + for (int j = 2; j < _map[i].length; j++) + { + _drawingObject.setObject((int) (i * _size_x), (int) (j * _size_y), _width, _height); + if (!objectIntersects()) return true; + } + } + + return true; + } + + private boolean objectIntersects() { + float[] location = _drawingObject.getCurrentPosition(); + for (int i = 0; i < _map.length; i++) + { + for (int j = 0; j < _map[i].length; j++) + { + if (_map[i][j] == _barrier) + { + if (i * _size_x >= location[0] && (i + 1) * _size_x <= location[1] && j * _size_y >= location[2] && (j + 1) * _size_y <= location[3]) + { + return true; + } + } + } + } + + return false; + } + + private Image drawMapWithObject() { + Image img = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_ARGB); + if (_drawingObject == null || _map == null) { + return img; + } + Graphics2D g = (Graphics2D) img.getGraphics(); + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[i].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + drawRoadPart(g, i, j); + } else if (_map[i][j] == _barrier) + { + drawBarrierPart(g, i, j); + } + } + } + _drawingObject.drawingObject(g); + return img; + } + + protected abstract void generateMap(); + protected abstract void drawRoadPart(Graphics2D g, int i, int j); + protected abstract void drawBarrierPart(Graphics2D g, int i, int j); +} -- 2.25.1 From 0df20f400884b13a83435001471e5ac2236dce1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 16:04:32 +0400 Subject: [PATCH 09/15] Added class SimpleMap --- SimpleMap.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 SimpleMap.java diff --git a/SimpleMap.java b/SimpleMap.java new file mode 100644 index 0000000..26db081 --- /dev/null +++ b/SimpleMap.java @@ -0,0 +1,40 @@ +import java.awt.*; +import java.util.Arrays; + +public class SimpleMap extends AbstractMap { + private final Color barrierColor = Color.black; + private final Color roadColor = Color.gray; + + @Override + protected void drawBarrierPart(Graphics2D g, int i, int j) { + g.setColor(barrierColor); + g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); + } + + @Override + protected void drawRoadPart(Graphics2D g, int i, int j) { + g.setColor(roadColor); + g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); + } + + @Override + protected void generateMap() { + _map = new int[100][100]; + _size_x = (float)_width / _map[0].length; + _size_y = (float)_height / _map.length; + int counter = 0; + for (int[] row : _map) { + Arrays.fill(row, _freeRoad); + } + while (counter < 50) + { + int x = _random.nextInt(0, 100); + int y = _random.nextInt(0, 100); + if (_map[y][x] == _freeRoad) + { + _map[y][x] = _barrier; + counter++; + } + } + } +} -- 2.25.1 From a2e79b08bd4c9403e1b1dd2bce5b676c9f990d95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 16:16:03 +0400 Subject: [PATCH 10/15] Added class ForestMap --- ForestMap.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 ForestMap.java diff --git a/ForestMap.java b/ForestMap.java new file mode 100644 index 0000000..6a2eaf6 --- /dev/null +++ b/ForestMap.java @@ -0,0 +1,49 @@ +import java.awt.*; +import java.util.Arrays; + +public class ForestMap extends AbstractMap { + private final Color barrierColor = Color.green; + private final Color roadColor = new Color(165, 42, 42, 255); + + @Override + protected void drawBarrierPart(Graphics2D g, int i, int j) + { + g.setColor(barrierColor); + g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); + } + + @Override + protected void drawRoadPart(Graphics2D g, int i, int j) + { + g.setColor(roadColor); + g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); + } + + @Override + protected void generateMap() { + _map = new int[50][50]; + _size_x = (float)_width / _map[0].length; + _size_y = (float)_height / _map.length; + int counter = 0; + for (int[] row : _map) { + Arrays.fill(row, _freeRoad); + } + while (counter < 20) + { + int x = _random.nextInt(2, 49); + int y = _random.nextInt(3, 50); + var points = new int[] { _map[x][y], _map[x][y - 1], _map[x][y - 2], _map[x - 1][y - 2], _map[x + 1][y - 2], _map[x][y - 3] }; + var forComparison = new int[] { _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad }; + if (Arrays.equals(points, forComparison)) + { + _map[x][y] = _barrier; + _map[x][y - 1] = _barrier; + _map[x][y - 2] = _barrier; + _map[x - 1][y - 2] = _barrier; + _map[x + 1][y - 2] = _barrier; + _map[x][y - 3] = _barrier; + counter++; + } + } + } +} -- 2.25.1 From 7758a3b7cc963bfebbaa4278d674a340721cf4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 17:30:41 +0400 Subject: [PATCH 11/15] New form has been created. General work is done --- FormArtillery.form | 24 ++++--- FormArtillery.java | 28 +++++++- FormMap.form | 158 +++++++++++++++++++++++++++++++++++++++++++++ FormMap.java | 110 +++++++++++++++++++++++++++++++ Program.java | 2 +- 5 files changed, 312 insertions(+), 10 deletions(-) create mode 100644 FormMap.form create mode 100644 FormMap.java diff --git a/FormArtillery.form b/FormArtillery.form index 243bd18..d1e17c1 100644 --- a/FormArtillery.form +++ b/FormArtillery.form @@ -53,7 +53,7 @@ - + @@ -63,7 +63,7 @@ - + @@ -73,12 +73,12 @@ - + - + @@ -93,7 +93,7 @@ - + @@ -107,7 +107,7 @@ - + @@ -122,7 +122,7 @@ - + @@ -137,7 +137,7 @@ - + @@ -145,6 +145,14 @@ + + + + + + + + diff --git a/FormArtillery.java b/FormArtillery.java index 6b22958..69c29cd 100644 --- a/FormArtillery.java +++ b/FormArtillery.java @@ -1,5 +1,7 @@ import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.Random; @@ -15,6 +17,7 @@ public class FormArtillery extends JFrame { private JButton buttonDown; private JButton buttonLeft; private JButton buttonRight; + private JButton createAdvancedButton; private DrawingArtillery _artillery; @@ -61,12 +64,35 @@ public class FormArtillery extends JFrame { if (_artillery != null) _artillery.moveTransport(Direction.Down); repaint(); }); + createAdvancedButton.addActionListener(e -> { + Random rnd = new Random(); + _artillery = new DrawingAdvancedArtillery( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color( + rnd.nextInt(0, 256), + rnd.nextInt(0, 256), + rnd.nextInt(0, 256)), + rnd.nextInt(4, 7), + new Color( + rnd.nextInt(0, 256), + rnd.nextInt(0, 256), + rnd.nextInt(0, 256)), + rnd.nextBoolean(), + rnd.nextBoolean() + ); + _artillery.setPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight()); + speedLabel.setText(String.format("Скорость: %s", _artillery.getArtillery().getSpeed())); + weightLabel.setText(String.format("Вес: %s", _artillery.getArtillery().getWeight())); + colorLabel.setText(String.format("Цвет: %x", _artillery.getArtillery().getBodyColor().getRGB())); + repaint(); + }); } @Override public void paint(Graphics g) { super.paint(g); - Graphics2D g2d = (Graphics2D) artilleryPane.getGraphics(); + Graphics2D g2d = (Graphics2D) pictureBox.getGraphics(); if (_artillery != null) { _artillery.drawTransport(g2d); } diff --git a/FormMap.form b/FormMap.form new file mode 100644 index 0000000..62483a3 --- /dev/null +++ b/FormMap.form @@ -0,0 +1,158 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..465f70b --- /dev/null +++ b/FormMap.java @@ -0,0 +1,110 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.util.Random; + +public class FormMap extends JFrame { + private JPanel artilleryPane; + private JLabel speedLabel; + private JLabel weightLabel; + private JLabel colorLabel; + private JPanel pictureBox; + private JButton buttonUp; + private JButton buttonDown; + private JButton buttonLeft; + private JButton buttonRight; + private JButton createButton; + private JButton createAdvancedButton; + private JComboBox comboBoxSelectorMap; + + private AbstractMap _abstractMap; + private Image bufferedImage; + + public FormMap() { + this.setTitle("Artillery"); + this.setContentPane(artilleryPane); + + _abstractMap = new SimpleMap(); + + createButton.addActionListener(e -> { + Random rnd = new Random(); + var artillery = new DrawingArtillery( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextInt(4, 6 + 1) + ); + setData(artillery); + }); + buttonLeft.setForeground(new Color(0, 0, 0, 0)); + buttonRight.setForeground(new Color(0, 0, 0, 0)); + buttonUp.setForeground(new Color(0, 0, 0, 0)); + buttonDown.setForeground(new Color(0, 0, 0, 0)); + buttonLeft.addActionListener(e -> { + if (_abstractMap != null) { + bufferedImage = _abstractMap.moveObject(Direction.Left); + repaint(); + } + }); + buttonRight.addActionListener(e -> { + if (_abstractMap != null) { + bufferedImage = _abstractMap.moveObject(Direction.Right); + repaint(); + } + }); + buttonUp.addActionListener(e -> { + if (_abstractMap != null) { + bufferedImage = _abstractMap.moveObject(Direction.Up); + repaint(); + } + }); + buttonDown.addActionListener(e -> { + if (_abstractMap != null) { + bufferedImage = _abstractMap.moveObject(Direction.Down); + repaint(); + } + }); + createAdvancedButton.addActionListener(e -> { + Random rnd = new Random(); + var artillery = new DrawingAdvancedArtillery( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 2000), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextInt(4, 6 + 1), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), + rnd.nextBoolean() + ); + setData(artillery); + }); + comboBoxSelectorMap.addItemListener(e -> { + if (e.getStateChange() == ItemEvent.SELECTED) { + switch (e.getItem().toString()) { + case "Простая карта" -> _abstractMap = new SimpleMap(); + case "Лесная карта" -> _abstractMap = new ForestMap(); + } + } + }); + } + + private void setData(DrawingArtillery artillery) { + Random rnd = new Random(); + artillery.setPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100), pictureBox.getWidth(), pictureBox.getHeight()); + speedLabel.setText(String.format("Скорость: %d", artillery.artillery.getSpeed())); + weightLabel.setText(String.format("Вес: %f", artillery.artillery.getWeight())); + colorLabel.setText(String.format("Цвет: %x", artillery.getArtillery().getBodyColor().getRGB())); + bufferedImage = _abstractMap.createMap(pictureBox.getWidth(), pictureBox.getHeight(), new DrawingObjectArtillery(artillery)); + repaint(); + } + + @Override + public void paint(Graphics g) { + super.paint(g); + + if (bufferedImage != null) { + pictureBox.paintComponents(bufferedImage.getGraphics()); + pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null); + } + } +} diff --git a/Program.java b/Program.java index 36372f9..8d8002d 100644 --- a/Program.java +++ b/Program.java @@ -2,7 +2,7 @@ import javax.swing.*; public class Program { public static void main(String[] args) { - FormArtillery form = new FormArtillery(); + FormMap form = new FormMap(); form.setSize(640, 480); form.setVisible(true); form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); -- 2.25.1 From 0abd2c2ca32ba2a496a5186339595564f4dea57f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 17:51:17 +0400 Subject: [PATCH 12/15] Fixes --- AbstractMap.java | 3 ++- ForestMap.java | 18 +++++++++--------- SimpleMap.java | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/AbstractMap.java b/AbstractMap.java index ce5d4b5..8c96b91 100644 --- a/AbstractMap.java +++ b/AbstractMap.java @@ -56,13 +56,14 @@ public abstract class AbstractMap { private boolean objectIntersects() { float[] location = _drawingObject.getCurrentPosition(); + Rectangle self = new Rectangle((int) location[0], (int) location[2], (int) location[1] - (int) location[0], (int) location[3] - (int) location[2]); for (int i = 0; i < _map.length; i++) { for (int j = 0; j < _map[i].length; j++) { if (_map[i][j] == _barrier) { - if (i * _size_x >= location[0] && (i + 1) * _size_x <= location[1] && j * _size_y >= location[2] && (j + 1) * _size_y <= location[3]) + if (self.intersects(new Rectangle(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y))) { return true; } diff --git a/ForestMap.java b/ForestMap.java index 6a2eaf6..0936adb 100644 --- a/ForestMap.java +++ b/ForestMap.java @@ -9,14 +9,14 @@ public class ForestMap extends AbstractMap { protected void drawBarrierPart(Graphics2D g, int i, int j) { g.setColor(barrierColor); - g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); + g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y); } @Override protected void drawRoadPart(Graphics2D g, int i, int j) { g.setColor(roadColor); - g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); + g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y); } @Override @@ -32,16 +32,16 @@ public class ForestMap extends AbstractMap { { int x = _random.nextInt(2, 49); int y = _random.nextInt(3, 50); - var points = new int[] { _map[x][y], _map[x][y - 1], _map[x][y - 2], _map[x - 1][y - 2], _map[x + 1][y - 2], _map[x][y - 3] }; + var points = new int[] { _map[y][x], _map[y - 1][x], _map[y - 2][x], _map[y - 2][x - 1], _map[y - 2][x + 1], _map[y - 3][x] }; var forComparison = new int[] { _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad, _freeRoad }; if (Arrays.equals(points, forComparison)) { - _map[x][y] = _barrier; - _map[x][y - 1] = _barrier; - _map[x][y - 2] = _barrier; - _map[x - 1][y - 2] = _barrier; - _map[x + 1][y - 2] = _barrier; - _map[x][y - 3] = _barrier; + _map[y][x] = _barrier; + _map[y - 1][x] = _barrier; + _map[y - 2][x] = _barrier; + _map[y - 2][x - 1] = _barrier; + _map[y - 2][x + 1] = _barrier; + _map[y - 3][x] = _barrier; counter++; } } diff --git a/SimpleMap.java b/SimpleMap.java index 26db081..f9cb346 100644 --- a/SimpleMap.java +++ b/SimpleMap.java @@ -8,13 +8,13 @@ public class SimpleMap extends AbstractMap { @Override protected void drawBarrierPart(Graphics2D g, int i, int j) { g.setColor(barrierColor); - g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); + g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y); } @Override protected void drawRoadPart(Graphics2D g, int i, int j) { g.setColor(roadColor); - g.fillRect(i * (int) _size_x, j * (int) _size_y, i * ((int) _size_x + 1), j * ((int) _size_y + 1)); + g.fillRect(j * (int) _size_x, i * (int) _size_y, (int) _size_x, (int) _size_y); } @Override -- 2.25.1 From fbd46924d1ba0f4ca3f126755d695c2898d21525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 19:19:17 +0400 Subject: [PATCH 13/15] Added interface IDrawingRollers --- DrawingArtillery.java | 2 +- DrawingRollers.java | 2 +- IDrawingRollers.java | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 IDrawingRollers.java diff --git a/DrawingArtillery.java b/DrawingArtillery.java index 2b64328..2ea1b66 100644 --- a/DrawingArtillery.java +++ b/DrawingArtillery.java @@ -2,7 +2,7 @@ import java.awt.*; public class DrawingArtillery { protected EntityArtillery artillery; - protected DrawingRollers drawingRollers; + protected IDrawingRollers drawingRollers; protected float _startPosX; protected float _startPosY; private Integer _pictureWidth = null; diff --git a/DrawingRollers.java b/DrawingRollers.java index d12eafd..0c68703 100644 --- a/DrawingRollers.java +++ b/DrawingRollers.java @@ -1,6 +1,6 @@ import java.awt.*; -public class DrawingRollers { +public class DrawingRollers implements IDrawingRollers { private RollersCount rollersCount; private Color color; diff --git a/IDrawingRollers.java b/IDrawingRollers.java new file mode 100644 index 0000000..a8fbe42 --- /dev/null +++ b/IDrawingRollers.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawingRollers { + void setRollersCount(int num); + void draw(Graphics2D g, int x, int y, int artilleryWidth, int artilleryHeight); +} -- 2.25.1 From 1ef0f5465badd8e07f3cb177713544e426f9a248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 22:22:51 +0400 Subject: [PATCH 14/15] Added two new types of rollers to draw --- DrawingCrossRollers.java | 83 ++++++++++++++++++++++++++++ DrawingSquaredRollers.java | 107 +++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 DrawingCrossRollers.java create mode 100644 DrawingSquaredRollers.java diff --git a/DrawingCrossRollers.java b/DrawingCrossRollers.java new file mode 100644 index 0000000..05ffd33 --- /dev/null +++ b/DrawingCrossRollers.java @@ -0,0 +1,83 @@ +import java.awt.*; +import java.awt.geom.Line2D; + +public class DrawingCrossRollers implements IDrawingRollers { + private RollersCount rollersCount; + private Color color; + + public DrawingCrossRollers(int rollersCount, Color bodyColor) { + setRollersCount(rollersCount); + color = bodyColor; + } + + public void setRollersCount(int num) { + if (num <= 4) { + rollersCount = RollersCount.Four; + } else if (num >= 6) { + rollersCount = RollersCount.Six; + } else { + rollersCount = RollersCount.Five; + } + } + + public void draw(Graphics2D g, int x, int y, int artilleryWidth, int artilleryHeight) { + g.setColor(color != null ? color : Color.BLACK); + g.fillOval(x + artilleryWidth / 20, y + artilleryHeight * 7 / 15, artilleryWidth * 4 / 20, artilleryHeight * 10 / 32); + g.fillOval(x + artilleryWidth * 15 / 20, y + artilleryHeight * 7 / 15, artilleryWidth * 4 / 20, artilleryHeight * 10 / 32); + switch (rollersCount) { + case Six: { + g.fillOval(x + artilleryWidth * 8 / 20, y + artilleryHeight * 10 / 16, artilleryWidth * 2 / 20, artilleryHeight * 6 / 32); + } + case Five: { + g.fillOval(x + artilleryWidth * 10 / 20, y + artilleryHeight * 10 / 16, artilleryWidth * 2 / 20, artilleryHeight * 6 / 32); + } + case Four: { + g.fillOval(x + artilleryWidth * 5 / 20, y + artilleryHeight * 9 / 16, artilleryWidth * 3 / 20, artilleryHeight * 8 / 32); + g.fillOval(x + artilleryWidth * 12 / 20, y + artilleryHeight * 9 / 16, artilleryWidth * 3 / 20, artilleryHeight * 8 / 32); + } + } + drawOrnament(g, x, y, artilleryWidth, artilleryHeight); + } + + private void drawOrnament(Graphics2D g, int x, int y, int artilleryWidth, int artilleryHeight) { + g.setStroke(new BasicStroke(1)); + g.setColor(Color.BLACK); + + + g.drawLine(x + artilleryWidth / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 32, x + artilleryWidth * 5 / 20 - 1, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 32); + g.drawLine(x + artilleryWidth * 3 / 20, y + artilleryHeight * 7 / 15, x + artilleryWidth * 3 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 10 / 32); + g.drawLine(x + artilleryWidth * 2 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 64, x + artilleryWidth * 4 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 15 / 64); + g.drawLine(x + artilleryWidth * 4 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 64, x + artilleryWidth * 2 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 15 / 64); + + g.drawLine(x + artilleryWidth * 15 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 32, x + artilleryWidth * 19 / 20 - 1, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 32); + g.drawLine(x + artilleryWidth * 17 / 20, y + artilleryHeight * 7 / 15, x + artilleryWidth * 17 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 10 / 32); + g.drawLine(x + artilleryWidth * 16 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 64, x + artilleryWidth * 18 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 15 / 64); + g.drawLine(x + artilleryWidth * 18 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 64, x + artilleryWidth * 16 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 15 / 64); + + switch(rollersCount) { + case Six: { + g.drawLine(x + artilleryWidth * 8 / 20, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32, x + artilleryWidth * 10 / 20, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32); + g.drawLine(x + artilleryWidth * 9 / 20, y + artilleryHeight * 10 / 16, x + artilleryWidth * 9 / 20, y + artilleryHeight * 10 / 16 + artilleryHeight * 6 / 32); + g.drawLine(x + artilleryWidth * 8 / 20 + artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 - artilleryHeight * 3 / 64, x + artilleryWidth * 10 / 20 - artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 + artilleryHeight * 3 / 64); + g.drawLine(x + artilleryWidth * 9 / 20 + artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 - artilleryHeight * 3 / 64, x + artilleryWidth * 8 / 20 + artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 + artilleryHeight * 3 / 64); + } + case Five: { + g.drawLine(x + artilleryWidth * 10 / 20, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32, x + artilleryWidth * 12 / 20, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32); + g.drawLine(x + artilleryWidth * 11 / 20, y + artilleryHeight * 10 / 16, x + artilleryWidth * 11 / 20, y + artilleryHeight * 10 / 16 + artilleryHeight * 6 / 32); + g.drawLine(x + artilleryWidth * 12 / 20 + artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 - artilleryHeight * 3 / 64, x + artilleryWidth * 12 / 20 - artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 + artilleryHeight * 3 / 64); + g.drawLine(x + artilleryWidth * 11 / 20 + artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 - artilleryHeight * 3 / 64, x + artilleryWidth * 10 / 20 + artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 + artilleryHeight * 3 / 64); + } + case Four: { + g.drawLine(x + artilleryWidth * 5 / 20, y + artilleryHeight * 9 / 16 + artilleryHeight * 4 / 32, x + artilleryWidth * 5 / 20 + artilleryWidth * 3 / 20, y + artilleryHeight * 9 / 16 + artilleryHeight * 4 / 32); + g.drawLine(x + artilleryWidth * 5 / 20 + artilleryWidth * 3 / 40, y + artilleryHeight * 9 / 16, x + artilleryWidth * 5 / 20 + artilleryWidth * 3 / 40, y + artilleryHeight * 9 / 16 + artilleryHeight * 8 / 32); + g.drawLine(x + artilleryWidth * 5 / 20 + artilleryWidth * 3 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 2 / 32, x + artilleryWidth * 5 / 20 + artilleryWidth * 9 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 6 / 32); + g.drawLine(x + artilleryWidth * 5 / 20 + artilleryWidth * 9 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 2 / 32, x + artilleryWidth * 5 / 20 + artilleryWidth * 3 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 6 / 32); + + g.drawLine(x + artilleryWidth * 12 / 20, y + artilleryHeight * 9 / 16 + artilleryHeight * 4 / 32, x + artilleryWidth * 12 / 20 + artilleryWidth * 3 / 20, y + artilleryHeight * 9 / 16 + artilleryHeight * 4 / 32); + g.drawLine(x + artilleryWidth * 12 / 20 + artilleryWidth * 3 / 40, y + artilleryHeight * 9 / 16, x + artilleryWidth * 12 / 20 + artilleryWidth * 3 / 40, y + artilleryHeight * 9 / 16 + artilleryHeight * 8 / 32); + g.drawLine(x + artilleryWidth * 12 / 20 + artilleryWidth * 3 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 2 / 32, x + artilleryWidth * 12 / 20 + artilleryWidth * 9 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 6 / 32); + g.drawLine(x + artilleryWidth * 12 / 20 + artilleryWidth * 9 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 2 / 32, x + artilleryWidth * 12 / 20 + artilleryWidth * 3 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 6 / 32); + } + } + } +} diff --git a/DrawingSquaredRollers.java b/DrawingSquaredRollers.java new file mode 100644 index 0000000..b187b12 --- /dev/null +++ b/DrawingSquaredRollers.java @@ -0,0 +1,107 @@ +import java.awt.*; + +public class DrawingSquaredRollers implements IDrawingRollers { + private RollersCount rollersCount; + private Color color; + + public DrawingSquaredRollers(int rollersCount, Color bodyColor) { + setRollersCount(rollersCount); + color = bodyColor; + } + + public void setRollersCount(int num) { + if (num <= 4) { + rollersCount = RollersCount.Four; + } else if (num >= 6) { + rollersCount = RollersCount.Six; + } else { + rollersCount = RollersCount.Five; + } + } + + public void draw(Graphics2D g, int x, int y, int artilleryWidth, int artilleryHeight) { + g.setColor(color != null ? color : Color.BLACK); + g.fillOval(x + artilleryWidth / 20, y + artilleryHeight * 7 / 15, artilleryWidth * 4 / 20, artilleryHeight * 10 / 32); + g.fillOval(x + artilleryWidth * 15 / 20, y + artilleryHeight * 7 / 15, artilleryWidth * 4 / 20, artilleryHeight * 10 / 32); + switch (rollersCount) { + case Six: { + g.fillOval(x + artilleryWidth * 8 / 20, y + artilleryHeight * 10 / 16, artilleryWidth * 2 / 20, artilleryHeight * 6 / 32); + } + case Five: { + g.fillOval(x + artilleryWidth * 10 / 20, y + artilleryHeight * 10 / 16, artilleryWidth * 2 / 20, artilleryHeight * 6 / 32); + } + case Four: { + g.fillOval(x + artilleryWidth * 5 / 20, y + artilleryHeight * 9 / 16, artilleryWidth * 3 / 20, artilleryHeight * 8 / 32); + g.fillOval(x + artilleryWidth * 12 / 20, y + artilleryHeight * 9 / 16, artilleryWidth * 3 / 20, artilleryHeight * 8 / 32); + } + } + drawOrnament(g, x, y, artilleryWidth, artilleryHeight); + } + + private void drawOrnament(Graphics2D g, int x, int y, int artilleryWidth, int artilleryHeight) { + g.setStroke(new BasicStroke(1)); + g.setColor(Color.BLACK); + + Polygon bigRollerFirstPattern = new Polygon(); + bigRollerFirstPattern.addPoint(x + artilleryWidth / 20 + 1, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 32); + bigRollerFirstPattern.addPoint(x + artilleryWidth * 3 / 20, y + artilleryHeight * 7 / 15 + 1); + bigRollerFirstPattern.addPoint(x + artilleryWidth * 5 / 20 - 1, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 32); + bigRollerFirstPattern.addPoint(x + artilleryWidth * 3 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 10 / 32 - 1); + g.drawPolygon(bigRollerFirstPattern); + bigRollerFirstPattern.translate(artilleryWidth * 14 / 20, 0); + g.drawPolygon(bigRollerFirstPattern); + + Polygon bigRollerSecondPattern = new Polygon(); + bigRollerSecondPattern.addPoint(x + artilleryWidth * 2 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 64); + bigRollerSecondPattern.addPoint(x + artilleryWidth * 4 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 5 / 64); + bigRollerSecondPattern.addPoint(x + artilleryWidth * 4 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 15 / 64); + bigRollerSecondPattern.addPoint(x + artilleryWidth * 2 / 20, y + artilleryHeight * 7 / 15 + artilleryHeight * 15 / 64); + g.drawPolygon(bigRollerSecondPattern); + bigRollerSecondPattern.translate(artilleryWidth * 14 / 20, 0); + g.drawPolygon(bigRollerSecondPattern); + + Polygon smallRollerFirstPattern = new Polygon(); + smallRollerFirstPattern.addPoint(x + artilleryWidth * 8 / 20, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32); + smallRollerFirstPattern.addPoint(x + artilleryWidth * 9 / 20, y + artilleryHeight * 10 / 16); + smallRollerFirstPattern.addPoint(x + artilleryWidth * 10 / 20, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32); + smallRollerFirstPattern.addPoint(x + artilleryWidth * 9 / 20, y + artilleryHeight * 10 / 16 + artilleryHeight * 6 / 32); + + Polygon smallRollerSecondPattern = new Polygon(); + smallRollerSecondPattern.addPoint(x + artilleryWidth * 8 / 20 + artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 - artilleryHeight * 3 / 64); + smallRollerSecondPattern.addPoint(x + artilleryWidth * 9 / 20 + artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 - artilleryHeight * 3 / 64); + smallRollerSecondPattern.addPoint(x + artilleryWidth * 10 / 20 - artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 + artilleryHeight * 3 / 64); + smallRollerSecondPattern.addPoint(x + artilleryWidth * 8 / 20 + artilleryWidth * 2 / 80, y + artilleryHeight * 10 / 16 + artilleryHeight * 3 / 32 + artilleryHeight * 3 / 64); + + switch(rollersCount) { + case Six: { + g.drawPolygon(smallRollerFirstPattern); + g.drawPolygon(smallRollerSecondPattern); + } + case Five: { + smallRollerFirstPattern.translate(artilleryWidth * 2 / 20, 0); + smallRollerSecondPattern.translate(artilleryWidth * 2 / 20, 0); + g.drawPolygon(smallRollerFirstPattern); + g.drawPolygon(smallRollerSecondPattern); + } + case Four: { + Polygon mediumRollerFirstPattern = new Polygon(); + mediumRollerFirstPattern.addPoint(x + artilleryWidth * 5 / 20, y + artilleryHeight * 9 / 16 + artilleryHeight * 4 / 32); + mediumRollerFirstPattern.addPoint(x + artilleryWidth * 5 / 20 + artilleryWidth * 3 / 40, y + artilleryHeight * 9 / 16); + mediumRollerFirstPattern.addPoint(x + artilleryWidth * 5 / 20 + artilleryWidth * 3 / 20, y + artilleryHeight * 9 / 16 + artilleryHeight * 4 / 32); + mediumRollerFirstPattern.addPoint(x + artilleryWidth * 5 / 20 + artilleryWidth * 3 / 40, y + artilleryHeight * 9 / 16 + artilleryHeight * 8 / 32); + g.drawPolygon(mediumRollerFirstPattern); + mediumRollerFirstPattern.translate(artilleryWidth * 7 / 20, 0); + g.drawPolygon(mediumRollerFirstPattern); + + Polygon mediumRollerSecondPattern = new Polygon(); + mediumRollerSecondPattern.addPoint(x + artilleryWidth * 5 / 20 + artilleryWidth * 3 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 2 / 32); + mediumRollerSecondPattern.addPoint(x + artilleryWidth * 5 / 20 + artilleryWidth * 9 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 2 / 32); + mediumRollerSecondPattern.addPoint(x + artilleryWidth * 5 / 20 + artilleryWidth * 9 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 6 / 32); + mediumRollerSecondPattern.addPoint(x + artilleryWidth * 5 / 20 + artilleryWidth * 3 / 80, y + artilleryHeight * 9 / 16 + artilleryHeight * 6 / 32); + g.drawPolygon(mediumRollerSecondPattern); + mediumRollerSecondPattern.translate(artilleryWidth * 7 / 20, 0); + g.drawPolygon(mediumRollerSecondPattern); + } + } + } +} -- 2.25.1 From fb4c5f341692fc103e40cdeb09631e6e3ad145b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Sat, 8 Oct 2022 23:03:27 +0400 Subject: [PATCH 15/15] Work is done 02 --- DrawingArtillery.java | 2 +- DrawingCrossRollers.java | 1 - RollersType.java | 17 +++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 RollersType.java diff --git a/DrawingArtillery.java b/DrawingArtillery.java index 2ea1b66..2d8c095 100644 --- a/DrawingArtillery.java +++ b/DrawingArtillery.java @@ -16,7 +16,7 @@ public class DrawingArtillery { public DrawingArtillery(int speed, float weight, Color bodyColor, int rollersCount) { artillery = new EntityArtillery(speed, weight, bodyColor); - drawingRollers = new DrawingRollers(rollersCount, bodyColor); + drawingRollers = RollersType.random(rollersCount, bodyColor); } protected DrawingArtillery(int speed, float weight, Color bodyColor, int rollersCount, int artilleryWidth, int artilleryHeight) { diff --git a/DrawingCrossRollers.java b/DrawingCrossRollers.java index 05ffd33..c8ce547 100644 --- a/DrawingCrossRollers.java +++ b/DrawingCrossRollers.java @@ -1,5 +1,4 @@ import java.awt.*; -import java.awt.geom.Line2D; public class DrawingCrossRollers implements IDrawingRollers { private RollersCount rollersCount; diff --git a/RollersType.java b/RollersType.java new file mode 100644 index 0000000..efa60c6 --- /dev/null +++ b/RollersType.java @@ -0,0 +1,17 @@ +import java.awt.*; +import java.util.Random; + +public enum RollersType { + Standard, + Squared, + Cross; + + public static IDrawingRollers random(int rollersCount, Color bodyColor) { + return switch (new Random().nextInt(RollersType.values().length)) { + case 0 -> new DrawingRollers(rollersCount, bodyColor); + case 1 -> new DrawingSquaredRollers(rollersCount, bodyColor); + case 2 -> new DrawingCrossRollers(rollersCount, bodyColor); + default -> null; + }; + } +} -- 2.25.1