2022-09-24 19:51:41 +04:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
2022-09-24 21:33:44 +04:00
|
|
|
import java.awt.event.*;
|
2022-09-24 19:51:41 +04:00
|
|
|
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);
|
|
|
|
|
2022-09-24 21:33:44 +04:00
|
|
|
formFrame.addComponentListener(new ComponentListener() {
|
2022-09-24 19:51:41 +04:00
|
|
|
@Override
|
2022-09-24 21:33:44 +04:00
|
|
|
public void componentResized(ComponentEvent e) {
|
|
|
|
if (_locomotive != null) _locomotive.ChangeBorders(formFrame.getWidth(), formFrame.getHeight());
|
2022-09-24 19:51:41 +04:00
|
|
|
repaint();
|
|
|
|
}
|
2022-09-24 21:33:44 +04:00
|
|
|
|
|
|
|
@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)), _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();
|
2022-09-24 19:51:41 +04:00
|
|
|
});
|
|
|
|
|
2022-09-24 21:33:44 +04:00
|
|
|
statusPanel.add(moveUpButton);
|
|
|
|
statusPanel.add(moveDownButton);
|
|
|
|
statusPanel.add(moveLeftButton);
|
|
|
|
statusPanel.add(moveRightButton);
|
2022-09-24 19:51:41 +04:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|