131 lines
5.7 KiB
Java
131 lines
5.7 KiB
Java
|
package laba1Loco;
|
|||
|
|
|||
|
import java.awt.Graphics;
|
|||
|
import java.awt.Graphics2D;
|
|||
|
import java.awt.event.ActionEvent;
|
|||
|
import java.awt.event.ActionListener;
|
|||
|
|
|||
|
import javax.swing.JButton;
|
|||
|
import javax.swing.JComponent;
|
|||
|
import javax.swing.JFrame;
|
|||
|
import javax.swing.JOptionPane;
|
|||
|
import javax.swing.JTextField;
|
|||
|
|
|||
|
public class FormTrainCollecltion {
|
|||
|
class Canvas extends JComponent{
|
|||
|
public TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains;
|
|||
|
public Canvas(){
|
|||
|
}
|
|||
|
public void paintComponent (Graphics g){
|
|||
|
super.paintComponent(g);
|
|||
|
if (_trains.ShowTrains() != null) {
|
|||
|
g.drawImage(_trains.ShowTrains(), 0, 0, this);
|
|||
|
}
|
|||
|
super.repaint();
|
|||
|
}
|
|||
|
}
|
|||
|
Canvas canv;
|
|||
|
static int pictureBoxWidth = 860;
|
|||
|
static int pictureBoxHeight = 580;
|
|||
|
private TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> _trains;
|
|||
|
public void Draw(){
|
|||
|
canv.repaint();
|
|||
|
}
|
|||
|
FormTrainCollecltion(){
|
|||
|
canv = new Canvas();
|
|||
|
JFrame w = new JFrame ("TrainCollecltion");
|
|||
|
_trains = new TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>(pictureBoxWidth, pictureBoxHeight);
|
|||
|
canv._trains = _trains;
|
|||
|
|
|||
|
JButton ButtonAddTrain = new JButton("ButtonAddTrain");
|
|||
|
ButtonAddTrain.addActionListener(
|
|||
|
new ActionListener() {
|
|||
|
public void actionPerformed(ActionEvent e){
|
|||
|
FormTrain form = new FormTrain();
|
|||
|
form.buttonSelectTrain.addActionListener(
|
|||
|
new ActionListener() {
|
|||
|
public void actionPerformed(ActionEvent e){
|
|||
|
if (_trains.Add(form._drawingTrain) != -1)
|
|||
|
{
|
|||
|
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|||
|
System.out.println("Объект добавлен");
|
|||
|
Draw();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|||
|
System.out.println("Не удалось добавить объект");
|
|||
|
}
|
|||
|
form.w.dispose();
|
|||
|
}
|
|||
|
}
|
|||
|
);
|
|||
|
}
|
|||
|
}
|
|||
|
);
|
|||
|
|
|||
|
JTextField TextBoxNumber = new JTextField();
|
|||
|
JButton ButtonRemoveTrain = new JButton("ButtonRemoveTrain");
|
|||
|
ButtonRemoveTrain.addActionListener(
|
|||
|
new ActionListener() {
|
|||
|
public void actionPerformed(ActionEvent e){
|
|||
|
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
for (char it : TextBoxNumber.getText().toCharArray())
|
|||
|
if (it < '0' || it > '9')
|
|||
|
{
|
|||
|
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|||
|
System.out.println("Не удалось удалить объект");
|
|||
|
return;
|
|||
|
}
|
|||
|
if (TextBoxNumber.getText().length() == 0)
|
|||
|
{
|
|||
|
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|||
|
System.out.println("Не удалось удалить объект");
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
int pos = Integer.parseInt(TextBoxNumber.getText());
|
|||
|
if (_trains.remove(pos) != null)
|
|||
|
{
|
|||
|
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|||
|
System.out.println("Объект удален");
|
|||
|
Draw();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|||
|
System.out.println("Не удалось удалить объект");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
);
|
|||
|
|
|||
|
JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection");
|
|||
|
ButtonRefreshCollection.addActionListener(
|
|||
|
new ActionListener() {
|
|||
|
public void actionPerformed(ActionEvent e){
|
|||
|
Draw();
|
|||
|
}
|
|||
|
}
|
|||
|
);
|
|||
|
|
|||
|
w.setSize (1000, 600);
|
|||
|
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
|
|||
|
w.setLayout(null);
|
|||
|
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
|
|||
|
ButtonAddTrain.setBounds(pictureBoxWidth, 0, 120, 20);
|
|||
|
TextBoxNumber.setBounds(pictureBoxWidth, 30, 120, 20);
|
|||
|
ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 120, 20);
|
|||
|
ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 120, 20);
|
|||
|
w.add(canv);
|
|||
|
w.add(ButtonAddTrain);
|
|||
|
w.add(ButtonRemoveTrain);
|
|||
|
w.add(ButtonRefreshCollection);
|
|||
|
w.add(TextBoxNumber);
|
|||
|
w.setVisible(true);
|
|||
|
}
|
|||
|
}
|