lab4 with additional task
This commit is contained in:
parent
150d41e1a5
commit
c526d2cfc1
@ -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();
|
||||
}
|
||||
}
|
@ -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<DrawingShip> 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());
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ public class ShipsGenericCollection<T extends DrawingShip, U extends IMoveableOb
|
||||
if (ship != null)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
21
src/generics/TrashCollection.java
Normal file
21
src/generics/TrashCollection.java
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user