PIbd-12_Karamushko_M.K._Air.../FormAircraft.java

131 lines
4.4 KiB
Java
Raw Permalink Normal View History

2022-10-28 15:45:35 +04:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Random;
2022-11-15 18:20:34 +04:00
public class FormAircraft extends JDialog implements Form {
2022-10-28 15:45:35 +04:00
private JButton createButton;
private JButton upButton;
private JButton rightButton;
private JButton downButton;
private JButton leftButton;
private JPanel mainPanel;
private JPanel DrawPlace;
private JLabel speedLabel;
private JLabel weightLabel;
private JLabel colorLabel;
2022-11-15 18:20:34 +04:00
private JButton createModifButton;
private JButton selectButton;
2022-10-28 15:45:35 +04:00
DrawingAircraft _airFighter;
2022-11-15 18:20:34 +04:00
private DrawingAircraft selectedAircraft;
2022-10-28 15:45:35 +04:00
2022-11-15 18:20:34 +04:00
public DrawingAircraft getSelectedAircraft() {
return selectedAircraft;
2022-10-28 15:45:35 +04:00
}
2022-11-25 13:49:46 +04:00
public FormAircraft() {}
public FormAircraft(DrawingAircraft aircraft) {
_airFighter = aircraft;
}
2022-11-15 18:20:34 +04:00
2022-10-28 15:45:35 +04:00
public void run() {
2022-11-15 18:20:34 +04:00
add(mainPanel);
2022-10-28 15:45:35 +04:00
Canvas canv = new Canvas(this);
DrawPlace.add(canv);
createButton.addActionListener(e -> {
Dimension canvSize = canv.getSize();
Random rnd = new Random();
2022-11-15 18:20:34 +04:00
Color color = JColorChooser.showDialog(this, "Цвет", Color.BLACK);
if(color == null) color = Color.BLACK;
_airFighter = new DrawingAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), color);
_airFighter.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height);
Color bodyColor = _airFighter.AirFighter.BodyColor;
String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")";
speedLabel.setText("Скорость: " + _airFighter.AirFighter.Speed + " ");
weightLabel.setText("Вес: " + _airFighter.AirFighter.Weight + " ");
colorLabel.setText("Цвет: " + colorString);
canv.repaint();
});
createModifButton.addActionListener(e -> {
Dimension canvSize = canv.getSize();
Random rnd = new Random();
Color color = JColorChooser.showDialog(this, "Цвет", Color.BLACK);
Color dopColor = JColorChooser.showDialog(this, "Цвет", Color.BLACK);
if(color == null) color = Color.BLACK;
if(dopColor == null) dopColor = Color.BLACK;
_airFighter = new DrawingModernAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000),
color, dopColor,
rnd.nextInt(0, 2) == 1, rnd.nextInt(0, 2) == 1);
2022-10-28 15:45:35 +04:00
_airFighter.SetPosition((int)rnd.nextInt(10, 100), (int)rnd.nextInt(10, 100), canvSize.width, canvSize.height);
Color bodyColor = _airFighter.AirFighter.BodyColor;
String colorString = "(" + bodyColor.getRed() + ", " + bodyColor.getGreen() + ", " + bodyColor.getBlue() + ")";
speedLabel.setText("Скорость: " + _airFighter.AirFighter.Speed + " ");
weightLabel.setText("Вес: " + _airFighter.AirFighter.Weight + " ");
colorLabel.setText("Цвет: " + colorString);
canv.repaint();
});
2022-11-15 18:20:34 +04:00
selectButton.addActionListener(e -> {
selectedAircraft = _airFighter;
dispose();
});
addComponentListener(new ComponentAdapter() {
2022-10-28 15:45:35 +04:00
@Override
public void componentResized(ComponentEvent e) {
if(_airFighter == null) return;
_airFighter.ChangeBorders(canv.getSize().width, canv.getSize().height);
2022-11-15 18:20:34 +04:00
revalidate();
2022-10-28 15:45:35 +04:00
}
});
upButton.addActionListener(e -> {
if(_airFighter == null) return;
_airFighter.MoveTransport(Direction.Up);
canv.repaint();
});
rightButton.addActionListener(e -> {
if(_airFighter == null) return;
_airFighter.MoveTransport(Direction.Right);
canv.repaint();
});
downButton.addActionListener(e -> {
if(_airFighter == null) return;
_airFighter.MoveTransport(Direction.Down);
canv.repaint();
});
leftButton.addActionListener(e -> {
if(_airFighter == null) return;
_airFighter.MoveTransport(Direction.Left);
canv.repaint();
});
}
2022-11-02 09:51:14 +04:00
@Override
public void Draw(Graphics2D g) {
2022-10-28 15:45:35 +04:00
if(_airFighter == null) return;
_airFighter.DrawTransport(g);
}
}