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 {
|
2023-12-09 21:01:39 +04:00
|
|
|
|
private CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruiser;
|
2023-12-09 17:25:15 +04:00
|
|
|
|
|
|
|
|
|
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);
|
2023-12-09 21:01:39 +04:00
|
|
|
|
JButton ButtonAddCruiser = new JButton("Добавить объект");
|
|
|
|
|
ButtonAddCruiser.setBounds(600, 150, 150, 30);
|
|
|
|
|
ButtonAddCruiser.addActionListener(new ActionListener() {
|
2023-12-09 17:25:15 +04:00
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
GameFrame form = new GameFrame();
|
2023-12-09 21:01:39 +04:00
|
|
|
|
if (form.getSelectedCruiser() != null) {
|
|
|
|
|
if (_cruiser.plus(_cruiser, form.SelectedCruiser) != -1) {
|
2023-12-09 17:25:15 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-12-09 21:01:39 +04:00
|
|
|
|
this.add(ButtonAddCruiser);
|
|
|
|
|
JButton ButtonRemoveCruiser = new JButton("Удалить объект");
|
|
|
|
|
ButtonRemoveCruiser.setBounds(600, 250, 150, 30);
|
|
|
|
|
ButtonRemoveCruiser.addActionListener(new ActionListener() {
|
2023-12-09 17:25:15 +04:00
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2023-12-09 21:01:39 +04:00
|
|
|
|
if (_cruiser == null) {
|
2023-12-09 17:25:15 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String tmp = textFieldRemove.getText();
|
|
|
|
|
int num;
|
|
|
|
|
try {
|
|
|
|
|
num = Integer.parseInt(tmp);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-09 21:01:39 +04:00
|
|
|
|
_cruiser.minus(_cruiser, num);
|
2023-12-09 17:25:15 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-12-09 21:01:39 +04:00
|
|
|
|
this.add(ButtonRemoveCruiser);
|
2023-12-09 17:25:15 +04:00
|
|
|
|
JButton ButtonRefresh = new JButton("Обновить");
|
|
|
|
|
ButtonRefresh.setBounds(600, 300, 150, 30);
|
|
|
|
|
ButtonRefresh.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2023-12-09 21:01:39 +04:00
|
|
|
|
if (_cruiser == null) {
|
2023-12-09 17:25:15 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
this.add(ButtonRefresh);
|
2023-12-09 21:01:39 +04:00
|
|
|
|
_cruiser = new CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>(SCREEN_W, SCREEN_H);
|
2023-12-09 17:25:15 +04:00
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void paintComponent(Graphics g) {
|
|
|
|
|
super.paintComponent(g);
|
|
|
|
|
draw(g);
|
|
|
|
|
}
|
|
|
|
|
public void draw(Graphics g) {
|
2023-12-09 21:01:39 +04:00
|
|
|
|
_cruiser.ShowCruiser(g);
|
2023-12-09 17:25:15 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|