From 84307044fe94fee7980c5b433a232512cc2451e3 Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 7 Nov 2023 00:00:07 +0400 Subject: [PATCH 1/7] Create Lab 4, modified class SetGeneric --- ProjectElectricLocomotive/SetGeneric.java | 99 ++++++++++++++--------- 1 file changed, 63 insertions(+), 36 deletions(-) diff --git a/ProjectElectricLocomotive/SetGeneric.java b/ProjectElectricLocomotive/SetGeneric.java index 6aa1ad8..f5a3cb2 100644 --- a/ProjectElectricLocomotive/SetGeneric.java +++ b/ProjectElectricLocomotive/SetGeneric.java @@ -1,12 +1,21 @@ package ProjectElectricLocomotive; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.NoSuchElementException; + public class SetGeneric{ - private T[] _places; - public int Count(){ - return _places.length; + private ArrayList _places; + public int Count() + { + return _places.size(); } - public SetGeneric(int count) { - _places = (T[]) new DrawingLocomotive[count]; + + public int maxCount; + public SetGeneric(int count) + { + maxCount = count; + _places = new ArrayList(count); } public int Insert(T loco) @@ -17,47 +26,65 @@ public class SetGeneric{ public int Insert(T loco, int position) { int NoEmpty = 0, temp = 0; - for (int i = position; i < Count(); i++) + if(position < 0 || position > Count()) + return -1; + if(Count() > maxCount) + return -1; + else { - if (_places[i] != null) NoEmpty++; - } - if (NoEmpty == Count() - position - 1) return -1; - - if (position < Count() && position >= 0) - { - for (int j = position; j < Count(); j++) - { - if (_places[j] == null) - { - temp = j; - break; - } - } - // shift right - for (int i = temp; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = loco; + _places.add(position, loco); return position; } - return -1; } public T Remove(int position) { if (position >= Count() || position < 0) return null; - - T tmp = _places[position]; - _places[position] = null; - return tmp; + else + { + _places.remove(position); + } + return _places.get(position); } - public T Get(int position) - { - // TODO проверка позиции - if (position < 0 || position >= Count()) return null; - return _places[position]; + public Iterable GetLocomotives(final Integer maxLocomotives) { + 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() && + (maxLocomotives == null || count < maxLocomotives); + } + + //ну и, соответственно, переходим к следующему, если хэз некст == true + @Override + public T next() { + if (hasNext()) { + count++; + return _places.get(currentIndex++); + } + throw new NoSuchElementException(); + } + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + }; } + +// public T Get(int position) +// { +// // TODO проверка позиции +// if (position < 0 || position >= Count()) return null; +// return _places[position]; +// } } \ No newline at end of file -- 2.25.1 From 00af902200919c84144625f5624b4cdfaa33d982 Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 7 Nov 2023 00:40:53 +0400 Subject: [PATCH 2/7] Created class LocomotivesGenericStorage --- .idea/java.iml | 11 ++++++++++ .idea/modules.xml | 2 +- .../LocomotiveGenericCollection.java | 20 ++++++++++++------- ProjectElectricLocomotive/SetGeneric.java | 13 ++++++------ 4 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 .idea/java.iml diff --git a/.idea/java.iml b/.idea/java.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/.idea/java.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 561b3e5..a127731 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java index cf1207c..0aeddea 100644 --- a/ProjectElectricLocomotive/LocomotiveGenericCollection.java +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -7,9 +7,11 @@ public class LocomotiveGenericCollection _collection; @@ -24,7 +26,6 @@ public class LocomotiveGenericCollection{ return _places.get(position); } + public T Get(int position) + { + if (position < 0 || position >= Count()) return null; + return _places.get(position); + } + public Iterable GetLocomotives(final Integer maxLocomotives) { return new Iterable() { @Override @@ -80,11 +86,4 @@ public class SetGeneric{ } }; } - -// public T Get(int position) -// { -// // TODO проверка позиции -// if (position < 0 || position >= Count()) return null; -// return _places[position]; -// } } \ No newline at end of file -- 2.25.1 From 3989c62269756180160f60d9398023633ec591d5 Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 7 Nov 2023 00:52:46 +0400 Subject: [PATCH 3/7] Modified FormLocomotiveCollections --- .../FormLocomotiveCollections.form | 66 ++++++++++++++----- .../FormLocomotiveCollections.java | 4 ++ 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index b1bcaa2..ac1e08b 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -3,7 +3,7 @@ - + @@ -21,7 +21,7 @@ - + @@ -39,20 +39,62 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + - + @@ -60,7 +102,7 @@ - + @@ -68,20 +110,12 @@ - + - - - - - - - - - + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index 6f879ae..ebabad0 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -15,6 +15,10 @@ public class FormLocomotiveCollections { private JButton ButtonRefreshCollection; private JButton ButtonRemoveLocomotive; private JButton ButtonCreateRandomLoco; + private JTextField textBoxStorageName; + private JButton ButtonAddObject; + private JList listBoxStorage; + private JButton ButtomRemoveObject; public DrawingLocomotive loco; LocomotiveGenericCollection _locomotives; -- 2.25.1 From 93d391fad32c51b330fe7c48a394b2a211b3c7ee Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 7 Nov 2023 20:36:52 +0400 Subject: [PATCH 4/7] some modifications , but locos doesn't show --- .../FormLocomotiveCollections.form | 6 +- .../FormLocomotiveCollections.java | 74 +++++++++++++++++-- .../LocomotiveGenericCollection.java | 17 +++-- .../LocomotivesGenericStorage.java | 71 ++++++++++++++++++ ProjectElectricLocomotive/SetGeneric.java | 28 +++++-- 5 files changed, 174 insertions(+), 22 deletions(-) create mode 100644 ProjectElectricLocomotive/LocomotivesGenericStorage.java diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index ac1e08b..70fb649 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -66,7 +66,9 @@ - + + + @@ -110,7 +112,7 @@ - + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index ebabad0..5b0e831 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -1,6 +1,7 @@ package ProjectElectricLocomotive; import javax.swing.*; +import javax.swing.event.ListSelectionEvent; import java.awt.*; //готовая лаба 3 @@ -18,19 +19,53 @@ public class FormLocomotiveCollections { private JTextField textBoxStorageName; private JButton ButtonAddObject; private JList listBoxStorage; - private JButton ButtomRemoveObject; + private JButton ButtonRemoveObject; public DrawingLocomotive loco; + private DefaultListModel listModel; + final LocomotivesGenericStorage _storage; LocomotiveGenericCollection _locomotives; public JPanel getPictureBoxCollections() { return MainPanel; } + public FormLocomotiveCollections() { _locomotives = new LocomotiveGenericCollection<>(400, 300); + _storage = new LocomotivesGenericStorage(pictureBoxCollections.getWidth(), pictureBoxCollections.getHeight()); + ButtonAddObject.addActionListener(e -> + { + String NameStorage = textBoxStorageName.getText(); + if (NameStorage.equals("")) { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Где данные? Напиши хоть что-нибудь", + "Ошибка", + JOptionPane.INFORMATION_MESSAGE); + return; + } + _storage.AddSet(NameStorage); + ReloadObjects(); + Refresh(); + }); + + ButtonRemoveObject.addActionListener(e -> + { + if (listBoxStorage.getSelectedIndex() == -1) { + return; + } + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Коллекция удалена", "Удаление", JOptionPane.INFORMATION_MESSAGE); + _storage.DelSet((String) listBoxStorage.getSelectedValue()); //ТУТ СТРИНГ ОБРАТИ ВНИМАНИЕ КАК-НИБУДЬ + ReloadObjects(); + }); ButtonAddLocomotive.addActionListener(e -> { + if (listBoxStorage.getSelectedIndex() == -1) + return; + var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); // ТУТ ЕЩЕ РАЗ - ОБРАТИ ВНИМАНИЕ НА СТРИНГ + if (obj == null) { + return; + } FrameElectricLocomotive frameElectricLocomotive = new FrameElectricLocomotive(); frameElectricLocomotive.setVisible(true); frameElectricLocomotive._formLocomotiveCollection.ButtonSelectLocomotive.addActionListener(e2 -> { @@ -38,7 +73,7 @@ public class FormLocomotiveCollections { frameElectricLocomotive.dispose(); if (loco != null) { //проверяем, удалось ли нам загрузить объект - if (_locomotives.AddOverload(loco) != -1) { + if (obj.AddOverload(loco)!= -1) { JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); Refresh(); } else { @@ -48,16 +83,26 @@ public class FormLocomotiveCollections { }); }); - ButtonCreateRandomLoco.addActionListener(e->{ - if(frameDopClassParameters!=null) frameDopClassParameters.dispose(); + ButtonCreateRandomLoco.addActionListener(e -> { + if (frameDopClassParameters != null) frameDopClassParameters.dispose(); frameDopClassParameters = new FrameDopClassParameters(); frameDopClassParameters.setVisible(true); }); ButtonRemoveLocomotive.addActionListener(e -> { + if (listBoxStorage.getSelectedIndex() == -1) { + return; + } + var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); + if (obj == null) { + return; + } + int pos; try { - int pos = Integer.parseInt(textFieldNumber.getText()); + pos = Integer.parseInt(textFieldNumber.getText()); + DrawingLocomotive deletedLoco = obj.SubOverload(pos); if (_locomotives.SubOverload(pos) != null) { + // logic for push deleted loco in stack Refresh(); JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Объект удален", @@ -82,7 +127,26 @@ public class FormLocomotiveCollections { }); } + + private void ReloadObjects() { + int index = listBoxStorage.getSelectedIndex(); + listBoxStorage.setListData(_storage.Keys().toArray()); + if (listBoxStorage.getModel().getSize() > 0 && (index == -1 || index >= listBoxStorage.getModel().getSize())) { + listBoxStorage.setSelectedIndex(0); + } else if (listBoxStorage.getModel().getSize() > 0 && index > -1 && index < listBoxStorage.getModel().getSize()) { + listBoxStorage.setSelectedIndex(index); + } + listBoxStorage.invalidate(); + } + public void Refresh() { + if (listBoxStorage.getSelectedIndex() == -1) { + return; + } + var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); + if (obj == null) { + return; + } Graphics g = pictureBoxCollections.getGraphics(); pictureBoxCollections.paint(g); _locomotives.ShowLocomotives(g); diff --git a/ProjectElectricLocomotive/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java index 0aeddea..7b3b154 100644 --- a/ProjectElectricLocomotive/LocomotiveGenericCollection.java +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -26,11 +26,11 @@ public class LocomotiveGenericCollection> _locomotiveStorage; + public List Keys() + { + return _locomotiveStorage.keySet().stream().toList(); + } + private final int _pictureWidth; + private final int _pictureHeight; + public LocomotivesGenericStorage(int pictureWidth, int pictureHeight) { + _locomotiveStorage = new HashMap<>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(String name) + { + if (!(_locomotiveStorage.containsKey(name))) + { + _locomotiveStorage.put(name, new LocomotiveGenericCollection(_pictureWidth, _pictureHeight)); + } + } + + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(String name) + { + if (_locomotiveStorage.keySet().contains(name)) + { + _locomotiveStorage.remove(name); + } + } + + + /// + /// Доступ к набору + /// + /// + /// + public LocomotiveGenericCollection get(String ind) + { + // TODO Продумать логику получения набора + if (_locomotiveStorage.keySet().contains(ind)) + { + return _locomotiveStorage.get(ind); + } + return null; + } + + public DrawingObjectLocomotive get(String name, int ind) + { + if (_locomotiveStorage.keySet().contains(ind)) + { + return _locomotiveStorage.get(name).GetU(ind); + } + return null; + } +} diff --git a/ProjectElectricLocomotive/SetGeneric.java b/ProjectElectricLocomotive/SetGeneric.java index ba12886..53b4476 100644 --- a/ProjectElectricLocomotive/SetGeneric.java +++ b/ProjectElectricLocomotive/SetGeneric.java @@ -6,12 +6,14 @@ import java.util.NoSuchElementException; public class SetGeneric{ private ArrayList _places; + public int Count() { return _places.size(); } public int maxCount; + public SetGeneric(int count) { maxCount = count; @@ -25,11 +27,8 @@ public class SetGeneric{ public int Insert(T loco, int position) { - int NoEmpty = 0, temp = 0; if(position < 0 || position > Count()) return -1; - if(Count() > maxCount) - return -1; else { _places.add(position, loco); @@ -43,9 +42,11 @@ public class SetGeneric{ return null; else { + T plane = _places.get(position); _places.remove(position); + _places.set(position, null); + return plane; } - return _places.get(position); } public T Get(int position) @@ -54,7 +55,21 @@ public class SetGeneric{ return _places.get(position); } - public Iterable GetLocomotives(final Integer maxLocomotives) { + public void Set(int position, T loco) { + // Проверка позиции + // Проверка свободных мест в списке + if (position < 0 || position >= maxCount || Count() == maxCount) { + return; + } + // Вставка в список по позиции + _places.set(position, loco); + } + + public ArrayList GetEnumerator() { + return _places; + } + +/* public Iterable GetLocomotives(final Integer maxLocomotives) { return new Iterable() { @Override public Iterator iterator() { @@ -84,6 +99,5 @@ public class SetGeneric{ } }; } - }; - } + };*/ } \ No newline at end of file -- 2.25.1 From bbc0734544471bde99c74c336959a363503c9f87 Mon Sep 17 00:00:00 2001 From: ekallin Date: Wed, 8 Nov 2023 00:17:28 +0400 Subject: [PATCH 5/7] more modifications , but now locos doesn't remove =/ --- .../FormElectricLocomotive.java | 3 +- .../FormLocomotiveCollections.form | 18 ++- .../FormLocomotiveCollections.java | 119 ++++++++++++++---- .../FrameElectricLocomotive.java | 6 +- .../LocomotivesGenericStorage.java | 1 - 5 files changed, 114 insertions(+), 33 deletions(-) diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index 1b9ae8b..da6f052 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -21,7 +21,8 @@ public class FormElectricLocomotive { public DrawingLocomotive SelectedLocomotive; public boolean IsSelect = false; - public JPanel getPictureBox() { + public JPanel getPictureBox() + { return pictureBox; } public FormElectricLocomotive() diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index 70fb649..c0478f4 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -1,9 +1,9 @@
- + - + @@ -12,9 +12,9 @@ - - - + + + @@ -122,6 +122,14 @@ + + + + + + + +
diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index 5b0e831..0958ab8 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -3,11 +3,11 @@ package ProjectElectricLocomotive; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import java.awt.*; +import java.util.Stack; //готовая лаба 3 public class FormLocomotiveCollections { - FrameDopClassParameters frameDopClassParameters; private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; @@ -20,19 +20,24 @@ public class FormLocomotiveCollections { private JButton ButtonAddObject; private JList listBoxStorage; private JButton ButtonRemoveObject; + private JButton Button_OpenFormRemovedLocomotives; public DrawingLocomotive loco; - private DefaultListModel listModel; final LocomotivesGenericStorage _storage; - LocomotiveGenericCollection _locomotives; + FrameElectricLocomotive _frameRemovedLocomotives; + private Stack stackRemoveObjects; + FrameDopClassParameters frameDopClassParameters; +// LocomotiveGenericCollection _locomotives; public JPanel getPictureBoxCollections() { return MainPanel; } public FormLocomotiveCollections() { - _locomotives = new LocomotiveGenericCollection<>(400, 300); - _storage = new LocomotivesGenericStorage(pictureBoxCollections.getWidth(), pictureBoxCollections.getHeight()); + //_locomotives = new LocomotiveGenericCollection<>(400, 300); + _storage = new LocomotivesGenericStorage(/*pictureBoxCollections.getWidth(), pictureBoxCollections.getHeight()*/400,300); + stackRemoveObjects = new Stack<>(); + listBoxStorage.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged); ButtonAddObject.addActionListener(e -> { @@ -68,8 +73,8 @@ public class FormLocomotiveCollections { } FrameElectricLocomotive frameElectricLocomotive = new FrameElectricLocomotive(); frameElectricLocomotive.setVisible(true); - frameElectricLocomotive._formLocomotiveCollection.ButtonSelectLocomotive.addActionListener(e2 -> { - loco = frameElectricLocomotive._formLocomotiveCollection._drawingLocomotive; + frameElectricLocomotive._formElectricLocomotive.ButtonSelectLocomotive.addActionListener(e2 -> { + loco = frameElectricLocomotive._formElectricLocomotive._drawingLocomotive; frameElectricLocomotive.dispose(); if (loco != null) { //проверяем, удалось ли нам загрузить объект @@ -90,6 +95,40 @@ public class FormLocomotiveCollections { }); ButtonRemoveLocomotive.addActionListener(e -> { + /*if (listBoxStorage.getSelectedIndex() == -1) { + return; + } + var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); + if (obj == null) { + return; + } + int pos = 0; + try { + pos = Integer.parseInt(textFieldNumber.getText()); + } + catch (NumberFormatException ex) + { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Неверное значение", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + } + DrawingLocomotive deletedLoco = obj.SubOverload(pos); + if (deletedLoco != null) { + // logic for push deleted loco in stack + stackRemoveObjects.push(deletedLoco); + Refresh(); + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Объект удален", + "Успех", + JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Не удалось удалить объект", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + }*/ + if (listBoxStorage.getSelectedIndex() == -1) { return; } @@ -100,32 +139,62 @@ public class FormLocomotiveCollections { int pos; try { pos = Integer.parseInt(textFieldNumber.getText()); - DrawingLocomotive deletedLoco = obj.SubOverload(pos); - if (_locomotives.SubOverload(pos) != null) { - // logic for push deleted loco in stack - Refresh(); - JOptionPane.showMessageDialog(this.getPictureBoxCollections(), - "Объект удален", - "Успех", - JOptionPane.INFORMATION_MESSAGE); - } else { - JOptionPane.showMessageDialog(this.getPictureBoxCollections(), - "Не удалось удалить объект", - "Ошибка", - JOptionPane.ERROR_MESSAGE); - } - } catch (Exception ex) { + } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Неверное значение", "Ошибка", JOptionPane.ERROR_MESSAGE); + return; } + Object[] options = {"Да", "Нет"}; + int n = JOptionPane.showOptionDialog(this.getPictureBoxCollections(), + "Удалить объект?", + "Все серьезно", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + options, + options[0] + ); + if (n == 1) { + return; + } + DrawingLocomotive removedPlane = obj.SubOverload(pos); + if (removedPlane != null) { + stackRemoveObjects.push(removedPlane); + + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Объект удален", + "Успех", + JOptionPane.INFORMATION_MESSAGE);Refresh(); + } else { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Не удалось удалить объект", + "Ошибка", + JOptionPane.ERROR_MESSAGE); + } + }); ButtonRefreshCollection.addActionListener(e -> { Refresh(); }); + Button_OpenFormRemovedLocomotives.addActionListener(e -> { + if(stackRemoveObjects.empty()) { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Вы ничего не удалили, кажется...", + "ой", + JOptionPane.INFORMATION_MESSAGE); + return; + } + _frameRemovedLocomotives = new FrameElectricLocomotive(); + _frameRemovedLocomotives._formElectricLocomotive._drawingLocomotive = stackRemoveObjects.pop(); + _frameRemovedLocomotives.setVisible(true); + _frameRemovedLocomotives._formElectricLocomotive.Draw(); + + }); + } private void ReloadObjects() { @@ -139,6 +208,10 @@ public class FormLocomotiveCollections { listBoxStorage.invalidate(); } + private void listBoxObjectsSelectedIndexChanged(ListSelectionEvent e) { + Refresh(); + } + public void Refresh() { if (listBoxStorage.getSelectedIndex() == -1) { return; @@ -149,6 +222,6 @@ public class FormLocomotiveCollections { } Graphics g = pictureBoxCollections.getGraphics(); pictureBoxCollections.paint(g); - _locomotives.ShowLocomotives(g); + obj.ShowLocomotives(g); } } diff --git a/ProjectElectricLocomotive/FrameElectricLocomotive.java b/ProjectElectricLocomotive/FrameElectricLocomotive.java index 9344738..9f1079d 100644 --- a/ProjectElectricLocomotive/FrameElectricLocomotive.java +++ b/ProjectElectricLocomotive/FrameElectricLocomotive.java @@ -3,13 +3,13 @@ package ProjectElectricLocomotive; import javax.swing.*; public class FrameElectricLocomotive extends JFrame { - public FormElectricLocomotive _formLocomotiveCollection; + public FormElectricLocomotive _formElectricLocomotive; public FrameElectricLocomotive() { super(); setTitle("Электролокомотив"); setDefaultCloseOperation(DISPOSE_ON_CLOSE); - _formLocomotiveCollection = new FormElectricLocomotive(); - setContentPane(_formLocomotiveCollection.getPictureBox()); + _formElectricLocomotive = new FormElectricLocomotive(); + setContentPane(_formElectricLocomotive.getPictureBox()); setDefaultLookAndFeelDecorated(false); setLocation(500, 200); pack(); diff --git a/ProjectElectricLocomotive/LocomotivesGenericStorage.java b/ProjectElectricLocomotive/LocomotivesGenericStorage.java index 7d766ae..08c709d 100644 --- a/ProjectElectricLocomotive/LocomotivesGenericStorage.java +++ b/ProjectElectricLocomotive/LocomotivesGenericStorage.java @@ -52,7 +52,6 @@ public class LocomotivesGenericStorage { /// public LocomotiveGenericCollection get(String ind) { - // TODO Продумать логику получения набора if (_locomotiveStorage.keySet().contains(ind)) { return _locomotiveStorage.get(ind); -- 2.25.1 From d168f11ea61157954d782d7b976eee2f51cff89b Mon Sep 17 00:00:00 2001 From: ekallin Date: Sat, 18 Nov 2023 23:58:18 +0400 Subject: [PATCH 6/7] 4 lab not formatted --- .idea/uiDesigner.xml | 10 ++-- .../FormElectricLocomotive.form | 2 +- .../FormElectricLocomotive.java | 57 +++++++++++++------ .../FormLocomotiveCollections.java | 57 ++++--------------- ProjectElectricLocomotive/SetGeneric.java | 38 +------------ 5 files changed, 59 insertions(+), 105 deletions(-) diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml index 2b63946..0ed6dd1 100644 --- a/.idea/uiDesigner.xml +++ b/.idea/uiDesigner.xml @@ -86,11 +86,6 @@ - - - - - @@ -99,6 +94,11 @@ + + + + + diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.form b/ProjectElectricLocomotive/FormElectricLocomotive.form index 2734e4b..17a7f6f 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.form +++ b/ProjectElectricLocomotive/FormElectricLocomotive.form @@ -1,6 +1,6 @@
- + diff --git a/ProjectElectricLocomotive/FormElectricLocomotive.java b/ProjectElectricLocomotive/FormElectricLocomotive.java index da6f052..efb0b17 100644 --- a/ProjectElectricLocomotive/FormElectricLocomotive.java +++ b/ProjectElectricLocomotive/FormElectricLocomotive.java @@ -5,11 +5,11 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; -public class FormElectricLocomotive { +public class FormElectricLocomotive extends JDialog{ public DrawingLocomotive _drawingLocomotive; AbstractStrategy _abstractStrategy; private JButton buttonCreateElectricLocomotive; - private JPanel pictureBox; + public JComponent pictureBox; private JButton buttonUp; private JButton buttonDown; private JButton buttonLeft; @@ -21,10 +21,27 @@ public class FormElectricLocomotive { public DrawingLocomotive SelectedLocomotive; public boolean IsSelect = false; - public JPanel getPictureBox() + public JComponent getPictureBox() { return pictureBox; } + + private class Canvas extends JPanel{ + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingLocomotive == null){ + return; + } + + Graphics2D g2d = (Graphics2D)g; + g2d.setColor(getBackground()); + g2d.fillRect(0, 0, getWidth(), getHeight()); + super.paintComponents(g); + _drawingLocomotive.DrawTransport(g2d); + super.repaint(); + } + } public FormElectricLocomotive() { buttonUp.setName("buttonUp"); @@ -34,11 +51,18 @@ public class FormElectricLocomotive { buttonCreateLocomotive.addActionListener(e -> { Random rnd = new Random(); - Color color = JColorChooser.showDialog(null, "Цвет", null); - _drawingLocomotive = new DrawingLocomotive(rnd.nextInt(100, 300), - rnd.nextInt(1000, 3000), color, + Color color = JColorChooser.showDialog( + null, + "Цвет", + null + ); + _drawingLocomotive = new DrawingLocomotive( + rnd.nextInt(100, 300), + rnd.nextInt(1000, 3000), + color, pictureBox.getWidth(), - pictureBox.getHeight()); + pictureBox.getHeight() + ); _drawingLocomotive.SetWheelsCount(rnd.nextInt(2, 5)); _drawingLocomotive.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); @@ -93,9 +117,10 @@ public class FormElectricLocomotive { if (_abstractStrategy == null) { return; } - _abstractStrategy.SetData(new - DrawingObjectLocomotive(_drawingLocomotive), pictureBox.getWidth(), - pictureBox.getHeight()); + _abstractStrategy.SetData( + new DrawingObjectLocomotive(_drawingLocomotive), pictureBox.getWidth(), + pictureBox.getHeight() + ); comboBoxStrategy.setEnabled(false); } if (_abstractStrategy == null) { @@ -136,11 +161,11 @@ public class FormElectricLocomotive { buttonRight.addActionListener(buttonMoveClickedListener); } public void Draw() { - if (_drawingLocomotive.EntityLocomotive == null) { - return; - } - Graphics g = pictureBox.getGraphics(); - pictureBox.paint(g); - _drawingLocomotive.DrawTransport(g); + pictureBox.repaint(); + } + + private void createUIComponents(){ + pictureBox = new Canvas(); + pictureBox.setBounds(new Rectangle(400, 300)); } } diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index 0958ab8..3ffaeba 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -8,6 +8,7 @@ import java.util.Stack; //готовая лаба 3 public class FormLocomotiveCollections { + FormElectricLocomotive formElectricLocomotive; private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; @@ -24,19 +25,16 @@ public class FormLocomotiveCollections { public DrawingLocomotive loco; final LocomotivesGenericStorage _storage; FrameElectricLocomotive _frameRemovedLocomotives; - - private Stack stackRemoveObjects; + private Stack _stackRemoveObjects; FrameDopClassParameters frameDopClassParameters; -// LocomotiveGenericCollection _locomotives; public JPanel getPictureBoxCollections() { return MainPanel; } - public FormLocomotiveCollections() { - //_locomotives = new LocomotiveGenericCollection<>(400, 300); - _storage = new LocomotivesGenericStorage(/*pictureBoxCollections.getWidth(), pictureBoxCollections.getHeight()*/400,300); - stackRemoveObjects = new Stack<>(); + formElectricLocomotive = new FormElectricLocomotive(); + _storage = new LocomotivesGenericStorage(400,300); + _stackRemoveObjects = new Stack<>(); listBoxStorage.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged); ButtonAddObject.addActionListener(e -> @@ -95,40 +93,6 @@ public class FormLocomotiveCollections { }); ButtonRemoveLocomotive.addActionListener(e -> { - /*if (listBoxStorage.getSelectedIndex() == -1) { - return; - } - var obj = _storage.get(listBoxStorage.getSelectedValue().toString()); - if (obj == null) { - return; - } - int pos = 0; - try { - pos = Integer.parseInt(textFieldNumber.getText()); - } - catch (NumberFormatException ex) - { - JOptionPane.showMessageDialog(this.getPictureBoxCollections(), - "Неверное значение", - "Ошибка", - JOptionPane.ERROR_MESSAGE); - } - DrawingLocomotive deletedLoco = obj.SubOverload(pos); - if (deletedLoco != null) { - // logic for push deleted loco in stack - stackRemoveObjects.push(deletedLoco); - Refresh(); - JOptionPane.showMessageDialog(this.getPictureBoxCollections(), - "Объект удален", - "Успех", - JOptionPane.INFORMATION_MESSAGE); - } else { - JOptionPane.showMessageDialog(this.getPictureBoxCollections(), - "Не удалось удалить объект", - "Ошибка", - JOptionPane.ERROR_MESSAGE); - }*/ - if (listBoxStorage.getSelectedIndex() == -1) { return; } @@ -161,19 +125,19 @@ public class FormLocomotiveCollections { } DrawingLocomotive removedPlane = obj.SubOverload(pos); if (removedPlane != null) { - stackRemoveObjects.push(removedPlane); + _stackRemoveObjects.push(removedPlane); + Refresh(); JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Объект удален", "Успех", - JOptionPane.INFORMATION_MESSAGE);Refresh(); + JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Не удалось удалить объект", "Ошибка", JOptionPane.ERROR_MESSAGE); } - }); ButtonRefreshCollection.addActionListener(e -> { @@ -181,7 +145,7 @@ public class FormLocomotiveCollections { }); Button_OpenFormRemovedLocomotives.addActionListener(e -> { - if(stackRemoveObjects.empty()) { + if(_stackRemoveObjects.empty()) { JOptionPane.showMessageDialog(this.getPictureBoxCollections(), "Вы ничего не удалили, кажется...", "ой", @@ -189,10 +153,9 @@ public class FormLocomotiveCollections { return; } _frameRemovedLocomotives = new FrameElectricLocomotive(); - _frameRemovedLocomotives._formElectricLocomotive._drawingLocomotive = stackRemoveObjects.pop(); + _frameRemovedLocomotives._formElectricLocomotive._drawingLocomotive = _stackRemoveObjects.pop(); _frameRemovedLocomotives.setVisible(true); _frameRemovedLocomotives._formElectricLocomotive.Draw(); - }); } diff --git a/ProjectElectricLocomotive/SetGeneric.java b/ProjectElectricLocomotive/SetGeneric.java index 53b4476..f731887 100644 --- a/ProjectElectricLocomotive/SetGeneric.java +++ b/ProjectElectricLocomotive/SetGeneric.java @@ -1,8 +1,6 @@ package ProjectElectricLocomotive; import java.util.ArrayList; -import java.util.Iterator; -import java.util.NoSuchElementException; public class SetGeneric{ private ArrayList _places; @@ -17,7 +15,7 @@ public class SetGeneric{ public SetGeneric(int count) { maxCount = count; - _places = new ArrayList(count); + _places = new ArrayList<>(count); } public int Insert(T loco) @@ -27,7 +25,7 @@ public class SetGeneric{ public int Insert(T loco, int position) { - if(position < 0 || position > Count()) + if(position < 0 || position > maxCount) return -1; else { @@ -43,7 +41,6 @@ public class SetGeneric{ else { T plane = _places.get(position); - _places.remove(position); _places.set(position, null); return plane; } @@ -69,35 +66,4 @@ public class SetGeneric{ return _places; } -/* public Iterable GetLocomotives(final Integer maxLocomotives) { - 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() && - (maxLocomotives == null || count < maxLocomotives); - } - - //ну и, соответственно, переходим к следующему, если хэз некст == true - @Override - public T next() { - if (hasNext()) { - count++; - return _places.get(currentIndex++); - } - throw new NoSuchElementException(); - } - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - };*/ } \ No newline at end of file -- 2.25.1 From 0c640c67699c4530eba05901380f6f63fc2bd3a2 Mon Sep 17 00:00:00 2001 From: ekallin Date: Tue, 21 Nov 2023 17:33:28 +0400 Subject: [PATCH 7/7] ready lab 4 for pull request --- .../LocomotivesGenericStorage.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/ProjectElectricLocomotive/LocomotivesGenericStorage.java b/ProjectElectricLocomotive/LocomotivesGenericStorage.java index 08c709d..fb81354 100644 --- a/ProjectElectricLocomotive/LocomotivesGenericStorage.java +++ b/ProjectElectricLocomotive/LocomotivesGenericStorage.java @@ -1,10 +1,7 @@ package ProjectElectricLocomotive; -import java.util.ArrayList; -import java.util.Dictionary; import java.util.HashMap; import java.util.List; -import java.util.stream.Collectors; public class LocomotivesGenericStorage { final HashMap> _locomotiveStorage; @@ -20,10 +17,6 @@ public class LocomotivesGenericStorage { _pictureHeight = pictureHeight; } - /// - /// Добавление набора - /// - /// Название набора public void AddSet(String name) { if (!(_locomotiveStorage.containsKey(name))) @@ -32,10 +25,6 @@ public class LocomotivesGenericStorage { } } - /// - /// Удаление набора - /// - /// Название набора public void DelSet(String name) { if (_locomotiveStorage.keySet().contains(name)) @@ -44,12 +33,6 @@ public class LocomotivesGenericStorage { } } - - /// - /// Доступ к набору - /// - /// - /// public LocomotiveGenericCollection get(String ind) { if (_locomotiveStorage.keySet().contains(ind)) -- 2.25.1