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/.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 1b9ae8b..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,9 +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"); @@ -33,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)); @@ -92,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) { @@ -135,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.form b/ProjectElectricLocomotive/FormLocomotiveCollections.form index b1bcaa2..c0478f4 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.form +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.form @@ -1,9 +1,9 @@ - + - + @@ -12,16 +12,16 @@ - - - + + + - + @@ -39,20 +39,64 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + - + @@ -60,7 +104,7 @@ - + @@ -68,24 +112,24 @@ - + - - - - - - - - - + + + + + + + + + diff --git a/ProjectElectricLocomotive/FormLocomotiveCollections.java b/ProjectElectricLocomotive/FormLocomotiveCollections.java index 6f879ae..3ffaeba 100644 --- a/ProjectElectricLocomotive/FormLocomotiveCollections.java +++ b/ProjectElectricLocomotive/FormLocomotiveCollections.java @@ -1,12 +1,14 @@ package ProjectElectricLocomotive; import javax.swing.*; +import javax.swing.event.ListSelectionEvent; import java.awt.*; +import java.util.Stack; //готовая лаба 3 public class FormLocomotiveCollections { - FrameDopClassParameters frameDopClassParameters; + FormElectricLocomotive formElectricLocomotive; private JPanel MainPanel; private JPanel pictureBoxCollections; private JPanel Instruments; @@ -15,26 +17,66 @@ public class FormLocomotiveCollections { private JButton ButtonRefreshCollection; private JButton ButtonRemoveLocomotive; private JButton ButtonCreateRandomLoco; + private JTextField textBoxStorageName; + private JButton ButtonAddObject; + private JList listBoxStorage; + private JButton ButtonRemoveObject; + private JButton Button_OpenFormRemovedLocomotives; public DrawingLocomotive loco; - LocomotiveGenericCollection _locomotives; - + final LocomotivesGenericStorage _storage; + FrameElectricLocomotive _frameRemovedLocomotives; + private Stack _stackRemoveObjects; + FrameDopClassParameters frameDopClassParameters; public JPanel getPictureBoxCollections() { return MainPanel; } public FormLocomotiveCollections() { - _locomotives = new LocomotiveGenericCollection<>(400, 300); + formElectricLocomotive = new FormElectricLocomotive(); + _storage = new LocomotivesGenericStorage(400,300); + _stackRemoveObjects = new Stack<>(); + listBoxStorage.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged); + 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 -> { - loco = frameElectricLocomotive._formLocomotiveCollection._drawingLocomotive; + frameElectricLocomotive._formElectricLocomotive.ButtonSelectLocomotive.addActionListener(e2 -> { + loco = frameElectricLocomotive._formElectricLocomotive._drawingLocomotive; frameElectricLocomotive.dispose(); if (loco != null) { //проверяем, удалось ли нам загрузить объект - if (_locomotives.AddOverload(loco) != -1) { + if (obj.AddOverload(loco)!= -1) { JOptionPane.showMessageDialog(getPictureBoxCollections(), "Объект добавлен"); Refresh(); } else { @@ -44,32 +86,57 @@ 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()); - if (_locomotives.SubOverload(pos) != null) { - Refresh(); - JOptionPane.showMessageDialog(this.getPictureBoxCollections(), - "Объект удален", - "Успех", - JOptionPane.INFORMATION_MESSAGE); - } else { - JOptionPane.showMessageDialog(this.getPictureBoxCollections(), - "Не удалось удалить объект", - "Ошибка", - JOptionPane.ERROR_MESSAGE); - } - } catch (Exception ex) { + pos = Integer.parseInt(textFieldNumber.getText()); + } 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); + Refresh(); + + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Объект удален", + "Успех", + JOptionPane.INFORMATION_MESSAGE); + } else { + JOptionPane.showMessageDialog(this.getPictureBoxCollections(), + "Не удалось удалить объект", + "Ошибка", + JOptionPane.ERROR_MESSAGE); } }); @@ -77,10 +144,47 @@ public class FormLocomotiveCollections { 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() { + 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(); + } + + private void listBoxObjectsSelectedIndexChanged(ListSelectionEvent e) { + Refresh(); + } + 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); + 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/LocomotiveGenericCollection.java b/ProjectElectricLocomotive/LocomotiveGenericCollection.java index cf1207c..7b3b154 100644 --- a/ProjectElectricLocomotive/LocomotiveGenericCollection.java +++ b/ProjectElectricLocomotive/LocomotiveGenericCollection.java @@ -7,9 +7,11 @@ public class LocomotiveGenericCollection _collection; @@ -24,23 +26,28 @@ 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) + { + 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 6aa1ad8..f731887 100644 --- a/ProjectElectricLocomotive/SetGeneric.java +++ b/ProjectElectricLocomotive/SetGeneric.java @@ -1,12 +1,21 @@ package ProjectElectricLocomotive; +import java.util.ArrayList; + 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) @@ -16,48 +25,45 @@ 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 > 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 + { + T plane = _places.get(position); + _places.set(position, null); + return plane; + } } public T Get(int position) { - // TODO проверка позиции if (position < 0 || position >= Count()) return null; - return _places[position]; + return _places.get(position); } + + public void Set(int position, T loco) { + // Проверка позиции + // Проверка свободных мест в списке + if (position < 0 || position >= maxCount || Count() == maxCount) { + return; + } + // Вставка в список по позиции + _places.set(position, loco); + } + + public ArrayList GetEnumerator() { + return _places; + } + } \ No newline at end of file