From 226955f1fe8dd3abe03a6badb00a64c4a4498037 Mon Sep 17 00:00:00 2001 From: Salikh Date: Sat, 30 Dec 2023 09:41:01 +0400 Subject: [PATCH] four commit --- src/BomberGenericCollection.java | 20 ++-- src/BomberGenericStorage.java | 37 +++++++ src/BomberTrashCollection.java | 16 +++ src/FrameAirBomber.java | 8 ++ src/FramePlaneCollection.java | 180 +++++++++++++++++++++++-------- src/Main.java | 2 +- src/SetGeneric.java | 43 ++++---- 7 files changed, 233 insertions(+), 73 deletions(-) create mode 100644 src/BomberGenericStorage.java create mode 100644 src/BomberTrashCollection.java diff --git a/src/BomberGenericCollection.java b/src/BomberGenericCollection.java index 2b33df8..80d2590 100644 --- a/src/BomberGenericCollection.java +++ b/src/BomberGenericCollection.java @@ -4,11 +4,11 @@ public class BomberGenericCollection /// Ширина окна прорисовки /// - private int pictureWidth; + private final int pictureWidth; /// /// Высота окна прорисовки /// - private int _pictureHeight; + private final int _pictureHeight; /// /// Размер занимаемого объектом места (ширина) /// @@ -32,7 +32,10 @@ public class BomberGenericCollection(width * height, (Class) DrawingAir.class); + collection = new SetGeneric(width * height); + } + public int size(){ + return collection.getCount(); } /// /// Перегрузка оператора сложения @@ -52,11 +55,7 @@ public class BomberGenericCollection /// /// - public boolean Remove(int pos) - { - T obj = collection.Get(pos); - if (obj == null) - return false; + public boolean Remove(int pos) { return collection.remove(pos); } /// @@ -70,6 +69,11 @@ public class BomberGenericCollection= collection.getCount()) + return null; + return collection.Get(position); + } /// /// Вывод всего набора объектов /// diff --git a/src/BomberGenericStorage.java b/src/BomberGenericStorage.java new file mode 100644 index 0000000..390a046 --- /dev/null +++ b/src/BomberGenericStorage.java @@ -0,0 +1,37 @@ +import java.util.ArrayList; +import java.util.HashMap; +public class BomberGenericStorage { + final HashMap> planeStorages; + private final int _pictureWidth; + private final int _pictureHeight; + + public BomberGenericStorage(int pictureWidth, int pictureHeight){ + planeStorages = new HashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public ArrayList Keys(){ + return new ArrayList<>(planeStorages.keySet()); + } + public void AddSet(String name){ + if(planeStorages.containsKey(name)) + return; + planeStorages.put(name, new BomberGenericCollection<>(_pictureWidth, _pictureHeight)); + } + + public void DelSet(String name){ + if(!planeStorages.containsKey(name)) + return; + planeStorages.remove(name); + } + + public BomberGenericCollection getCollection(String name){ + if(!planeStorages.containsKey(name)) + return null; + return planeStorages.get(name); + } + + public DrawingAir getPlanes(String collectionName, int position){ + return planeStorages.get(collectionName).Get(position); + } +} diff --git a/src/BomberTrashCollection.java b/src/BomberTrashCollection.java new file mode 100644 index 0000000..630660c --- /dev/null +++ b/src/BomberTrashCollection.java @@ -0,0 +1,16 @@ +import java.util.LinkedList; +public class BomberTrashCollection { + LinkedList linkedList; + public BomberTrashCollection(){ + linkedList = new LinkedList<>(); + } + public void Push(T plane){ + linkedList.push(plane); + } + public T Pop(){ + return linkedList.pop(); + } + public int GetSize(){ + return linkedList.size(); + } +} diff --git a/src/FrameAirBomber.java b/src/FrameAirBomber.java index 2e368f9..8763779 100644 --- a/src/FrameAirBomber.java +++ b/src/FrameAirBomber.java @@ -24,6 +24,7 @@ public class FrameAirBomber extends JFrame { pictureBox = new JComponent(){ public void paintComponent(Graphics graphics){ super.paintComponent(graphics); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Graphics2D graphics2D = (Graphics2D) graphics; if (drawingPlane != null) drawingPlane.drawTransport(graphics2D); super.repaint(); @@ -192,4 +193,11 @@ public class FrameAirBomber extends JFrame { } selectedPlane = drawingPlane; } + + public void ChangePlane(DrawingAir newPlane){ + drawingPlane = newPlane; + drawingPlane.setPosition(0,0); + pictureBox.setBounds(0,0,getContentPane().getWidth(),getContentPane().getHeight()); + draw(); + } } \ No newline at end of file diff --git a/src/FramePlaneCollection.java b/src/FramePlaneCollection.java index 0865276..e33ee06 100644 --- a/src/FramePlaneCollection.java +++ b/src/FramePlaneCollection.java @@ -1,102 +1,194 @@ import javax.swing.*; +import javax.swing.border.StrokeBorder; + import java.awt.*; import java.io.IOException; +import java.util.ArrayList; +import java.util.Objects; public class FramePlaneCollection extends JFrame { - private BomberGenericCollection planes; - JComponent pictureBoxCollection; - TextField textFieldNumber; + private BomberTrashCollection trashCollection = new BomberTrashCollection<>(); + private final BomberGenericStorage storage; + private JComponent pictureBoxCollection; + private TextField textFieldNumber; + private TextField textFieldStorageName; + private JList listStorages; + private DefaultListModel listModel; public FramePlaneCollection(){ super("Набор самолетов"); - setSize(new Dimension(900,600)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - creteGUI(); - planes = new BomberGenericCollection<>(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight()); - pictureBoxCollection.repaint(); + createGui(); + pack(); + storage = new BomberGenericStorage(pictureBoxCollection.getWidth(), pictureBoxCollection.getHeight()); setVisible(true); } - - public void creteGUI(){ + private void createGui(){ + //components initialisation pictureBoxCollection = new JComponent(){ public void paintComponent(Graphics graphics){ super.paintComponent(graphics); Graphics2D graphics2D = (Graphics2D) graphics; - if (planes != null) planes.ShowPlanes(graphics2D); + if (listStorages == null || storage == null) + return; + var collection = storage.getCollection(listStorages.getSelectedValue()); + if (collection == null) + return; + collection.ShowPlanes(graphics2D); super.repaint(); } }; - pictureBoxCollection.setBounds(0, 0, 700, 600); + pictureBoxCollection.setPreferredSize(new Dimension(700, 600)); JButton buttonAddPlane = new JButton("Добавить самолет"); textFieldNumber = new TextField(); JButton buttonRemovePlane = new JButton("Удалить самолет"); JButton buttonRefreshCollection = new JButton("Обновить коллекцию"); - //ActionListeners - buttonAddPlane.addActionListener(e -> ButtonAddPlaneClick()); - buttonRemovePlane.addActionListener(e -> ButtonRemovePlaneClick()); - buttonRefreshCollection.addActionListener(e -> ButtonRefreshCollectionClick()); - //addition to panel - JPanel panelCollection = new JPanel(new GridBagLayout()); + JButton buttonAddSet = new JButton("Добавить набор"); + JButton buttonDeleteSet = new JButton("Удалить набор"); + JButton buttonTrash = new JButton("Корзина"); + textFieldStorageName = new TextField(); + listModel = new DefaultListModel<>(); + JScrollPane scrollPane = new JScrollPane(); + listStorages= new JList<>(listModel); + scrollPane.setViewportView(listStorages); + //ActionListeners and ActionCommand addition + buttonAddSet.addActionListener(e -> buttonAddSet_Click()); + buttonDeleteSet.addActionListener(e -> buttonDeleteSet_Click()); + buttonAddPlane.addActionListener(e -> buttonAddPlaneClick()); + buttonRemovePlane.addActionListener(e -> buttonRemovePlaneClick()); + 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))); + panelTools.setToolTipText("Инструменты"); + JPanel panelSets = new JPanel(new GridBagLayout()); + panelSets.setBorder(new StrokeBorder(new BasicStroke(3))); + panelSets.setToolTipText("Наборы"); GridBagConstraints constraints = new GridBagConstraints(); - constraints.insets.left = constraints.insets.right = 2; - constraints.insets.top = constraints.insets.bottom = 20; + constraints.insets.left = constraints.insets.right = 5; + constraints.insets.top = constraints.insets.bottom = 5; constraints.fill = GridBagConstraints.BOTH; + //addition to panelSets constraints.gridx = 0; constraints.gridy = 0; - panelCollection.add(buttonAddPlane, constraints); + panelSets.add(textFieldStorageName, constraints); constraints.gridx = 0; constraints.gridy = 1; - panelCollection.add(textFieldNumber, constraints); + panelSets.add(buttonAddSet, constraints); constraints.gridx = 0; constraints.gridy = 2; - panelCollection.add(buttonRemovePlane, constraints); + panelSets.add(scrollPane, constraints); + constraints.gridx = 0; + constraints.gridy = 3; + panelSets.add(buttonDeleteSet, constraints); + //addition to panelTools + constraints.gridx = 0; + constraints.gridy = 0; + panelTools.add(panelSets, constraints); + constraints.gridx = 0; + constraints.gridy = 1; + panelTools.add(buttonAddPlane, constraints); + constraints.gridx = 0; + constraints.gridy = 2; + panelTools.add(textFieldNumber, constraints); + constraints.gridx = 0; + constraints.gridy = 3; + panelTools.add(buttonRemovePlane, constraints); + constraints.gridx = 0; + constraints.gridy = 4; + panelTools.add(buttonRefreshCollection, constraints); constraints.gridx = 0; constraints.gridy = 5; - panelCollection.add(buttonRefreshCollection, constraints); - JPanel upperPanel = new JPanel(new BorderLayout()); + panelTools.add(buttonTrash, constraints); + //addition to frame setLayout(new BorderLayout()); - add(panelCollection, BorderLayout.EAST); + add(panelTools, BorderLayout.EAST); add(pictureBoxCollection, BorderLayout.CENTER); } - - private void ButtonAddPlaneClick(){ + private void reloadObjects(){ + int index = listStorages.getSelectedIndex(); + listModel.clear(); + ArrayList keys = storage.Keys(); + for (String key : keys) { + listModel.addElement(key); + } + if(listModel.size() > 0 && (index == -1 || index >= listModel.size())) + listStorages.setSelectedIndex(0); + else if(listModel.size() > 0) + listStorages.setSelectedIndex(index); + } + public void buttonAddSet_Click() { + if(textFieldStorageName.getText() == null ) { + JOptionPane.showMessageDialog(this, "Не все данные заполнены"); + return; + } + String name = textFieldStorageName.getText(); + if (Objects.equals(name, "")) { + JOptionPane.showMessageDialog(this, "Не все данные заполнены"); + return; + } + storage.AddSet(name); + reloadObjects(); + } + public void buttonDeleteSet_Click() { + if (listStorages.getSelectedIndex() == -1) + return; + storage.DelSet(listStorages.getSelectedValue()); + reloadObjects(); + } + private void buttonAddPlaneClick() { + if (listStorages.getSelectedIndex() == -1) + return; + var obj = storage.getCollection(listStorages.getSelectedValue()); + if (obj == null) + return; FrameAirBomber form; try { form = new FrameAirBomber(); - } catch (IOException e){ + } catch (IOException e) { throw new RuntimeException(e); } - form.selectPlaneButton.addActionListener(e -> { + form.selectPlaneButton.addActionListener(e->{ form.select(); - var selectedPlane = form.getSelectedPlane(); + DrawingAir plane = form.getSelectedPlane(); form.dispose(); - if (planes.Insert(selectedPlane)) - { + if (obj.Insert(plane)) { JOptionPane.showMessageDialog(this, "Объект добавлен"); pictureBoxCollection.repaint(); } - else - { + else { JOptionPane.showMessageDialog(this, "Не удалось добавить объект"); } }); } - - private void ButtonRemovePlaneClick() - { + private void buttonRemovePlaneClick(){ + if (listStorages.getSelectedIndex() == -1) + return; + var obj = storage.getCollection(listStorages.getSelectedValue()); + if (obj == null) + return; int pos = Integer.parseInt(textFieldNumber.getText()); - if (planes.Remove(pos)) - { + var plane = obj.Get(pos); + if (obj.Remove(pos)){ JOptionPane.showMessageDialog(this, "Объект удален"); + trashCollection.Push(plane); pictureBoxCollection.repaint(); } - else - { + else{ JOptionPane.showMessageDialog(this, "Не удалось удалить объект"); } } - - private void ButtonRefreshCollectionClick() - { + private void buttonRefreshCollectionClick(){ pictureBoxCollection.repaint(); } + private void buttonTrashClick(){ + if(trashCollection.GetSize() == 0) + return; + try { + FrameAirBomber form = new FrameAirBomber(); + form.ChangePlane(trashCollection.Pop()); + } catch (Exception e){ + throw new RuntimeException(); + } + } } diff --git a/src/Main.java b/src/Main.java index 2df0988..c61bf97 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,5 @@ import java.io.IOException; public class Main { - public static void main(String[] args) throws IOException { new FrameHard(); } + public static void main(String[] args) throws IOException { new FramePlaneCollection(); } } diff --git a/src/SetGeneric.java b/src/SetGeneric.java index 24ec845..55b051a 100644 --- a/src/SetGeneric.java +++ b/src/SetGeneric.java @@ -1,39 +1,42 @@ import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; -public class SetGeneric{ - private final T[] places; - public int getCount() {return places.length;} - public SetGeneric(int count, Class type){ - places = (T[])Array.newInstance(type, count); +public class SetGeneric{ + private final ArrayList places; + private final int maxCount; + public int getCount() {return places.size();} + public SetGeneric(int count){ + maxCount = count; + places = new ArrayList<>(); } public boolean insert(T plane){ return insert(plane, 0); } public boolean insert(T plane, int position){ - if (!(position >= 0 && position < places.length)) + if (!(position >= 0 && position <= places.size() && places.size() < maxCount)) return false; - if (places[position] != null) - { - int ind = position; - while (ind < places.length && places[ind] != null) - ind++; - if (ind == places.length) - return false; - for (int i = ind - 1; i >= position; i--) - places[i + 1] = places[i]; - } - places[position] = plane; + places.add(position, plane); return true; } public boolean remove(int position){ - if(!(position >= 0 && position < getCount())) + if(!(position >= 0 && position < places.size())) return false; - places[position] = null; + places.remove(position); return true; } public T Get(int position){ if(!(position >= 0 && position < getCount())) return null; - return places[position]; + return places.get(position); + } + public ArrayList getPlanes(int maxPlanes){ + ArrayList toRet = new ArrayList<>(); + for(int i = 0; i < places.size(); i++){ + toRet.add(places.get(i)); + if(i == maxPlanes) + return toRet; + } + return toRet; } } \ No newline at end of file