From 495500860c2ba800b590d55d462165d575471b49 Mon Sep 17 00:00:00 2001 From: Stranni15k Date: Thu, 22 Dec 2022 23:31:43 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=202?= =?UTF-8?q?=20=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Direction.java | 3 ++- src/DrawingLocomotive.java | 43 +++++++++++++++++++++++++++----------- src/DrawingWheel.java | 11 ++++++---- src/EntityLocomotive.java | 2 +- src/FormLocomotive.java | 20 ++++++++++-------- 5 files changed, 52 insertions(+), 27 deletions(-) diff --git a/src/Direction.java b/src/Direction.java index 996463a..872d70f 100644 --- a/src/Direction.java +++ b/src/Direction.java @@ -2,5 +2,6 @@ public enum Direction { Up, Down, Left, - Right + Right, + None } \ No newline at end of file diff --git a/src/DrawingLocomotive.java b/src/DrawingLocomotive.java index 697504e..b8a2a89 100644 --- a/src/DrawingLocomotive.java +++ b/src/DrawingLocomotive.java @@ -1,24 +1,35 @@ import javax.swing.*; import java.awt.*; +import java.util.Random; + public class DrawingLocomotive extends JComponent { public EntityLocomotive Locomotive; - private float _startPosX; - private float _startPosY; + public float _startPosX; + public float _startPosY; private Integer _pictureWidth = null; private Integer _pictureHeight = null; - private final int _locomotiveWidth = 150; - private final int _locomotiveHeight = 100; - private DrawingWheel _wheel; - public DrawingLocomotive() { - super(); - } - public void Init(int speed, float weight, Color bodyColor, int wheelCount) + private int _locomotiveWidth = 150; + private int _locomotiveHeight = 100; + private IDrawningObjectWheel _wheel; + public DrawingLocomotive(int speed, float weight, Color bodyColor, int wheelCount) { - Locomotive = new EntityLocomotive(); - Locomotive.Init(speed, weight, bodyColor); - _wheel = new DrawingWheel(); + Random random = new Random(); + Locomotive = new EntityLocomotive(speed, weight, bodyColor); + int randomPattern=random.nextInt(3); + if (randomPattern==0) + _wheel = new DrawingWheel(); + else if(randomPattern==1) + _wheel = new DrawingWheelCircle(); + else + _wheel = new DrawningWheelTriangle(); _wheel.SetWheelAmount(wheelCount); } + protected DrawingLocomotive(int speed, float weight, Color bodyColor, int whelcount, int locomotiveWidth, int locomotiveHeight) + { + this(speed, weight, bodyColor, whelcount); + _locomotiveWidth = locomotiveWidth; + _locomotiveHeight = locomotiveHeight; + } public void SetPosition(int x, int y, int width, int height) { if (width <= _locomotiveWidth + x || height <= _locomotiveHeight + y || x<0 || y<0) @@ -119,4 +130,12 @@ public class DrawingLocomotive extends JComponent { _startPosY = _pictureHeight - _locomotiveHeight; } } + public float[] GetCurrentPosition(){ + float[] tuple = new float[4]; + tuple[0] = _startPosX; + tuple[1] =_startPosY; + tuple[2] = _startPosX + _locomotiveWidth; + tuple[3] = _startPosY + _locomotiveHeight; + return tuple; + } } diff --git a/src/DrawingWheel.java b/src/DrawingWheel.java index ebad914..6da2639 100644 --- a/src/DrawingWheel.java +++ b/src/DrawingWheel.java @@ -1,7 +1,8 @@ -import javax.swing.*; import java.awt.*; -public class DrawingWheel extends JComponent { +public class DrawingWheel implements IDrawningObjectWheel { private AdditionalDirection _wheel; + + @Override public void SetWheelAmount(int wheelAmount) { for (AdditionalDirection item: _wheel.values()) { if (item.getCount() == wheelAmount) { @@ -10,8 +11,10 @@ public class DrawingWheel extends JComponent { } } } + + @Override public void DrawWheel(Graphics gr, int _startPosWheelX, int _startPosWheelY, Color pen) { - super.paintComponent(gr); + // super.paintComponent(gr); Graphics2D g=(Graphics2D)gr; if (_wheel.getCount() >= 2) { @@ -24,7 +27,7 @@ public class DrawingWheel extends JComponent { if (_wheel.getCount() >= 4) { paintWheel(g, _startPosWheelX +25, _startPosWheelY, pen); } - super.repaint(); + // super.repaint(); } protected void paintWheel(Graphics2D g, int _startPosX1, int _startPosY, Color pen){ try { g.setPaint(pen); } diff --git a/src/EntityLocomotive.java b/src/EntityLocomotive.java index 92e2e67..2887c7a 100644 --- a/src/EntityLocomotive.java +++ b/src/EntityLocomotive.java @@ -14,7 +14,7 @@ public class EntityLocomotive { } public float Step() { return Speed() * 20 / Weight(); } - public void Init(int speed, float weight, Color bodyColor) + public EntityLocomotive(int speed, float weight, Color bodyColor) { Random random = new Random(); this.speed = speed <= 0 ? random.nextInt(50, 150) : speed; diff --git a/src/FormLocomotive.java b/src/FormLocomotive.java index 51cbe14..3995248 100644 --- a/src/FormLocomotive.java +++ b/src/FormLocomotive.java @@ -50,16 +50,9 @@ public class FormLocomotive extends JFrame { } catch (Exception c) { } Random random = new Random(); - _locomotive = new DrawingLocomotive(); - _locomotive.Init(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256), + _locomotive = new DrawingLocomotive(random.nextInt(50, 150), random.nextInt(40, 70), new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)), GetWheelAmount()); - ChangePictureBoxLocomotiveBorders(); - _locomotive.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxLocomotiveWidth, pictureBoxLocomotiveHeight); - toolStripStatusLabelSpeed.setText("Скорость: " + _locomotive.Locomotive.Speed()); - toolStripStatusLabelWeight.setText("Вес: " + _locomotive.Locomotive.Weight()); - - toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_locomotive.Locomotive.BodyColor().getRGB())); - pictureBoxLocomotive.add(_locomotive, BorderLayout.CENTER); + SetData(); } }); addComponentListener(new ComponentAdapter() { @@ -123,4 +116,13 @@ public class FormLocomotive extends JFrame { return 4; } } + private void SetData() { + Random random = new Random(); + ChangePictureBoxLocomotiveBorders(); + _locomotive.SetPosition(random.nextInt(20, 100), random.nextInt(50, 100), pictureBoxLocomotiveWidth, pictureBoxLocomotiveHeight); + toolStripStatusLabelSpeed.setText("Скорость: " + _locomotive.Locomotive.Speed()); + toolStripStatusLabelWeight.setText("Вес: " + _locomotive.Locomotive.Weight()); + toolStripStatusLabelBodyColor.setText("Цвет: " + Integer.toHexString(_locomotive.Locomotive.BodyColor().getRGB())); + pictureBoxLocomotive.add(_locomotive, BorderLayout.CENTER); + } } -- 2.25.1