2022-11-05 22:34:57 +04:00
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
2022-11-05 23:24:05 +04:00
|
|
|
|
import java.awt.event.ComponentAdapter;
|
|
|
|
|
import java.awt.event.ComponentEvent;
|
2022-11-05 22:34:57 +04:00
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
2022-11-21 21:34:45 +04:00
|
|
|
|
public class FormTracktor extends JDialog {
|
2022-11-05 22:34:57 +04:00
|
|
|
|
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;
|
2022-11-07 00:26:51 +04:00
|
|
|
|
private JButton buttonCreateModif;
|
2022-11-21 21:34:45 +04:00
|
|
|
|
private JButton buttonSelect;
|
2022-11-05 22:34:57 +04:00
|
|
|
|
|
|
|
|
|
private DrawningTracktor _tracktor;
|
2022-11-21 21:34:45 +04:00
|
|
|
|
private DrawningTracktor selectedTracktor;
|
2022-11-05 22:34:57 +04:00
|
|
|
|
|
|
|
|
|
public FormTracktor(){
|
|
|
|
|
setTitle("Трактор");
|
|
|
|
|
setContentPane(ContentPanel);
|
|
|
|
|
setSize(800, 500);
|
|
|
|
|
|
|
|
|
|
// Обработка нажатия кнопки "Создать"
|
|
|
|
|
buttonCreate.addActionListener(e->{
|
|
|
|
|
Random rnd = new Random();
|
2022-11-21 21:34:45 +04:00
|
|
|
|
Color color = JColorChooser.showDialog(this, "Цвет", new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)));
|
|
|
|
|
if (color == null) {
|
|
|
|
|
color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
|
|
|
|
}
|
|
|
|
|
_tracktor = new DrawningTracktor(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color, rnd.nextInt(3,8));
|
2022-11-07 00:26:51 +04:00
|
|
|
|
setData();
|
|
|
|
|
});
|
2022-11-05 23:32:00 +04:00
|
|
|
|
|
2022-11-07 00:26:51 +04:00
|
|
|
|
// Обработка нажатия кнопки "Модификация"
|
|
|
|
|
buttonCreateModif.addActionListener(e->{
|
|
|
|
|
Random rnd = new Random();
|
2022-11-21 21:34:45 +04:00
|
|
|
|
Color color = JColorChooser.showDialog(this, "Основной цвет", Color.white);
|
|
|
|
|
if (color == null) {
|
|
|
|
|
color = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
|
|
|
|
}
|
|
|
|
|
Color dopColor = JColorChooser.showDialog(this, "Дополнительный цвет", Color.white);
|
|
|
|
|
if (dopColor == null) {
|
|
|
|
|
dopColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256));
|
|
|
|
|
}
|
2022-11-07 00:26:51 +04:00
|
|
|
|
_tracktor = new DrawningTrackedVehicle(
|
|
|
|
|
rnd.nextInt(100, 300),
|
|
|
|
|
rnd.nextInt(1000, 2000),
|
2022-11-21 21:34:45 +04:00
|
|
|
|
color,
|
2022-11-07 00:26:51 +04:00
|
|
|
|
rnd.nextInt(3,8),
|
2022-11-21 21:34:45 +04:00
|
|
|
|
dopColor,
|
2022-11-07 00:26:51 +04:00
|
|
|
|
rnd.nextBoolean(),
|
|
|
|
|
rnd.nextBoolean()
|
2022-11-05 22:34:57 +04:00
|
|
|
|
);
|
2022-11-07 00:26:51 +04:00
|
|
|
|
setData();
|
2022-11-05 22:34:57 +04:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-11-05 23:24:05 +04:00
|
|
|
|
|
2022-11-21 21:34:45 +04:00
|
|
|
|
buttonSelect.addActionListener(e->{
|
|
|
|
|
selectedTracktor = _tracktor;
|
|
|
|
|
dispose();
|
|
|
|
|
});
|
|
|
|
|
|
2022-11-05 23:24:05 +04:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-11-05 22:34:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 00:26:51 +04:00
|
|
|
|
private void setData() {
|
|
|
|
|
Random rnd = new Random();
|
|
|
|
|
_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();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-22 00:21:46 +04:00
|
|
|
|
public FormTracktor(DrawningTracktor tracktor) {
|
|
|
|
|
this();
|
|
|
|
|
_tracktor = tracktor;
|
|
|
|
|
repaint();
|
|
|
|
|
}
|
2022-11-21 21:34:45 +04:00
|
|
|
|
public DrawningTracktor getSelectedTracktor() {
|
|
|
|
|
return selectedTracktor;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 22:34:57 +04:00
|
|
|
|
@Override
|
|
|
|
|
public void paint(Graphics g){
|
|
|
|
|
super.paint(g);
|
2022-11-07 00:26:51 +04:00
|
|
|
|
Graphics2D g2d = (Graphics2D)pictureBox.getGraphics();
|
2022-11-05 22:34:57 +04:00
|
|
|
|
if (_tracktor != null){
|
2022-11-07 00:26:51 +04:00
|
|
|
|
_tracktor.DrawTransport(g2d);
|
2022-11-05 22:34:57 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|