2023-11-13 21:25:33 +03:00

219 lines
7.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
private JComboBox comboBoxStrategy;
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);
//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("Шаг");
//Размеры, позиция кнопок
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);
//Добавление листенеров к кнопкам
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);
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();
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));
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;
}
}
}