PIbd-21_Kouvshinoff_T._A._W.../laba1Loco/FormTrainCollecltion.java
2023-10-23 21:52:50 +04:00

142 lines
6.2 KiB
Java
Raw Permalink 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 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 {
private 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();
}
}
);
JButton toForm4GenericDopClass = new JButton("ToForm4GenericDopClass");
toForm4GenericDopClass.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass();
}
}
);
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);
toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 120, 20);
w.add(canv);
w.add(ButtonAddTrain);
w.add(ButtonRemoveTrain);
w.add(ButtonRefreshCollection);
w.add(TextBoxNumber);
w.add(toForm4GenericDopClass);
w.setVisible(true);
}
}