PIbd-23_Polevoy_S.V._SelfPr.../FormArtillery.java

106 lines
4.5 KiB
Java
Raw Normal View History

2022-09-25 19:20:16 +04:00
import javax.swing.*;
import java.awt.*;
2022-10-24 23:03:50 +04:00
import java.awt.event.*;
2022-09-25 19:20:16 +04:00
import java.util.Random;
public class FormArtillery extends JFrame {
private JPanel artilleryPane;
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;
private JButton createAdvancedButton;
2022-10-24 23:03:50 +04:00
private JButton buttonSelect;
2022-09-25 19:20:16 +04:00
private DrawingArtillery _artillery;
2022-10-24 23:03:50 +04:00
private DrawingArtillery selectedArtillery;
2022-09-25 19:20:16 +04:00
public FormArtillery() {
this.setTitle("Artillery");
this.setContentPane(artilleryPane);
createButton.addActionListener(e -> {
Random rnd = new Random();
2022-10-24 23:03:50 +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));
}
_artillery = new DrawingArtillery(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color, rnd.nextInt(4, 7));
2022-10-08 15:25:07 +04:00
_artillery.setPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight());
2022-09-25 19:20:16 +04:00
speedLabel.setText(String.format("Скорость: %s", _artillery.getArtillery().getSpeed()));
weightLabel.setText(String.format("Вес: %s", _artillery.getArtillery().getWeight()));
colorLabel.setText(String.format("Цвет: %x", _artillery.getArtillery().getBodyColor().getRGB()));
repaint();
});
pictureBox.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
2022-09-27 16:22:49 +04:00
if (_artillery != null) _artillery.changeBorders(pictureBox.getWidth(), pictureBox.getHeight());
2022-09-25 19:20:16 +04:00
repaint();
}
});
buttonLeft.addActionListener(e -> {
if (_artillery != null) _artillery.moveTransport(Direction.Left);
repaint();
});
buttonRight.addActionListener(e -> {
if (_artillery != null) _artillery.moveTransport(Direction.Right);
repaint();
});
buttonUp.addActionListener(e -> {
if (_artillery != null) _artillery.moveTransport(Direction.Up);
repaint();
});
buttonDown.addActionListener(e -> {
if (_artillery != null) _artillery.moveTransport(Direction.Down);
repaint();
});
createAdvancedButton.addActionListener(e -> {
Random rnd = new Random();
2022-10-24 23:03:50 +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));
}
_artillery = new DrawingAdvancedArtillery(
rnd.nextInt(100, 300),
rnd.nextInt(1000, 2000),
2022-10-24 23:03:50 +04:00
color,
rnd.nextInt(4, 7),
2022-10-24 23:03:50 +04:00
dopColor,
rnd.nextBoolean(),
rnd.nextBoolean()
);
_artillery.setPosition(10 + rnd.nextInt(90), 10 + rnd.nextInt(90), pictureBox.getWidth(), pictureBox.getHeight());
speedLabel.setText(String.format("Скорость: %s", _artillery.getArtillery().getSpeed()));
weightLabel.setText(String.format("Вес: %s", _artillery.getArtillery().getWeight()));
colorLabel.setText(String.format("Цвет: %x", _artillery.getArtillery().getBodyColor().getRGB()));
repaint();
});
2022-10-24 23:03:50 +04:00
buttonSelect.addActionListener(e -> {
selectedArtillery = _artillery;
dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
});
}
public DrawingArtillery getSelectedArtillery() {
return selectedArtillery;
2022-09-25 19:20:16 +04:00
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) pictureBox.getGraphics();
2022-09-25 19:20:16 +04:00
if (_artillery != null) {
_artillery.drawTransport(g2d);
}
}
}