126 lines
4.9 KiB
Java
Raw Normal View History

package ProjectElectricLocomotive;
import javax.swing.*;
import java.awt.*;
2023-10-09 20:56:26 +04:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class FormElectricLocomotive {
2023-10-09 20:56:26 +04:00
//DrawingElectricLocomotive _drawingElectricLocomotive = new DrawingElectricLocomotive();
private JButton buttonCreateElectricLocomotive;
private JPanel pictureBox;
private JButton buttonUp;
private JButton buttonDown;
private JButton buttonLeft;
private JButton buttonRight;
2023-10-09 20:56:26 +04:00
public JComboBox comboBoxStrategy;
private JButton buttonStep;
private JButton buttonCreateLocomotive;
private DrawingLocomotive _drawingLocomotive;
private AbstractStrategy _abstractStrategy;
public JPanel getPictureBox() {
return pictureBox;
}
public FormElectricLocomotive()
{
buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight");
2023-10-09 20:56:26 +04:00
buttonCreateLocomotive.addActionListener(e -> {
Random rnd = new Random();
_drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), rnd.nextInt(1000, 3000),
new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)),
pictureBox.getWidth(), pictureBox.getHeight());
_drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
Draw();
});
2023-10-09 20:56:26 +04:00
buttonCreateElectricLocomotive.addActionListener(e -> {
Random random = new Random();
_drawingLocomotive = new DrawingElectricLocomotive(random.nextInt(100, 300),random.nextInt(1000, 3000),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
random.nextBoolean(),
random.nextBoolean(),
2023-10-09 20:56:26 +04:00
pictureBox.getWidth(), pictureBox.getHeight());
_drawingLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
Draw();
});
2023-10-09 20:56:26 +04:00
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") -> {
2023-10-09 20:56:26 +04:00
_drawingLocomotive.MoveTransport(DyrectionType.Up);
}
case ("buttonDown") -> {
2023-10-09 20:56:26 +04:00
_drawingLocomotive.MoveTransport(DyrectionType.Down);
}
case ("buttonLeft") -> {
2023-10-09 20:56:26 +04:00
_drawingLocomotive.MoveTransport(DyrectionType.Left);
}
case ("buttonRight") -> {
2023-10-09 20:56:26 +04:00
_drawingLocomotive.MoveTransport(DyrectionType.Right);
}
}
Draw();
};
buttonUp.addActionListener(buttonMoveClickedListener);
buttonDown.addActionListener(buttonMoveClickedListener);
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonRight.addActionListener(buttonMoveClickedListener);
}
public void Draw() {
2023-10-09 20:56:26 +04:00
if (_drawingLocomotive.EntityLocomotive == null) {
return;
}
Graphics g = pictureBox.getGraphics();
pictureBox.paint(g);
2023-10-09 20:56:26 +04:00
_drawingLocomotive.DrawTransport(g);
}
}