package Form; import Classes.Direction; import Classes.DrawingAircraft; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.util.Random; public class FormAirFighter extends JFrame { private DrawingAircraft _aircraft; private JPanel Form; private JPanel label; private JPanel pictureBox; private JTextArea speedLabel; private JTextArea weightLabel; private JTextArea colorLabel; private JButton moveRight; private JButton moveDown; private JButton moveLeft; private JButton moveUp; private JButton btnCreate; private void moveButtonClick(ActionEvent event) { if (_aircraft == null) return; String name = ((JButton)event.getSource()).getName(); switch (name) { case "left" -> { _aircraft.MoveTransport(Direction.Left); } case "right" -> { _aircraft.MoveTransport(Direction.Right); } case "up" -> { _aircraft.MoveTransport(Direction.Up); } case "down" -> { _aircraft.MoveTransport(Direction.Down); } } Draw(); } private void CreateEntity() { Random random = new Random(); _aircraft = new DrawingAircraft(); _aircraft.Init( random.nextInt(10, 300), random.nextFloat(1000, 2000), new Color( random.nextInt(0,256), random.nextInt(0,256), random.nextInt(0,256) ) ); _aircraft.SetPosition( random.nextInt(0, 100), random.nextInt(0, 100), Form.getWidth(), Form.getHeight() ); speedLabel.setText("Speed: " + _aircraft.Plane.getSpeed()); weightLabel.setText("Weight: " + _aircraft.Plane.getWeight()); colorLabel.setText("Color: " + _aircraft.Plane.getBodyColor()); Draw(); } private void resizeWindow() { _aircraft.ChangeBorders(pictureBox.getWidth(), pictureBox.getHeight()); Draw(); } private void Draw() { Graphics2D graphics = (Graphics2D) pictureBox.getGraphics(); graphics.clearRect(0, 0, pictureBox.getWidth(), pictureBox.getHeight()); pictureBox.paintComponents(graphics); _aircraft.DrawTransport(graphics); } public FormAirFighter() { super("AirFighter"); setContentPane(Form); setSize(new Dimension(500, 500)); // action move // ActionListener listener = this::moveButtonClick; moveRight.addActionListener(listener); moveDown.addActionListener(listener); moveLeft.addActionListener(listener); moveUp.addActionListener(listener); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); btnCreate.addActionListener(e -> CreateEntity()); Form.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { super.componentResized(e); resizeWindow(); } }); } }