269 lines
10 KiB
Java
Raw Permalink Normal View History

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;
2023-11-24 20:37:08 +03:00
public JButton buttonSelect;
2023-11-13 21:25:33 +03:00
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("Троллейбус");
2023-12-01 19:42:28 +03:00
frameTrolleybus.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
2023-11-11 20:48:58 +03:00
//Панель, на которую добавляю всё
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-24 20:37:08 +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-24 20:37:08 +03:00
//Кнопка выбора созданного автобуса/троллейбуса
buttonSelect = 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-24 20:37:08 +03:00
buttonSelect.setBounds(350, 400, 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-24 20:37:08 +03:00
//Листенер для кнопки выбора автобуса создаётся при вызове этой формы в новой форме
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-24 20:37:08 +03:00
panelTrolleybus.add(buttonSelect);
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();
2023-11-24 20:37:08 +03:00
Random rand = new Random();
//Для диалогов выбора цвета
Color color;
JColorChooser colorChooser;
int result;
2023-11-13 21:25:33 +03:00
switch (name) {
case "buttonCreate":
2023-11-24 20:37:08 +03:00
color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
// Диалог выбора цвета
colorChooser = new JColorChooser(color);
result = JOptionPane.showConfirmDialog(null, colorChooser, "Выберите цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
color = colorChooser.getColor();
}
2023-11-13 21:25:33 +03:00
_drawingBus = new DrawingBus(random.nextInt(100, 300),
random.nextInt(1000, 3000),
2023-11-24 20:37:08 +03:00
color,
2023-11-13 21:25:33 +03:00
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
break;
case "buttonCreateTrolleybus":
2023-11-24 20:37:08 +03:00
color = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
Color additionalColor = new Color(rand.nextInt(0, 256), rand.nextInt(0, 256), rand.nextInt(0, 256));
// Диалог выбора основного цвета
colorChooser = new JColorChooser(color);
result = JOptionPane.showConfirmDialog(null, colorChooser, "Выберите основной цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
color = colorChooser.getColor();
}
// Диалог выбора дополнительного цвета
colorChooser = new JColorChooser(additionalColor);
result = JOptionPane.showConfirmDialog(null, colorChooser, "Выберите дополнительный цвет", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
additionalColor = colorChooser.getColor();
}
2023-11-13 21:25:33 +03:00
_drawingBus = new DrawingTrolleybus(random.nextInt(100, 300),
random.nextInt(1000, 3000),
2023-11-24 20:37:08 +03:00
color,
additionalColor,
2023-11-13 21:25:33 +03:00
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-24 20:37:08 +03:00
public DrawingBus getBus() {
return _drawingBus;
}
public Frame getFrame() {
return frameTrolleybus;
}
2023-12-01 19:42:28 +03:00
// Метод для 4 усложн., когда отображается автобус из очереди
public void ShowBus(DrawingBus bus) {
_drawingBus = bus;
_drawingBus.SetPosition(100,100);
Draw();
}
2023-11-11 20:48:58 +03:00
}