110 lines
4.1 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 DoubleDeckerBus;
import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.Generics.TheBusesCollectionGeneric;
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FormBusCollection {
private final TheBusesCollectionGeneric<DrawningBus, DrawningObjectBus> _bus;
private int pictureBoxWidth = 605;
private int pictureBoxHeight = 426;
CollectionCanvas canvas;
public void Draw() {
if (canvas == null) {
return;
}
canvas.repaint();
}
public FormBusCollection() {
_bus = new TheBusesCollectionGeneric<>(pictureBoxWidth, pictureBoxHeight);
canvas = new CollectionCanvas();
JPanel toolBox = new JPanel();
JFrame collectionFrame = new JFrame();
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
collectionFrame.setSize(880,497);
toolBox.setBounds(623, 12, 227, 426);
canvas.setBounds(12,12,pictureBoxWidth,pictureBoxHeight);
JButton addButton = new JButton("Добавить");
JButton removeButton = new JButton("Удалить");
JButton refreshButton = new JButton("Обновить");
JTextField busNumb = new JTextField();
GridLayout lay = new GridLayout(4,1);
toolBox.setLayout(lay);
toolBox.add(addButton);
toolBox.add(busNumb);
toolBox.add(removeButton);
toolBox.add(refreshButton);
collectionFrame.add(toolBox);
collectionFrame.add(canvas);
collectionFrame.setVisible(true);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_bus == null) {
return;
}
FormDoubleDeckerBus form = new FormDoubleDeckerBus();
form.buttonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_bus.Insert(form.SelectedBus()) != -1)
{
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
form.SelectedBus()._pictureWidth = pictureBoxWidth;
form.SelectedBus()._pictureHeight = pictureBoxHeight;
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
canvas._bus = _bus;
form.BusFrame.dispose();
Draw();
}
});
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_bus == null) {
return;
}
String tmp = busNumb.getText();
int numb;
try {
numb = Integer.parseInt(tmp);
} catch (Exception ex){
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_bus.Remove(numb);
_bus.ShowTheBuses();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
});
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_bus == null) {
return;
}
_bus.ShowTheBuses();
Draw();
}
});
}
}