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); + } }