import javax.swing.*; import java.awt.*; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.Random; public class FormAircraft extends JDialog implements Form { 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; private JButton createModifButton; private JButton selectButton; DrawingAircraft _airFighter; private DrawingAircraft selectedAircraft; public DrawingAircraft getSelectedAircraft() { return selectedAircraft; } public FormAircraft() {} public FormAircraft(DrawingAircraft aircraft) { _airFighter = aircraft; } public void run() { add(mainPanel); Canvas canv = new Canvas(this); DrawPlace.add(canv); createButton.addActionListener(e -> { Dimension canvSize = canv.getSize(); Random rnd = new Random(); 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); _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(); }); selectButton.addActionListener(e -> { selectedAircraft = _airFighter; dispose(); }); addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { if(_airFighter == null) return; _airFighter.ChangeBorders(canv.getSize().width, canv.getSize().height); revalidate(); } }); 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(); }); } @Override public void Draw(Graphics2D g) { if(_airFighter == null) return; _airFighter.DrawTransport(g); } }