package ProjectElectricLocomotive; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.util.Random; public class FormElectricLocomotive { DrawingElectricLocomotive _drawingElectricLocomotive = new DrawingElectricLocomotive(); private JButton buttonCreate; private JPanel pictureBox; private JButton buttonUp; private JButton buttonDown; private JButton buttonLeft; private JButton buttonRight; public JPanel getPictureBox() { return pictureBox; } public FormElectricLocomotive() { buttonUp.setName("buttonUp"); buttonDown.setName("buttonDown"); buttonLeft.setName("buttonLeft"); buttonRight.setName("buttonRight"); buttonCreate.addActionListener(e -> { _drawingElectricLocomotive = new DrawingElectricLocomotive(); Random random = new Random(); _drawingElectricLocomotive.Init( random.nextInt(100, 300), random.nextInt(1000, 3000), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)), random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight() ); _drawingElectricLocomotive.SetWheelsCount(random.nextInt(2, 5)); _drawingElectricLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); Draw(); }); ActionListener buttonMoveClickedListener = e -> { String buttonName = ((JButton) e.getSource()).getName(); switch (buttonName) { case ("buttonUp") -> { _drawingElectricLocomotive.MoveTransport(DyrectionType.Up); } case ("buttonDown") -> { _drawingElectricLocomotive.MoveTransport(DyrectionType.Down); } case ("buttonLeft") -> { _drawingElectricLocomotive.MoveTransport(DyrectionType.Left); } case ("buttonRight") -> { _drawingElectricLocomotive.MoveTransport(DyrectionType.Right); } } Draw(); }; buttonUp.addActionListener(buttonMoveClickedListener); buttonDown.addActionListener(buttonMoveClickedListener); buttonLeft.addActionListener(buttonMoveClickedListener); buttonRight.addActionListener(buttonMoveClickedListener); } public void Draw() { if (_drawingElectricLocomotive.EntityElectricLocomotive == null) { return; } Graphics g = pictureBox.getGraphics(); pictureBox.paint(g); _drawingElectricLocomotive.DrawTransport(g); } }