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());