106 lines
4.2 KiB
Java
Raw Normal View History

2022-11-22 08:45:54 +04:00
import javax.swing.*;
import java.awt.*;
2022-12-06 11:02:26 +04:00
import java.awt.event.*;
2022-11-22 08:45:54 +04:00
import java.util.Random;
2022-12-06 11:02:26 +04:00
public class FormShip extends JDialog {
2022-11-22 08:45:54 +04:00
private JPanel shipPane;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
private JPanel pictureBox;
private JButton createButton;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
2022-11-22 10:13:07 +04:00
private JButton createAdvancedButton;
2022-12-06 11:02:26 +04:00
public JButton buttonSelect;
2022-11-22 08:45:54 +04:00
private DrawingShip _ship;
2022-12-06 11:02:26 +04:00
private DrawingShip selectedShip;
2022-11-22 08:45:54 +04:00
public FormShip() {
this.setTitle("Ship");
this.setContentPane(shipPane);
createButton.addActionListener(e -> {
Random rnd = new Random();
2022-12-06 11:02:26 +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));
}
_ship = new DrawingShip(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color, rnd.nextInt(1, 4));
2022-11-22 08:45:54 +04:00
_ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight());
speedLabel.setText(String.format("Speed: %s", _ship.getShip().getSpeed()));
weightLabel.setText(String.format("Weight: %s", _ship.getShip().getWeight()));
colorLabel.setText(String.format("Color: %x", _ship.getShip().getBodyColor().getRGB()));
repaint();
});
pictureBox.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if (_ship != null) _ship.changeBorders(pictureBox.getWidth(), pictureBox.getHeight());
repaint();
}
});
buttonLeft.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Left);
repaint();
});
buttonRight.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Right);
repaint();
});
buttonUp.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Up);
repaint();
});
buttonDown.addActionListener(e -> {
if (_ship != null) _ship.moveTransport(Direction.Down);
repaint();
});
2022-11-22 10:13:07 +04:00
createAdvancedButton.addActionListener(e -> {
Random rnd = new Random();
2022-12-06 11:02:26 +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-22 10:13:07 +04:00
_ship = new DrawingMotorShip(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
2022-12-06 11:02:26 +04:00
color,
2022-12-06 10:27:20 +04:00
rnd.nextInt(1, 4),
2022-12-06 11:02:26 +04:00
dopColor,
2022-11-22 10:13:07 +04:00
rnd.nextBoolean(),
rnd.nextBoolean()
);
_ship.SetPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight());
speedLabel.setText(String.format("Скорость: %s", _ship.getShip().getSpeed()));
weightLabel.setText(String.format("Вес: %s", _ship.getShip().getWeight()));
colorLabel.setText(String.format("Цвет: %x", _ship.getShip().getBodyColor().getRGB()));
repaint();
});
2022-12-06 11:02:26 +04:00
buttonSelect.addActionListener(e -> {
selectedShip = _ship;
dispose();
});
2022-11-22 08:45:54 +04:00
}
@Override
public void paint(Graphics g) {
super.paint(g);
2022-11-22 10:13:07 +04:00
Graphics2D g2d = (Graphics2D) pictureBox.getGraphics();
2022-11-22 08:45:54 +04:00
if (_ship != null) {
_ship.drawTransport(g2d);
}
}
2022-12-06 11:02:26 +04:00
public DrawingShip getSelectedShip() {
return selectedShip;
}
2022-11-22 08:45:54 +04:00
}