From 947ef24205d136bbbff290e7c2301725b3965fe3 Mon Sep 17 00:00:00 2001 From: AnnaLioness Date: Sat, 4 Nov 2023 19:41:05 +0400 Subject: [PATCH 1/3] change --- DrawingContainerShip.java | 34 ++++----- DrawingShip.java | 1 + FormContainerShip.java | 47 ++++--------- FormShipCollection.java | 138 ++++++++++++++++++++++++++++++++++--- SetGeneric.java | 127 ++++++++++++++++------------------ ShipGenericCollection.java | 11 +-- ShipGenericStorage.java | 86 +++++++++++++++++++++++ 7 files changed, 313 insertions(+), 131 deletions(-) create mode 100644 ShipGenericStorage.java diff --git a/DrawingContainerShip.java b/DrawingContainerShip.java index 07a3750..50906e5 100644 --- a/DrawingContainerShip.java +++ b/DrawingContainerShip.java @@ -6,26 +6,26 @@ public class DrawingContainerShip extends DrawingShip{ public DrawingContainerShip(int speed, double weight, Color bodyColor, Color additionalColor, boolean crane, boolean containers,int deck, int deckType, int width, int height) + { + super(speed, weight, bodyColor, width, height, 110, 65); + if (EntityShip != null) { - super(speed, weight, bodyColor, width, height, 110, 65); - if (EntityShip != null) - { - EntityShip = new EntityContainerShip(speed, weight, bodyColor, - additionalColor, crane, containers,deck, deckType); - - - if(deckType == 1){ - iDecksDrawing = new DrawingDecks(); - } - if(deckType == 2){ - iDecksDrawing = new DrawingDecksTrapez(); - } - if(deckType == 3){ - iDecksDrawing = new DrawingDecksRect(); - } - iDecksDrawing.setNumDecks(deck); + EntityShip = new EntityContainerShip(speed, weight, bodyColor, + additionalColor, crane, containers,deck, deckType); + + + if(deckType == 1){ + iDecksDrawing = new DrawingDecks(); } + if(deckType == 2){ + iDecksDrawing = new DrawingDecksTrapez(); + } + if(deckType == 3){ + iDecksDrawing = new DrawingDecksRect(); + } + iDecksDrawing.setNumDecks(deck); } + } public DrawingContainerShip(EntityContainerShip ship, IDecksDrawing decks, int width, int height){ super(ship,width, height); if(width < _pictureWidth || height < _pictureHeight){ diff --git a/DrawingShip.java b/DrawingShip.java index 8eee8f2..725cb40 100644 --- a/DrawingShip.java +++ b/DrawingShip.java @@ -72,6 +72,7 @@ public class DrawingShip { _pictureHeight = height; _shipHeight = shipHeight; _shipWidth = shipWidth; + EntityShip = new EntityShip(speed, weight, bodyColor); } diff --git a/FormContainerShip.java b/FormContainerShip.java index bac5db6..6b6386b 100644 --- a/FormContainerShip.java +++ b/FormContainerShip.java @@ -7,21 +7,21 @@ public class FormContainerShip{ private AbstractStrategy _abstractStrategy; Canvas canv; class Canvas extends JComponent{ - public DrawingShip _drawingShip; - - - public Canvas(){ - } - public void paintComponent (Graphics g){ - if (_drawingShip == null){ - return; + public DrawingShip _drawingShip; + + + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingShip == null){ + return; + } + super.paintComponents (g) ; + Graphics2D g2d = (Graphics2D)g; + _drawingShip.DrawShip(g2d); + super.repaint(); } - super.paintComponents (g) ; - Graphics2D g2d = (Graphics2D)g; - _drawingShip.DrawShip(g2d); - super.repaint(); } -} public DrawingShip SelectedShip; public boolean DialogResult = false; public JButton buttonSelectedShip; @@ -101,10 +101,7 @@ public class FormContainerShip{ color2 = selectedColor2; } _drawingShip = new DrawingContainerShip(random.nextInt(100, 300), - random.nextInt(1000, 3000), - color, - color2, - random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4),random.nextInt(1,4),960, 560); + random.nextInt(1000, 3000),color,color2,random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4),random.nextInt(1,4),960, 560); _drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100)); canv._drawingShip = _drawingShip; @@ -213,18 +210,4 @@ public class FormContainerShip{ w.setVisible (true); } } -class Canvas extends JComponent{ - public DrawingShip _drawingShip; - - public Canvas(){ - } - public void paintComponent (Graphics g){ - if (_drawingShip == null){ - return; - } - super.paintComponents (g) ; - Graphics2D g2d = (Graphics2D)g; - _drawingShip.DrawShip(g2d); - super.repaint(); - } -} + diff --git a/FormShipCollection.java b/FormShipCollection.java index 7d37b1d..adc98de 100644 --- a/FormShipCollection.java +++ b/FormShipCollection.java @@ -1,44 +1,89 @@ import java.awt.*; import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; + import java.awt.event.*; +import java.util.Stack; public class FormShipCollection { class Canvas extends JComponent{ - public ShipGenericCollection _ships; public Canvas(){} public void paintComponent(Graphics g){ super.paintComponent(g); - if(_ships.ShowShips() != null){ - g.drawImage(_ships.ShowShips(), 0, 0, this); + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + var obj = _storage.get(jListStorage.getSelectedValue()); + if (obj == null) + { + return; + } + if(obj.ShowShips() != null){ + g.drawImage(obj.ShowShips(), 0, 0, this); } super.repaint(); } } Canvas canv; - static int pictureBoxWidth = 860; + static int pictureBoxWidth = 820; static int pictureBoxHeight = 580; - private ShipGenericCollection _ships; + private Stack removedShips; + private ShipGenericStorage _storage; + 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); + } + } public void Draw(){ canv.repaint(); } FormShipCollection(){ canv = new Canvas(); JFrame w = new JFrame("FormShipCollection"); - _ships = new ShipGenericCollection(pictureBoxWidth, pictureBoxHeight); - canv._ships = _ships; + _storage = new ShipGenericStorage(pictureBoxWidth, pictureBoxHeight); + listModel = new DefaultListModel(); + jListStorage = new JList(listModel); JButton ButtonAddShip = new JButton("Добавить кораблик"); JButton ButtonDeleteShip = new JButton("Удалить кораблик"); JButton ButtonRefreshCollection = new JButton("Обновить коллекцию"); JButton forNewForm = new JButton("новая форма"); + JButton buttonGetRemoved = new JButton("Получить удалённое"); + JButton buttonAddSet = new JButton("Добавить коллекцию"); + JButton buttonRemoveSet = new JButton("Удалить коллекцию"); + JTextField textBoxSetName = new JTextField(); JTextField TextBoxNumber = new JTextField(); ButtonAddShip.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + var obj = _storage.get(jListStorage.getSelectedValue()); + if (obj == null) + { + return; + } FormContainerShip form = new FormContainerShip(); form.buttonSelectedShip.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ - if(_ships.Add(form._drawingShip) != -1){ + if(obj.Add(form._drawingShip) != -1){ JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); Draw(); } @@ -61,17 +106,29 @@ public class FormShipCollection { } } ); - + removedShips = new Stack(); ButtonDeleteShip.addActionListener( new ActionListener() { 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; } int pos = Integer.parseInt(TextBoxNumber.getText()); - if (_ships.remove(pos) != null) + var rem = obj.remove(pos); + if (rem != null) { + removedShips.push(rem); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); Draw(); @@ -84,6 +141,20 @@ public class FormShipCollection { } } ); + buttonGetRemoved.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (removedShips.empty()){ + JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + FormContainerShip form = new FormContainerShip(); + form._drawingShip = removedShips.peek(); + removedShips.pop(); + form.Draw(); + } + } + ); ButtonRefreshCollection.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ @@ -91,6 +162,42 @@ public class FormShipCollection { } } ); + buttonAddSet.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (textBoxSetName.getText().length() == 0) + { + JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE); + return; + } + _storage.AddSet(textBoxSetName.getText()); + ReloadObjects(); + } + } + ); + jListStorage.addListSelectionListener( + new ListSelectionListener() { + public void valueChanged(ListSelectionEvent e){ + Draw(); + } + } + ); + 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); @@ -100,12 +207,23 @@ public class FormShipCollection { ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 120, 20); ButtonRefreshCollection.setBounds(pictureBoxWidth, 120, 120, 20); forNewForm.setBounds(pictureBoxWidth, 150, 120, 20); + + buttonAddSet.setBounds(pictureBoxWidth, 180, 120, 20); + textBoxSetName.setBounds(pictureBoxWidth, 210, 120, 20); + jListStorage.setBounds(pictureBoxWidth, 240, 120, 60); + buttonRemoveSet.setBounds(pictureBoxWidth, 310, 120, 20); + buttonGetRemoved.setBounds(pictureBoxWidth, 340, 120, 20); w.add(canv); w.add(ButtonAddShip); w.add(ButtonDeleteShip); w.add(ButtonRefreshCollection); w.add(forNewForm); w.add(TextBoxNumber); + w.add(buttonAddSet); + w.add(textBoxSetName); + w.add(jListStorage); + w.add(buttonRemoveSet); + w.add(buttonGetRemoved); w.setVisible(true); } } diff --git a/SetGeneric.java b/SetGeneric.java index 0a1345e..bf306e5 100644 --- a/SetGeneric.java +++ b/SetGeneric.java @@ -1,47 +1,29 @@ +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();}; + public int _maxCount; public SetGeneric(int count) { - _places = new Object[count]; - Count = _places.length; + _maxCount = count; + _places = new ArrayList(count); } public int Insert(T ship) { - // TODO вставка в начало набора - int temp = 0; - int k = 0; - for(int j = 0; j < _places.length; j++) { - if (_places[j] != null) - { - k++; - } - } - if (k == _places.length) + if(_places.size() >= _maxCount) { return -1; } else { - for (int i = 0; i < _places.length; i++) - { - if (_places[i] == null) - { - temp = i; - break; - } - - } - for (int i = temp; i > 0; i--) - { - _places[i] = _places[i -1]; - - } - _places[0] = ship; + _places.add(0, ship); return 0; } } @@ -49,50 +31,31 @@ public class SetGeneric public boolean Insert(T ship, int position) { - int temp = 0; - int k = 0; - for (int j = position; j < _places.length; j++) - { - if (_places[j] != null) - { - k++; - } - } - if (position < _places.length && k < _places.length-position) - { - for (int i = position; i < _places.length; i++) - { - if (_places[i] == null) - { - temp = i; - break; - } - - } - for (int i = temp; i > position; i--) - { - _places[i] = _places[i - 1]; - - } - - _places[position] = ship; - return true; - } - else + if(_places.size() >= _maxCount) { return false; } + if(position < 0 || position > _places.size()) + { + return false; + } + if(position == _places.size()) + { + _places.add(ship); + } + else + { + _places.add(position, ship); + } + return true; } public boolean Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присвоив элементу массива - //значение null - if(position < _places.length) + if(position < _places.size() && _places.size() < _maxCount) { - _places[position] = null; + _places.remove(position); return true; } else @@ -100,19 +63,49 @@ public class SetGeneric return false; } + } public T Get(int position) { - // TODO проверка позиции - if(position < _places.length) + if(position < _places.size() && position >= 0) { - return (T)_places[position]; + return _places.get(position); } else { return null; } } + public Iterable GetShips(final Integer maxShips) { + 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() && (maxShips == null || count < maxShips); + } + + @Override + public T next() { + if (hasNext()) { + count++; + return _places.get(currentIndex++); + } + throw new NoSuchElementException(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + }; + } } diff --git a/ShipGenericCollection.java b/ShipGenericCollection.java index 1dc692b..385ceb4 100644 --- a/ShipGenericCollection.java +++ b/ShipGenericCollection.java @@ -72,14 +72,15 @@ public class ShipGenericCollection> _shipStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys() {return _shipStorages.keySet().stream().collect(Collectors.toList());} + /// + /// Ширина окна отрисовки + /// + private int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public ShipGenericStorage(int pictureWidth, int pictureHeight) + { + _shipStorages = new HashMap>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(String name) + { + + if (_shipStorages.containsKey(name)) + { + return; + } + else + { + _shipStorages.put(name, new ShipGenericCollection(_pictureWidth, _pictureHeight)); + } + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(String name) + { + if (!_shipStorages.containsKey(name)) + { + return; + } + else + { + _shipStorages.remove(name); + } + + } + /// + /// Доступ к набору + /// + /// + /// + public ShipGenericCollection get(String ind) + { + if (_shipStorages.containsKey(ind)) + { + return _shipStorages.get(ind); + } + else + { + return null; + } + } + public DrawningObjectShip get(String ind1, int ind2){ + if (!_shipStorages.containsKey(ind1)) + return null; + return _shipStorages.get(ind1).GetU(ind2); + } + +} -- 2.25.1 From 7770c539ae3c738266ff15637151f695a0e5b99e Mon Sep 17 00:00:00 2001 From: AnnaLioness Date: Sat, 4 Nov 2023 20:38:51 +0400 Subject: [PATCH 2/3] ready --- FormContainerShip.java | 3 --- FormShipCollection.java | 1 + ShipGenericCollection.java | 6 ++---- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/FormContainerShip.java b/FormContainerShip.java index 6b6386b..4cf8a2b 100644 --- a/FormContainerShip.java +++ b/FormContainerShip.java @@ -7,7 +7,6 @@ public class FormContainerShip{ private AbstractStrategy _abstractStrategy; Canvas canv; class Canvas extends JComponent{ - public DrawingShip _drawingShip; public Canvas(){ @@ -80,7 +79,6 @@ public class FormContainerShip{ color,960, 560); _drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100)); - canv._drawingShip = _drawingShip; Draw(); } } @@ -104,7 +102,6 @@ public class FormContainerShip{ random.nextInt(1000, 3000),color,color2,random.nextBoolean(), random.nextBoolean(), random.nextInt(1,4),random.nextInt(1,4),960, 560); _drawingShip.SetPosition(random.nextInt(10, 100),random.nextInt(10, 100)); - canv._drawingShip = _drawingShip; Draw(); } } diff --git a/FormShipCollection.java b/FormShipCollection.java index adc98de..381f0ab 100644 --- a/FormShipCollection.java +++ b/FormShipCollection.java @@ -150,6 +150,7 @@ public class FormShipCollection { } FormContainerShip form = new FormContainerShip(); form._drawingShip = removedShips.peek(); + form._drawingShip.SetPosition(100, 100); removedShips.pop(); form.Draw(); } diff --git a/ShipGenericCollection.java b/ShipGenericCollection.java index 385ceb4..fe1d607 100644 --- a/ShipGenericCollection.java +++ b/ShipGenericCollection.java @@ -72,18 +72,16 @@ public class ShipGenericCollection Date: Tue, 7 Nov 2023 16:05:40 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=BC=D0=B5=D0=BB=D0=BA=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FormContainerShip.java | 1 - FormShipCollection.java | 23 +++++++++++------------ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/FormContainerShip.java b/FormContainerShip.java index 4cf8a2b..111191d 100644 --- a/FormContainerShip.java +++ b/FormContainerShip.java @@ -181,7 +181,6 @@ public class FormContainerShip{ right.addActionListener(actioListener); w.setSize (1000, 600); - w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); w.setLayout(null); canv = new Canvas(); canv.setBounds(0, 0, 1000, 600); diff --git a/FormShipCollection.java b/FormShipCollection.java index 381f0ab..3f4b540 100644 --- a/FormShipCollection.java +++ b/FormShipCollection.java @@ -149,9 +149,8 @@ public class FormShipCollection { return; } FormContainerShip form = new FormContainerShip(); - form._drawingShip = removedShips.peek(); + form._drawingShip = removedShips.pop(); form._drawingShip.SetPosition(100, 100); - removedShips.pop(); form.Draw(); } } @@ -203,17 +202,17 @@ public class FormShipCollection { w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); w.setLayout(null); canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight); - ButtonAddShip.setBounds(pictureBoxWidth, 0, 120, 20); - TextBoxNumber.setBounds(pictureBoxWidth, 50, 120, 20); - ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 120, 20); - ButtonRefreshCollection.setBounds(pictureBoxWidth, 120, 120, 20); - forNewForm.setBounds(pictureBoxWidth, 150, 120, 20); + ButtonAddShip.setBounds(pictureBoxWidth, 0, 130, 20); + TextBoxNumber.setBounds(pictureBoxWidth, 50, 130, 20); + ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 130, 20); + ButtonRefreshCollection.setBounds(pictureBoxWidth, 130, 120, 20); + forNewForm.setBounds(pictureBoxWidth, 150, 130, 20); - buttonAddSet.setBounds(pictureBoxWidth, 180, 120, 20); - textBoxSetName.setBounds(pictureBoxWidth, 210, 120, 20); - jListStorage.setBounds(pictureBoxWidth, 240, 120, 60); - buttonRemoveSet.setBounds(pictureBoxWidth, 310, 120, 20); - buttonGetRemoved.setBounds(pictureBoxWidth, 340, 120, 20); + buttonAddSet.setBounds(pictureBoxWidth, 180, 130, 20); + textBoxSetName.setBounds(pictureBoxWidth, 210, 130, 20); + jListStorage.setBounds(pictureBoxWidth, 240, 130, 60); + buttonRemoveSet.setBounds(pictureBoxWidth, 310, 130, 20); + buttonGetRemoved.setBounds(pictureBoxWidth, 340, 130, 20); w.add(canv); w.add(ButtonAddShip); w.add(ButtonDeleteShip); -- 2.25.1