146 lines
5.3 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 {
public DrawingLocomotive _drawingLocomotive;
2023-10-10 01:30:20 +04:00
AbstractStrategy _abstractStrategy;
2023-10-09 20:56:26 +04:00
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;
2023-10-21 22:32:22 +04:00
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");
2023-10-09 20:56:26 +04:00
buttonCreateLocomotive.addActionListener(e -> {
Random rnd = new Random();
Color color = JColorChooser.showDialog(null, "Цвет", null);
2023-10-10 01:30:20 +04:00
_drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300),
rnd.nextInt(1000, 3000), color,
2023-10-10 01:30:20 +04:00
pictureBox.getWidth(),
pictureBox.getHeight());
_drawingLocomotive.SetWheelsCount(rnd.nextInt(2, 5));
2023-10-09 20:56:26 +04:00
_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();
Color color = JColorChooser.showDialog(null, "Цвет", null);
Color addColor = JColorChooser.showDialog(null, "Цвет2", null);
2023-10-10 01:30:20 +04:00
_drawingLocomotive = new DrawingElectricLocomotive(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
color,
addColor,
random.nextBoolean(),
random.nextBoolean(),
2023-10-10 01:30:20 +04:00
pictureBox.getWidth(),
pictureBox.getHeight()
);
_drawingLocomotive.SetWheelsCount(random.nextInt(2, 5));
2023-10-09 20:56:26 +04:00
_drawingLocomotive.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
Draw();
});
ButtonSelectLocomotive.addActionListener(e->{
SelectedLocomotive = _drawingLocomotive;
IsSelect = true;
});
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);
}
}