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