PIbd-23_Panina_A.D.Cruiser..../CruiserCollectionFrame.java

111 lines
3.7 KiB
Java
Raw Normal View History

2023-12-09 17:25:15 +04:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CruiserCollectionFrame extends JFrame {
private CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cars;
public CruiserCollectionFrame() {
this.setSize(800, 600);
this.setTitle("Collection");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
CollectionPanel panel = new CollectionPanel();
this.add(panel);
this.setVisible(true);
}
public class CollectionPanel extends JPanel {
static final int SCREEN_W = 800;
static final int SCREEN_H = 600;
CollectionPanel() {
this.setLayout(null);
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
JTextField textFieldRemove = new JTextField();
textFieldRemove.setBounds(600, 200, 150, 30);
this.add(textFieldRemove);
JButton ButtonAddCar = new JButton("Добавить машину");
ButtonAddCar.setBounds(600, 150, 150, 30);
ButtonAddCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
GameFrame form = new GameFrame();
if (form.getSelectedCar() != null) {
if (_cars.plus(_cars, form.SelectedCar) != -1) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
repaint();
}
});
this.add(ButtonAddCar);
JButton ButtonRemoveCar = new JButton("Удалить машину");
ButtonRemoveCar.setBounds(600, 250, 150, 30);
ButtonRemoveCar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_cars == null) {
return;
}
String tmp = textFieldRemove.getText();
int num;
try {
num = Integer.parseInt(tmp);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_cars.minus(_cars, num);
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
repaint();
}
});
this.add(ButtonRemoveCar);
JButton ButtonRefresh = new JButton("Обновить");
ButtonRefresh.setBounds(600, 300, 150, 30);
ButtonRefresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_cars == null) {
return;
}
repaint();
}
});
this.add(ButtonRefresh);
_cars = new CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>(SCREEN_W, SCREEN_H);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
_cars.ShowCars(g);
}
}
}