PIbd-23_Mochalov_D.V._Locom.../FormLocomotive.java

114 lines
4.7 KiB
Java

import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class FormLocomotive extends JComponent{
private DrawningLocomotive _locomotive;
private DrawningLocomotive SelectedLocomotive;
public DrawningLocomotive getSelectedLocomotive() {
return SelectedLocomotive;
}
public void SetLocomotive(DrawningObjectLocomotive locomotive){
_locomotive = locomotive.GetDrawningLocomotive();
repaint();
}
public FormLocomotive(JDialog caller) {
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();
Color colorFirst = JColorChooser.showDialog(null, "Цвет", new Color(rnd.nextInt(256), rnd.nextInt(256),rnd.nextInt(256)));
_locomotive = new DrawningLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000, colorFirst, rnd.nextInt(2) + 2);
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500-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();
});
JButton modifiedButton = new JButton("Modified");
modifiedButton.addActionListener(e -> {
Random rnd = new Random();
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)));
_locomotive = new DrawningWarmlyLocomotive(rnd.nextInt(200) + 100, rnd.nextInt(1000) + 1000,
colorFirst,
rnd.nextInt(2) + 2,
colorSecond,
rnd.nextBoolean(),
rnd.nextBoolean());
_locomotive.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), 800, 500 - 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();
});
JButton selectLocomotiveButton = new JButton("Select");
selectLocomotiveButton.addActionListener(e -> {
SelectedLocomotive = _locomotive;
caller.dispose();
});
statusPanel.add(createButton);
statusPanel.add(modifiedButton);
statusPanel.add(selectLocomotiveButton);
statusPanel.add(speedLabel);
statusPanel.add(weightLabel);
statusPanel.add(colorLabel);
JButton moveDownButton = new JButton("D");
moveDownButton.addActionListener(e -> {
if (_locomotive != null) _locomotive.MoveTransport(Direction.Down);
repaint();
});
JButton moveUpButton = new JButton("U");
moveUpButton.addActionListener(e -> {
if (_locomotive != null) _locomotive.MoveTransport(Direction.Up);
repaint();
});
JButton moveLeftButton = new JButton("L");
moveLeftButton.addActionListener(e -> {
if (_locomotive != null) _locomotive.MoveTransport(Direction.Left);
repaint();
});
JButton moveRightButton = new JButton("R");
moveRightButton.addActionListener(e -> {
if (_locomotive != null) _locomotive.MoveTransport(Direction.Right);
repaint();
});
statusPanel.add(moveUpButton);
statusPanel.add(moveDownButton);
statusPanel.add(moveLeftButton);
statusPanel.add(moveRightButton);
}
@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) {
new FormMapWithSetLocomotives();
}
}