PIbd-23_Panina_A.D.Cruiser..../CruiserCollectionFrame.java
2023-12-09 21:01:39 +04:00

90 lines
3.8 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.

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