2022-09-24 19:51:41 +04:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
public class FormLocomotive extends JComponent{
|
|
|
|
private DrawningLocomotive _locomotive;
|
2022-10-21 16:38:26 +04:00
|
|
|
private DrawningLocomotive SelectedLocomotive;
|
|
|
|
public DrawningLocomotive getSelectedLocomotive() {
|
|
|
|
return SelectedLocomotive;
|
|
|
|
}
|
2022-11-05 18:49:21 +04:00
|
|
|
public void SetLocomotive(DrawningObjectLocomotive locomotive){
|
|
|
|
_locomotive = locomotive.GetDrawningLocomotive();
|
|
|
|
repaint();
|
|
|
|
}
|
2022-10-21 21:27:07 +04:00
|
|
|
public FormLocomotive(JDialog caller) {
|
2022-09-24 21:33:44 +04:00
|
|
|
|
|
|
|
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();
|
2022-10-21 21:53:50 +04:00
|
|
|
|
|
|
|
Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
|
|
|
|
|
2022-11-15 20:22:49 +04:00
|
|
|
_locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst, rnd.nextInt(2) + 2);
|
2022-10-21 21:27:07 +04:00
|
|
|
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500-75);
|
2022-09-24 21:33:44 +04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2022-10-08 18:36:19 +04:00
|
|
|
JButton modifiedButton = new JButton("Modified");
|
|
|
|
modifiedButton.addActionListener(e -> {
|
|
|
|
Random rnd = new Random();
|
2022-10-21 21:53:50 +04:00
|
|
|
|
2022-10-22 01:15:02 +04:00
|
|
|
Color colorFirst = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
|
|
|
|
Color colorSecond = JColorChooser.showDialog(null, "Color", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
|
2022-10-21 21:53:50 +04:00
|
|
|
|
2022-10-08 18:36:19 +04:00
|
|
|
_locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
|
2022-10-21 21:53:50 +04:00
|
|
|
colorFirst,
|
2022-11-15 20:22:49 +04:00
|
|
|
rnd.nextInt(2) + 2,
|
2022-10-21 21:53:50 +04:00
|
|
|
colorSecond,
|
2022-10-08 18:36:19 +04:00
|
|
|
rnd.nextBoolean(),
|
|
|
|
rnd.nextBoolean());
|
2022-10-21 21:27:07 +04:00
|
|
|
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 75);
|
2022-10-08 18:36:19 +04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2022-10-21 16:38:26 +04:00
|
|
|
JButton selectLocomotiveButton = new JButton("Select");
|
|
|
|
selectLocomotiveButton.addActionListener(e -> {
|
|
|
|
SelectedLocomotive = _locomotive;
|
2022-10-21 21:27:07 +04:00
|
|
|
caller.dispose();
|
2022-10-21 16:38:26 +04:00
|
|
|
});
|
|
|
|
|
2022-09-24 21:33:44 +04:00
|
|
|
statusPanel.add(createButton);
|
2022-10-08 18:36:19 +04:00
|
|
|
statusPanel.add(modifiedButton);
|
2022-10-21 16:38:26 +04:00
|
|
|
statusPanel.add(selectLocomotiveButton);
|
2022-09-24 21:33:44 +04:00
|
|
|
statusPanel.add(speedLabel);
|
|
|
|
statusPanel.add(weightLabel);
|
|
|
|
statusPanel.add(colorLabel);
|
|
|
|
|
2022-10-08 16:09:23 +04:00
|
|
|
JButton moveDownButton = new JButton("D");
|
2022-09-24 21:33:44 +04:00
|
|
|
moveDownButton.addActionListener(e -> {
|
|
|
|
if (_locomotive != null) _locomotive.MoveTransport(Direction.Down);
|
|
|
|
repaint();
|
|
|
|
});
|
|
|
|
|
2022-10-08 16:09:23 +04:00
|
|
|
JButton moveUpButton = new JButton("U");
|
2022-09-24 21:33:44 +04:00
|
|
|
moveUpButton.addActionListener(e -> {
|
|
|
|
if (_locomotive != null) _locomotive.MoveTransport(Direction.Up);
|
|
|
|
repaint();
|
|
|
|
});
|
|
|
|
|
2022-10-08 16:09:23 +04:00
|
|
|
JButton moveLeftButton = new JButton("L");
|
2022-09-24 21:33:44 +04:00
|
|
|
moveLeftButton.addActionListener(e -> {
|
|
|
|
if (_locomotive != null) _locomotive.MoveTransport(Direction.Left);
|
|
|
|
repaint();
|
|
|
|
});
|
|
|
|
|
2022-10-08 16:09:23 +04:00
|
|
|
JButton moveRightButton = new JButton("R");
|
2022-09-24 21:33:44 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
@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) {
|
2022-11-15 20:22:49 +04:00
|
|
|
|
|
|
|
new FormMapWithSetLocomotives();
|
2022-09-24 19:51:41 +04:00
|
|
|
}
|
|
|
|
}
|