Переход на конструкторы
This commit is contained in:
parent
885fa94fd3
commit
e9053085ef
@ -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)
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user