Compare commits
8 Commits
f6fab2a232
...
f7cf9ea559
Author | SHA1 | Date | |
---|---|---|---|
f7cf9ea559 | |||
ac9d6db704 | |||
0a29f09918 | |||
d30a086f9d | |||
fefd0e637e | |||
0f8658bfeb | |||
9c05e95a9b | |||
6b03a65d8f |
3
Direction.java
Normal file
3
Direction.java
Normal file
@ -0,0 +1,3 @@
|
||||
public enum Direction {
|
||||
Up, Down, Left, Right
|
||||
}
|
129
DrawningLocomotive.java
Normal file
129
DrawningLocomotive.java
Normal file
@ -0,0 +1,129 @@
|
||||
import java.awt.*;
|
||||
|
||||
class DrawningLocomotive {
|
||||
public EntityLocomotive Locomotive;
|
||||
public ExtraWheelsDraw extraWheelsDraw;
|
||||
/// Левая координата отрисовки локомотива
|
||||
private float _startPosX;
|
||||
/// Верхняя координата отрисовки локомотива
|
||||
private float _startPosY;
|
||||
/// Ширина окна отрисовки
|
||||
private Integer _pictureWidth = null;
|
||||
/// Высота окна отрисовки
|
||||
private Integer _pictureHeight = null;
|
||||
/// Ширина отрисовки локомотива
|
||||
private final int _locomotiveWidth = 120;
|
||||
/// Высота отрисовки локомотива
|
||||
private final int _locomotiveHeight = 50;
|
||||
/// Инициализация свойств
|
||||
public void Init(int speed, float weight, Color bodyColor, int wheelsNum, EntityLocomotive entity)
|
||||
{
|
||||
Locomotive = entity;
|
||||
extraWheelsDraw = new ExtraWheelsDraw();
|
||||
extraWheelsDraw.Init(wheelsNum, bodyColor);
|
||||
Locomotive.Init(speed, weight, bodyColor);
|
||||
}
|
||||
/// Установка позиции локомотива
|
||||
public void SetPosition(int x, int y, int width, int height)
|
||||
{
|
||||
if (x < 0 || x + _locomotiveWidth >= width)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (y < 0 || y + _locomotiveHeight >= height)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
}
|
||||
|
||||
public void MoveTransport(Direction direction)
|
||||
{
|
||||
if (_pictureWidth == null || _pictureHeight == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
switch (direction)
|
||||
{
|
||||
// вправо
|
||||
case Right:
|
||||
if (_startPosX + _locomotiveWidth + Locomotive.Step() < _pictureWidth)
|
||||
{
|
||||
_startPosX += Locomotive.Step();
|
||||
}
|
||||
else _startPosX = _pictureWidth - _locomotiveWidth;
|
||||
break;
|
||||
//влево
|
||||
case Left:
|
||||
if (_startPosX - Locomotive.Step() >= 0)
|
||||
{
|
||||
_startPosX -= Locomotive.Step();
|
||||
}
|
||||
else _startPosX = 0;
|
||||
break;
|
||||
//вверх
|
||||
case Up:
|
||||
if (_startPosY - Locomotive.Step() >= 0)
|
||||
{
|
||||
_startPosY -= Locomotive.Step();
|
||||
}
|
||||
else _startPosY = 0;
|
||||
break;
|
||||
//вниз
|
||||
case Down:
|
||||
if (_startPosY + _locomotiveHeight + Locomotive.Step() < _pictureHeight)
|
||||
{
|
||||
_startPosY += Locomotive.Step();
|
||||
}
|
||||
else _startPosY = _pictureHeight - _locomotiveHeight;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawTransport(Graphics2D g) {
|
||||
if (_startPosX < 0 || _startPosY < 0
|
||||
|| _pictureHeight == null || _pictureWidth == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//тело
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect((int)_startPosX , (int)_startPosY, _locomotiveWidth - 20, _locomotiveHeight - 10);
|
||||
//окна
|
||||
g.setColor(Locomotive.getBodyColor());
|
||||
g.fillRect((int)_startPosX + 10, (int)_startPosY + 10, 10, 10);
|
||||
g.fillRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 10);
|
||||
g.fillRect((int)_startPosX + 80, (int)_startPosY + 10, 10, 10);
|
||||
//дверь
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20);
|
||||
//колеса
|
||||
extraWheelsDraw.DrawWheels((int)_startPosX, (int)_startPosY, g);
|
||||
//движок
|
||||
g.setColor(Locomotive.getBodyColor());
|
||||
g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30);
|
||||
}
|
||||
|
||||
public void ChangeBorders(int width, int height)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height - 75;
|
||||
if (_pictureWidth <= _locomotiveWidth || _pictureHeight <= _locomotiveHeight)
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
return;
|
||||
}
|
||||
if (_startPosX + _locomotiveWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _locomotiveWidth;
|
||||
}
|
||||
if (_startPosY + _locomotiveHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _locomotiveHeight;
|
||||
}
|
||||
}
|
||||
}
|
40
EntityLocomotive.java
Normal file
40
EntityLocomotive.java
Normal file
@ -0,0 +1,40 @@
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class EntityLocomotive {
|
||||
private int Speed;
|
||||
public int getSpeed() {
|
||||
return Speed;
|
||||
}
|
||||
private float Weight;
|
||||
public float getWeight() {
|
||||
return Weight;
|
||||
}
|
||||
private Color BodyColor;
|
||||
public Color getBodyColor() {
|
||||
return BodyColor;
|
||||
}
|
||||
|
||||
public float Step () {
|
||||
return Speed * 100 / Weight;
|
||||
}
|
||||
|
||||
public void Init(int speed, float weight, Color bodyColor)
|
||||
{
|
||||
Random rnd = new Random();
|
||||
if (speed <= 0) {
|
||||
Speed = 50 + rnd.nextInt(100);
|
||||
}
|
||||
else {
|
||||
Speed = speed;
|
||||
}
|
||||
|
||||
if (weight <= 0) {
|
||||
Weight = 40 + rnd.nextInt(30);
|
||||
}
|
||||
else {
|
||||
Weight = weight;
|
||||
}
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
43
ExtraWheelsDraw.java
Normal file
43
ExtraWheelsDraw.java
Normal file
@ -0,0 +1,43 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class ExtraWheelsDraw {
|
||||
private WheelsCount wheelsCount = WheelsCount.Two;
|
||||
public void setWheelsNum(int num) {
|
||||
switch (num) {
|
||||
case 0: {
|
||||
wheelsCount = WheelsCount.Three;
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
wheelsCount = WheelsCount.Four;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
private Color color;
|
||||
|
||||
public void Init(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 Four: {
|
||||
g.drawOval(startPosX + 70, startPosY + 40, 10, 10);
|
||||
}
|
||||
case Three: {
|
||||
g.drawOval(startPosX + 20, startPosY + 40, 10, 10);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
101
FormLocomotive.java
Normal file
101
FormLocomotive.java
Normal file
@ -0,0 +1,101 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
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);
|
||||
formFrame.setSize(800, 500);
|
||||
formFrame.setVisible(true);
|
||||
formFrame.setLocationRelativeTo(null);
|
||||
|
||||
formFrame.addComponentListener(new ComponentListener() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
if (_locomotive != null) _locomotive.ChangeBorders(formFrame.getWidth(), formFrame.getHeight());
|
||||
repaint();
|
||||
}
|
||||
@Override
|
||||
public void componentMoved(ComponentEvent e) {}
|
||||
@Override
|
||||
public void componentShown(ComponentEvent e) {}
|
||||
@Override
|
||||
public void componentHidden(ComponentEvent e) {}
|
||||
});
|
||||
|
||||
Panel statusPanel = new Panel();
|
||||
statusPanel.setBackground(Color.WHITE);
|
||||
statusPanel.setLayout(new FlowLayout());
|
||||
setLayout(new BorderLayout());
|
||||
add(statusPanel, BorderLayout.SOUTH);
|
||||
|
||||
Label speedLabel = new Label("Speed: ");
|
||||
Label weightLabel = new Label("Weight: ");
|
||||
Label colorLabel = new Label("Color: ");
|
||||
|
||||
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.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());
|
||||
colorLabel.setText("Color: " + _locomotive.Locomotive.getBodyColor().getRed() + " " + _locomotive.Locomotive.getBodyColor().getGreen() + " " + _locomotive.Locomotive.getBodyColor().getBlue() );
|
||||
repaint();
|
||||
});
|
||||
|
||||
statusPanel.add(createButton);
|
||||
statusPanel.add(speedLabel);
|
||||
statusPanel.add(weightLabel);
|
||||
statusPanel.add(colorLabel);
|
||||
|
||||
JButton moveDownButton = new JButton("Down");
|
||||
moveDownButton.addActionListener(e -> {
|
||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Down);
|
||||
repaint();
|
||||
});
|
||||
|
||||
JButton moveUpButton = new JButton("Up");
|
||||
moveUpButton.addActionListener(e -> {
|
||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Up);
|
||||
repaint();
|
||||
});
|
||||
|
||||
JButton moveLeftButton = new JButton("Left");
|
||||
moveLeftButton.addActionListener(e -> {
|
||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Left);
|
||||
repaint();
|
||||
});
|
||||
|
||||
JButton moveRightButton = new JButton("Right");
|
||||
moveRightButton.addActionListener(e -> {
|
||||
if (_locomotive != null) _locomotive.MoveTransport(Direction.Right);
|
||||
repaint();
|
||||
});
|
||||
|
||||
statusPanel.add(moveUpButton);
|
||||
statusPanel.add(moveDownButton);
|
||||
statusPanel.add(moveLeftButton);
|
||||
statusPanel.add(moveRightButton);
|
||||
|
||||
formFrame.getContentPane().add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
if (_locomotive != null) _locomotive.DrawTransport(g2);
|
||||
super.repaint();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
FormLocomotive formLocomotive = new FormLocomotive();
|
||||
}
|
||||
}
|
3
WheelsCount.java
Normal file
3
WheelsCount.java
Normal file
@ -0,0 +1,3 @@
|
||||
public enum WheelsCount {
|
||||
Two, Three, Four
|
||||
}
|
Loading…
Reference in New Issue
Block a user