163 lines
6.8 KiB
Java
163 lines
6.8 KiB
Java
import javax.swing.*;
|
||
import java.awt.*;
|
||
import java.awt.event.ActionListener;
|
||
import java.util.Random;
|
||
public class PictureBoxBulldozer extends JPanel {
|
||
private DrawningBulldozer drawningBulldozer;
|
||
private AbstractStrategy abstractStrategy;
|
||
private JButton buttonLeft;
|
||
private JButton buttonUp;
|
||
private JButton buttonRight;
|
||
private JButton buttonDown;
|
||
private JButton buttonCreateFastBulldozer;
|
||
private JButton buttonCreateBulldozer;
|
||
private JComboBox comboBoxStrategy;
|
||
private JButton buttonStep;
|
||
public PictureBoxBulldozer() {
|
||
setLayout(null);
|
||
setBounds(0, 0, 800, 450);
|
||
buttonCreateBulldozer = new JButton("Создать трактор");
|
||
buttonCreateBulldozer.setFocusable(false);
|
||
buttonCreateBulldozer.setBounds(12, 415, 150, 30);
|
||
add(buttonCreateBulldozer);
|
||
buttonCreateBulldozer.addActionListener(e -> {
|
||
Random random = new Random();
|
||
drawningBulldozer = new DrawningBulldozer((random.nextInt(200, 300)),
|
||
random.nextInt(1000, 3000),
|
||
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
|
||
random.nextInt(0, 256)),
|
||
random.nextBoolean(), random.nextBoolean(),
|
||
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)),
|
||
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
||
drawningBulldozer.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||
repaint();
|
||
});
|
||
buttonCreateFastBulldozer = new JButton("Создать трактор с ковшами");
|
||
buttonCreateFastBulldozer.setFocusable(false);
|
||
buttonCreateFastBulldozer.setBounds(180, 415, 200, 30);
|
||
add(buttonCreateFastBulldozer);
|
||
buttonCreateFastBulldozer.addActionListener(e -> {
|
||
Random random = new Random();
|
||
drawningBulldozer = new DrawningFastBulldozer((random.nextInt(200, 300)),
|
||
random.nextInt(1000, 3000),
|
||
new Color(random.nextInt(0, 256), random.nextInt(0, 256),
|
||
random.nextInt(0, 256)),
|
||
random.nextBoolean(), random.nextBoolean(),
|
||
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)),
|
||
this.getWidth(), this.getHeight(), random.nextInt(2, 5));
|
||
drawningBulldozer.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||
repaint();
|
||
});
|
||
ActionListener buttonMoveListener = e -> {
|
||
if (drawningBulldozer == null)
|
||
{
|
||
return;
|
||
}
|
||
String buttonName = ((JButton) e.getSource()).getName();
|
||
|
||
switch (buttonName) {
|
||
case ("buttonUp"):
|
||
drawningBulldozer.MoveTransport(DirectionBulldozer.Up);
|
||
break;
|
||
case ("buttonDown"):
|
||
drawningBulldozer.MoveTransport(DirectionBulldozer.Down);
|
||
break;
|
||
case ("buttonLeft"):
|
||
drawningBulldozer.MoveTransport(DirectionBulldozer.Left);
|
||
break;
|
||
case ("buttonRight"):
|
||
drawningBulldozer.MoveTransport(DirectionBulldozer.Right);
|
||
break;
|
||
}
|
||
repaint();
|
||
};
|
||
buttonLeft = new JButton();
|
||
buttonLeft.setName("buttonLeft");
|
||
buttonLeft.setFocusable(false);
|
||
buttonLeft.setPreferredSize(new Dimension(30, 30));
|
||
buttonLeft.setIcon(new ImageIcon("Resources/left.png"));
|
||
buttonLeft.addActionListener(buttonMoveListener);
|
||
buttonLeft.setBounds(686, 408, 30, 30);
|
||
add(buttonLeft);
|
||
buttonRight = new JButton();
|
||
buttonRight.setName("buttonRight");
|
||
buttonRight.setFocusable(false);
|
||
buttonRight.setPreferredSize(new Dimension(30, 30));
|
||
buttonRight.setIcon(new ImageIcon("Resources/right.png"));
|
||
buttonRight.addActionListener(buttonMoveListener);
|
||
buttonRight.setBounds(758, 408, 30, 30);
|
||
add(buttonRight);
|
||
buttonDown = new JButton();
|
||
buttonDown.setName("buttonDown");
|
||
buttonDown.setFocusable(false);
|
||
buttonDown.setPreferredSize(new Dimension(30, 30));
|
||
buttonDown.setIcon(new ImageIcon("Resources/down.png"));
|
||
buttonDown.addActionListener(buttonMoveListener);
|
||
buttonDown.setBounds(722, 408, 30, 30);
|
||
add(buttonDown);
|
||
buttonUp = new JButton();
|
||
buttonUp.setName("buttonUp");
|
||
buttonUp.setFocusable(false);
|
||
buttonUp.setPreferredSize(new Dimension(30, 30));
|
||
buttonUp.setIcon(new ImageIcon("Resources/up.png"));
|
||
buttonUp.addActionListener(buttonMoveListener);
|
||
buttonUp.setBounds(722, 372, 30, 30);
|
||
add(buttonUp);
|
||
String[] items = {
|
||
"0",
|
||
"1"
|
||
};
|
||
comboBoxStrategy = new JComboBox(items);
|
||
comboBoxStrategy.setBounds(667, 10, 120, 25);
|
||
buttonStep = new JButton("Шаг");
|
||
buttonStep.setFocusable(false);
|
||
buttonStep.setBounds(710, 40, 75, 30);
|
||
buttonStep.addActionListener(e -> {
|
||
if (drawningBulldozer == null) {
|
||
return;
|
||
}
|
||
if (comboBoxStrategy.isEnabled()) {
|
||
abstractStrategy = switch (comboBoxStrategy.getSelectedIndex()) {
|
||
case 0 -> new MoveToCenter();
|
||
case 1 -> new MoveToBorder();
|
||
default -> null;
|
||
};
|
||
if (abstractStrategy == null)
|
||
{
|
||
return;
|
||
}
|
||
abstractStrategy.setData(new DrawningObjectBulldozer(drawningBulldozer), this.getWidth(), this.getHeight());
|
||
}
|
||
if (abstractStrategy == null)
|
||
{
|
||
return;
|
||
}
|
||
comboBoxStrategy.setEnabled(false);
|
||
abstractStrategy.makeStep();
|
||
repaint();
|
||
if (abstractStrategy.getStatus() == Status.Finish)
|
||
{
|
||
comboBoxStrategy.setEnabled(true);
|
||
abstractStrategy = null;
|
||
}
|
||
});
|
||
add(comboBoxStrategy);
|
||
add(buttonStep);
|
||
setPreferredSize(new Dimension(800, 450));
|
||
}
|
||
@Override
|
||
protected void paintComponent(Graphics g) {
|
||
if (drawningBulldozer == null) {
|
||
return;
|
||
}
|
||
super.paintComponent(g);
|
||
Graphics2D g2d = (Graphics2D) g;
|
||
drawningBulldozer.DrawTransport(g2d);
|
||
}
|
||
} |