167 lines
7.2 KiB
Java
167 lines
7.2 KiB
Java
package DoubleDeckerBus;
|
|
|
|
import DoubleDeckerBus.DrawningObjects.DrawningDoubleDeckerBus;
|
|
import DoubleDeckerBus.DrawningObjects.DrawningBus;
|
|
import DoubleDeckerBus.MovementStrategy.*;
|
|
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.Random;
|
|
import javax.imageio.ImageIO;
|
|
import javax.swing.*;
|
|
|
|
public class FormDoubleDeckerBus {
|
|
static DrawningBus DrawningBus;
|
|
static AbstractStrategy _abstractStrategy;
|
|
public static void main(String[] args) throws IOException {
|
|
String[] items = {"Довести до центра", "Довести до края"};
|
|
|
|
JComboBox comboBoxStrategy = new JComboBox(items);
|
|
comboBoxStrategy.setBounds(562,12,151,28);
|
|
JFrame BusFrame = new JFrame();
|
|
BusFrame.setResizable(false);
|
|
JPanel BusPanel = new JPanel();
|
|
BusFrame.setLayout(new BorderLayout());
|
|
BusFrame.setSize(743, 576);
|
|
BusFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
BusFrame.setLayout(new BorderLayout(1,1));
|
|
BusPanel.setLayout(null);
|
|
BufferedImage RightIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/right.png"));
|
|
BufferedImage LeftIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/left.png"));
|
|
BufferedImage UpIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/up.png"));
|
|
BufferedImage DownIcon = ImageIO.read(new File("C:/Users/User/IdeaProjects/PIbd-21_Zhirnova_A_E_DoubleDeckerBus._Hard/Resources/down.png"));
|
|
|
|
JButton RightButton = new JButton(new ImageIcon(RightIcon));
|
|
JButton LeftButton = new JButton(new ImageIcon(LeftIcon));
|
|
JButton UpButton = new JButton(new ImageIcon(UpIcon));
|
|
JButton DownButton = new JButton(new ImageIcon(DownIcon));
|
|
JButton CreateButton = new JButton();
|
|
JButton CreateDoubleDeckerBusButton = new JButton();
|
|
JButton buttonStep = new JButton();
|
|
|
|
CreateDoubleDeckerBusButton.setBounds(198,477,180, 40);
|
|
CreateDoubleDeckerBusButton.setText("Создать двухэтажный автобус");
|
|
CreateButton.setText("Создать");
|
|
buttonStep.setBounds(619, 46, 94, 29);
|
|
buttonStep.setText("шаг");
|
|
CreateButton.setBounds(12, 477, 180, 40);
|
|
RightButton.setBounds(683,487,30,30);
|
|
LeftButton.setBounds(611,487,30,30);
|
|
UpButton.setBounds(647,451,30,30);
|
|
DownButton.setBounds(647,487,30,30);
|
|
|
|
BusPanel.add(CreateButton);
|
|
BusPanel.add(CreateDoubleDeckerBusButton);
|
|
BusPanel.add(RightButton);
|
|
BusPanel.add(LeftButton);
|
|
BusPanel.add(UpButton);
|
|
BusPanel.add(DownButton);
|
|
BusPanel.add(comboBoxStrategy);
|
|
BusPanel.add(buttonStep);
|
|
comboBoxStrategy.setSelectedIndex(-1);
|
|
|
|
BusFrame.add(BusPanel, BorderLayout.CENTER);
|
|
Random random = new Random();
|
|
CreateButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
DrawningBus = new DrawningBus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
|
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
|
BusPanel.getWidth(), BusPanel.getHeight(), BusPanel);
|
|
DrawningBus.DrawTransport();
|
|
comboBoxStrategy.enable(true);
|
|
}
|
|
});
|
|
|
|
CreateDoubleDeckerBusButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
DrawningBus = new DrawningDoubleDeckerBus(random.nextInt(100, 300), random.nextDouble(1000, 3000),
|
|
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
|
Color.getHSBColor(random.nextInt(0, 301), random.nextInt(0, 301), random.nextInt(0, 301)),
|
|
random.nextInt(1, 6), BusPanel.getWidth(), BusPanel.getHeight(), random.nextBoolean(), random.nextBoolean(),
|
|
BusPanel);
|
|
DrawningBus.DrawTransport();
|
|
comboBoxStrategy.enable(true);
|
|
}
|
|
});
|
|
|
|
|
|
buttonStep.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if (DrawningBus == 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 DrawningObjectBus(DrawningBus), BusPanel.getWidth(),
|
|
BusPanel.getHeight());
|
|
comboBoxStrategy.enable(false);
|
|
}
|
|
if (_abstractStrategy == null) {
|
|
return;
|
|
}
|
|
_abstractStrategy.MakeStep();
|
|
DrawningBus.DrawTransport();
|
|
if (_abstractStrategy.GetStatus() == Status.Finish) {
|
|
comboBoxStrategy.enable(true);
|
|
_abstractStrategy = null;
|
|
}
|
|
}
|
|
});
|
|
RightButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if(DrawningBus.EntityBus() == null) {
|
|
return;
|
|
}
|
|
DrawningBus.MoveTransport(DirectionType.Right);
|
|
DrawningBus.DrawTransport();
|
|
}
|
|
});
|
|
LeftButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if(DrawningBus.EntityBus() == null)
|
|
return;
|
|
DrawningBus.MoveTransport(DirectionType.Left);
|
|
DrawningBus.DrawTransport();
|
|
}
|
|
});
|
|
UpButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if(DrawningBus.EntityBus() == null)
|
|
return;
|
|
DrawningBus.MoveTransport(DirectionType.Up);
|
|
DrawningBus.DrawTransport();
|
|
}
|
|
});
|
|
DownButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
if(DrawningBus.EntityBus() == null)
|
|
return;
|
|
DrawningBus.MoveTransport(DirectionType.Down);
|
|
DrawningBus.DrawTransport();
|
|
}
|
|
});
|
|
|
|
BusFrame.setVisible(true);
|
|
}
|
|
} |