172 lines
5.9 KiB
Java
172 lines
5.9 KiB
Java
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 extends JDialog{
|
||
public DrawingLocomotive _drawingLocomotive;
|
||
AbstractStrategy _abstractStrategy;
|
||
private JButton buttonCreateElectricLocomotive;
|
||
public JComponent 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 JComponent getPictureBox()
|
||
{
|
||
return pictureBox;
|
||
}
|
||
|
||
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();
|
||
}
|
||
}
|
||
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;
|
||
});
|
||
|
||
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() {
|
||
pictureBox.repaint();
|
||
}
|
||
|
||
private void createUIComponents(){
|
||
pictureBox = new Canvas();
|
||
pictureBox.setBounds(new Rectangle(400, 300));
|
||
}
|
||
}
|