diff --git a/ProjectStormtrooper/DrawingPlane.java b/ProjectStormtrooper/DrawingPlane.java index 80f8b25..db224d4 100644 --- a/ProjectStormtrooper/DrawingPlane.java +++ b/ProjectStormtrooper/DrawingPlane.java @@ -1,9 +1,10 @@ package ProjectStormtrooper; +import javax.swing.*; import java.awt.*; import java.util.Random; -public class DrawingPlane { +public class DrawingPlane extends JPanel { public EntityPlane EntityPlane; protected IDrawingEngines _drawingEngines; protected int _pictureWidth; diff --git a/ProjectStormtrooper/FormPlaneCollection.form b/ProjectStormtrooper/FormPlaneCollection.form index 7377d0e..c0cb0dd 100644 --- a/ProjectStormtrooper/FormPlaneCollection.form +++ b/ProjectStormtrooper/FormPlaneCollection.form @@ -14,7 +14,7 @@ - + @@ -122,6 +122,14 @@ + + + + + + + + diff --git a/ProjectStormtrooper/FormPlaneCollection.java b/ProjectStormtrooper/FormPlaneCollection.java index 81ece05..e975064 100644 --- a/ProjectStormtrooper/FormPlaneCollection.java +++ b/ProjectStormtrooper/FormPlaneCollection.java @@ -5,10 +5,12 @@ import javax.swing.event.ListSelectionEvent; import java.awt.*; import java.awt.event.ActionEvent; import java.util.Objects; +import java.util.Stack; public class FormPlaneCollection { final PlanesGenericStorage _storage; FrameDoubleParametrized _frameDoubleParametrized; + FrameStormtrooper _frameRemovedPlanes; private JPanel PanelWrapper; private JPanel GroupBoxInstruments; private JPanel PictureBoxCollection; @@ -22,7 +24,9 @@ public class FormPlaneCollection { private JList listBoxStorages; private JButton buttonRemoveStorage; private JPanel storagesPanel; + private JButton buttonShowRemovedPlanes; public DrawingPlane SelectedPlane; + Stack _removedPlanes; public JPanel getPanelWrapper() { @@ -32,6 +36,7 @@ public class FormPlaneCollection { public FormPlaneCollection() { PictureBoxCollection.setPreferredSize(new Dimension(600, 500)); _storage = new PlanesGenericStorage(600, 500); + _removedPlanes = new Stack<>(); buttonAddPlane.addActionListener(this::buttonAddPlaneClicked); buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked); buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked); @@ -39,6 +44,7 @@ public class FormPlaneCollection { buttonAddStorage.addActionListener(this::buttonAddStorageClicked); buttonRemoveStorage.addActionListener(this::buttonRemoveStorageClicked); listBoxStorages.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged); + buttonShowRemovedPlanes.addActionListener(this::buttonShowRemovedPlanesClicked); } private void ReloadObjects() { @@ -101,8 +107,8 @@ public class FormPlaneCollection { FrameStormtrooper frameStormtrooper = new FrameStormtrooper(); frameStormtrooper.setVisible(true); - frameStormtrooper._formPlaneCollection.buttonSelectPlane.addActionListener(ev -> { - SelectedPlane = frameStormtrooper._formPlaneCollection._drawingPlane; + frameStormtrooper._formStromtrooper.buttonSelectPlane.addActionListener(ev -> { + SelectedPlane = frameStormtrooper._formStromtrooper._drawingPlane; frameStormtrooper.dispose(); if (SelectedPlane != null) { if (obj.Add(SelectedPlane) > -1) { @@ -152,7 +158,9 @@ public class FormPlaneCollection { if (n == 1) { return; } - if (obj.Sub(pos) != null) { + DrawingPlane removedPlane = obj.Sub(pos); + if (removedPlane != null) { + _removedPlanes.push(removedPlane); refreshPictureBox(); JOptionPane.showMessageDialog(this.getPanelWrapper(), "Объект удален", @@ -178,6 +186,20 @@ public class FormPlaneCollection { _frameDoubleParametrized.setVisible(true); } + public void buttonShowRemovedPlanesClicked(ActionEvent e) { + if (_removedPlanes.empty()) { + JOptionPane.showMessageDialog(this.getPanelWrapper(), + "Нет удаленных объектов", + "Инфо", + JOptionPane.INFORMATION_MESSAGE); + return; + } + _frameRemovedPlanes = new FrameStormtrooper(); + _frameRemovedPlanes._formStromtrooper._drawingPlane = _removedPlanes.pop(); + _frameRemovedPlanes.setVisible(true); + _frameRemovedPlanes._formStromtrooper.Draw(); + } + public void refreshPictureBox() { if (listBoxStorages.getSelectedIndex() == -1) { return; diff --git a/ProjectStormtrooper/FormStormtrooper.form b/ProjectStormtrooper/FormStormtrooper.form index 6993efe..e24fe2e 100644 --- a/ProjectStormtrooper/FormStormtrooper.form +++ b/ProjectStormtrooper/FormStormtrooper.form @@ -1,6 +1,6 @@
- + diff --git a/ProjectStormtrooper/FormStormtrooper.java b/ProjectStormtrooper/FormStormtrooper.java index 04f0ead..7a52f25 100644 --- a/ProjectStormtrooper/FormStormtrooper.java +++ b/ProjectStormtrooper/FormStormtrooper.java @@ -3,6 +3,7 @@ package ProjectStormtrooper; import javax.swing.*; import java.awt.*; import javax.swing.JColorChooser; +import java.awt.event.ActionEvent; import java.util.Random; import java.awt.event.ActionListener; @@ -10,27 +11,43 @@ public class FormStormtrooper extends JDialog { public DrawingPlane _drawingPlane; AbstractStrategy _abstractStrategy; private JButton buttonCreateStormtrooper; - private JPanel pictureBox; + private JComponent pictureBox; private JButton buttonDown; private JButton buttonUp; private JButton buttonLeft; - private JButton buttonRight; + public JButton buttonRight; private JButton buttonCreatePlane; private JComboBox comboBoxStrategy; private JButton buttonStep; public JButton buttonSelectPlane; - public JPanel getPictureBox() { + public JComponent getPictureBox() { return pictureBox; } + private class Canvas extends JPanel{ + public Canvas(){ + } + public void paintComponent (Graphics g){ + if (_drawingPlane == null){ + return; + } + + Graphics2D g2d = (Graphics2D)g; + g2d.setColor(getBackground()); + g2d.fillRect(0, 0, getWidth(), getHeight()); + super.paintComponents(g); + _drawingPlane.DrawTransport(g2d); + super.repaint(); + } + } + public FormStormtrooper() { buttonUp.setName("buttonUp"); buttonDown.setName("buttonDown"); buttonLeft.setName("buttonLeft"); buttonRight.setName("buttonRight"); - buttonCreateStormtrooper.addActionListener(e -> { Random random = new Random(); @@ -100,42 +117,36 @@ public class FormStormtrooper extends JDialog { } }); + buttonUp.addActionListener(this::buttonMoveClickedListener); + buttonDown.addActionListener(this::buttonMoveClickedListener); + buttonLeft.addActionListener(this::buttonMoveClickedListener); + buttonRight.addActionListener(this::buttonMoveClickedListener); + } - ActionListener buttonMoveClickedListener = e -> { - String buttonName = ((JButton) e.getSource()).getName(); + public void buttonMoveClickedListener(ActionEvent e) { + String buttonName = ((JButton) e.getSource()).getName(); - switch (buttonName) { - case ("buttonUp") -> { - _drawingPlane.MoveTransport(EnumDirectionType.Up); - } - case ("buttonDown") -> { - _drawingPlane.MoveTransport(EnumDirectionType.Down); - } - case ("buttonLeft") -> { - _drawingPlane.MoveTransport(EnumDirectionType.Left); - } - case ("buttonRight") -> { - _drawingPlane.MoveTransport(EnumDirectionType.Right); - } + switch (buttonName) { + case ("buttonUp") -> { + _drawingPlane.MoveTransport(EnumDirectionType.Up); } + case ("buttonDown") -> { + _drawingPlane.MoveTransport(EnumDirectionType.Down); + } + case ("buttonLeft") -> { + _drawingPlane.MoveTransport(EnumDirectionType.Left); + } + case ("buttonRight") -> { + _drawingPlane.MoveTransport(EnumDirectionType.Right); + } + default -> _drawingPlane.MoveTransport(EnumDirectionType.Right); + } - Draw(); - }; - - buttonUp.addActionListener(buttonMoveClickedListener); - buttonDown.addActionListener(buttonMoveClickedListener); - buttonLeft.addActionListener(buttonMoveClickedListener); - buttonRight.addActionListener(buttonMoveClickedListener); + Draw(); } public void Draw() { - if (_drawingPlane == null) { - return; - } - - Graphics g = pictureBox.getGraphics(); - pictureBox.paint(g); - _drawingPlane.DrawTransport(g); + pictureBox.repaint(); } private void createUIComponents() { @@ -144,5 +155,7 @@ public class FormStormtrooper extends JDialog { "MoveToRightBottom" }; comboBoxStrategy = new JComboBox(strategiesList); + pictureBox = new Canvas(); + pictureBox.setBounds(new Rectangle(400, 300)); } } diff --git a/ProjectStormtrooper/FrameStormtrooper.java b/ProjectStormtrooper/FrameStormtrooper.java index c4dcebb..302a6d0 100644 --- a/ProjectStormtrooper/FrameStormtrooper.java +++ b/ProjectStormtrooper/FrameStormtrooper.java @@ -3,13 +3,13 @@ package ProjectStormtrooper; import javax.swing.*; public class FrameStormtrooper extends JFrame { - public FormStormtrooper _formPlaneCollection; + public FormStormtrooper _formStromtrooper; public FrameStormtrooper() { super(); setTitle("Штурмовик"); setDefaultCloseOperation(DISPOSE_ON_CLOSE); - _formPlaneCollection = new FormStormtrooper(); - setContentPane(_formPlaneCollection.getPictureBox()); + _formStromtrooper = new FormStormtrooper(); + setContentPane(_formStromtrooper.getPictureBox()); setDefaultLookAndFeelDecorated(false); setLocation(300, 100); pack(); diff --git a/ProjectStormtrooper/PlanesGenericCollection.java b/ProjectStormtrooper/PlanesGenericCollection.java index 6c8f481..d263455 100644 --- a/ProjectStormtrooper/PlanesGenericCollection.java +++ b/ProjectStormtrooper/PlanesGenericCollection.java @@ -1,6 +1,7 @@ package ProjectStormtrooper; import java.awt.*; +import java.util.Stack; public class PlanesGenericCollection { private int _pictureWidth; diff --git a/ProjectStormtrooper/PlanesGenericStorage.java b/ProjectStormtrooper/PlanesGenericStorage.java index 18f4864..a86d59d 100644 --- a/ProjectStormtrooper/PlanesGenericStorage.java +++ b/ProjectStormtrooper/PlanesGenericStorage.java @@ -33,4 +33,10 @@ public class PlanesGenericStorage { return _planeStorages.get(ind); return null; } + public DrawingObjectPlane GetByDoubleParameter(String storageName, int planeIndex) { + if (_planeStorages.containsKey(storageName)) { + return _planeStorages.get(storageName).GetU(planeIndex); + } + return null; + } }