package Form; import Classes.Direction; import Classes.DrawingAircraft; import Classes.DrawingMilitaryAircraft; 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 JButton btnModification; 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 SetData() { Random rnd = new Random(); _aircraft.SetPosition( rnd.nextInt(0, 100), rnd.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()); } private void CreateEntity() { Random random = new Random(); _aircraft = new DrawingAircraft(random.nextInt(10, 300), random.nextFloat(1000, 2000),new Color( random.nextInt(0,256), random.nextInt(0,256), random.nextInt(0,256) )); SetData(); Draw(); } private void CreateModifiedEntity() { Random rnd = new Random(); _aircraft = new DrawingMilitaryAircraft(rnd.nextInt(100, 300), rnd.nextInt(1000, 2000), new Color(rnd.nextInt(0,256),rnd.nextInt(0,256),rnd.nextInt(0,256)), new Color(rnd.nextInt(0,256),rnd.nextInt(0,256),rnd.nextInt(0,256)), rnd.nextBoolean(), rnd.nextBoolean()); SetData(); 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()); btnModification.addActionListener(e -> CreateModifiedEntity()); Form.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { super.componentResized(e); resizeWindow(); } }); } }