269 lines
10 KiB
Java
269 lines
10 KiB
Java
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{
|
||
private DrawingBus _drawingBus;
|
||
private AbstractStrategy _abstractStrategy;
|
||
private JFrame frameTrolleybus;
|
||
private JPanel panelTrolleybus;
|
||
private JButton buttonCreate, buttonCreateTrolleybus, buttonUp, buttonDown, buttonRight, buttonLeft, buttonStep;
|
||
public JButton buttonSelect;
|
||
private JComboBox comboBoxStrategy;
|
||
|
||
public FormTrolleybus(){
|
||
//Само окно
|
||
frameTrolleybus = new JFrame();
|
||
frameTrolleybus.setLayout(new BorderLayout());
|
||
frameTrolleybus.setSize(900, 500);
|
||
frameTrolleybus.setTitle("Троллейбус");
|
||
frameTrolleybus.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||
|
||
//Панель, на которую добавляю всё
|
||
panelTrolleybus = new JPanel();
|
||
panelTrolleybus.setLayout(null);
|
||
|
||
//ComboBox
|
||
String[] strings = {"В центр", "В край"};
|
||
comboBoxStrategy = new JComboBox(strings);
|
||
comboBoxStrategy.setBounds(750, 20, 90, 30);
|
||
|
||
//Кнопка создания автобуса
|
||
buttonCreate = new JButton("Создать автобус");
|
||
buttonCreate.setToolTipText("buttonCreate");
|
||
//Кнопка создания троллейбуса
|
||
buttonCreateTrolleybus = new JButton("Создать троллейбус");
|
||
buttonCreateTrolleybus.setToolTipText("buttonCreateTrolleybus");
|
||
//Кнопка вверх
|
||
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");
|
||
//Кнопка шага
|
||
buttonStep = new JButton("Шаг");
|
||
//Кнопка выбора созданного автобуса/троллейбуса
|
||
buttonSelect = new JButton("Выбрать");
|
||
|
||
//Размеры, позиция кнопок
|
||
buttonCreate.setBounds(10,400,150,30);
|
||
buttonCreateTrolleybus.setBounds(170, 400, 150, 30);
|
||
buttonUp.setBounds(800,380,30,30);
|
||
buttonDown.setBounds(800,420,30,30);
|
||
buttonLeft.setBounds(760,420,30,30);
|
||
buttonRight.setBounds(840,420,30,30);
|
||
buttonStep.setBounds(750, 70, 90, 30);
|
||
buttonSelect.setBounds(350, 400, 90, 30);
|
||
|
||
//Добавление листенеров к кнопкам
|
||
buttonCreate.addActionListener(new ActionListener() {
|
||
public void actionPerformed(ActionEvent e) {
|
||
ButtonCreate_Click(e);
|
||
}
|
||
});
|
||
|
||
buttonCreateTrolleybus.addActionListener(new ActionListener() {
|
||
public void actionPerformed(ActionEvent e) {
|
||
ButtonCreate_Click(e);
|
||
}
|
||
});
|
||
|
||
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);
|
||
}
|
||
});
|
||
|
||
buttonStep.addActionListener(new ActionListener() {
|
||
public void actionPerformed(ActionEvent e) {
|
||
ButtonStep_Click(buttonStep, e);
|
||
}
|
||
});
|
||
|
||
//Листенер для кнопки выбора автобуса создаётся при вызове этой формы в новой форме
|
||
|
||
panelTrolleybus.add(buttonCreate);
|
||
panelTrolleybus.add(buttonCreateTrolleybus);
|
||
panelTrolleybus.add(buttonUp);
|
||
panelTrolleybus.add(buttonDown);
|
||
panelTrolleybus.add(buttonLeft);
|
||
panelTrolleybus.add(buttonRight);
|
||
panelTrolleybus.add(buttonStep);
|
||
panelTrolleybus.add(comboBoxStrategy);
|
||
panelTrolleybus.add(buttonSelect);
|
||
frameTrolleybus.add(panelTrolleybus, BorderLayout.CENTER);
|
||
|
||
frameTrolleybus.setVisible(true);
|
||
}
|
||
// Метод прорисовки троллейбуса
|
||
private void Draw(){
|
||
if (_drawingBus == null) {
|
||
return;
|
||
}
|
||
Graphics g = panelTrolleybus.getGraphics();
|
||
// Очистка перед перерисовкой
|
||
panelTrolleybus.paint(g);
|
||
_drawingBus.DrawTransport(g);
|
||
}
|
||
private void ButtonCreate_Click(ActionEvent e) {
|
||
Random random = new Random();
|
||
JButton info = (JButton)e.getSource();
|
||
String name = info.getToolTipText();
|
||
Random rand = new Random();
|
||
//Для диалогов выбора цвета
|
||
Color color;
|
||
JColorChooser colorChooser;
|
||
int result;
|
||
switch (name) {
|
||
case "buttonCreate":
|
||
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();
|
||
}
|
||
_drawingBus = new DrawingBus(random.nextInt(100, 300),
|
||
random.nextInt(1000, 3000),
|
||
color,
|
||
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
||
break;
|
||
case "buttonCreateTrolleybus":
|
||
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();
|
||
}
|
||
|
||
_drawingBus = new DrawingTrolleybus(random.nextInt(100, 300),
|
||
random.nextInt(1000, 3000),
|
||
color,
|
||
additionalColor,
|
||
random.nextBoolean(),
|
||
random.nextBoolean(),
|
||
panelTrolleybus.getWidth(), panelTrolleybus.getHeight());
|
||
break;
|
||
}
|
||
_drawingBus.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100));
|
||
Draw();
|
||
}
|
||
protected void ButtonMove_Click(Object sender, ActionEvent e) {
|
||
if (_drawingBus == null)
|
||
{
|
||
return;
|
||
}
|
||
JButton info = (JButton)e.getSource();
|
||
String name = info.getToolTipText();
|
||
switch (name)
|
||
{
|
||
case "buttonUp":
|
||
_drawingBus.MoveTransport(DirectionType.Up);
|
||
break;
|
||
case "buttonDown":
|
||
_drawingBus.MoveTransport(DirectionType.Down);
|
||
break;
|
||
case "buttonLeft":
|
||
_drawingBus.MoveTransport(DirectionType.Left);
|
||
break;
|
||
case "buttonRight":
|
||
_drawingBus.MoveTransport(DirectionType.Right);
|
||
break;
|
||
}
|
||
Draw();
|
||
}
|
||
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;
|
||
}
|
||
}
|
||
|
||
public DrawingBus getBus() {
|
||
return _drawingBus;
|
||
}
|
||
public Frame getFrame() {
|
||
return frameTrolleybus;
|
||
}
|
||
// Метод для 4 усложн., когда отображается автобус из очереди
|
||
public void ShowBus(DrawingBus bus) {
|
||
_drawingBus = bus;
|
||
_drawingBus.SetPosition(100,100);
|
||
Draw();
|
||
}
|
||
} |