From 8e2dd53ece14cb31c11444476430676f6de04a54 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 16:09:23 +0400 Subject: [PATCH 01/14] Initial Commit. LabWork02 started --- FormLocomotive.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/FormLocomotive.java b/FormLocomotive.java index fe6d577..0d453aa 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -55,25 +55,25 @@ public class FormLocomotive extends JComponent{ statusPanel.add(weightLabel); statusPanel.add(colorLabel); - JButton moveDownButton = new JButton("Down"); + JButton moveDownButton = new JButton("D"); moveDownButton.addActionListener(e -> { if (_locomotive != null) _locomotive.MoveTransport(Direction.Down); repaint(); }); - JButton moveUpButton = new JButton("Up"); + JButton moveUpButton = new JButton("U"); moveUpButton.addActionListener(e -> { if (_locomotive != null) _locomotive.MoveTransport(Direction.Up); repaint(); }); - JButton moveLeftButton = new JButton("Left"); + JButton moveLeftButton = new JButton("L"); moveLeftButton.addActionListener(e -> { if (_locomotive != null) _locomotive.MoveTransport(Direction.Left); repaint(); }); - JButton moveRightButton = new JButton("Right"); + JButton moveRightButton = new JButton("R"); moveRightButton.addActionListener(e -> { if (_locomotive != null) _locomotive.MoveTransport(Direction.Right); repaint(); @@ -96,6 +96,6 @@ public class FormLocomotive extends JComponent{ } public static void main(String[] args) { - FormLocomotive formLocomotive = new FormLocomotive(); + new FormLocomotive(); } } From 885fa94fd3562c4b840c3af4dce6bf1b56d7da44 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 17:10:11 +0400 Subject: [PATCH 02/14] IDrawningObject & AbstractMap created & fixed for Java --- AbstractMap.java | 163 +++++++++++++++++++++++++++++++++++++++++++ IDrawningObject.java | 17 +++++ 2 files changed, 180 insertions(+) create mode 100644 AbstractMap.java create mode 100644 IDrawningObject.java diff --git a/AbstractMap.java b/AbstractMap.java new file mode 100644 index 0000000..f56d3ae --- /dev/null +++ b/AbstractMap.java @@ -0,0 +1,163 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Random; + +public abstract class AbstractMap { + private IDrawningObject _drawningObject = 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; + + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics g, int i, int j); + protected abstract void DrawBarrierPart(Graphics g, int i, int j); + + public BufferedImage CreateMap(int width, int height, IDrawningObject drawningObject) + { + _width = width; + _height = height; + _drawningObject = drawningObject; + GenerateMap(); // abstract void + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + + public BufferedImage MoveObject(Direction direction) + { + boolean isFree = true; + + int startPosX = (int)(_drawningObject.GetCurrentPosition()[3] / _size_x); + int startPosY = (int)(_drawningObject.GetCurrentPosition()[0] / _size_y); + + int objectWidth = (int)(_drawningObject.GetCurrentPosition()[1] / _size_x); + int objectHeight = (int)(_drawningObject.GetCurrentPosition()[2] / _size_y); + + switch (direction) + { + case Right: + for (int i = objectWidth; i <= objectWidth + (int)(_drawningObject.getStep() / _size_x); i++) + { + for (int j = startPosY; j <= objectHeight; j++) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Left: + for (int i = startPosX; i >= (int)(_drawningObject.getStep() / _size_x); i--) + { + for (int j = startPosY; j <= objectHeight; j++) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Up: + for (int i = startPosX; i <= objectWidth; i++) + { + for (int j = startPosY; j >= (int)(_drawningObject.getStep() / _size_y); j--) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + + case Down: + for (int i = startPosX; i <= objectWidth; i++) + { + for (int j = objectHeight; j <= objectHeight + (int)(_drawningObject.getStep() / _size_y); j++) + { + if (_map[i][j] == _barrier) + { + isFree = false; + break; + } + } + } + break; + } + if (isFree) + { + _drawningObject.MoveObject(direction); + } + return DrawMapWithObject(); + } + + private boolean SetObjectOnMap() + { + if (_drawningObject == null || _map == null) + { + return false; + } + int x = _random.nextInt( 10); + int y = _random.nextInt( 10); + _drawningObject.SetObject(x, y, _width, _height); + + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + if (i * _size_x >= x && j * _size_y >= y && + i * _size_x <= x + _drawningObject.GetCurrentPosition()[1] && + j * _size_y <= j + _drawningObject.GetCurrentPosition()[2]) + { + if (_map[i][j] == _barrier) + { + return false; + } + } + } + } + + return true; + } + + private BufferedImage DrawMapWithObject() + { + BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); + if (_drawningObject == null || _map == null) + { + return bmp; + } + Graphics gr = bmp.getGraphics(); + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + if (_map[i][j] == _freeRoad) + { + DrawRoadPart(gr, i, j); + } + else if (_map[i][j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + _drawningObject.DrawningObject(gr); + return bmp; + } + +} diff --git a/IDrawningObject.java b/IDrawningObject.java new file mode 100644 index 0000000..80b49ff --- /dev/null +++ b/IDrawningObject.java @@ -0,0 +1,17 @@ +import java.awt.*; +public interface IDrawningObject { + /// Шаг перемещения объекта + float getStep(); + /// Установка позиции объекта + void SetObject(int x, int y, int width, int height); + /// Изменение направления перемещения объекта + void MoveObject(Direction direction); + /// Отрисовка объекта + void DrawningObject(Graphics g); + /// Получение текущей позиции объекта + float[] GetCurrentPosition(); + //0 - up + //1 - right + //2 - down + //3 - left +} From e9053085ef2db48aed4f30ea3643bd878ab408bb Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 17:29:51 +0400 Subject: [PATCH 03/14] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=85=D0=BE?= =?UTF-8?q?=D0=B4=20=D0=BD=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80?= =?UTF-8?q?=D1=83=D0=BA=D1=82=D0=BE=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrawningLocomotive.java | 10 +++++----- EntityLocomotive.java | 2 +- ExtraWheelsDraw.java | 2 +- FormLocomotive.java | 5 +---- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 7baf701..e2cc62a 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -1,4 +1,5 @@ import java.awt.*; +import java.util.Random; class DrawningLocomotive { public EntityLocomotive Locomotive; @@ -16,12 +17,11 @@ class DrawningLocomotive { /// Высота отрисовки локомотива private final int _locomotiveHeight = 50; /// Инициализация свойств - public void Init(int speed, float weight, Color bodyColor, int wheelsNum, EntityLocomotive entity) + private final Random random = new Random(); + public DrawningLocomotive(int speed, float weight, Color bodyColor) { - Locomotive = entity; - extraWheelsDraw = new ExtraWheelsDraw(); - extraWheelsDraw.Init(wheelsNum, bodyColor); - Locomotive.Init(speed, weight, bodyColor); + extraWheelsDraw = new ExtraWheelsDraw(random.nextInt(3), bodyColor); + Locomotive = new EntityLocomotive(speed, weight, bodyColor); } /// Установка позиции локомотива public void SetPosition(int x, int y, int width, int height) diff --git a/EntityLocomotive.java b/EntityLocomotive.java index 440b8a1..a4d15d7 100644 --- a/EntityLocomotive.java +++ b/EntityLocomotive.java @@ -19,7 +19,7 @@ public class EntityLocomotive { return Speed * 100 / Weight; } - public void Init(int speed, float weight, Color bodyColor) + public EntityLocomotive(int speed, float weight, Color bodyColor) { Random rnd = new Random(); if (speed <= 0) { diff --git a/ExtraWheelsDraw.java b/ExtraWheelsDraw.java index 4a19792..551b428 100644 --- a/ExtraWheelsDraw.java +++ b/ExtraWheelsDraw.java @@ -18,7 +18,7 @@ public class ExtraWheelsDraw { } private Color color; - public void Init(int num, Color color) { + public ExtraWheelsDraw(int num, Color color) { setWheelsNum(num); this.color = color; } diff --git a/FormLocomotive.java b/FormLocomotive.java index 0d453aa..55b048b 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -5,7 +5,6 @@ import java.util.Random; public class FormLocomotive extends JComponent{ private DrawningLocomotive _locomotive; - private EntityLocomotive _entity; public FormLocomotive() { JFrame formFrame = new JFrame("Locomotive"); formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -40,9 +39,7 @@ public class FormLocomotive extends JComponent{ JButton createButton = new JButton("Create"); createButton.addActionListener(e -> { Random rnd = new Random(); - _locomotive = new DrawningLocomotive(); - _entity = new EntityLocomotive(); - _locomotive.Init(100 + rnd.nextInt(200), 1000 + rnd.nextInt(1000), Color.getHSBColor(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), rnd.nextInt(3), _entity); + _locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75); speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); From c68da6d45bb2cfb6fc8b91acee756335e496e164 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 17:54:48 +0400 Subject: [PATCH 04/14] Added and Fixed DrawningLocomotive & Direction --- Direction.java | 2 +- DrawningLocomotive.java | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Direction.java b/Direction.java index cc121e9..e34e162 100644 --- a/Direction.java +++ b/Direction.java @@ -1,3 +1,3 @@ public enum Direction { - Up, Down, Left, Right + None,Up, Down, Left, Right } diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index e2cc62a..1e78705 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -5,17 +5,17 @@ class DrawningLocomotive { public EntityLocomotive Locomotive; public ExtraWheelsDraw extraWheelsDraw; /// Левая координата отрисовки локомотива - private float _startPosX; + protected float _startPosX; /// Верхняя координата отрисовки локомотива - private float _startPosY; + protected float _startPosY; /// Ширина окна отрисовки private Integer _pictureWidth = null; /// Высота окна отрисовки private Integer _pictureHeight = null; /// Ширина отрисовки локомотива - private final int _locomotiveWidth = 120; + private int _locomotiveWidth = 110; /// Высота отрисовки локомотива - private final int _locomotiveHeight = 50; + private int _locomotiveHeight = 50; /// Инициализация свойств private final Random random = new Random(); public DrawningLocomotive(int speed, float weight, Color bodyColor) @@ -23,6 +23,15 @@ class DrawningLocomotive { extraWheelsDraw = new ExtraWheelsDraw(random.nextInt(3), bodyColor); Locomotive = new EntityLocomotive(speed, weight, bodyColor); } + + // Новый конструктор + protected DrawningLocomotive (int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) + { + this(speed, weight, bodyColor); + _locomotiveWidth = locomotiveWidth; + _locomotiveHeight = locomotiveHeight; + } + /// Установка позиции локомотива public void SetPosition(int x, int y, int width, int height) { @@ -126,4 +135,10 @@ class DrawningLocomotive { _startPosY = _pictureHeight - _locomotiveHeight; } } + + // Получение текущей позиции объекта + public float[] GetCurrentPosition() + { + return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _locomotiveWidth, /*DOWN*/ _startPosY + _locomotiveHeight, /*LEFT*/ _startPosX}; + } } From 673ba65efe5375c5de6ce965969cfb32f28be709 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 18:08:44 +0400 Subject: [PATCH 05/14] Added and Fixed DrawningObjectLocomotive --- DrawningObjectLocomotive.java | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 DrawningObjectLocomotive.java diff --git a/DrawningObjectLocomotive.java b/DrawningObjectLocomotive.java new file mode 100644 index 0000000..38617df --- /dev/null +++ b/DrawningObjectLocomotive.java @@ -0,0 +1,39 @@ +import java.awt.*; + +public class DrawningObjectLocomotive implements IDrawningObject { + private DrawningLocomotive _locomotive = null; + + public DrawningObjectLocomotive(DrawningLocomotive locomotive) + { + _locomotive = locomotive; + } + public float getStep() { + if (_locomotive.Locomotive != null) { + return _locomotive.Locomotive.Step(); + } + return 0; + } + + public void DrawningObject(Graphics g) + { + if (_locomotive != null) _locomotive.DrawTransport((Graphics2D) g); + } + + public float[] GetCurrentPosition() + { + if (_locomotive != null) { + return _locomotive.GetCurrentPosition(); + } + return null; + } + + public void MoveObject(Direction direction) + { + if (_locomotive != null) _locomotive.MoveTransport(direction); + } + + public void SetObject(int x, int y, int width, int height) + { + if (_locomotive != null) _locomotive.SetPosition(x, y, width, height); + } +} From 4526388454fbcf02f2086c04394283d263df96b2 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 18:36:19 +0400 Subject: [PATCH 06/14] Added and Fixed EntityWarmlyLocomotive & DrawningWarmlyLocomotive --- DrawningWarmlyLocomotive.java | 30 ++++++++++++++++++++++++++++++ EntityWarmlyLocomotive.java | 15 +++++++++++++++ FormLocomotive.java | 18 ++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 DrawningWarmlyLocomotive.java create mode 100644 EntityWarmlyLocomotive.java diff --git a/DrawningWarmlyLocomotive.java b/DrawningWarmlyLocomotive.java new file mode 100644 index 0000000..8b9324f --- /dev/null +++ b/DrawningWarmlyLocomotive.java @@ -0,0 +1,30 @@ +import java.awt.*; +public class DrawningWarmlyLocomotive extends DrawningLocomotive{ + public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean storage) + { + super(speed, weight, bodyColor, 110, 50); + Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage); + } + public void DrawTransport(Graphics g) + { + if (Locomotive instanceof EntityWarmlyLocomotive) + { + EntityWarmlyLocomotive warmlyLocomotive = (EntityWarmlyLocomotive) Locomotive; + //Pen pen = new(Color.Black); + //Brush extraBrush = new SolidBrush(warmlyLocomotive.ExtraColor); + super.DrawTransport((Graphics2D)g); + + if (warmlyLocomotive.Pipe) + { + //TODO + + } + + if (warmlyLocomotive.FuelStorage) + { + //TODO + + } + } + } +} diff --git a/EntityWarmlyLocomotive.java b/EntityWarmlyLocomotive.java new file mode 100644 index 0000000..2e1a502 --- /dev/null +++ b/EntityWarmlyLocomotive.java @@ -0,0 +1,15 @@ +import java.awt.*; + +public class EntityWarmlyLocomotive extends EntityLocomotive{ + public final Color ExtraColor; + public final boolean Pipe; + public final boolean FuelStorage; + public EntityWarmlyLocomotive (int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean fuelStorage) + { + super(speed, weight, bodyColor); + ExtraColor = extraColor; + Pipe = pipe; + FuelStorage = fuelStorage; + } + +} diff --git a/FormLocomotive.java b/FormLocomotive.java index 55b048b..6616754 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -47,7 +47,23 @@ public class FormLocomotive extends JComponent{ repaint(); }); + JButton modifiedButton = new JButton("Modified"); + modifiedButton.addActionListener(e -> { + Random rnd = new Random(); + _locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), + rnd.nextBoolean()); + _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75); + speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); + weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); + colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() ); + repaint(); + }); + statusPanel.add(createButton); + statusPanel.add(modifiedButton); statusPanel.add(speedLabel); statusPanel.add(weightLabel); statusPanel.add(colorLabel); @@ -84,6 +100,8 @@ public class FormLocomotive extends JComponent{ formFrame.getContentPane().add(this); } + + @Override protected void paintComponent(Graphics g) { super.paintComponent(g); From b18173f05ac0c8032864f1dfa15774cdf0a36a13 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 18:55:47 +0400 Subject: [PATCH 07/14] Fixed DrawningWarmlyLocomotive --- DrawningLocomotive.java | 2 +- DrawningWarmlyLocomotive.java | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 1e78705..bc3cbef 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -100,7 +100,7 @@ class DrawningLocomotive { } //тело g.setColor(Color.BLACK); - g.drawRect((int)_startPosX , (int)_startPosY, _locomotiveWidth - 20, _locomotiveHeight - 10); + g.drawRect((int)_startPosX , (int)_startPosY, 110 - 10, 50 - 10); //окна g.setColor(Locomotive.getBodyColor()); g.fillRect((int)_startPosX + 10, (int)_startPosY + 10, 10, 10); diff --git a/DrawningWarmlyLocomotive.java b/DrawningWarmlyLocomotive.java index 8b9324f..cdc0625 100644 --- a/DrawningWarmlyLocomotive.java +++ b/DrawningWarmlyLocomotive.java @@ -2,29 +2,37 @@ import java.awt.*; public class DrawningWarmlyLocomotive extends DrawningLocomotive{ public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean storage) { - super(speed, weight, bodyColor, 110, 50); + super(speed, weight, bodyColor, 130, 70); Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage); } - public void DrawTransport(Graphics g) + @Override + public void DrawTransport(Graphics2D g) { if (Locomotive instanceof EntityWarmlyLocomotive) { EntityWarmlyLocomotive warmlyLocomotive = (EntityWarmlyLocomotive) Locomotive; - //Pen pen = new(Color.Black); - //Brush extraBrush = new SolidBrush(warmlyLocomotive.ExtraColor); - super.DrawTransport((Graphics2D)g); + + g.setColor(warmlyLocomotive.ExtraColor); if (warmlyLocomotive.Pipe) { - //TODO - + g.fillRect((int)_startPosX + 10, (int)_startPosY, 30, 20); + g.fillRect((int)_startPosX + 60, (int)_startPosY, 20, 20); + g.fillRect((int)_startPosX + 60, (int)_startPosY + 10, 30, 10); } if (warmlyLocomotive.FuelStorage) { - //TODO - + g.fillRect((int)_startPosX + 110, (int)_startPosY + 10, 10, 50); + g.fillRect((int)_startPosX + 110, (int)_startPosY + 40, 20, 20); + g.fillRect((int)_startPosX + 110, (int)_startPosY, 30, 10); } + + _startPosY += 20; + + super.DrawTransport((Graphics2D)g); + + _startPosY -= 20; } } } From febb4eaa6badb225d669b0a1ab89e60452a7affc Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 19:10:38 +0400 Subject: [PATCH 08/14] Added & fixed SimpleMap --- SimpleMap.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 SimpleMap.java diff --git a/SimpleMap.java b/SimpleMap.java new file mode 100644 index 0000000..4311230 --- /dev/null +++ b/SimpleMap.java @@ -0,0 +1,49 @@ +import java.awt.*; + +public class SimpleMap extends AbstractMap{ + /// Цвет участка закрытого + private final Color barrierColor = Color.BLACK; + /// Цвет участка открытого + private final Color roadColor = Color.GRAY; + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + g.setColor(barrierColor); + g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + + 1)), (int)(j * (_size_y + 1))); + } + + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + g.setColor(roadColor); + g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + + 1)),(int)( j * (_size_y + 1))); + } + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + while (counter < 50) + { + int x = _random.nextInt(100); + int y = _random.nextInt(100); + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + counter++; + } + } + } +} From 8e0a97dd2bc81460e4c117755bb2f1775a3c646b Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 20:32:35 +0400 Subject: [PATCH 09/14] Added & fixed FormMap --- DrawningWarmlyLocomotive.java | 2 +- FormLocomotive.java | 5 +- FormMap.java | 126 ++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 FormMap.java diff --git a/DrawningWarmlyLocomotive.java b/DrawningWarmlyLocomotive.java index cdc0625..bc5cc09 100644 --- a/DrawningWarmlyLocomotive.java +++ b/DrawningWarmlyLocomotive.java @@ -2,7 +2,7 @@ import java.awt.*; public class DrawningWarmlyLocomotive extends DrawningLocomotive{ public DrawningWarmlyLocomotive(int speed, float weight, Color bodyColor, Color extraColor, boolean pipe, boolean storage) { - super(speed, weight, bodyColor, 130, 70); + super(speed, weight, bodyColor, 140, 70); Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage); } @Override diff --git a/FormLocomotive.java b/FormLocomotive.java index 6616754..7535926 100644 --- a/FormLocomotive.java +++ b/FormLocomotive.java @@ -99,9 +99,6 @@ public class FormLocomotive extends JComponent{ formFrame.getContentPane().add(this); } - - - @Override protected void paintComponent(Graphics g) { super.paintComponent(g); @@ -111,6 +108,6 @@ public class FormLocomotive extends JComponent{ } public static void main(String[] args) { - new FormLocomotive(); + new FormMap(); } } diff --git a/FormMap.java b/FormMap.java new file mode 100644 index 0000000..9741bc5 --- /dev/null +++ b/FormMap.java @@ -0,0 +1,126 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.util.Random; + +public class FormMap extends JComponent { + + private AbstractMap _abstractMap; + private BufferedImage bufferImg = null; + + public FormMap() { + JFrame formFrame = new JFrame("Form Map"); + formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + formFrame.setSize(1000, 500); + formFrame.setVisible(true); + formFrame.setLocationRelativeTo(null); + + Panel statusPanel = new Panel(); + statusPanel.setBackground(Color.WHITE); + statusPanel.setLayout(new FlowLayout()); + setLayout(new BorderLayout()); + add(statusPanel, BorderLayout.SOUTH); + + Label speedLabel = new Label("Speed: "); + Label weightLabel = new Label("Weight: "); + Label colorLabel = new Label("Color: "); + + String[] maps = { + "Simple Map", + "Spike Map", + "Rail Map" + }; + JComboBox mapSelectComboBox = new JComboBox(maps); + mapSelectComboBox.setEditable(true); + mapSelectComboBox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String item = (String)mapSelectComboBox.getSelectedItem(); + if (item == null) return; + switch (item) { + case ("Simple Map"): + _abstractMap = new SimpleMap(); + break; + case ("Spike Map"): + break; + case ("Rail Map"): + break; + } + } + }); + + JButton createButton = new JButton("Create"); + createButton.addActionListener(e -> { + Random rnd = new Random(); + var locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))); + speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); + weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); + colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue()); + if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 500, new DrawningObjectLocomotive(locomotive)); + repaint(); + }); + + JButton modifiedButton = new JButton("Modified"); + modifiedButton.addActionListener(e -> { + Random rnd = new Random(); + var locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), + rnd.nextBoolean(), + rnd.nextBoolean()); + speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); + weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); + colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue() ); + if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 500, new DrawningObjectLocomotive(locomotive)); + repaint(); + }); + statusPanel.add(mapSelectComboBox); + statusPanel.add(createButton); + statusPanel.add(modifiedButton); + statusPanel.add(speedLabel); + statusPanel.add(weightLabel); + statusPanel.add(colorLabel); + + JButton moveDownButton = new JButton("D"); + moveDownButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Down); + repaint(); + }); + + JButton moveUpButton = new JButton("U"); + moveUpButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Up); + repaint(); + }); + + JButton moveLeftButton = new JButton("L"); + moveLeftButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Left); + repaint(); + }); + + JButton moveRightButton = new JButton("R"); + moveRightButton.addActionListener(e -> { + if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Right); + repaint(); + }); + + statusPanel.add(moveUpButton); + statusPanel.add(moveDownButton); + statusPanel.add(moveLeftButton); + statusPanel.add(moveRightButton); + + formFrame.getContentPane().add(this); + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D)g; + if (bufferImg != null) g2.drawImage(bufferImg, 0,0,1000,500,null); + super.repaint(); + } + +} From aba03789ef85ad047b3e807b14f793710297462b Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 20:43:57 +0400 Subject: [PATCH 10/14] Added & fixed SpikeMap --- FormMap.java | 1 + SpikeMap.java | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 SpikeMap.java diff --git a/FormMap.java b/FormMap.java index 9741bc5..2208630 100644 --- a/FormMap.java +++ b/FormMap.java @@ -44,6 +44,7 @@ public class FormMap extends JComponent { _abstractMap = new SimpleMap(); break; case ("Spike Map"): + _abstractMap = new SpikeMap(); break; case ("Rail Map"): break; diff --git a/SpikeMap.java b/SpikeMap.java new file mode 100644 index 0000000..4e41c4d --- /dev/null +++ b/SpikeMap.java @@ -0,0 +1,53 @@ +import java.awt.*; + +public class SpikeMap extends AbstractMap{ + /// Цвет участка закрытого + private final Color barrierColor = Color.BLACK; + /// Цвет участка открытого + private final Color roadColor = Color.GREEN; + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + g.setColor(barrierColor); + g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + + 1)), (int)(j * (_size_y + 1))); + } + + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + g.setColor(roadColor); + g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + + 1)),(int)( j * (_size_y + 1))); + } + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + while (counter < 15) + { + int x = _random.nextInt(95) + 1; + int y = _random.nextInt(95) + 1; + if (_map[x][y] == _freeRoad) + { + _map[x][y] = _barrier; + if (_map[x + 1][y] == _freeRoad) _map[x + 1][y] = _barrier; + if (_map[x - 1][y] == _freeRoad) _map[x - 1][y] = _barrier; + if (_map[x][y + 1] == _freeRoad) _map[x][y + 1] = _barrier; + if (_map[x][y - 1] == _freeRoad) _map[x][y - 1] = _barrier; + counter++; + } + } + } +} From bf246e17d42a3fa09185dc7b0e0acf7a17a0109c Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 20:51:36 +0400 Subject: [PATCH 11/14] Added & fixed RailMap --- FormMap.java | 8 ++++--- RailMap.java | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ SimpleMap.java | 2 +- SpikeMap.java | 2 +- 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 RailMap.java diff --git a/FormMap.java b/FormMap.java index 2208630..1e42c4b 100644 --- a/FormMap.java +++ b/FormMap.java @@ -47,6 +47,7 @@ public class FormMap extends JComponent { _abstractMap = new SpikeMap(); break; case ("Rail Map"): + _abstractMap = new RailMap(); break; } } @@ -59,7 +60,7 @@ public class FormMap extends JComponent { speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue()); - if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 500, new DrawningObjectLocomotive(locomotive)); + if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawningObjectLocomotive(locomotive)); repaint(); }); @@ -74,7 +75,7 @@ public class FormMap extends JComponent { speedLabel.setText("Speed: " + locomotive.Locomotive.getSpeed()); weightLabel.setText("Weight: " + (int)locomotive.Locomotive.getWeight()); colorLabel.setText("Color: " + locomotive.Locomotive.getBodyColor().getRed() + " " + locomotive.Locomotive.getBodyColor().getGreen() + " " + locomotive.Locomotive.getBodyColor().getBlue() ); - if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 500, new DrawningObjectLocomotive(locomotive)); + if (_abstractMap != null) bufferImg = _abstractMap.CreateMap(1000, 490, new DrawningObjectLocomotive(locomotive)); repaint(); }); statusPanel.add(mapSelectComboBox); @@ -114,13 +115,14 @@ public class FormMap extends JComponent { statusPanel.add(moveRightButton); formFrame.getContentPane().add(this); + super.repaint(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; - if (bufferImg != null) g2.drawImage(bufferImg, 0,0,1000,500,null); + if (bufferImg != null) g2.drawImage(bufferImg, 0,0,1000,490,null); super.repaint(); } diff --git a/RailMap.java b/RailMap.java new file mode 100644 index 0000000..836dac8 --- /dev/null +++ b/RailMap.java @@ -0,0 +1,58 @@ +import java.awt.*; + +public class RailMap extends AbstractMap{ + /// Цвет участка закрытого + private final Color barrierColor = Color.BLACK; + /// Цвет участка открытого + private final Color roadColor = Color.PINK; + + @Override + protected void DrawBarrierPart(Graphics g, int i, int j) + { + g.setColor(barrierColor); + g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + + 1)), (int)(j * (_size_y + 1))); + } + + @Override + protected void DrawRoadPart(Graphics g, int i, int j) + { + g.setColor(roadColor); + g.fillRect( (int)(i * _size_x), (int)(j * _size_y), (int)(i * (_size_x + + 1)),(int)( j * (_size_y + 1))); + } + @Override + protected void GenerateMap() + { + _map = new int[100][100]; + _size_x = (float)_width / _map.length; + _size_y = (float)_height / _map[0].length; + int counter = 0; + for (int i = 0; i < _map.length; ++i) + { + for (int j = 0; j < _map[0].length; ++j) + { + _map[i][j] = _freeRoad; + } + } + while (counter < 1) + { + int y = _random.nextInt(85); + + for(int x = 0; x < 99; x++) + { + _map[x][y] = _barrier; + _map[x][y + 5] = _barrier; + + if (x % 5 == 0) + { + _map[x][y + 1] = _barrier; + _map[x][y + 2] = _barrier; + _map[x][y + 3] = _barrier; + _map[x][y + 4] = _barrier; + } + } + counter += 1; + } + } +} diff --git a/SimpleMap.java b/SimpleMap.java index 4311230..2e00387 100644 --- a/SimpleMap.java +++ b/SimpleMap.java @@ -38,7 +38,7 @@ public class SimpleMap extends AbstractMap{ while (counter < 50) { int x = _random.nextInt(100); - int y = _random.nextInt(100); + int y = _random.nextInt(85); if (_map[x][y] == _freeRoad) { _map[x][y] = _barrier; diff --git a/SpikeMap.java b/SpikeMap.java index 4e41c4d..c7bb422 100644 --- a/SpikeMap.java +++ b/SpikeMap.java @@ -38,7 +38,7 @@ public class SpikeMap extends AbstractMap{ while (counter < 15) { int x = _random.nextInt(95) + 1; - int y = _random.nextInt(95) + 1; + int y = _random.nextInt(85) + 1; if (_map[x][y] == _freeRoad) { _map[x][y] = _barrier; From 18a72916b66e845f9545716cf50bb50e271a46f5 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 22:13:56 +0400 Subject: [PATCH 12/14] Added IDrawningExtra, ExtraWheelsDraw implements IDrawningExtra --- DrawningLocomotive.java | 2 +- DrawningWarmlyLocomotive.java | 2 +- ExtraWheelsDraw.java | 8 ++++---- IDrawningExtra.java | 6 ++++++ 4 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 IDrawningExtra.java diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index bc3cbef..ae45dbd 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -110,7 +110,7 @@ class DrawningLocomotive { g.setColor(Color.BLACK); g.drawRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20); //колеса - extraWheelsDraw.DrawWheels((int)_startPosX, (int)_startPosY, g); + extraWheelsDraw.DrawExtra((int)_startPosX, (int)_startPosY, g); //движок g.setColor(Locomotive.getBodyColor()); g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30); diff --git a/DrawningWarmlyLocomotive.java b/DrawningWarmlyLocomotive.java index bc5cc09..f7fd094 100644 --- a/DrawningWarmlyLocomotive.java +++ b/DrawningWarmlyLocomotive.java @@ -23,7 +23,7 @@ public class DrawningWarmlyLocomotive extends DrawningLocomotive{ if (warmlyLocomotive.FuelStorage) { - g.fillRect((int)_startPosX + 110, (int)_startPosY + 10, 10, 50); + g.fillRect((int)_startPosX + 120, (int)_startPosY + 10, 10, 50); g.fillRect((int)_startPosX + 110, (int)_startPosY + 40, 20, 20); g.fillRect((int)_startPosX + 110, (int)_startPosY, 30, 10); } diff --git a/ExtraWheelsDraw.java b/ExtraWheelsDraw.java index 551b428..abe9d15 100644 --- a/ExtraWheelsDraw.java +++ b/ExtraWheelsDraw.java @@ -1,8 +1,8 @@ import java.awt.*; -public class ExtraWheelsDraw { +public class ExtraWheelsDraw implements IDrawningExtra{ private WheelsCount wheelsCount = WheelsCount.Two; - public void setWheelsNum(int num) { + public void setExtraNum(int num) { switch (num) { case 0: { wheelsCount = WheelsCount.Three; @@ -19,11 +19,11 @@ public class ExtraWheelsDraw { private Color color; public ExtraWheelsDraw(int num, Color color) { - setWheelsNum(num); + setExtraNum(num); this.color = color; } - public void DrawWheels(int startPosX, int startPosY, Graphics2D g) { + public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { g.setColor(color); g.drawOval(startPosX, startPosY + 40, 10, 10); g.drawOval(startPosX + 90, startPosY + 40, 10, 10); diff --git a/IDrawningExtra.java b/IDrawningExtra.java new file mode 100644 index 0000000..725e87a --- /dev/null +++ b/IDrawningExtra.java @@ -0,0 +1,6 @@ +import java.awt.*; + +public interface IDrawningExtra { + void setExtraNum(int num); + void DrawExtra(int startPosX, int startPosY, Graphics2D g); +} From 400672b6ddca6b39288fb4ac0dbc75d9a5a1f516 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 23:13:09 +0400 Subject: [PATCH 13/14] Added ExtraObjects & their Init. --- DrawningLocomotive.java | 19 ++++++++++------ ExtraRoundWheelDraw.java | 40 ++++++++++++++++++++++++++++++++++ ExtraStarWheelDraw.java | 47 ++++++++++++++++++++++++++++++++++++++++ ExtraWheelsDraw.java | 16 ++++++++++---- 4 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 ExtraRoundWheelDraw.java create mode 100644 ExtraStarWheelDraw.java diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index ae45dbd..7e3054c 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -3,7 +3,7 @@ import java.util.Random; class DrawningLocomotive { public EntityLocomotive Locomotive; - public ExtraWheelsDraw extraWheelsDraw; + public IDrawningExtra[] drawningExtra; /// Левая координата отрисовки локомотива protected float _startPosX; /// Верхняя координата отрисовки локомотива @@ -20,7 +20,10 @@ class DrawningLocomotive { private final Random random = new Random(); public DrawningLocomotive(int speed, float weight, Color bodyColor) { - extraWheelsDraw = new ExtraWheelsDraw(random.nextInt(3), bodyColor); + int randExtra = random.nextInt(3); + drawningExtra = new IDrawningExtra[2]; + drawningExtra[0] = new ExtraWheelsDraw(randExtra, bodyColor); + drawningExtra[1] = random.nextBoolean() ? new ExtraRoundWheelDraw(randExtra) : new ExtraStarWheelDraw(randExtra); Locomotive = new EntityLocomotive(speed, weight, bodyColor); } @@ -99,18 +102,20 @@ class DrawningLocomotive { return; } //тело - g.setColor(Color.BLACK); - g.drawRect((int)_startPosX , (int)_startPosY, 110 - 10, 50 - 10); - //окна g.setColor(Locomotive.getBodyColor()); + g.fillRect((int)_startPosX , (int)_startPosY, 110 - 10, 50 - 10); + //окна + g.setColor(Color.BLUE); g.fillRect((int)_startPosX + 10, (int)_startPosY + 10, 10, 10); g.fillRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 10); g.fillRect((int)_startPosX + 80, (int)_startPosY + 10, 10, 10); //дверь g.setColor(Color.BLACK); - g.drawRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20); + g.fillRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20); //колеса - extraWheelsDraw.DrawExtra((int)_startPosX, (int)_startPosY, g); + drawningExtra[0].DrawExtra((int)_startPosX, (int)_startPosY, g); + //extra + drawningExtra[1].DrawExtra((int)_startPosX, (int)_startPosY, g); //движок g.setColor(Locomotive.getBodyColor()); g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30); diff --git a/ExtraRoundWheelDraw.java b/ExtraRoundWheelDraw.java new file mode 100644 index 0000000..5f28863 --- /dev/null +++ b/ExtraRoundWheelDraw.java @@ -0,0 +1,40 @@ +import java.awt.*; + +public class ExtraRoundWheelDraw implements IDrawningExtra{ + private WheelsCount wheelsCount = WheelsCount.Two; + public void setExtraNum(int num) { + switch (num) { + case 0: { + wheelsCount = WheelsCount.Three; + break; + } + case 1: { + wheelsCount = WheelsCount.Four; + break; + } + default: + break; + } + } + + public ExtraRoundWheelDraw (int num) { + setExtraNum(num); + } + + public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + g.setColor(Color.BLACK); + g.fillOval(startPosX + 5, startPosY + 35, 10, 10); + g.fillOval(startPosX + 95, startPosY + 35, 10, 10); + switch (wheelsCount) { + case Four: { + g.fillOval(startPosX + 75, startPosY + 35, 10, 10); + } + case Three: { + g.fillOval(startPosX + 25, startPosY + 35, 10, 10); + break; + } + default: + break; + } + } +} diff --git a/ExtraStarWheelDraw.java b/ExtraStarWheelDraw.java new file mode 100644 index 0000000..dac1eb8 --- /dev/null +++ b/ExtraStarWheelDraw.java @@ -0,0 +1,47 @@ +import java.awt.*; + +public class ExtraStarWheelDraw implements IDrawningExtra{ + private WheelsCount wheelsCount = WheelsCount.Two; + public void setExtraNum(int num) { + switch (num) { + case 0: { + wheelsCount = WheelsCount.Three; + break; + } + case 1: { + wheelsCount = WheelsCount.Four; + break; + } + default: + break; + } + } + + public ExtraStarWheelDraw (int num) { + setExtraNum(num); + } + + public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + DrawStarOnWheel(startPosX, startPosY + 30, g); + DrawStarOnWheel(startPosX + 90, startPosY + 30, g); + switch (wheelsCount) { + case Four: { + DrawStarOnWheel(startPosX + 70, startPosY + 30, g); + } + case Three: { + DrawStarOnWheel(startPosX + 20, startPosY + 30, g); + break; + } + default: + break; + } + } + + private void DrawStarOnWheel(int startPosX, int startPosY, Graphics2D g) { + g.drawLine(startPosX + 10, startPosY, startPosX + 15, startPosY + 17); + g.drawLine(startPosX + 10, startPosY, startPosX + 5, startPosY + 17); + g.drawLine(startPosX + 15, startPosY + 17, startPosX + 2, startPosY + 8); + g.drawLine(startPosX + 5, startPosY + 17, startPosX + 18, startPosY + 8); + g.drawLine(startPosX + 2, startPosY + 8, startPosX + 18, startPosY + 8); + } +} diff --git a/ExtraWheelsDraw.java b/ExtraWheelsDraw.java index abe9d15..a515643 100644 --- a/ExtraWheelsDraw.java +++ b/ExtraWheelsDraw.java @@ -24,15 +24,23 @@ public class ExtraWheelsDraw implements IDrawningExtra{ } public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { + g.setColor(Color.BLACK); + g.drawOval(startPosX, startPosY + 30, 20, 20); + g.drawOval(startPosX + 90, startPosY + 30, 20, 20); g.setColor(color); - g.drawOval(startPosX, startPosY + 40, 10, 10); - g.drawOval(startPosX + 90, startPosY + 40, 10, 10); + g.fillOval(startPosX, startPosY + 30, 20, 20); + g.fillOval(startPosX + 90, startPosY + 30, 20, 20); switch (wheelsCount) { case Four: { - g.drawOval(startPosX + 70, startPosY + 40, 10, 10); + g.setColor(Color.BLACK); + g.drawOval(startPosX + 70, startPosY + 30, 20, 20); + g.setColor(color); + g.fillOval(startPosX + 70, startPosY + 30, 20, 20); } case Three: { - g.drawOval(startPosX + 20, startPosY + 40, 10, 10); + g.fillOval(startPosX + 20, startPosY + 30, 20, 20); + g.setColor(Color.BLACK); + g.drawOval(startPosX + 20, startPosY + 30, 20, 20); break; } default: From 2920306b706be1f72aa6535c7b147c881dcac788 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Tue, 11 Oct 2022 16:29:28 +0400 Subject: [PATCH 14/14] Fixed Problems. --- DrawningLocomotive.java | 24 +++++++++++++++++------- FormMap.java | 8 ++++---- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/DrawningLocomotive.java b/DrawningLocomotive.java index 7e3054c..5a4e57b 100644 --- a/DrawningLocomotive.java +++ b/DrawningLocomotive.java @@ -3,7 +3,8 @@ import java.util.Random; class DrawningLocomotive { public EntityLocomotive Locomotive; - public IDrawningExtra[] drawningExtra; + public ExtraWheelsDraw extraWheelsDraw; + public IDrawningExtra drawningExtra; /// Левая координата отрисовки локомотива protected float _startPosX; /// Верхняя координата отрисовки локомотива @@ -20,10 +21,19 @@ class DrawningLocomotive { private final Random random = new Random(); public DrawningLocomotive(int speed, float weight, Color bodyColor) { - int randExtra = random.nextInt(3); - drawningExtra = new IDrawningExtra[2]; - drawningExtra[0] = new ExtraWheelsDraw(randExtra, bodyColor); - drawningExtra[1] = random.nextBoolean() ? new ExtraRoundWheelDraw(randExtra) : new ExtraStarWheelDraw(randExtra); + int randExtra = random.nextInt(2); + extraWheelsDraw = new ExtraWheelsDraw(randExtra, bodyColor); + switch (random.nextInt(3)){ + case 0: + drawningExtra = new ExtraStarWheelDraw(randExtra); + break; + case 1: + drawningExtra = new ExtraRoundWheelDraw(randExtra); + break; + case 2: + drawningExtra = new ExtraWheelsDraw(randExtra, bodyColor); + break; + } Locomotive = new EntityLocomotive(speed, weight, bodyColor); } @@ -113,9 +123,9 @@ class DrawningLocomotive { g.setColor(Color.BLACK); g.fillRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20); //колеса - drawningExtra[0].DrawExtra((int)_startPosX, (int)_startPosY, g); + extraWheelsDraw.DrawExtra((int)_startPosX, (int)_startPosY, g); //extra - drawningExtra[1].DrawExtra((int)_startPosX, (int)_startPosY, g); + drawningExtra.DrawExtra((int)_startPosX, (int)_startPosY, g); //движок g.setColor(Locomotive.getBodyColor()); g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30); diff --git a/FormMap.java b/FormMap.java index 1e42c4b..4b0064d 100644 --- a/FormMap.java +++ b/FormMap.java @@ -85,25 +85,25 @@ public class FormMap extends JComponent { statusPanel.add(weightLabel); statusPanel.add(colorLabel); - JButton moveDownButton = new JButton("D"); + JButton moveDownButton = new JButton("Down"); moveDownButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Down); repaint(); }); - JButton moveUpButton = new JButton("U"); + JButton moveUpButton = new JButton("Up"); moveUpButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Up); repaint(); }); - JButton moveLeftButton = new JButton("L"); + JButton moveLeftButton = new JButton("Left"); moveLeftButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Left); repaint(); }); - JButton moveRightButton = new JButton("R"); + JButton moveRightButton = new JButton("Right"); moveRightButton.addActionListener(e -> { if(bufferImg != null) bufferImg = _abstractMap.MoveObject(Direction.Right); repaint();