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

182 lines
8.5 KiB
Java
Raw Normal View History

2023-12-09 17:25:15 +04:00
import javax.swing.*;
2023-12-23 16:20:46 +04:00
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
2023-12-09 17:25:15 +04:00
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
2023-12-23 16:20:46 +04:00
import java.util.List;
2023-12-09 17:25:15 +04:00
public class CruiserCollectionFrame extends JFrame {
2023-12-23 16:20:46 +04:00
private CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers;
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 {
2023-12-23 16:20:46 +04:00
private CruiserGenericStorage _storage;
TrashCollection<DrawingCruiser> _trash = new TrashCollection<>();
private JList<String> listBoxStoragesJList;
private DefaultListModel<String> listBoxStorages;
2023-12-09 17:25:15 +04:00
static final int SCREEN_W = 800;
static final int SCREEN_H = 600;
CollectionPanel() {
2023-12-23 16:20:46 +04:00
_storage = new CruiserGenericStorage(SCREEN_W,
SCREEN_H);
2023-12-09 17:25:15 +04:00
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) {
2023-12-23 16:20:46 +04:00
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
2023-12-09 17:25:15 +04:00
GameFrame form = new GameFrame();
2023-12-29 16:43:56 +04:00
if (form.getSelectedCruiser() != null) {
if (_cruisers.plus(_cruisers, form.SelectedCruiser)) {
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);
2023-12-29 16:43:56 +04:00
JButton ButtomRemoveCruiser = new JButton("Удалить объект");
ButtomRemoveCruiser.setBounds(600, 250, 150, 30);
ButtomRemoveCruiser.addActionListener(new ActionListener() {
2023-12-09 17:25:15 +04:00
@Override
public void actionPerformed(ActionEvent e) {
2023-12-23 16:20:46 +04:00
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
if (_cruisers == 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-23 16:20:46 +04:00
DrawingCruiser deletedCruiser = _cruisers.Get(num);
_trash.Add(deletedCruiser);
_cruisers.minus(_cruisers, num);
2023-12-09 17:25:15 +04:00
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
repaint();
}
});
2023-12-29 16:43:56 +04:00
this.add(ButtomRemoveCruiser);
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-23 16:20:46 +04:00
if(listBoxStoragesJList.getSelectedIndex() == -1) {
2023-12-09 17:25:15 +04:00
return;
}
2023-12-23 16:20:46 +04:00
_cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
2023-12-09 17:25:15 +04:00
repaint();
}
});
this.add(ButtonRefresh);
2023-12-23 16:20:46 +04:00
JButton ButtonDelStorage = new JButton("Удалить набор");
ButtonDelStorage.setBounds(600, 450, 170, 30);
ButtonDelStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
_storage.DelSet(listBoxStoragesJList.getSelectedValue());
ReloadObjects();
}
});
this.add(ButtonDelStorage);
JButton ButtonTrash = new JButton("Удаленные объекты");
ButtonTrash.setBounds(600, 500, 170, 30);
ButtonTrash.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_trash.GetSize() == 0)
return;
GameFrame form = new GameFrame(_trash.GetLast());
_trash.PopHead();
}
});
this.add(ButtonTrash);
JTextField textBoxStorageName = new JTextField();
textBoxStorageName.setBounds(600, 10, 150, 30);
this.add(textBoxStorageName);
JButton ButtonAddObject = new JButton("Добавить набор");
ButtonAddObject.setBounds(600, 40, 150, 30);
ButtonAddObject.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (textBoxStorageName.getText().equals( "") || textBoxStorageName.getText().equals(null))
{
return;
}
_storage.AddSet(textBoxStorageName.getText());
ReloadObjects();
repaint();
}});
this.add(ButtonAddObject);
listBoxStorages = new DefaultListModel<>();
listBoxStoragesJList = new JList<>(listBoxStorages);
listBoxStoragesJList.setBounds(600, 350, 150, 70);
listBoxStoragesJList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
_cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
repaint();
}
}
});
this.add(listBoxStoragesJList);
_cruisers = new CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>(SCREEN_W, SCREEN_H);
}
private void ReloadObjects()
{
int index = listBoxStoragesJList.getSelectedIndex();
listBoxStorages.clear();
List<String> keys = _storage.Keys();
for(int i = 0; i < keys.size(); i++){
listBoxStorages.addElement(keys.get(i));
}
if(listBoxStorages.size() > 0 && (index == -1 || index >= listBoxStorages.size()))
listBoxStoragesJList.setSelectedIndex(0);
else if(listBoxStorages.size() > 0)
listBoxStoragesJList.setSelectedIndex(index);
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-23 16:20:46 +04:00
if (listBoxStoragesJList.getSelectedIndex() != -1) {
2023-12-29 16:43:56 +04:00
_storage.getByIndex(listBoxStorages.get(listBoxStoragesJList.getSelectedIndex())).Showcruisers(g);
_cruisers.Showcruisers(g);
2023-12-23 16:20:46 +04:00
}
2023-12-09 17:25:15 +04:00
}
}
}