Фикс для работы удаления и отрисовки объекта

This commit is contained in:
Никита Потапов 2023-11-07 12:57:17 +04:00
parent deab7a4c3f
commit 25b615e736
8 changed files with 93 additions and 42 deletions

View File

@ -1,9 +1,10 @@
package ProjectStormtrooper; package ProjectStormtrooper;
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.Random;
public class DrawingPlane { public class DrawingPlane extends JPanel {
public EntityPlane EntityPlane; public EntityPlane EntityPlane;
protected IDrawingEngines _drawingEngines; protected IDrawingEngines _drawingEngines;
protected int _pictureWidth; protected int _pictureWidth;

View File

@ -14,7 +14,7 @@
<border type="none"/> <border type="none"/>
<children/> <children/>
</grid> </grid>
<grid id="7baef" binding="GroupBoxInstruments" layout-manager="GridLayoutManager" row-count="10" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="7baef" binding="GroupBoxInstruments" layout-manager="GridLayoutManager" row-count="11" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="East"/> <constraints border-constraint="East"/>
<properties/> <properties/>
@ -122,6 +122,14 @@
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</vspacer> </vspacer>
<component id="a5c67" class="javax.swing.JButton" binding="buttonShowRemovedPlanes">
<constraints>
<grid row="10" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Показать удаленный"/>
</properties>
</component>
</children> </children>
</grid> </grid>
</children> </children>

View File

@ -5,10 +5,12 @@ import javax.swing.event.ListSelectionEvent;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.util.Objects; import java.util.Objects;
import java.util.Stack;
public class FormPlaneCollection { public class FormPlaneCollection {
final PlanesGenericStorage _storage; final PlanesGenericStorage _storage;
FrameDoubleParametrized _frameDoubleParametrized; FrameDoubleParametrized _frameDoubleParametrized;
FrameStormtrooper _frameRemovedPlanes;
private JPanel PanelWrapper; private JPanel PanelWrapper;
private JPanel GroupBoxInstruments; private JPanel GroupBoxInstruments;
private JPanel PictureBoxCollection; private JPanel PictureBoxCollection;
@ -22,7 +24,9 @@ public class FormPlaneCollection {
private JList listBoxStorages; private JList listBoxStorages;
private JButton buttonRemoveStorage; private JButton buttonRemoveStorage;
private JPanel storagesPanel; private JPanel storagesPanel;
private JButton buttonShowRemovedPlanes;
public DrawingPlane SelectedPlane; public DrawingPlane SelectedPlane;
Stack<DrawingPlane> _removedPlanes;
public JPanel getPanelWrapper() { public JPanel getPanelWrapper() {
@ -32,6 +36,7 @@ public class FormPlaneCollection {
public FormPlaneCollection() { public FormPlaneCollection() {
PictureBoxCollection.setPreferredSize(new Dimension(600, 500)); PictureBoxCollection.setPreferredSize(new Dimension(600, 500));
_storage = new PlanesGenericStorage(600, 500); _storage = new PlanesGenericStorage(600, 500);
_removedPlanes = new Stack<>();
buttonAddPlane.addActionListener(this::buttonAddPlaneClicked); buttonAddPlane.addActionListener(this::buttonAddPlaneClicked);
buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked); buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked);
buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked); buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked);
@ -39,6 +44,7 @@ public class FormPlaneCollection {
buttonAddStorage.addActionListener(this::buttonAddStorageClicked); buttonAddStorage.addActionListener(this::buttonAddStorageClicked);
buttonRemoveStorage.addActionListener(this::buttonRemoveStorageClicked); buttonRemoveStorage.addActionListener(this::buttonRemoveStorageClicked);
listBoxStorages.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged); listBoxStorages.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged);
buttonShowRemovedPlanes.addActionListener(this::buttonShowRemovedPlanesClicked);
} }
private void ReloadObjects() { private void ReloadObjects() {
@ -101,8 +107,8 @@ public class FormPlaneCollection {
FrameStormtrooper frameStormtrooper = new FrameStormtrooper(); FrameStormtrooper frameStormtrooper = new FrameStormtrooper();
frameStormtrooper.setVisible(true); frameStormtrooper.setVisible(true);
frameStormtrooper._formPlaneCollection.buttonSelectPlane.addActionListener(ev -> { frameStormtrooper._formStromtrooper.buttonSelectPlane.addActionListener(ev -> {
SelectedPlane = frameStormtrooper._formPlaneCollection._drawingPlane; SelectedPlane = frameStormtrooper._formStromtrooper._drawingPlane;
frameStormtrooper.dispose(); frameStormtrooper.dispose();
if (SelectedPlane != null) { if (SelectedPlane != null) {
if (obj.Add(SelectedPlane) > -1) { if (obj.Add(SelectedPlane) > -1) {
@ -152,7 +158,9 @@ public class FormPlaneCollection {
if (n == 1) { if (n == 1) {
return; return;
} }
if (obj.Sub(pos) != null) { DrawingPlane removedPlane = obj.Sub(pos);
if (removedPlane != null) {
_removedPlanes.push(removedPlane);
refreshPictureBox(); refreshPictureBox();
JOptionPane.showMessageDialog(this.getPanelWrapper(), JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Объект удален", "Объект удален",
@ -178,6 +186,20 @@ public class FormPlaneCollection {
_frameDoubleParametrized.setVisible(true); _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() { public void refreshPictureBox() {
if (listBoxStorages.getSelectedIndex() == -1) { if (listBoxStorages.getSelectedIndex() == -1) {
return; return;

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectStormtrooper.FormStormtrooper"> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectStormtrooper.FormStormtrooper">
<grid id="27dc6" binding="pictureBox" layout-manager="GridLayoutManager" row-count="5" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="27dc6" binding="pictureBox" custom-create="true" layout-manager="GridLayoutManager" row-count="5" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<xy x="20" y="20" width="900" height="500"/> <xy x="20" y="20" width="900" height="500"/>

View File

@ -3,6 +3,7 @@ package ProjectStormtrooper;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import javax.swing.JColorChooser; import javax.swing.JColorChooser;
import java.awt.event.ActionEvent;
import java.util.Random; import java.util.Random;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
@ -10,27 +11,43 @@ public class FormStormtrooper extends JDialog {
public DrawingPlane _drawingPlane; public DrawingPlane _drawingPlane;
AbstractStrategy _abstractStrategy; AbstractStrategy _abstractStrategy;
private JButton buttonCreateStormtrooper; private JButton buttonCreateStormtrooper;
private JPanel pictureBox; private JComponent pictureBox;
private JButton buttonDown; private JButton buttonDown;
private JButton buttonUp; private JButton buttonUp;
private JButton buttonLeft; private JButton buttonLeft;
private JButton buttonRight; public JButton buttonRight;
private JButton buttonCreatePlane; private JButton buttonCreatePlane;
private JComboBox comboBoxStrategy; private JComboBox comboBoxStrategy;
private JButton buttonStep; private JButton buttonStep;
public JButton buttonSelectPlane; public JButton buttonSelectPlane;
public JPanel getPictureBox() { public JComponent getPictureBox() {
return pictureBox; 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() { public FormStormtrooper() {
buttonUp.setName("buttonUp"); buttonUp.setName("buttonUp");
buttonDown.setName("buttonDown"); buttonDown.setName("buttonDown");
buttonLeft.setName("buttonLeft"); buttonLeft.setName("buttonLeft");
buttonRight.setName("buttonRight"); buttonRight.setName("buttonRight");
buttonCreateStormtrooper.addActionListener(e -> { buttonCreateStormtrooper.addActionListener(e -> {
Random random = new Random(); 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 -> { public void buttonMoveClickedListener(ActionEvent e) {
String buttonName = ((JButton) e.getSource()).getName(); String buttonName = ((JButton) e.getSource()).getName();
switch (buttonName) { switch (buttonName) {
case ("buttonUp") -> { case ("buttonUp") -> {
_drawingPlane.MoveTransport(EnumDirectionType.Up); _drawingPlane.MoveTransport(EnumDirectionType.Up);
}
case ("buttonDown") -> {
_drawingPlane.MoveTransport(EnumDirectionType.Down);
}
case ("buttonLeft") -> {
_drawingPlane.MoveTransport(EnumDirectionType.Left);
}
case ("buttonRight") -> {
_drawingPlane.MoveTransport(EnumDirectionType.Right);
}
} }
case ("buttonDown") -> {
_drawingPlane.MoveTransport(EnumDirectionType.Down);
}
case ("buttonLeft") -> {
_drawingPlane.MoveTransport(EnumDirectionType.Left);
}
case ("buttonRight") -> {
_drawingPlane.MoveTransport(EnumDirectionType.Right);
}
default -> _drawingPlane.MoveTransport(EnumDirectionType.Right);
}
Draw(); Draw();
};
buttonUp.addActionListener(buttonMoveClickedListener);
buttonDown.addActionListener(buttonMoveClickedListener);
buttonLeft.addActionListener(buttonMoveClickedListener);
buttonRight.addActionListener(buttonMoveClickedListener);
} }
public void Draw() { public void Draw() {
if (_drawingPlane == null) { pictureBox.repaint();
return;
}
Graphics g = pictureBox.getGraphics();
pictureBox.paint(g);
_drawingPlane.DrawTransport(g);
} }
private void createUIComponents() { private void createUIComponents() {
@ -144,5 +155,7 @@ public class FormStormtrooper extends JDialog {
"MoveToRightBottom" "MoveToRightBottom"
}; };
comboBoxStrategy = new JComboBox(strategiesList); comboBoxStrategy = new JComboBox(strategiesList);
pictureBox = new Canvas();
pictureBox.setBounds(new Rectangle(400, 300));
} }
} }

View File

@ -3,13 +3,13 @@ package ProjectStormtrooper;
import javax.swing.*; import javax.swing.*;
public class FrameStormtrooper extends JFrame { public class FrameStormtrooper extends JFrame {
public FormStormtrooper _formPlaneCollection; public FormStormtrooper _formStromtrooper;
public FrameStormtrooper() { public FrameStormtrooper() {
super(); super();
setTitle("Штурмовик"); setTitle("Штурмовик");
setDefaultCloseOperation(DISPOSE_ON_CLOSE); setDefaultCloseOperation(DISPOSE_ON_CLOSE);
_formPlaneCollection = new FormStormtrooper(); _formStromtrooper = new FormStormtrooper();
setContentPane(_formPlaneCollection.getPictureBox()); setContentPane(_formStromtrooper.getPictureBox());
setDefaultLookAndFeelDecorated(false); setDefaultLookAndFeelDecorated(false);
setLocation(300, 100); setLocation(300, 100);
pack(); pack();

View File

@ -1,6 +1,7 @@
package ProjectStormtrooper; package ProjectStormtrooper;
import java.awt.*; import java.awt.*;
import java.util.Stack;
public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveableObject> { public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveableObject> {
private int _pictureWidth; private int _pictureWidth;

View File

@ -33,4 +33,10 @@ public class PlanesGenericStorage {
return _planeStorages.get(ind); return _planeStorages.get(ind);
return null; return null;
} }
public DrawingObjectPlane GetByDoubleParameter(String storageName, int planeIndex) {
if (_planeStorages.containsKey(storageName)) {
return _planeStorages.get(storageName).GetU(planeIndex);
}
return null;
}
} }