From d168f11ea61157954d782d7b976eee2f51cff89b Mon Sep 17 00:00:00 2001 From: ekallin Date: Sat, 18 Nov 2023 23:58:18 +0400 Subject: [PATCH] 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