2023-11-11 20:48:58 +03:00
|
|
|
|
package Trolleybus;
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
public class FormTrolleybus{
|
2023-11-13 21:25:33 +03:00
|
|
|
|
private DrawingBus _drawingBus;
|
|
|
|
|
private AbstractStrategy _abstractStrategy;
|
2023-11-11 20:48:58 +03:00
|
|
|
|
private JFrame frameTrolleybus;
|
|
|
|
|
private JPanel panelTrolleybus;
|
2023-11-13 21:25:33 +03:00
|
|
|
|
private JButton buttonCreate, buttonCreateTrolleybus, buttonUp, buttonDown, buttonRight, buttonLeft, buttonStep;
|
|
|
|
|
private JComboBox comboBoxStrategy;
|
2023-11-11 20:48:58 +03:00
|
|
|
|
|
|
|
|
|
public FormTrolleybus(){
|
|
|
|
|
//Само окно
|
|
|
|
|
frameTrolleybus = new JFrame();
|
|
|
|
|
frameTrolleybus.setLayout(new BorderLayout());
|
|
|
|
|
frameTrolleybus.setSize(900, 500);
|
|
|
|
|
frameTrolleybus.setTitle("Троллейбус");
|
|
|
|
|
frameTrolleybus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
|
|
|
|
|
//Панель, на которую добавляю всё
|
|
|
|
|
panelTrolleybus = new JPanel();
|
|
|
|
|
panelTrolleybus.setLayout(null);
|
|
|
|
|
|
2023-11-13 21:25:33 +03:00
|
|
|
|
//ComboBox
|
|
|
|
|
String[] strings = {"В центр", "В край"};
|
|
|
|
|
comboBoxStrategy = new JComboBox(strings);
|
|
|
|
|
comboBoxStrategy.setBounds(750, 20, 90, 30);
|
|
|
|
|
|
|
|
|
|
//Кнопка создания автобуса
|
2023-11-11 20:48:58 +03:00
|
|
|
|
buttonCreate = new JButton("Создать");
|
2023-11-13 21:25:33 +03:00
|
|
|
|
buttonCreate.setToolTipText("buttonCreate");
|
|
|
|
|
//Кнопка создания троллейбуса
|
|
|
|
|
buttonCreateTrolleybus = new JButton("Создать троллейбус");
|
|
|
|
|
buttonCreateTrolleybus.setToolTipText("buttonCreateTrolleybus");
|
2023-11-11 20:48:58 +03:00
|
|
|
|
//Кнопка вверх
|
|
|
|
|
buttonUp = new JButton();
|
|
|
|
|
buttonUp.setIcon(new ImageIcon("Up.png"));
|
|
|
|
|
buttonUp.setToolTipText("buttonUp");
|
|
|
|
|
//Кнопка вниз
|
|
|
|
|
buttonDown = new JButton();
|
|
|
|
|
buttonDown.setIcon(new ImageIcon("Down.png"));
|
|
|
|
|
buttonDown.setToolTipText("buttonDown");
|
|
|
|
|
//Кнопка вправо
|
|
|
|
|
buttonRight = new JButton();
|
|
|
|
|
buttonRight.setIcon(new ImageIcon("Right.png"));
|
|
|
|
|
buttonRight.setToolTipText("buttonRight");
|
|
|
|
|
//Кнопка влево
|
|
|
|
|
buttonLeft = new JButton();
|
|
|
|
|
buttonLeft.setIcon(new ImageIcon("Left.png"));
|
|
|
|
|
buttonLeft.setToolTipText("buttonLeft");
|
2023-11-13 21:25:33 +03:00
|
|
|
|
//Кнопка шага
|
|
|
|
|
buttonStep = new JButton("Шаг");
|
2023-11-11 20:48:58 +03:00
|
|
|
|
//Размеры, позиция кнопок
|
2023-11-13 21:25:33 +03:00
|
|
|
|
buttonCreate.setBounds(10,400,150,30);
|
|
|
|
|
buttonCreateTrolleybus.setBounds(170, 400, 150, 30);
|
2023-11-11 20:48:58 +03:00
|
|
|
|
buttonUp.setBounds(800,380,30,30);
|
|
|
|
|
buttonDown.setBounds(800,420,30,30);
|
|
|
|
|
buttonLeft.setBounds(760,420,30,30);
|
|
|
|
|
buttonRight.setBounds(840,420,30,30);
|
2023-11-13 21:25:33 +03:00
|
|
|
|
buttonStep.setBounds(750, 70, 90, 30);
|
2023-11-11 20:48:58 +03:00
|
|
|
|
|
|
|
|
|
//Добавление листенеров к кнопкам
|
|
|
|
|
buttonCreate.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
ButtonCreate_Click(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-13 21:25:33 +03:00
|
|
|
|
buttonCreateTrolleybus.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
ButtonCreate_Click(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-11 20:48:58 +03:00
|
|
|
|
buttonUp.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
ButtonMove_Click(buttonUp, e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
buttonDown.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
ButtonMove_Click(buttonDown, e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
buttonRight.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
ButtonMove_Click(buttonRight, e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
buttonLeft.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
ButtonMove_Click(buttonLeft, e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-13 21:25:33 +03:00
|
|
|
|
buttonStep.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
ButtonStep_Click(buttonStep, e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-11 20:48:58 +03:00
|
|
|
|
panelTrolleybus.add(buttonCreate);
|
2023-11-13 21:25:33 +03:00
|
|
|
|
panelTrolleybus.add(buttonCreateTrolleybus);
|
2023-11-11 20:48:58 +03:00
|
|
|
|
panelTrolleybus.add(buttonUp);
|
|
|
|
|
panelTrolleybus.add(buttonDown);
|
|
|
|
|
panelTrolleybus.add(buttonLeft);
|
|
|
|
|
panelTrolleybus.add(buttonRight);
|
2023-11-13 21:25:33 +03:00
|
|
|
|
panelTrolleybus.add(buttonStep);
|
|
|
|
|
panelTrolleybus.add(comboBoxStrategy);
|
2023-11-11 20:48:58 +03:00
|
|
|
|
frameTrolleybus.add(panelTrolleybus, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
|
|
frameTrolleybus.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
// Метод прорисовки троллейбуса
|
|
|
|
|
private void Draw(){
|
2023-11-13 21:25:33 +03:00
|
|
|
|
if (_drawingBus == null) {
|
2023-11-11 20:48:58 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Graphics g = panelTrolleybus.getGraphics();
|
|
|
|
|
// Очистка перед перерисовкой
|
|
|
|
|
panelTrolleybus.paint(g);
|
2023-11-13 21:25:33 +03:00
|
|
|
|
_drawingBus.DrawTransport(g);
|
2023-11-11 20:48:58 +03:00
|
|
|
|
}
|
|
|
|
|
private void ButtonCreate_Click(ActionEvent e) {
|
|
|
|
|
Random random = new Random();
|
2023-11-13 21:25:33 +03:00
|
|
|
|
JButton info = (JButton)e.getSource();
|
|
|
|
|
String name = info.getToolTipText();
|
|
|
|
|
switch (name) {
|
|
|
|
|
case "buttonCreate":
|
|
|
|
|
_drawingBus = new DrawingBus(random.nextInt(100, 300),
|
|
|
|
|
random.nextInt(1000, 3000),
|
|
|
|
|
new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256)),
|
|
|
|
|
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
|
|
|
|
break;
|
|
|
|
|
case "buttonCreateTrolleybus":
|
|
|
|
|
_drawingBus = new DrawingTrolleybus(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(),
|
|
|
|
|
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
_drawingBus.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
2023-11-11 20:48:58 +03:00
|
|
|
|
Draw();
|
|
|
|
|
}
|
|
|
|
|
protected void ButtonMove_Click(Object sender, ActionEvent e) {
|
2023-11-13 21:25:33 +03:00
|
|
|
|
if (_drawingBus == null)
|
2023-11-11 20:48:58 +03:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
JButton info = (JButton)e.getSource();
|
|
|
|
|
String name = info.getToolTipText();
|
|
|
|
|
switch (name)
|
|
|
|
|
{
|
|
|
|
|
case "buttonUp":
|
2023-11-13 21:25:33 +03:00
|
|
|
|
_drawingBus.MoveTransport(DirectionType.Up);
|
2023-11-11 20:48:58 +03:00
|
|
|
|
break;
|
|
|
|
|
case "buttonDown":
|
2023-11-13 21:25:33 +03:00
|
|
|
|
_drawingBus.MoveTransport(DirectionType.Down);
|
2023-11-11 20:48:58 +03:00
|
|
|
|
break;
|
|
|
|
|
case "buttonLeft":
|
2023-11-13 21:25:33 +03:00
|
|
|
|
_drawingBus.MoveTransport(DirectionType.Left);
|
2023-11-11 20:48:58 +03:00
|
|
|
|
break;
|
|
|
|
|
case "buttonRight":
|
2023-11-13 21:25:33 +03:00
|
|
|
|
_drawingBus.MoveTransport(DirectionType.Right);
|
2023-11-11 20:48:58 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Draw();
|
|
|
|
|
}
|
2023-11-13 21:25:33 +03:00
|
|
|
|
private void ButtonStep_Click(Object sender, ActionEvent e)
|
|
|
|
|
{
|
|
|
|
|
if (_drawingBus == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (comboBoxStrategy.isEnabled())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (comboBoxStrategy.getSelectedIndex() == 0) {
|
|
|
|
|
_abstractStrategy = new MoveToCenter();
|
|
|
|
|
}
|
|
|
|
|
else if (comboBoxStrategy.getSelectedIndex() == 1) {
|
|
|
|
|
_abstractStrategy = new MoveToBorder();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
_abstractStrategy = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_abstractStrategy == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_abstractStrategy.SetData(new DrawingObjectBus(_drawingBus), panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
|
|
|
|
comboBoxStrategy.setEnabled(false);
|
|
|
|
|
}
|
|
|
|
|
if (_abstractStrategy == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_abstractStrategy.MakeStep();
|
|
|
|
|
Draw();
|
|
|
|
|
if (_abstractStrategy.GetStatus() == Status.Finish)
|
|
|
|
|
{
|
|
|
|
|
comboBoxStrategy.setEnabled(true);
|
|
|
|
|
_abstractStrategy = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-11 20:48:58 +03:00
|
|
|
|
}
|