122 lines
4.1 KiB
Java
122 lines
4.1 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.Random;
|
|
|
|
public class FormElectricLocomotive extends JFrame {
|
|
|
|
private DrawningElectricLocomotive _drawningElectricLocomotive;
|
|
|
|
private JPanel panel;
|
|
private JButton buttonCreate;
|
|
private JButton buttonUp;
|
|
private JButton buttonDown;
|
|
private JButton buttonLeft;
|
|
private JButton buttonRight;
|
|
private JLabel pictureBoxElectricLocomotive;
|
|
|
|
public FormElectricLocomotive() {
|
|
initComponents();
|
|
}
|
|
|
|
private void initComponents() {
|
|
setTitle("Electric Locomotive");
|
|
setSize(520, 300);
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
panel = new JPanel();
|
|
buttonCreate = new JButton("Create");
|
|
buttonUp = new JButton("Up");
|
|
buttonDown = new JButton("Down");
|
|
buttonLeft = new JButton("Left");
|
|
buttonRight = new JButton("Right");
|
|
pictureBoxElectricLocomotive = new JLabel();
|
|
|
|
panel.setLayout(new FlowLayout());
|
|
panel.add(buttonCreate);
|
|
panel.add(buttonUp);
|
|
panel.add(buttonDown);
|
|
panel.add(buttonLeft);
|
|
panel.add(buttonRight);
|
|
add(panel, BorderLayout.NORTH);
|
|
add(pictureBoxElectricLocomotive, BorderLayout.CENTER);
|
|
|
|
buttonCreate.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
createElectricLocomotive();
|
|
}
|
|
});
|
|
|
|
buttonUp.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
moveTransport(DirectionType.Up);
|
|
}
|
|
});
|
|
|
|
buttonDown.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
moveTransport(DirectionType.Down);
|
|
}
|
|
});
|
|
|
|
buttonLeft.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
moveTransport(DirectionType.Left);
|
|
}
|
|
});
|
|
|
|
buttonRight.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
moveTransport(DirectionType.Right);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void createElectricLocomotive() {
|
|
Random random = new Random();
|
|
_drawningElectricLocomotive = new DrawningElectricLocomotive();
|
|
_drawningElectricLocomotive.init(
|
|
random.nextInt(200) + 100,
|
|
random.nextInt(2000) + 1000,
|
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
|
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
|
|
random.nextBoolean(),
|
|
random.nextBoolean(),
|
|
random.nextInt(0,5)
|
|
);
|
|
_drawningElectricLocomotive.setPictureSize(pictureBoxElectricLocomotive.getWidth(), pictureBoxElectricLocomotive.getHeight());
|
|
_drawningElectricLocomotive.setPosition(random.nextInt(pictureBoxElectricLocomotive.getWidth()), random.nextInt(pictureBoxElectricLocomotive.getHeight()));
|
|
|
|
draw();
|
|
}
|
|
|
|
private void moveTransport(DirectionType direction) {
|
|
if (_drawningElectricLocomotive != null) {
|
|
if (_drawningElectricLocomotive.moveTransport(direction)) {
|
|
draw();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void draw() {
|
|
if (_drawningElectricLocomotive != null) {
|
|
Image image = new ImageIcon(_drawningElectricLocomotive.getImage()).getImage();
|
|
Image scaledImage = image.getScaledInstance(pictureBoxElectricLocomotive.getWidth(), pictureBoxElectricLocomotive.getHeight(), Image.SCALE_SMOOTH);
|
|
pictureBoxElectricLocomotive.setIcon(new ImageIcon(scaledImage));
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
SwingUtilities.invokeLater(() -> {
|
|
FormElectricLocomotive form = new FormElectricLocomotive();
|
|
form.setVisible(true);
|
|
});
|
|
}
|
|
}
|