98 lines
3.0 KiB
Java
98 lines
3.0 KiB
Java
import javax.swing.*;
|
||
import java.awt.*;
|
||
import java.awt.event.ComponentAdapter;
|
||
import java.awt.event.ComponentEvent;
|
||
import java.beans.PropertyChangeEvent;
|
||
import java.beans.PropertyChangeListener;
|
||
import java.util.Random;
|
||
|
||
public class FormTracktor extends JFrame {
|
||
private JPanel ContentPanel;
|
||
private JButton buttonCreate;
|
||
private JLabel speedLabel;
|
||
private JLabel weightLabel;
|
||
private JLabel colorLabel;
|
||
private JButton buttonLeft;
|
||
private JButton buttonDown;
|
||
private JButton buttonRight;
|
||
private JButton buttonUp;
|
||
private JPanel pictureBox;
|
||
|
||
private DrawningTracktor _tracktor;
|
||
|
||
public FormTracktor(){
|
||
setTitle("Трактор");
|
||
setContentPane(ContentPanel);
|
||
setSize(800, 500);
|
||
|
||
// Обработка нажатия кнопки "Создать"
|
||
buttonCreate.addActionListener(e->{
|
||
Random rnd = new Random();
|
||
_tracktor = new DrawningTracktor();
|
||
_tracktor.Init(
|
||
rnd.nextInt(100, 300),
|
||
rnd.nextInt(1000, 2000),
|
||
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
|
||
rnd.nextInt(3,8)
|
||
);
|
||
_tracktor.SetPosition(
|
||
rnd.nextInt(10, 100),
|
||
rnd.nextInt(10, 100),
|
||
pictureBox.getWidth(), pictureBox.getHeight()
|
||
);
|
||
speedLabel.setText("Скорость: " + _tracktor.getTracktor().getSpeed());
|
||
weightLabel.setText("Вес: " + _tracktor.getTracktor().getWeight());
|
||
colorLabel.setText("Цвет: " + String.format("%h",_tracktor.getTracktor().getBodyColor()));
|
||
repaint();
|
||
});
|
||
|
||
buttonUp.addActionListener(e->{
|
||
if (_tracktor != null){
|
||
_tracktor.MoveTransport(Direction.Up);
|
||
repaint();
|
||
}
|
||
});
|
||
|
||
buttonLeft.addActionListener(e->{
|
||
if (_tracktor != null){
|
||
_tracktor.MoveTransport(Direction.Left);
|
||
repaint();
|
||
}
|
||
});
|
||
|
||
buttonDown.addActionListener(e->{
|
||
if (_tracktor != null){
|
||
_tracktor.MoveTransport(Direction.Down);
|
||
repaint();
|
||
}
|
||
});
|
||
|
||
buttonRight.addActionListener(e->{
|
||
if (_tracktor != null){
|
||
_tracktor.MoveTransport(Direction.Right);
|
||
repaint();
|
||
}
|
||
});
|
||
|
||
pictureBox.addComponentListener(new ComponentAdapter() {
|
||
@Override
|
||
public void componentResized(ComponentEvent e) {
|
||
super.componentResized(e);
|
||
if (_tracktor != null){
|
||
_tracktor.ChangeBorders(e.getComponent().getWidth(), e.getComponent().getHeight());
|
||
repaint();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void paint(Graphics g){
|
||
super.paint(g);
|
||
g = pictureBox.getGraphics();
|
||
if (_tracktor != null){
|
||
_tracktor.DrawTransport(g);
|
||
}
|
||
}
|
||
}
|