PIbd21.LyovushkinaA.A.Conta.../FormShipCollection.java

231 lines
9.3 KiB
Java
Raw Normal View History

2023-10-24 01:06:14 +04:00
import java.awt.*;
import javax.swing.*;
2023-11-04 19:41:05 +04:00
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
2023-10-24 01:06:14 +04:00
import java.awt.event.*;
2023-11-04 19:41:05 +04:00
import java.util.Stack;
2023-10-24 01:06:14 +04:00
public class FormShipCollection {
class Canvas extends JComponent{
public Canvas(){}
public void paintComponent(Graphics g){
super.paintComponent(g);
2023-11-04 19:41:05 +04:00
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if(obj.ShowShips() != null){
g.drawImage(obj.ShowShips(), 0, 0, this);
2023-10-24 01:06:14 +04:00
}
super.repaint();
}
}
Canvas canv;
2023-11-04 19:41:05 +04:00
static int pictureBoxWidth = 820;
2023-10-24 01:06:14 +04:00
static int pictureBoxHeight = 580;
2023-11-04 19:41:05 +04:00
private Stack<DrawingShip> removedShips;
private ShipGenericStorage _storage;
private JList<String> jListStorage;
private DefaultListModel<String> listModel;
private void ReloadObjects()
{
int index = jListStorage.getSelectedIndex();
listModel.clear();
for (String key : _storage.Keys()) {
listModel.addElement(key);
}
if (listModel.size() > 0 && (index == -1 || index >= listModel.size()))
{
jListStorage.setSelectedIndex(0);
}
else if (listModel.size() > 0 && index > -1 && index < listModel.size())
{
jListStorage.setSelectedIndex(index);
}
}
2023-10-24 01:06:14 +04:00
public void Draw(){
canv.repaint();
}
FormShipCollection(){
canv = new Canvas();
JFrame w = new JFrame("FormShipCollection");
2023-11-04 19:41:05 +04:00
_storage = new ShipGenericStorage(pictureBoxWidth, pictureBoxHeight);
listModel = new DefaultListModel<String>();
jListStorage = new JList<String>(listModel);
2023-10-24 01:06:14 +04:00
JButton ButtonAddShip = new JButton("Добавить кораблик");
JButton ButtonDeleteShip = new JButton("Удалить кораблик");
JButton ButtonRefreshCollection = new JButton("Обновить коллекцию");
JButton forNewForm = new JButton("новая форма");
2023-11-04 19:41:05 +04:00
JButton buttonGetRemoved = new JButton("Получить удалённое");
JButton buttonAddSet = new JButton("Добавить коллекцию");
JButton buttonRemoveSet = new JButton("Удалить коллекцию");
JTextField textBoxSetName = new JTextField();
2023-10-24 01:06:14 +04:00
JTextField TextBoxNumber = new JTextField();
ButtonAddShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
2023-11-04 19:41:05 +04:00
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
2023-10-24 01:06:14 +04:00
FormContainerShip form = new FormContainerShip();
form.buttonSelectedShip.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
2023-11-04 19:41:05 +04:00
if(obj.Add(form._drawingShip) != -1){
2023-10-24 01:06:14 +04:00
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
else{
JOptionPane.showMessageDialog(null, "Объект не добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
form.w.dispose();
}
}
);
}
}
);
forNewForm.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
NewFormRand newFormRand = new NewFormRand();
}
}
);
2023-11-04 19:41:05 +04:00
removedShips = new Stack<DrawingShip>();
2023-10-24 01:06:14 +04:00
ButtonDeleteShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
2023-11-04 19:41:05 +04:00
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
2023-10-24 01:06:14 +04:00
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
int pos = Integer.parseInt(TextBoxNumber.getText());
2023-11-04 19:41:05 +04:00
var rem = obj.remove(pos);
if (rem != null)
2023-10-24 01:06:14 +04:00
{
2023-11-04 19:41:05 +04:00
removedShips.push(rem);
2023-10-24 01:06:14 +04:00
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
}
}
);
2023-11-04 19:41:05 +04:00
buttonGetRemoved.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (removedShips.empty()){
JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
FormContainerShip form = new FormContainerShip();
form._drawingShip = removedShips.peek();
2023-11-04 20:38:51 +04:00
form._drawingShip.SetPosition(100, 100);
2023-11-04 19:41:05 +04:00
removedShips.pop();
form.Draw();
}
}
);
2023-10-24 01:06:14 +04:00
ButtonRefreshCollection.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
Draw();
}
}
);
2023-11-04 19:41:05 +04:00
buttonAddSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (textBoxSetName.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_storage.AddSet(textBoxSetName.getText());
ReloadObjects();
}
}
);
jListStorage.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e){
Draw();
}
}
);
buttonRemoveSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект " + jListStorage.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
_storage.DelSet(jListStorage.getSelectedValue());
ReloadObjects();
}
}
);
2023-10-24 01:06:14 +04:00
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
ButtonAddShip.setBounds(pictureBoxWidth, 0, 120, 20);
TextBoxNumber.setBounds(pictureBoxWidth, 50, 120, 20);
ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 120, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 120, 120, 20);
forNewForm.setBounds(pictureBoxWidth, 150, 120, 20);
2023-11-04 19:41:05 +04:00
buttonAddSet.setBounds(pictureBoxWidth, 180, 120, 20);
textBoxSetName.setBounds(pictureBoxWidth, 210, 120, 20);
jListStorage.setBounds(pictureBoxWidth, 240, 120, 60);
buttonRemoveSet.setBounds(pictureBoxWidth, 310, 120, 20);
buttonGetRemoved.setBounds(pictureBoxWidth, 340, 120, 20);
2023-10-24 01:06:14 +04:00
w.add(canv);
w.add(ButtonAddShip);
w.add(ButtonDeleteShip);
w.add(ButtonRefreshCollection);
w.add(forNewForm);
w.add(TextBoxNumber);
2023-11-04 19:41:05 +04:00
w.add(buttonAddSet);
w.add(textBoxSetName);
w.add(jListStorage);
w.add(buttonRemoveSet);
w.add(buttonGetRemoved);
2023-10-24 01:06:14 +04:00
w.setVisible(true);
}
}