package ProjectElectricLocomotive; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class FormElectricLocomotive { public DrawingLocomotive _drawingLocomotive; AbstractStrategy _abstractStrategy; private JButton buttonCreateElectricLocomotive; private JPanel pictureBox; private JButton buttonUp; private JButton buttonDown; private JButton buttonLeft; private JButton buttonRight; public JComboBox comboBoxStrategy; private JButton buttonStep; private JButton buttonCreateLocomotive; public JButton ButtonSelectLocomotive; public DrawingLocomotive SelectedLocomotive; public boolean IsSelect = false; public JPanel getPictureBox() { return pictureBox; } public FormElectricLocomotive() { buttonUp.setName("buttonUp"); buttonDown.setName("buttonDown"); buttonLeft.setName("buttonLeft"); buttonRight.setName("buttonRight"); buttonCreateLocomotive.addActionListener(e -> { Random rnd = new Random(); Color color = JColorChooser.showDialog(null, "Цвет", null); _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), rnd.nextInt(1000, 3000), color, pictureBox.getWidth(), pictureBox.getHeight()); _drawingLocomotive.SetWheelsCount(rnd.nextInt(2, 5)); _drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); Draw(); }); buttonCreateElectricLocomotive.addActionListener(e -> { Random random = new Random(); Color color = JColorChooser.showDialog(null, "Цвет", null); Color addColor = JColorChooser.showDialog(null, "Цвет2", null); _drawingLocomotive = new DrawingElectricLocomotive( random.nextInt(100, 300), random.nextInt(1000, 3000), color, addColor, random.nextBoolean(), random.nextBoolean(), pictureBox.getWidth(), pictureBox.getHeight() ); _drawingLocomotive.SetWheelsCount(random.nextInt(2, 5)); _drawingLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); Draw(); }); ButtonSelectLocomotive.addActionListener(e->{ SelectedLocomotive = _drawingLocomotive; IsSelect = true; // DialogResult = DialogResult.OK; }); buttonStep.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (_drawingLocomotive == null) { return; } if (comboBoxStrategy.isEnabled()) { switch(comboBoxStrategy.getSelectedIndex()) { case 0: _abstractStrategy = new MoveToCenter(); break; case 1: _abstractStrategy = new MoveToRigthCorner(); break; default: _abstractStrategy = null; break; } ; if (_abstractStrategy == null) { return; } _abstractStrategy.SetData(new DrawingObjectLocomotive(_drawingLocomotive), pictureBox.getWidth(), pictureBox.getHeight()); comboBoxStrategy.setEnabled(false); } if (_abstractStrategy == null) { return; } _abstractStrategy.MakeStep(); Draw(); if (_abstractStrategy.GetStatus() == Status.Finish) { comboBoxStrategy.setEnabled(true); _abstractStrategy = null; } } }); ActionListener buttonMoveClickedListener = e -> { String buttonName = ((JButton) e.getSource()).getName(); switch (buttonName) { case ("buttonUp") -> { _drawingLocomotive.MoveTransport(DyrectionType.Up); } case ("buttonDown") -> { _drawingLocomotive.MoveTransport(DyrectionType.Down); } case ("buttonLeft") -> { _drawingLocomotive.MoveTransport(DyrectionType.Left); } case ("buttonRight") -> { _drawingLocomotive.MoveTransport(DyrectionType.Right); } } Draw(); }; buttonUp.addActionListener(buttonMoveClickedListener); buttonDown.addActionListener(buttonMoveClickedListener); buttonLeft.addActionListener(buttonMoveClickedListener); buttonRight.addActionListener(buttonMoveClickedListener); } public void Draw() { if (_drawingLocomotive.EntityLocomotive == null) { return; } Graphics g = pictureBox.getGraphics(); pictureBox.paint(g); _drawingLocomotive.DrawTransport(g); } }