90 lines
3.8 KiB
Java
90 lines
3.8 KiB
Java
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);
|
||
}
|
||
}
|
||
} |