2023-09-14 20:54:49 +04:00
|
|
|
|
package ProjectElectricLocomotive;
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
2023-10-09 20:56:26 +04:00
|
|
|
|
import java.awt.event.ActionEvent;
|
2023-09-14 20:54:49 +04:00
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
2023-11-18 23:58:18 +04:00
|
|
|
|
public class FormElectricLocomotive extends JDialog{
|
2023-10-21 19:25:05 +04:00
|
|
|
|
public DrawingLocomotive _drawingLocomotive;
|
2023-10-10 01:30:20 +04:00
|
|
|
|
AbstractStrategy _abstractStrategy;
|
2023-10-09 20:56:26 +04:00
|
|
|
|
private JButton buttonCreateElectricLocomotive;
|
2023-11-18 23:58:18 +04:00
|
|
|
|
public JComponent pictureBox;
|
2023-09-14 20:54:49 +04:00
|
|
|
|
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;
|
2023-10-21 19:25:05 +04:00
|
|
|
|
public DrawingLocomotive SelectedLocomotive;
|
|
|
|
|
public boolean IsSelect = false;
|
2023-09-14 20:54:49 +04:00
|
|
|
|
|
2023-11-18 23:58:18 +04:00
|
|
|
|
public JComponent getPictureBox()
|
2023-11-08 00:17:28 +04:00
|
|
|
|
{
|
2023-09-14 20:54:49 +04:00
|
|
|
|
return pictureBox;
|
|
|
|
|
}
|
2023-11-18 23:58:18 +04:00
|
|
|
|
|
|
|
|
|
private class Canvas extends JPanel{
|
|
|
|
|
public Canvas(){
|
|
|
|
|
}
|
|
|
|
|
public void paintComponent (Graphics g){
|
|
|
|
|
if (_drawingLocomotive == null){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Graphics2D g2d = (Graphics2D)g;
|
|
|
|
|
g2d.setColor(getBackground());
|
|
|
|
|
g2d.fillRect(0, 0, getWidth(), getHeight());
|
|
|
|
|
super.paintComponents(g);
|
|
|
|
|
_drawingLocomotive.DrawTransport(g2d);
|
|
|
|
|
super.repaint();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-14 20:54:49 +04:00
|
|
|
|
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();
|
2023-11-18 23:58:18 +04:00
|
|
|
|
Color color = JColorChooser.showDialog(
|
|
|
|
|
null,
|
|
|
|
|
"Цвет",
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
_drawingLocomotive = new DrawingLocomotive(
|
|
|
|
|
rnd.nextInt(100, 300),
|
|
|
|
|
rnd.nextInt(1000, 3000),
|
|
|
|
|
color,
|
2023-10-10 01:30:20 +04:00
|
|
|
|
pictureBox.getWidth(),
|
2023-11-18 23:58:18 +04:00
|
|
|
|
pictureBox.getHeight()
|
|
|
|
|
);
|
2023-10-10 01:30:20 +04:00
|
|
|
|
|
|
|
|
|
_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-09-14 20:54:49 +04:00
|
|
|
|
|
2023-10-09 20:56:26 +04:00
|
|
|
|
buttonCreateElectricLocomotive.addActionListener(e -> {
|
|
|
|
|
Random random = new Random();
|
2023-10-21 19:25:05 +04:00
|
|
|
|
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),
|
2023-10-21 19:25:05 +04:00
|
|
|
|
color,
|
|
|
|
|
addColor,
|
2023-09-14 20:54:49 +04:00
|
|
|
|
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));
|
2023-09-14 20:54:49 +04:00
|
|
|
|
Draw();
|
|
|
|
|
});
|
|
|
|
|
|
2023-10-21 19:25:05 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
2023-11-18 23:58:18 +04:00
|
|
|
|
_abstractStrategy.SetData(
|
|
|
|
|
new DrawingObjectLocomotive(_drawingLocomotive), pictureBox.getWidth(),
|
|
|
|
|
pictureBox.getHeight()
|
|
|
|
|
);
|
2023-10-09 20:56:26 +04:00
|
|
|
|
comboBoxStrategy.setEnabled(false);
|
|
|
|
|
}
|
|
|
|
|
if (_abstractStrategy == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_abstractStrategy.MakeStep();
|
|
|
|
|
Draw();
|
|
|
|
|
if (_abstractStrategy.GetStatus() == Status.Finish) {
|
|
|
|
|
comboBoxStrategy.setEnabled(true);
|
|
|
|
|
_abstractStrategy = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2023-09-14 20:54:49 +04:00
|
|
|
|
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);
|
2023-09-14 20:54:49 +04:00
|
|
|
|
}
|
|
|
|
|
case ("buttonDown") -> {
|
2023-10-09 20:56:26 +04:00
|
|
|
|
_drawingLocomotive.MoveTransport(DyrectionType.Down);
|
2023-09-14 20:54:49 +04:00
|
|
|
|
}
|
|
|
|
|
case ("buttonLeft") -> {
|
2023-10-09 20:56:26 +04:00
|
|
|
|
_drawingLocomotive.MoveTransport(DyrectionType.Left);
|
2023-09-14 20:54:49 +04:00
|
|
|
|
}
|
|
|
|
|
case ("buttonRight") -> {
|
2023-10-09 20:56:26 +04:00
|
|
|
|
_drawingLocomotive.MoveTransport(DyrectionType.Right);
|
2023-09-14 20:54:49 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Draw();
|
|
|
|
|
};
|
|
|
|
|
buttonUp.addActionListener(buttonMoveClickedListener);
|
|
|
|
|
buttonDown.addActionListener(buttonMoveClickedListener);
|
|
|
|
|
buttonLeft.addActionListener(buttonMoveClickedListener);
|
|
|
|
|
buttonRight.addActionListener(buttonMoveClickedListener);
|
|
|
|
|
}
|
|
|
|
|
public void Draw() {
|
2023-11-18 23:58:18 +04:00
|
|
|
|
pictureBox.repaint();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createUIComponents(){
|
|
|
|
|
pictureBox = new Canvas();
|
|
|
|
|
pictureBox.setBounds(new Rectangle(400, 300));
|
2023-09-14 20:54:49 +04:00
|
|
|
|
}
|
|
|
|
|
}
|