Extra task completed

This commit is contained in:
Данила Мочалов 2022-09-25 13:45:19 +04:00
parent d30a086f9d
commit 0a29f09918
4 changed files with 59 additions and 21 deletions

View File

@ -2,6 +2,7 @@ import java.awt.*;
class DrawningLocomotive { class DrawningLocomotive {
public EntityLocomotive Locomotive; public EntityLocomotive Locomotive;
public ExtraWheelsDraw extraWheelsDraw;
/// Левая координата отрисовки локомотива /// Левая координата отрисовки локомотива
private float _startPosX; private float _startPosX;
/// Верхняя координата отрисовки локомотива /// Верхняя координата отрисовки локомотива
@ -15,9 +16,10 @@ class DrawningLocomotive {
/// Высота отрисовки локомотива /// Высота отрисовки локомотива
private final int _locomotiveHeight = 50; private final int _locomotiveHeight = 50;
/// Инициализация свойств /// Инициализация свойств
public void Init(int speed, float weight, Color bodyColor, EntityLocomotive entity) public void Init(int speed, float weight, Color bodyColor, int wheelsNum, EntityLocomotive entity)
{ {
Locomotive = entity; Locomotive = entity;
extraWheelsDraw = new ExtraWheelsDraw(wheelsNum, bodyColor);
Locomotive.Init(speed, weight, bodyColor); Locomotive.Init(speed, weight, bodyColor);
} }
/// Установка позиции локомотива /// Установка позиции локомотива
@ -98,14 +100,10 @@ class DrawningLocomotive {
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20); g.drawRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20);
//колеса //колеса
g.drawOval((int) _startPosX, (int)_startPosY + 40, 10, 10); extraWheelsDraw.DrawWheels((int)_startPosX, (int)_startPosY, g);
g.drawOval((int) _startPosX + 20, (int)_startPosY + 40, 10, 10);
g.drawOval((int) _startPosX + 70, (int)_startPosY + 40, 10, 10);
g.drawOval((int) _startPosX + 90, (int)_startPosY + 40, 10, 10);
//движок //движок
g.setColor(Locomotive.getBodyColor()); g.setColor(Locomotive.getBodyColor());
g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30); g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30);
} }
public void ChangeBorders(int width, int height) public void ChangeBorders(int width, int height)

48
ExtraWheelsDraw.java Normal file
View File

@ -0,0 +1,48 @@
import java.awt.*;
public class ExtraWheelsDraw {
private WheelsCount wheelsCount;
private int wheelsNum;
public void setWheelsNum(int num) {
wheelsNum = num;
switch (wheelsNum) {
case 0: {
wheelsCount = WheelsCount.Two;
break;
}
case 1: {
wheelsCount = WheelsCount.Three;
break;
}
case 2: {
wheelsCount = WheelsCount.Four;
break;
}
}
}
private final Color color;
public ExtraWheelsDraw(int num, Color color) {
setWheelsNum(num);
this.color = color;
}
public void DrawWheels(int startPosX, int startPosY, Graphics2D g) {
g.setColor(color);
g.drawOval(startPosX, startPosY + 40, 10, 10);
g.drawOval(startPosX + 90, startPosY + 40, 10, 10);
switch (wheelsCount) {
case Two: {
break;
}
case Three: {
g.drawOval(startPosX + 20, startPosY + 40, 10, 10);
break;
}
case Four: {
g.drawOval(startPosX + 20, startPosY + 40, 10, 10);
g.drawOval(startPosX + 70, startPosY + 40, 10, 10);
}
}
}
}

View File

@ -19,21 +19,12 @@ public class FormLocomotive extends JComponent{
if (_locomotive != null) _locomotive.ChangeBorders(formFrame.getWidth(), formFrame.getHeight()); if (_locomotive != null) _locomotive.ChangeBorders(formFrame.getWidth(), formFrame.getHeight());
repaint(); repaint();
} }
@Override @Override
public void componentMoved(ComponentEvent e) { public void componentMoved(ComponentEvent e) {}
}
@Override @Override
public void componentShown(ComponentEvent e) { public void componentShown(ComponentEvent e) {}
}
@Override @Override
public void componentHidden(ComponentEvent e) { public void componentHidden(ComponentEvent e) {}
}
}); });
Panel statusPanel = new Panel(); Panel statusPanel = new Panel();
@ -51,7 +42,7 @@ public class FormLocomotive extends JComponent{
Random rnd = new Random(); Random rnd = new Random();
_locomotive = new DrawningLocomotive(); _locomotive = new DrawningLocomotive();
_entity = new EntityLocomotive(); _entity = new EntityLocomotive();
_locomotive.Init(100 + rnd.nextInt(200), 1000 + rnd.nextInt(1000), Color.getHSBColor(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)), _entity); _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());
@ -106,7 +97,5 @@ public class FormLocomotive extends JComponent{
public static void main(String[] args) { public static void main(String[] args) {
FormLocomotive formLocomotive = new FormLocomotive(); FormLocomotive formLocomotive = new FormLocomotive();
} }
} }

3
WheelsCount.java Normal file
View File

@ -0,0 +1,3 @@
public enum WheelsCount {
Two, Three, Four
}