From accd1149e2dc02e06ae81abe0fdf3ce16f808239 Mon Sep 17 00:00:00 2001 From: Timourka Date: Tue, 24 Oct 2023 02:19:14 +0400 Subject: [PATCH 1/5] 4Base --- laba1Loco/FormTrain.java | 1 - laba1Loco/FormTrainCollecltion.java | 146 ++++++++++++++++++++++--- laba1Loco/SetGeneric.java | 67 ++++++------ laba1Loco/TrainsGenericCollection.java | 2 +- laba1Loco/TrainsGenericStorage.java | 66 +++++++++++ 5 files changed, 230 insertions(+), 52 deletions(-) create mode 100644 laba1Loco/TrainsGenericStorage.java diff --git a/laba1Loco/FormTrain.java b/laba1Loco/FormTrain.java index 8786420..c50ed47 100644 --- a/laba1Loco/FormTrain.java +++ b/laba1Loco/FormTrain.java @@ -3,7 +3,6 @@ package laba1Loco; import java.awt.*; import java.util.*; import javax.swing.*; -import javax.swing.Timer; import java.awt.event.*; public class FormTrain{ diff --git a/laba1Loco/FormTrainCollecltion.java b/laba1Loco/FormTrainCollecltion.java index b61cd46..933a71c 100644 --- a/laba1Loco/FormTrainCollecltion.java +++ b/laba1Loco/FormTrainCollecltion.java @@ -5,47 +5,94 @@ import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +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 FormTrainCollecltion { private class Canvas extends JComponent{ - public TrainsGenericCollection _trains; public Canvas(){ } public void paintComponent (Graphics g){ super.paintComponent(g); - if (_trains.ShowTrains() != null) { - g.drawImage(_trains.ShowTrains(), 0, 0, this); + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + var obj = _storage.get(jListStorage.getSelectedValue()); + if (obj == null) + { + return; + } + if (obj.ShowTrains() != null) { + g.drawImage(obj.ShowTrains(), 0, 0, this); } super.repaint(); } } Canvas canv; - static int pictureBoxWidth = 860; + static int pictureBoxWidth = 820; static int pictureBoxHeight = 580; - private TrainsGenericCollection _trains; + private TrainsGenericStorage _storage; public void Draw(){ canv.repaint(); } + + private JList jListStorage; + private DefaultListModel listModel; + /// + /// Заполнение listBoxObjects + /// + 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); + } + } + FormTrainCollecltion(){ + listModel = new DefaultListModel(); + jListStorage = new JList(listModel); canv = new Canvas(); JFrame w = new JFrame ("TrainCollecltion"); - _trains = new TrainsGenericCollection(pictureBoxWidth, pictureBoxHeight); - canv._trains = _trains; + _storage = new TrainsGenericStorage(pictureBoxWidth, pictureBoxHeight); - JButton ButtonAddTrain = new JButton("ButtonAddTrain"); + JButton ButtonAddTrain = new JButton("Add Train"); ButtonAddTrain.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ + if (jListStorage.getSelectedIndex() == -1) + { + return; + } + var obj = _storage.get(jListStorage.getSelectedValue()); + if (obj == null) + { + return; + } + FormTrain form = new FormTrain(); form.buttonSelectTrain.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ - if (_trains.Add(form._drawingTrain) != -1) + if (obj.Add(form._drawingTrain) != -1) { JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE); System.out.println("Объект добавлен"); @@ -65,10 +112,20 @@ public class FormTrainCollecltion { ); JTextField TextBoxNumber = new JTextField(); - JButton ButtonRemoveTrain = new JButton("ButtonRemoveTrain"); + JButton ButtonRemoveTrain = new JButton("Remove Train"); ButtonRemoveTrain.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; @@ -88,7 +145,7 @@ public class FormTrainCollecltion { } int pos = Integer.parseInt(TextBoxNumber.getText()); - if (_trains.remove(pos) != null) + if (obj.remove(pos) != null) { JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); System.out.println("Объект удален"); @@ -103,7 +160,7 @@ public class FormTrainCollecltion { } ); - JButton ButtonRefreshCollection = new JButton("ButtonRefreshCollection"); + JButton ButtonRefreshCollection = new JButton("Refresh Collection"); ButtonRefreshCollection.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e){ @@ -121,21 +178,76 @@ public class FormTrainCollecltion { } ); + 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); - ButtonAddTrain.setBounds(pictureBoxWidth, 0, 120, 20); - TextBoxNumber.setBounds(pictureBoxWidth, 30, 120, 20); - ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 120, 20); - ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 120, 20); - toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 120, 20); + ButtonAddTrain.setBounds(pictureBoxWidth, 0, 160, 20); + TextBoxNumber.setBounds(pictureBoxWidth, 30, 160, 20); + ButtonRemoveTrain.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); + w.add(canv); w.add(ButtonAddTrain); w.add(ButtonRemoveTrain); w.add(ButtonRefreshCollection); w.add(TextBoxNumber); w.add(toForm4GenericDopClass); + + w.add(buttonAddSet); + w.add(textBoxSetName); + w.add(jListStorage); + w.add(buttonRemoveSet); + w.setVisible(true); } } diff --git a/laba1Loco/SetGeneric.java b/laba1Loco/SetGeneric.java index 04d1c55..1583f6c 100644 --- a/laba1Loco/SetGeneric.java +++ b/laba1Loco/SetGeneric.java @@ -1,22 +1,29 @@ package laba1Loco; +import java.util.ArrayList; +import java.util.Iterator; + 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); } /// /// Добавление объекта в набор @@ -25,20 +32,10 @@ public class SetGeneric { /// public int Insert(T train) { - int i = 0; - for (;i < _places.length; i++) - { - if (_places[i] == null) - break; - } - if (i == _places.length) + if (_places.size() >= _maxCount) return -1; - for (; i > 0; i--) - { - _places[i] = _places[i - 1]; - } - _places[i] = train; - return i; + _places.add(0, train); + return 0; } /// /// Добавление объекта в набор на конкретную позицию @@ -48,20 +45,17 @@ public class SetGeneric { /// public boolean Insert(T train, int position) { - if (position < 0 || position >= _places.length) + if (_places.size() >= _maxCount) return false; - for (; position < _places.length; position++) - { - if (_places[position] == null) - break; - } - if (position == _places.length) + + if (position < 0 || position > _places.size()) return false; - for (; position > 0; position--) - { - _places[position] = _places[position - 1]; - } - _places[position] = train; + + if (position == _places.size()) + _places.add(train); + else + _places.add(position, train); + return true; } /// @@ -71,9 +65,9 @@ public class SetGeneric { /// 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; } /// @@ -83,8 +77,15 @@ public class SetGeneric { /// 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 Iterator iterator() { + return _places.iterator(); } } diff --git a/laba1Loco/TrainsGenericCollection.java b/laba1Loco/TrainsGenericCollection.java index 3f652ed..23324ad 100644 --- a/laba1Loco/TrainsGenericCollection.java +++ b/laba1Loco/TrainsGenericCollection.java @@ -112,7 +112,7 @@ public class TrainsGenericCollection private void DrawObjects(Graphics2D g) { - for (int i = 0; i < _collection.Count; i++) + for (int i = 0; i < _collection.Count(); i++) { T t = _collection.Get(i); if (t != null) diff --git a/laba1Loco/TrainsGenericStorage.java b/laba1Loco/TrainsGenericStorage.java new file mode 100644 index 0000000..715133b --- /dev/null +++ b/laba1Loco/TrainsGenericStorage.java @@ -0,0 +1,66 @@ +package laba1Loco; + +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + +public class TrainsGenericStorage { + /// + /// Словарь (хранилище) + /// + HashMap> _carStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys(){return _carStorages.keySet().stream().collect(Collectors.toList());} + /// + /// Ширина окна отрисовки + /// + private int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public TrainsGenericStorage(int pictureWidth, int pictureHeight) + { + _carStorages = new HashMap>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(String name) + { + if (_carStorages.containsKey(name)) + return; + _carStorages.put(name, new TrainsGenericCollection(_pictureWidth, _pictureHeight)); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(String name) + { + if (!_carStorages.containsKey(name)) + return; + _carStorages.remove(name); + } + /// + /// Доступ к набору + /// + /// + /// + public TrainsGenericCollection get(String ind) + { + if (_carStorages.containsKey(ind)) + return _carStorages.get(ind); + return null; + } +} -- 2.25.1 From c70b820dbe2bb0aae695617b5ad6b3b0455a98e4 Mon Sep 17 00:00:00 2001 From: Timourka Date: Tue, 24 Oct 2023 02:41:12 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D1=80=D1=8D=D0=B9=D0=BD=D0=B6=D0=91=D1=8D=D0=B9=D0=B7=D0=B4?= =?UTF-8?q?=D0=A6=D0=B8=D0=BA=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- laba1Loco/SetGeneric.java | 190 ++++++++++++++----------- laba1Loco/TrainsGenericCollection.java | 15 +- 2 files changed, 118 insertions(+), 87 deletions(-) diff --git a/laba1Loco/SetGeneric.java b/laba1Loco/SetGeneric.java index 1583f6c..d592135 100644 --- a/laba1Loco/SetGeneric.java +++ b/laba1Loco/SetGeneric.java @@ -2,90 +2,120 @@ package laba1Loco; import java.util.ArrayList; import java.util.Iterator; +import java.util.NoSuchElementException; public class SetGeneric { /// - /// Массив объектов, которые храним - /// - private ArrayList_places; - /// - /// Количество объектов в массиве - /// - public int Count () { return _places.size();}; - /// - /// Максимальное количество объектов в списке - /// - private int _maxCount; - /// - /// Конструктор - /// - /// - public SetGeneric(int count) - { - _maxCount = count; - _places = new ArrayList(count); - } - /// - /// Добавление объекта в набор - /// - /// Добавляемый поезд - /// - public int Insert(T train) - { - if (_places.size() >= _maxCount) - return -1; - _places.add(0, train); - return 0; - } - /// - /// Добавление объекта в набор на конкретную позицию - /// - /// Добавляемый поезд - /// Позиция - /// - public boolean Insert(T train, int position) - { - if (_places.size() >= _maxCount) - return false; + /// Массив объектов, которые храним + /// + private ArrayList_places; + /// + /// Количество объектов в массиве + /// + public int Count () { return _places.size();}; + /// + /// Максимальное количество объектов в списке + /// + private int _maxCount; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _maxCount = count; + _places = new ArrayList(count); + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый поезд + /// + public int Insert(T train) + { + if (_places.size() >= _maxCount) + return -1; + _places.add(0, train); + return 0; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый поезд + /// Позиция + /// + public boolean Insert(T train, int position) + { + if (_places.size() >= _maxCount) + return false; - if (position < 0 || position > _places.size()) - return false; + if (position < 0 || position > _places.size()) + return false; - if (position == _places.size()) - _places.add(train); - else - _places.add(position, train); + if (position == _places.size()) + _places.add(train); + else + _places.add(position, train); - return true; - } - /// - /// Удаление объекта из набора с конкретной позиции - /// - /// - /// - public boolean Remove(int position) - { - if (position < 0 || position >= _places.size()) - return false; - _places.remove(position); - return true; - } - /// - /// Получение объекта из набора по позиции - /// - /// - /// - public T Get(int position) - { - if (position < 0 || position >= _places.size()) - return null; - return _places.get(position); - } - /// - /// Проход по списку - /// - /// - public Iterator iterator() { - return _places.iterator(); - } + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public boolean Remove(int position) + { + if (position < 0 || position >= _places.size()) + return false; + _places.remove(position); + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position < 0 || position >= _places.size()) + return null; + return _places.get(position); + } + + /// + /// Проход по списку + /// + /// + + public Iterable GetTrains(final Integer maxTrains) { + 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() && (maxTrains == null || count < maxTrains); + } + + @Override + public T next() { + if (hasNext()) { + count++; + return _places.get(currentIndex++); + } + throw new NoSuchElementException(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + }; + } } diff --git a/laba1Loco/TrainsGenericCollection.java b/laba1Loco/TrainsGenericCollection.java index 23324ad..12461bc 100644 --- a/laba1Loco/TrainsGenericCollection.java +++ b/laba1Loco/TrainsGenericCollection.java @@ -112,17 +112,18 @@ public class TrainsGenericCollection private void DrawObjects(Graphics2D g) { - for (int i = 0; i < _collection.Count(); i++) + int i = 0; + for (T train : _collection.GetTrains(100)) { - T t = _collection.Get(i); - if (t != null) + if (train != null) { - t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); - if (t instanceof DrawingLoco) - ((DrawingLoco) t).DrawTransport(g); + train.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + if (train instanceof DrawingLoco) + ((DrawingLoco)train).DrawTransport(g); else - t.DrawTransport(g); + train.DrawTransport(g); } + i++; } } } -- 2.25.1 From b96bc6dcad534d7d28cdb9cff869dfdc7c1d643c Mon Sep 17 00:00:00 2001 From: Timourka Date: Tue, 24 Oct 2023 11:34:16 +0400 Subject: [PATCH 3/5] Uraaaaa --- laba1Loco/FormTrain.java | 3 --- laba1Loco/FormTrainCollecltion.java | 30 +++++++++++++++++++++++++++-- laba1Loco/TrainsGenericStorage.java | 24 ++++++++++++++--------- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/laba1Loco/FormTrain.java b/laba1Loco/FormTrain.java index c50ed47..2cc367c 100644 --- a/laba1Loco/FormTrain.java +++ b/laba1Loco/FormTrain.java @@ -7,7 +7,6 @@ import java.awt.event.*; public class FormTrain{ private class Canvas extends JComponent{ - public DrawingTrain _drawingTrain; public Canvas(){ } public void paintComponent (Graphics g){ @@ -147,7 +146,6 @@ public class FormTrain{ pictureBoxWidth, pictureBoxHeight); _drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); - canv._drawingTrain = _drawingTrain; Draw(); } } @@ -172,7 +170,6 @@ public class FormTrain{ pictureBoxWidth, pictureBoxHeight); _drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); - canv._drawingTrain = _drawingTrain; Draw(); } } diff --git a/laba1Loco/FormTrainCollecltion.java b/laba1Loco/FormTrainCollecltion.java index 933a71c..55817e6 100644 --- a/laba1Loco/FormTrainCollecltion.java +++ b/laba1Loco/FormTrainCollecltion.java @@ -4,6 +4,7 @@ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.LinkedList; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -45,6 +46,7 @@ public class FormTrainCollecltion { canv.repaint(); } + private LinkedList linkedListRemoved; private JList jListStorage; private DefaultListModel listModel; /// @@ -110,7 +112,8 @@ public class FormTrainCollecltion { } } ); - + + linkedListRemoved = new LinkedList(); JTextField TextBoxNumber = new JTextField(); JButton ButtonRemoveTrain = new JButton("Remove Train"); ButtonRemoveTrain.addActionListener( @@ -145,8 +148,10 @@ public class FormTrainCollecltion { } int pos = Integer.parseInt(TextBoxNumber.getText()); - if (obj.remove(pos) != null) + var removed = obj.remove(pos); + if (removed != null) { + linkedListRemoved.add(removed); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); System.out.println("Объект удален"); Draw(); @@ -160,6 +165,23 @@ public class FormTrainCollecltion { } ); + JButton buttonGetRemoved = new JButton("buttonGetRemoved"); + buttonGetRemoved.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (linkedListRemoved.size()==0){ + JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Нет удалённых"); + return; + } + FormTrain form = new FormTrain(); + form._drawingTrain = linkedListRemoved.getLast(); + linkedListRemoved.removeLast(); + form.Draw(); + } + } + ); + JButton ButtonRefreshCollection = new JButton("Refresh Collection"); ButtonRefreshCollection.addActionListener( new ActionListener() { @@ -236,6 +258,8 @@ public class FormTrainCollecltion { jListStorage.setBounds(pictureBoxWidth, 210, 160, 80); buttonRemoveSet.setBounds(pictureBoxWidth, 300, 160, 20); + buttonGetRemoved.setBounds(pictureBoxWidth, 330, 160, 20); + w.add(canv); w.add(ButtonAddTrain); w.add(ButtonRemoveTrain); @@ -248,6 +272,8 @@ public class FormTrainCollecltion { w.add(jListStorage); w.add(buttonRemoveSet); + w.add(buttonGetRemoved); + w.setVisible(true); } } diff --git a/laba1Loco/TrainsGenericStorage.java b/laba1Loco/TrainsGenericStorage.java index 715133b..114a486 100644 --- a/laba1Loco/TrainsGenericStorage.java +++ b/laba1Loco/TrainsGenericStorage.java @@ -8,11 +8,11 @@ public class TrainsGenericStorage { /// /// Словарь (хранилище) /// - HashMap> _carStorages; + HashMap> _trainStorages; /// /// Возвращение списка названий наборов /// - public List Keys(){return _carStorages.keySet().stream().collect(Collectors.toList());} + public List Keys(){return _trainStorages.keySet().stream().collect(Collectors.toList());} /// /// Ширина окна отрисовки /// @@ -28,7 +28,7 @@ public class TrainsGenericStorage { /// public TrainsGenericStorage(int pictureWidth, int pictureHeight) { - _carStorages = new HashMap>(); + _trainStorages = new HashMap>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -38,9 +38,9 @@ public class TrainsGenericStorage { /// Название набора public void AddSet(String name) { - if (_carStorages.containsKey(name)) + if (_trainStorages.containsKey(name)) return; - _carStorages.put(name, new TrainsGenericCollection(_pictureWidth, _pictureHeight)); + _trainStorages.put(name, new TrainsGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -48,9 +48,9 @@ public class TrainsGenericStorage { /// Название набора public void DelSet(String name) { - if (!_carStorages.containsKey(name)) + if (!_trainStorages.containsKey(name)) return; - _carStorages.remove(name); + _trainStorages.remove(name); } /// /// Доступ к набору @@ -59,8 +59,14 @@ public class TrainsGenericStorage { /// public TrainsGenericCollection get(String ind) { - if (_carStorages.containsKey(ind)) - return _carStorages.get(ind); + if (_trainStorages.containsKey(ind)) + return _trainStorages.get(ind); return null; } + + public DrawningObjectTrain get(String ind1, int ind2){ + if (!_trainStorages.containsKey(ind1)) + return null; + return _trainStorages.get(ind1).GetU(ind2); + } } -- 2.25.1 From 4db9d1e1622b2bab41814a736aea4c3177aff304 Mon Sep 17 00:00:00 2001 From: Timourka Date: Sun, 5 Nov 2023 01:47:52 +0400 Subject: [PATCH 4/5] Uraaaaa --- laba1Loco/FormTrain.java | 3 --- laba1Loco/FormTrainCollecltion.java | 30 +++++++++++++++++++++++++++-- laba1Loco/TrainsGenericStorage.java | 24 ++++++++++++++--------- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/laba1Loco/FormTrain.java b/laba1Loco/FormTrain.java index c50ed47..2cc367c 100644 --- a/laba1Loco/FormTrain.java +++ b/laba1Loco/FormTrain.java @@ -7,7 +7,6 @@ import java.awt.event.*; public class FormTrain{ private class Canvas extends JComponent{ - public DrawingTrain _drawingTrain; public Canvas(){ } public void paintComponent (Graphics g){ @@ -147,7 +146,6 @@ public class FormTrain{ pictureBoxWidth, pictureBoxHeight); _drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); - canv._drawingTrain = _drawingTrain; Draw(); } } @@ -172,7 +170,6 @@ public class FormTrain{ pictureBoxWidth, pictureBoxHeight); _drawingTrain.SetPosition(random.nextInt(10, 100), random.nextInt(10, 100)); - canv._drawingTrain = _drawingTrain; Draw(); } } diff --git a/laba1Loco/FormTrainCollecltion.java b/laba1Loco/FormTrainCollecltion.java index 933a71c..55817e6 100644 --- a/laba1Loco/FormTrainCollecltion.java +++ b/laba1Loco/FormTrainCollecltion.java @@ -4,6 +4,7 @@ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.LinkedList; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -45,6 +46,7 @@ public class FormTrainCollecltion { canv.repaint(); } + private LinkedList linkedListRemoved; private JList jListStorage; private DefaultListModel listModel; /// @@ -110,7 +112,8 @@ public class FormTrainCollecltion { } } ); - + + linkedListRemoved = new LinkedList(); JTextField TextBoxNumber = new JTextField(); JButton ButtonRemoveTrain = new JButton("Remove Train"); ButtonRemoveTrain.addActionListener( @@ -145,8 +148,10 @@ public class FormTrainCollecltion { } int pos = Integer.parseInt(TextBoxNumber.getText()); - if (obj.remove(pos) != null) + var removed = obj.remove(pos); + if (removed != null) { + linkedListRemoved.add(removed); JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE); System.out.println("Объект удален"); Draw(); @@ -160,6 +165,23 @@ public class FormTrainCollecltion { } ); + JButton buttonGetRemoved = new JButton("buttonGetRemoved"); + buttonGetRemoved.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent e){ + if (linkedListRemoved.size()==0){ + JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE); + System.out.println("Нет удалённых"); + return; + } + FormTrain form = new FormTrain(); + form._drawingTrain = linkedListRemoved.getLast(); + linkedListRemoved.removeLast(); + form.Draw(); + } + } + ); + JButton ButtonRefreshCollection = new JButton("Refresh Collection"); ButtonRefreshCollection.addActionListener( new ActionListener() { @@ -236,6 +258,8 @@ public class FormTrainCollecltion { jListStorage.setBounds(pictureBoxWidth, 210, 160, 80); buttonRemoveSet.setBounds(pictureBoxWidth, 300, 160, 20); + buttonGetRemoved.setBounds(pictureBoxWidth, 330, 160, 20); + w.add(canv); w.add(ButtonAddTrain); w.add(ButtonRemoveTrain); @@ -248,6 +272,8 @@ public class FormTrainCollecltion { w.add(jListStorage); w.add(buttonRemoveSet); + w.add(buttonGetRemoved); + w.setVisible(true); } } diff --git a/laba1Loco/TrainsGenericStorage.java b/laba1Loco/TrainsGenericStorage.java index 715133b..114a486 100644 --- a/laba1Loco/TrainsGenericStorage.java +++ b/laba1Loco/TrainsGenericStorage.java @@ -8,11 +8,11 @@ public class TrainsGenericStorage { /// /// Словарь (хранилище) /// - HashMap> _carStorages; + HashMap> _trainStorages; /// /// Возвращение списка названий наборов /// - public List Keys(){return _carStorages.keySet().stream().collect(Collectors.toList());} + public List Keys(){return _trainStorages.keySet().stream().collect(Collectors.toList());} /// /// Ширина окна отрисовки /// @@ -28,7 +28,7 @@ public class TrainsGenericStorage { /// public TrainsGenericStorage(int pictureWidth, int pictureHeight) { - _carStorages = new HashMap>(); + _trainStorages = new HashMap>(); _pictureWidth = pictureWidth; _pictureHeight = pictureHeight; } @@ -38,9 +38,9 @@ public class TrainsGenericStorage { /// Название набора public void AddSet(String name) { - if (_carStorages.containsKey(name)) + if (_trainStorages.containsKey(name)) return; - _carStorages.put(name, new TrainsGenericCollection(_pictureWidth, _pictureHeight)); + _trainStorages.put(name, new TrainsGenericCollection(_pictureWidth, _pictureHeight)); } /// /// Удаление набора @@ -48,9 +48,9 @@ public class TrainsGenericStorage { /// Название набора public void DelSet(String name) { - if (!_carStorages.containsKey(name)) + if (!_trainStorages.containsKey(name)) return; - _carStorages.remove(name); + _trainStorages.remove(name); } /// /// Доступ к набору @@ -59,8 +59,14 @@ public class TrainsGenericStorage { /// public TrainsGenericCollection get(String ind) { - if (_carStorages.containsKey(ind)) - return _carStorages.get(ind); + if (_trainStorages.containsKey(ind)) + return _trainStorages.get(ind); return null; } + + public DrawningObjectTrain get(String ind1, int ind2){ + if (!_trainStorages.containsKey(ind1)) + return null; + return _trainStorages.get(ind1).GetU(ind2); + } } -- 2.25.1 From bdab79faad2d9baee0ee43505a352340d2a1c24b Mon Sep 17 00:00:00 2001 From: Timourka Date: Sun, 5 Nov 2023 01:52:42 +0400 Subject: [PATCH 5/5] a little mistake --- laba1Loco/WheelDrawingBalls.java | 4 ++-- laba1Loco/WheelDrawingDavidStar.java | 4 ++-- laba1Loco/WheelDrawingSimple.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/laba1Loco/WheelDrawingBalls.java b/laba1Loco/WheelDrawingBalls.java index c61a78a..b07b509 100644 --- a/laba1Loco/WheelDrawingBalls.java +++ b/laba1Loco/WheelDrawingBalls.java @@ -48,7 +48,7 @@ public class WheelDrawingBalls implements IWheelDrawing{ DrawWheel(_startPosX + 46, _startPosY + 34, color, g2d); DrawWheel(_startPosX + 72, _startPosY + 34, color, g2d); - if (numWheel == NumWheel.TwoWheel){ + if (numWheel == NumWheel.ThreeWheel){ DrawWheel(_startPosX + 14, _startPosY + 34, color, g2d); DrawWheel(_startPosX + 59, _startPosY + 34, color, g2d); } @@ -65,7 +65,7 @@ public class WheelDrawingBalls implements IWheelDrawing{ DrawWheel(_startPosX + 46+85, _startPosY + 34, color, g2d); DrawWheel(_startPosX + 72+85, _startPosY + 34, color, g2d); - if (numWheel == NumWheel.TwoWheel){ + if (numWheel == NumWheel.ThreeWheel){ DrawWheel(_startPosX + 14+85, _startPosY + 34, color, g2d); DrawWheel(_startPosX + 59+85, _startPosY + 34, color, g2d); } diff --git a/laba1Loco/WheelDrawingDavidStar.java b/laba1Loco/WheelDrawingDavidStar.java index ab43f3b..54b352a 100644 --- a/laba1Loco/WheelDrawingDavidStar.java +++ b/laba1Loco/WheelDrawingDavidStar.java @@ -55,7 +55,7 @@ public class WheelDrawingDavidStar implements IWheelDrawing{ DrawWheel(_startPosX + 46, _startPosY + 34, color, g2d); DrawWheel(_startPosX + 72, _startPosY + 34, color, g2d); - if (numWheel == NumWheel.TwoWheel){ + if (numWheel == NumWheel.ThreeWheel){ DrawWheel(_startPosX + 14, _startPosY + 34, color, g2d); DrawWheel(_startPosX + 59, _startPosY + 34, color, g2d); } @@ -72,7 +72,7 @@ public class WheelDrawingDavidStar implements IWheelDrawing{ DrawWheel(_startPosX + 46+85, _startPosY + 34, color, g2d); DrawWheel(_startPosX + 72+85, _startPosY + 34, color, g2d); - if (numWheel == NumWheel.TwoWheel){ + if (numWheel == NumWheel.ThreeWheel){ DrawWheel(_startPosX + 14+85, _startPosY + 34, color, g2d); DrawWheel(_startPosX + 59+85, _startPosY + 34, color, g2d); } diff --git a/laba1Loco/WheelDrawingSimple.java b/laba1Loco/WheelDrawingSimple.java index 2392750..56d9073 100644 --- a/laba1Loco/WheelDrawingSimple.java +++ b/laba1Loco/WheelDrawingSimple.java @@ -39,7 +39,7 @@ public class WheelDrawingSimple implements IWheelDrawing{ g2d.fillOval(_startPosX + 26, _startPosY + 34, 8, 8); g2d.fillOval(_startPosX + 46, _startPosY + 34, 8, 8); g2d.fillOval(_startPosX + 72, _startPosY + 34, 8, 8); - if (numWheel == NumWheel.TwoWheel){ + if (numWheel == NumWheel.ThreeWheel){ g2d.fillOval(_startPosX + 14, _startPosY + 34, 8, 8); g2d.fillOval(_startPosX + 59, _startPosY + 34, 8, 8); } @@ -55,7 +55,7 @@ public class WheelDrawingSimple implements IWheelDrawing{ g2d.fillOval(_startPosX + 26+85, _startPosY + 34, 8, 8); g2d.fillOval(_startPosX + 46+85, _startPosY + 34, 8, 8); g2d.fillOval(_startPosX + 72+85, _startPosY + 34, 8, 8); - if (numWheel == NumWheel.TwoWheel){ + if (numWheel == NumWheel.ThreeWheel){ g2d.fillOval(_startPosX + 14+85, _startPosY + 34, 8, 8); g2d.fillOval(_startPosX + 59+85, _startPosY + 34, 8, 8); } -- 2.25.1