From c526d2cfc18b07088e89cdab0bb0cd6177445053 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Thu, 23 Nov 2023 22:47:50 +0400 Subject: [PATCH] lab4 with additional task --- src/frames/FrameBattleship.java | 9 +++++++- src/frames/FrameShipsCollection.java | 27 ++++++++++++++++++++---- src/generics/ShipsGenericCollection.java | 2 +- src/generics/TrashCollection.java | 21 ++++++++++++++++++ 4 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 src/generics/TrashCollection.java diff --git a/src/frames/FrameBattleship.java b/src/frames/FrameBattleship.java index d285489..9793ab7 100644 --- a/src/frames/FrameBattleship.java +++ b/src/frames/FrameBattleship.java @@ -28,7 +28,7 @@ public class FrameBattleship extends JFrame { public FrameBattleship() throws IOException { super("Линкор"); setSize(new Dimension(900,500)); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //components initialisation pictureBoxBattleship = new JComponent(){ public void paintComponent(Graphics graphics){ @@ -184,4 +184,11 @@ public class FrameBattleship extends JFrame { } selectedShip = drawingShip; } + public void setShip(DrawingShip ship){ + ship.setPosition(0,0); + drawingShip = ship; + pictureBoxBattleship.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); + drawingShip.drawTransport((Graphics2D) pictureBoxBattleship.getGraphics()); + draw(); + } } \ No newline at end of file diff --git a/src/frames/FrameShipsCollection.java b/src/frames/FrameShipsCollection.java index f2b7393..28d4717 100644 --- a/src/frames/FrameShipsCollection.java +++ b/src/frames/FrameShipsCollection.java @@ -9,9 +9,11 @@ import java.util.Objects; import drawing_objects.DrawingShip; import generics.ShipsGenericStorage; +import generics.TrashCollection; public class FrameShipsCollection extends JFrame { private final ShipsGenericStorage storage; + TrashCollection trashCollection = new TrashCollection<>(); private JComponent pictureBoxCollection; private TextField textFieldNumber; private TextField textFieldStorageName; @@ -47,6 +49,7 @@ public class FrameShipsCollection extends JFrame { JButton buttonRefreshCollection = new JButton("Обновить коллекцию"); JButton buttonAddSet = new JButton("Добавить набор"); JButton buttonDeleteSet = new JButton("Удалить набор"); + JButton buttonTrash = new JButton("Корзина"); textFieldStorageName = new TextField(); listModel = new DefaultListModel<>(); JScrollPane scrollPane = new JScrollPane(); @@ -58,6 +61,7 @@ public class FrameShipsCollection extends JFrame { buttonAddShip.addActionListener(e -> buttonAddShipClick()); buttonRemoveShip.addActionListener(e -> buttonRemoveShipClick()); buttonRefreshCollection.addActionListener(e -> buttonRefreshCollectionClick()); + buttonTrash.addActionListener(e -> buttonTrashClick()); //panels and constraints initialisation JPanel panelTools = new JPanel(new GridBagLayout()); panelTools.setBorder(new StrokeBorder(new BasicStroke(3))); @@ -98,6 +102,9 @@ public class FrameShipsCollection extends JFrame { constraints.gridx = 0; constraints.gridy = 4; panelTools.add(buttonRefreshCollection, constraints); + constraints.gridx = 0; + constraints.gridy = 5; + panelTools.add(buttonTrash, constraints); //addition to frame setLayout(new BorderLayout()); add(panelTools, BorderLayout.EAST); @@ -115,7 +122,7 @@ public class FrameShipsCollection extends JFrame { else if(listModel.size() > 0) listStorages.setSelectedIndex(index); } - public void buttonAddSet_Click() { + private void buttonAddSet_Click() { if(textFieldStorageName.getText() == null ) { JOptionPane.showMessageDialog(this, "Не все данные заполнены"); return; @@ -128,7 +135,7 @@ public class FrameShipsCollection extends JFrame { storage.addSet(name); reloadObjects(); } - public void buttonDeleteSet_Click() { + private void buttonDeleteSet_Click() { if (listStorages.getSelectedIndex() == -1) return; storage.delSet(listStorages.getSelectedValue()); @@ -151,7 +158,6 @@ public class FrameShipsCollection extends JFrame { DrawingShip ship = form.getSelectedShip(); form.dispose(); if (ship != null && obj.insert(ship)) { - System.out.println(ship.getWidth() + " " + ship.getHeight() + " " + ship.getPosX() + " " + ship.getPosY()); JOptionPane.showMessageDialog(this, "Объект добавлен"); pictureBoxCollection.repaint(); } @@ -167,8 +173,10 @@ public class FrameShipsCollection extends JFrame { if (obj == null) return; int pos = Integer.parseInt(textFieldNumber.getText()); - if (storage.getSet(listStorages.getSelectedValue()).remove(pos)){ + var ship = obj.get(pos); + if (obj.remove(pos)){ JOptionPane.showMessageDialog(this, "Объект удален"); + trashCollection.add(ship); pictureBoxCollection.repaint(); } else{ @@ -178,4 +186,15 @@ public class FrameShipsCollection extends JFrame { private void buttonRefreshCollectionClick(){ pictureBoxCollection.repaint(); } + private void buttonTrashClick(){ + if(trashCollection.getSize() == 0) + return; + FrameBattleship form; + try { + form = new FrameBattleship(); + } catch (IOException e) { + throw new RuntimeException(e); + } + form.setShip(trashCollection.pop()); + } } diff --git a/src/generics/ShipsGenericCollection.java b/src/generics/ShipsGenericCollection.java index ab82ed1..4806eab 100644 --- a/src/generics/ShipsGenericCollection.java +++ b/src/generics/ShipsGenericCollection.java @@ -61,7 +61,7 @@ public class ShipsGenericCollection { + LinkedList list; + public TrashCollection(){ + list = new LinkedList<>(); + } + public int getSize(){ + return list.size(); + } + public void add(T ship){ + list.add(ship); + } + public T pop(){ + if(list.size() == 0) + return null; + return list.pop(); + } +}