From 083c28bd975b3488cfae665d2e56a2b2b7f025f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 22 Nov 2023 18:42:51 +0400 Subject: [PATCH] lab4 maybe done --- .../main/java/org/example/FormHydroplane.java | 7 + .../org/example/FormPlaneCollecltion.java | 211 +++++++++++++++--- .../org/example/PlanesGenericCollection.java | 15 +- .../org/example/PlanesGenericStorage.java | 52 +++++ .../src/main/java/org/example/SetGeneric.java | 86 ++++--- 5 files changed, 306 insertions(+), 65 deletions(-) create mode 100644 Hydroplane/src/main/java/org/example/PlanesGenericStorage.java diff --git a/Hydroplane/src/main/java/org/example/FormHydroplane.java b/Hydroplane/src/main/java/org/example/FormHydroplane.java index 5b78423..980bd66 100644 --- a/Hydroplane/src/main/java/org/example/FormHydroplane.java +++ b/Hydroplane/src/main/java/org/example/FormHydroplane.java @@ -21,6 +21,13 @@ public class FormHydroplane{ canv.repaint(); } + public void ChangePlane(DrawingPlane newPlane){ + newPlane.SetPosition(0,0); + _drawingPlane = newPlane; + canv._drawingPlane = _drawingPlane; + + } + public FormHydroplane(){ w=new JFrame ("Hydroplane"); JButton buttonCreatePlane = new JButton("createPlane"); diff --git a/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java b/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java index 2a3dd2a..98d1ef2 100644 --- a/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java +++ b/Hydroplane/src/main/java/org/example/FormPlaneCollecltion.java @@ -4,25 +4,38 @@ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.LinkedList; +import java.util.Stack; +import javax.swing.DefaultListModel; import javax.swing.JButton; +import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFrame; +import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JTextField; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; public class FormPlaneCollecltion { private class Canvas extends JComponent{ - - public PlanesGenericCollection _planes; - - public Canvas(){} - + public Canvas(){ + } public void paintComponent (Graphics g){ super.paintComponent(g); - if (_planes.ShowPlanes() != null) { - g.drawImage(_planes.ShowPlanes(), 0, 0, this); + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + var obj = _storage.get(jListStorage.getSelectedValue()); + if (obj == null) + { + return; + } + if (obj.ShowPlanes() != null) { + g.drawImage(obj.ShowPlanes(), 0, 0, this); } super.repaint(); } @@ -33,31 +46,68 @@ public class FormPlaneCollecltion { static int pictureBoxWidth = 800; static int pictureBoxHeight = 580; - private PlanesGenericCollection _planes; + private PlanesGenericStorage _storage; public void Draw(){ canv.repaint(); } + private Stack stackRemoved; + private JList jListStorage; + private DefaultListModel 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); + } + } + FormPlaneCollecltion() { + listModel = new DefaultListModel(); + jListStorage = new JList(listModel); canv = new Canvas(); JFrame w = new JFrame("PlaneCollecltion"); - _planes = new PlanesGenericCollection(pictureBoxWidth, pictureBoxHeight); - canv._planes = _planes; + _storage = new PlanesGenericStorage(pictureBoxWidth, pictureBoxHeight); - JButton ButtonAddPlane = new JButton("ButtonAddPlane"); + JButton ButtonAddPlane = new JButton("AddPlane"); ButtonAddPlane.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + var obj = _storage.get(jListStorage.getSelectedValue()); + if (obj == null) + { + return; + } + FormHydroplane form = new FormHydroplane(); form.buttonSelectPlane.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { - if (_planes.Add(form._drawingPlane) != -1) { + if (obj.Add(form._drawingPlane) != -1) + { JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Объект добавлен"); Draw(); - } else { + } + else + { JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не удалось добавить объект"); } form.w.dispose(); } @@ -67,39 +117,79 @@ public class FormPlaneCollecltion { } ); + stackRemoved = new Stack(); JTextField TextBoxNumber = new JTextField(); - JButton ButtonRemovePlane = new JButton("ButtonRemovePlane"); + JButton ButtonRemovePlane = new JButton("RemovePlane"); ButtonRemovePlane.addActionListener( new ActionListener() { - public void actionPerformed(ActionEvent e) { - if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { + public void actionPerformed(ActionEvent e){ + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + var obj = _storage.get(jListStorage.getSelectedValue()); + if (obj == null) + { + return; + } + + if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) + { return; } for (char it : TextBoxNumber.getText().toCharArray()) - if (it < '0' || it > '9') { + if (it < '0' || it > '9') + { JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не удалось удалить объект"); return; } - if (TextBoxNumber.getText().length() == 0) { + if (TextBoxNumber.getText().length() == 0) + { JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не удалось удалить объект"); return; } int pos = Integer.parseInt(TextBoxNumber.getText()); - if (_planes.remove(pos) != null) { + var removed = obj.remove(pos); + if (removed != null) + { + stackRemoved.add(removed); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Объект удален"); Draw(); - } else { + } + else + { JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не удалось удалить объект"); } } } ); - JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection"); + JButton buttonGetRemoved = new JButton("buttonGetRemoved"); + buttonGetRemoved.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (stackRemoved.size()==0){ + JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Нет удалённых"); + return; + } + System.out.println("Удалённый объект"); + FormHydroplane form = new FormHydroplane(); + form.ChangePlane(stackRemoved.pop()); + form.Draw(); + } + } + ); + + JButton ButtonRefreshCollection = new JButton("Refresh Collection"); ButtonRefreshCollection.addActionListener( new ActionListener() { - public void actionPerformed(ActionEvent e) { + public void actionPerformed(ActionEvent e){ Draw(); } } @@ -108,27 +198,86 @@ public class FormPlaneCollecltion { JButton toForm4GenericDopClass = new JButton("ToForm4GenericDopClass"); toForm4GenericDopClass.addActionListener( new ActionListener() { - public void actionPerformed(ActionEvent e) { - Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass(); + public void actionPerformed(ActionEvent e){ + Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass(); } } ); - w.setSize(1000, 600); - w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + JTextField textBoxSetName = new JTextField(); + JButton buttonAddSet = new JButton("Add Set"); + buttonAddSet.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (textBoxSetName.getText().length() == 0) + { + JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Не все данные заполнены"); + return; + } + _storage.AddSet(textBoxSetName.getText()); + ReloadObjects(); + } + } + ); + + jListStorage.addListSelectionListener( + new ListSelectionListener() { + public void valueChanged(ListSelectionEvent e){ + Draw(); + } + } + ); + + JButton buttonRemoveSet = new JButton("Remove Set"); + 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(); + } + } + ); + + w.setSize (1000, 600); + w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); w.setLayout(null); canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); - ButtonAddPlane.setBounds(pictureBoxWidth, 0, 150, 20); - TextBoxNumber.setBounds(pictureBoxWidth, 30, 150, 20); - ButtonRemovePlane.setBounds(pictureBoxWidth, 60, 150, 20); - ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 150, 20); - toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 150, 20); + ButtonAddPlane.setBounds(pictureBoxWidth, 0, 160, 20); + TextBoxNumber.setBounds(pictureBoxWidth, 30, 160, 20); + ButtonRemovePlane.setBounds(pictureBoxWidth, 60, 160, 20); + ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 160, 20); + toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 160, 20); + + buttonAddSet.setBounds(pictureBoxWidth, 150, 160, 20); + textBoxSetName.setBounds(pictureBoxWidth, 180, 160, 20); + jListStorage.setBounds(pictureBoxWidth, 210, 160, 80); + buttonRemoveSet.setBounds(pictureBoxWidth, 300, 160, 20); + + buttonGetRemoved.setBounds(pictureBoxWidth, 330, 160, 20); + w.add(canv); w.add(ButtonAddPlane); w.add(ButtonRemovePlane); w.add(ButtonRefreshCollection); w.add(TextBoxNumber); w.add(toForm4GenericDopClass); + + w.add(buttonAddSet); + w.add(textBoxSetName); + w.add(jListStorage); + w.add(buttonRemoveSet); + + w.add(buttonGetRemoved); + w.setVisible(true); } } diff --git a/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java b/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java index bf67a62..6578e48 100644 --- a/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java +++ b/Hydroplane/src/main/java/org/example/PlanesGenericCollection.java @@ -75,17 +75,18 @@ public class PlanesGenericCollection> _planeStorages; + + public List Keys(){ + return _planeStorages.keySet().stream().collect(Collectors.toList()); + } + + private int _pictureWidth; + + private int _pictureHeight; + + public PlanesGenericStorage(int pictureWidth, int pictureHeight) + { + _planeStorages = new HashMap>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(String name) + { + if (_planeStorages.containsKey(name)) + return; + _planeStorages.put(name, new PlanesGenericCollection(_pictureWidth, _pictureHeight)); + } + + public void DelSet(String name) + { + if (!_planeStorages.containsKey(name)) + return; + _planeStorages.remove(name); + } + + public PlanesGenericCollection get(String ind) + { + if (_planeStorages.containsKey(ind)) + return _planeStorages.get(ind); + return null; + } + + public DrawningObjectPlane get(String ind1, int ind2){ + if (!_planeStorages.containsKey(ind1)) + return null; + return _planeStorages.get(ind1).GetU(ind2); + } +} diff --git a/Hydroplane/src/main/java/org/example/SetGeneric.java b/Hydroplane/src/main/java/org/example/SetGeneric.java index 4ed3002..99763fe 100644 --- a/Hydroplane/src/main/java/org/example/SetGeneric.java +++ b/Hydroplane/src/main/java/org/example/SetGeneric.java @@ -1,57 +1,89 @@ package org.example; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.NoSuchElementException; public class SetGeneric { - private Object[] _places; + private ArrayList_places; - public int Count; + public int Count () { return _places.size();}; + + private int _maxCount; public SetGeneric(int count) { - _places = new Object[count]; - Count = _places.length; + _maxCount = count; + _places = new ArrayList(count); } public int Insert(T plane) { - if (_places[Count - 1] != null) + if (_places.size() >= _maxCount) return -1; - return Insert(plane, 0); + _places.add(0, plane); + return 0; } - public int Insert(T plane, int position) + public boolean Insert(T plane, int position) { - if (position < 0 || position >= Count) - return -1; - if (_places[position] != null) - { - int indexEnd = position + 1; - while (_places[indexEnd] != null) - { - indexEnd++; - } - for (int i = indexEnd + 1; i > position; i--) - { - _places[i] = _places[i - 1]; - } + if (_places.size() >= _maxCount) + return false; - } - _places[position] = plane; - return position; + if (position < 0 || position > _places.size()) + return false; + + if (position == _places.size()) + _places.add(plane); + else + _places.add(position, plane); + + return true; } public boolean Remove(int position) { - if (position < 0 || position >= _places.length) + 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 >= _places.length) + if (position < 0 || position >= _places.size()) return null; - return (T)_places[position]; + return _places.get(position); + } + + public Iterable GetPlanes(final Integer maxPlanes) { + return new Iterable() { + @Override + public Iterator iterator() { + return new Iterator() { + private int currentIndex = 0; + private int count = 0; + + @Override + public boolean hasNext() { + return currentIndex < _places.size() && (maxPlanes == null || count < maxPlanes); + } + + @Override + public T next() { + if (hasNext()) { + count++; + return _places.get(currentIndex++); + } + throw new NoSuchElementException(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + }; } }