2023-10-23 18:05:55 +04:00
|
|
|
|
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 {
|
2023-10-23 21:52:50 +04:00
|
|
|
|
private class Canvas extends JComponent{
|
2023-10-23 18:05:55 +04:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2023-10-23 21:52:50 +04:00
|
|
|
|
JButton toForm4GenericDopClass = new JButton("ToForm4GenericDopClass");
|
|
|
|
|
toForm4GenericDopClass.addActionListener(
|
|
|
|
|
new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e){
|
|
|
|
|
Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2023-10-23 18:05:55 +04:00
|
|
|
|
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);
|
2023-10-23 21:52:50 +04:00
|
|
|
|
toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 120, 20);
|
2023-10-23 18:05:55 +04:00
|
|
|
|
w.add(canv);
|
|
|
|
|
w.add(ButtonAddTrain);
|
|
|
|
|
w.add(ButtonRemoveTrain);
|
|
|
|
|
w.add(ButtonRefreshCollection);
|
|
|
|
|
w.add(TextBoxNumber);
|
2023-10-23 21:52:50 +04:00
|
|
|
|
w.add(toForm4GenericDopClass);
|
2023-10-23 18:05:55 +04:00
|
|
|
|
w.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
}
|