lab4 with additional task

This commit is contained in:
Zakharov_Rostislav 2023-11-23 22:47:50 +04:00
parent 150d41e1a5
commit c526d2cfc1
4 changed files with 53 additions and 6 deletions

View File

@ -28,7 +28,7 @@ public class FrameBattleship extends JFrame {
public FrameBattleship() throws IOException { public FrameBattleship() throws IOException {
super("Линкор"); super("Линкор");
setSize(new Dimension(900,500)); setSize(new Dimension(900,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//components initialisation //components initialisation
pictureBoxBattleship = new JComponent(){ pictureBoxBattleship = new JComponent(){
public void paintComponent(Graphics graphics){ public void paintComponent(Graphics graphics){
@ -184,4 +184,11 @@ public class FrameBattleship extends JFrame {
} }
selectedShip = drawingShip; 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();
}
} }

View File

@ -9,9 +9,11 @@ import java.util.Objects;
import drawing_objects.DrawingShip; import drawing_objects.DrawingShip;
import generics.ShipsGenericStorage; import generics.ShipsGenericStorage;
import generics.TrashCollection;
public class FrameShipsCollection extends JFrame { public class FrameShipsCollection extends JFrame {
private final ShipsGenericStorage storage; private final ShipsGenericStorage storage;
TrashCollection<DrawingShip> trashCollection = new TrashCollection<>();
private JComponent pictureBoxCollection; private JComponent pictureBoxCollection;
private TextField textFieldNumber; private TextField textFieldNumber;
private TextField textFieldStorageName; private TextField textFieldStorageName;
@ -47,6 +49,7 @@ public class FrameShipsCollection extends JFrame {
JButton buttonRefreshCollection = new JButton("Обновить коллекцию"); JButton buttonRefreshCollection = new JButton("Обновить коллекцию");
JButton buttonAddSet = new JButton("Добавить набор"); JButton buttonAddSet = new JButton("Добавить набор");
JButton buttonDeleteSet = new JButton("Удалить набор"); JButton buttonDeleteSet = new JButton("Удалить набор");
JButton buttonTrash = new JButton("Корзина");
textFieldStorageName = new TextField(); textFieldStorageName = new TextField();
listModel = new DefaultListModel<>(); listModel = new DefaultListModel<>();
JScrollPane scrollPane = new JScrollPane(); JScrollPane scrollPane = new JScrollPane();
@ -58,6 +61,7 @@ public class FrameShipsCollection extends JFrame {
buttonAddShip.addActionListener(e -> buttonAddShipClick()); buttonAddShip.addActionListener(e -> buttonAddShipClick());
buttonRemoveShip.addActionListener(e -> buttonRemoveShipClick()); buttonRemoveShip.addActionListener(e -> buttonRemoveShipClick());
buttonRefreshCollection.addActionListener(e -> buttonRefreshCollectionClick()); buttonRefreshCollection.addActionListener(e -> buttonRefreshCollectionClick());
buttonTrash.addActionListener(e -> buttonTrashClick());
//panels and constraints initialisation //panels and constraints initialisation
JPanel panelTools = new JPanel(new GridBagLayout()); JPanel panelTools = new JPanel(new GridBagLayout());
panelTools.setBorder(new StrokeBorder(new BasicStroke(3))); panelTools.setBorder(new StrokeBorder(new BasicStroke(3)));
@ -98,6 +102,9 @@ public class FrameShipsCollection extends JFrame {
constraints.gridx = 0; constraints.gridx = 0;
constraints.gridy = 4; constraints.gridy = 4;
panelTools.add(buttonRefreshCollection, constraints); panelTools.add(buttonRefreshCollection, constraints);
constraints.gridx = 0;
constraints.gridy = 5;
panelTools.add(buttonTrash, constraints);
//addition to frame //addition to frame
setLayout(new BorderLayout()); setLayout(new BorderLayout());
add(panelTools, BorderLayout.EAST); add(panelTools, BorderLayout.EAST);
@ -115,7 +122,7 @@ public class FrameShipsCollection extends JFrame {
else if(listModel.size() > 0) else if(listModel.size() > 0)
listStorages.setSelectedIndex(index); listStorages.setSelectedIndex(index);
} }
public void buttonAddSet_Click() { private void buttonAddSet_Click() {
if(textFieldStorageName.getText() == null ) { if(textFieldStorageName.getText() == null ) {
JOptionPane.showMessageDialog(this, "Не все данные заполнены"); JOptionPane.showMessageDialog(this, "Не все данные заполнены");
return; return;
@ -128,7 +135,7 @@ public class FrameShipsCollection extends JFrame {
storage.addSet(name); storage.addSet(name);
reloadObjects(); reloadObjects();
} }
public void buttonDeleteSet_Click() { private void buttonDeleteSet_Click() {
if (listStorages.getSelectedIndex() == -1) if (listStorages.getSelectedIndex() == -1)
return; return;
storage.delSet(listStorages.getSelectedValue()); storage.delSet(listStorages.getSelectedValue());
@ -151,7 +158,6 @@ public class FrameShipsCollection extends JFrame {
DrawingShip ship = form.getSelectedShip(); DrawingShip ship = form.getSelectedShip();
form.dispose(); form.dispose();
if (ship != null && obj.insert(ship)) { if (ship != null && obj.insert(ship)) {
System.out.println(ship.getWidth() + " " + ship.getHeight() + " " + ship.getPosX() + " " + ship.getPosY());
JOptionPane.showMessageDialog(this, "Объект добавлен"); JOptionPane.showMessageDialog(this, "Объект добавлен");
pictureBoxCollection.repaint(); pictureBoxCollection.repaint();
} }
@ -167,8 +173,10 @@ public class FrameShipsCollection extends JFrame {
if (obj == null) if (obj == null)
return; return;
int pos = Integer.parseInt(textFieldNumber.getText()); 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, "Объект удален"); JOptionPane.showMessageDialog(this, "Объект удален");
trashCollection.add(ship);
pictureBoxCollection.repaint(); pictureBoxCollection.repaint();
} }
else{ else{
@ -178,4 +186,15 @@ public class FrameShipsCollection extends JFrame {
private void buttonRefreshCollectionClick(){ private void buttonRefreshCollectionClick(){
pictureBoxCollection.repaint(); 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());
}
} }

View File

@ -61,7 +61,7 @@ public class ShipsGenericCollection<T extends DrawingShip, U extends IMoveableOb
if (ship != null) if (ship != null)
{ {
int inRow = pictureWidth / placeSizeWidth; int inRow = pictureWidth / placeSizeWidth;
ship.setPosition(i % inRow * placeSizeWidth, (collection.getCount() / inRow - 1 - i / inRow) * placeSizeHeight + 5); ship.setPosition(i % inRow * placeSizeWidth, pictureHeight - pictureHeight % placeSizeHeight - (i / inRow + 1) * placeSizeHeight + 5);
ship.drawTransport(g); ship.drawTransport(g);
} }
} }

View File

@ -0,0 +1,21 @@
package generics;
import java.util.LinkedList;
public class TrashCollection<T> {
LinkedList<T> 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();
}
}