Переход на конструкторы

This commit is contained in:
Данила Мочалов 2022-10-08 17:29:51 +04:00
parent 885fa94fd3
commit e9053085ef
4 changed files with 8 additions and 11 deletions

View File

@ -1,4 +1,5 @@
import java.awt.*; import java.awt.*;
import java.util.Random;
class DrawningLocomotive { class DrawningLocomotive {
public EntityLocomotive Locomotive; public EntityLocomotive Locomotive;
@ -16,12 +17,11 @@ class DrawningLocomotive {
/// Высота отрисовки локомотива /// Высота отрисовки локомотива
private final int _locomotiveHeight = 50; 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(random.nextInt(3), bodyColor);
extraWheelsDraw = new ExtraWheelsDraw(); Locomotive = new EntityLocomotive(speed, weight, bodyColor);
extraWheelsDraw.Init(wheelsNum, bodyColor);
Locomotive.Init(speed, weight, bodyColor);
} }
/// Установка позиции локомотива /// Установка позиции локомотива
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)

View File

@ -19,7 +19,7 @@ public class EntityLocomotive {
return Speed * 100 / Weight; 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(); Random rnd = new Random();
if (speed <= 0) { if (speed <= 0) {

View File

@ -18,7 +18,7 @@ public class ExtraWheelsDraw {
} }
private Color color; private Color color;
public void Init(int num, Color color) { public ExtraWheelsDraw(int num, Color color) {
setWheelsNum(num); setWheelsNum(num);
this.color = color; this.color = color;
} }

View File

@ -5,7 +5,6 @@ import java.util.Random;
public class FormLocomotive extends JComponent{ public class FormLocomotive extends JComponent{
private DrawningLocomotive _locomotive; private DrawningLocomotive _locomotive;
private EntityLocomotive _entity;
public FormLocomotive() { public FormLocomotive() {
JFrame formFrame = new JFrame("Locomotive"); JFrame formFrame = new JFrame("Locomotive");
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@ -40,9 +39,7 @@ public class FormLocomotive extends JComponent{
JButton createButton = new JButton("Create"); JButton createButton = new JButton("Create");
createButton.addActionListener(e -> { createButton.addActionListener(e -> {
Random rnd = new Random(); Random rnd = new Random();
_locomotive = new DrawningLocomotive(); _locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, new Color(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)));
_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.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75); _locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), formFrame.getWidth(), formFrame.getHeight() - 75);
speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed()); speedLabel.setText("Speed: " + _locomotive.Locomotive.getSpeed());
weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight()); weightLabel.setText("Weight: " + (int)_locomotive.Locomotive.getWeight());