From e9053085ef2db48aed4f30ea3643bd878ab408bb Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 8 Oct 2022 17:29:51 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=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());