PIbd-23-Radaev-A.V.-Catamar.../FormCatamaranCollection.java

119 lines
4.5 KiB
Java
Raw Normal View History

2023-12-11 14:47:27 +04:00
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormCatamaranCollection {
private final CatamaranGenericCollection<DrawningCatamaran, DrawningObjectCatamaran> _catamaran;
private int pictureBoxWidth = 605;
private int pictureBoxHeight = 426;
CollectionCanvas canv;
void Draw() {
if (canv == null)
return;
canv.repaint();
}
public FormCatamaranCollection() {
_catamaran = new CatamaranGenericCollection<>(pictureBoxWidth, pictureBoxHeight);
canv = new CollectionCanvas();
JPanel toolBox = new JPanel();
JFrame collectionFrame = new JFrame();
collectionFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
collectionFrame.setSize(880, 450);
toolBox.setBounds(623, 12, 227, 426);
canv.setBounds(12, 12, pictureBoxWidth, pictureBoxHeight);
JButton addButton = new JButton("Добавить");
JButton removeButton = new JButton("Удалить");
JButton refreshButton = new JButton("Обновить");
JTextField catamaranNumb = new JTextField();
GridLayout lay = new GridLayout(7, 1);
lay.setVgap(10);
toolBox.setLayout(lay);
toolBox.add(addButton);
toolBox.add(catamaranNumb);
toolBox.add(removeButton);
toolBox.add(refreshButton);
collectionFrame.add(toolBox);
collectionFrame.add(canv);
collectionFrame.setVisible(true);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_catamaran == null) {
return;
}
FormCatamaran form = new FormCatamaran();
form.buttonSelect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_catamaran.Insert(form.SelectedCatamaran()) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация",
JOptionPane.INFORMATION_MESSAGE);
form.SelectedCatamaran()._pictureWidth = pictureBoxWidth;
form.SelectedCatamaran()._pictureHeight = pictureBoxHeight;
Draw();
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация",
JOptionPane.INFORMATION_MESSAGE);
}
canv._catamaran = _catamaran;
form.CatamaranFrame.dispose();
Draw();
}
});
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_catamaran == null) {
return;
}
String tmp = catamaranNumb.getText();
int numb;
try {
numb = Integer.parseInt(tmp);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_catamaran.Remove(numb);
_catamaran.ShowCatamaran();
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
});
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_catamaran == null) {
return;
}
_catamaran.ShowCatamaran();
Draw();
}
});
}
}
class CollectionCanvas extends JComponent {
public CatamaranGenericCollection<DrawningCatamaran, DrawningObjectCatamaran> _catamaran;
public CollectionCanvas() {
}
@Override
public void paintComponent(Graphics g) {
if (_catamaran == null) {
return;
}
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(_catamaran.ShowCatamaran(), 0, 0, this);
super.repaint();
}
}