PIbd-21 Potapov N.S. LabWork04 Hard #4
@ -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;
|
||||
|
@ -14,7 +14,7 @@
|
||||
<border type="none"/>
|
||||
<children/>
|
||||
</grid>
|
||||
<grid id="7baef" binding="GroupBoxInstruments" layout-manager="GridLayoutManager" row-count="8" 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"/>
|
||||
<constraints border-constraint="East"/>
|
||||
<properties/>
|
||||
@ -22,7 +22,7 @@
|
||||
<children>
|
||||
<component id="c9ec0" class="javax.swing.JButton" binding="buttonAddPlane">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="2" 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="Добавить самолет"/>
|
||||
@ -30,12 +30,12 @@
|
||||
</component>
|
||||
<vspacer id="82bd6">
|
||||
<constraints>
|
||||
<grid row="6" 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="8" 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>
|
||||
</vspacer>
|
||||
<component id="17d80" class="javax.swing.JTextField" binding="textFieldNumber">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
@ -43,7 +43,7 @@
|
||||
</component>
|
||||
<component id="639f2" class="javax.swing.JButton" binding="buttonRemovePlane">
|
||||
<constraints>
|
||||
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="5" 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="Удалить самолет"/>
|
||||
@ -51,7 +51,7 @@
|
||||
</component>
|
||||
<component id="86611" class="javax.swing.JButton" binding="buttonRefreshCollection">
|
||||
<constraints>
|
||||
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="7" 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="Обновить коллекцию"/>
|
||||
@ -59,22 +59,77 @@
|
||||
</component>
|
||||
<vspacer id="75ae2">
|
||||
<constraints>
|
||||
<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="3" 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>
|
||||
</vspacer>
|
||||
<vspacer id="7b32d">
|
||||
<constraints>
|
||||
<grid row="4" 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="6" 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>
|
||||
</vspacer>
|
||||
<component id="a5cad" class="javax.swing.JButton" binding="buttonOpenGenerateWindow">
|
||||
<constraints>
|
||||
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<grid row="9" 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>
|
||||
<grid id="7e3fb" binding="storagesPanel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="f806" class="javax.swing.JTextField" binding="textFieldStorageName">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="78225" class="javax.swing.JButton" binding="buttonAddStorage">
|
||||
<constraints>
|
||||
<grid row="1" 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>
|
||||
<component id="ab120" class="javax.swing.JList" binding="listBoxStorages">
|
||||
<constraints>
|
||||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
|
||||
<preferred-size width="150" height="50"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
<properties/>
|
||||
</component>
|
||||
<component id="98764" class="javax.swing.JButton" binding="buttonRemoveStorage">
|
||||
<constraints>
|
||||
<grid row="3" 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>
|
||||
</grid>
|
||||
<vspacer id="5d143">
|
||||
<constraints>
|
||||
<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>
|
||||
</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>
|
||||
</grid>
|
||||
</children>
|
||||
|
@ -1,12 +1,16 @@
|
||||
package ProjectStormtrooper;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.util.Objects;
|
||||
import java.util.Stack;
|
||||
|
||||
public class FormPlaneCollection {
|
||||
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> _planes;
|
||||
final PlanesGenericStorage _storage;
|
||||
FrameDoubleParametrized _frameDoubleParametrized;
|
||||
FrameStormtrooper _frameRemovedPlanes;
|
||||
private JPanel PanelWrapper;
|
||||
private JPanel GroupBoxInstruments;
|
||||
private JPanel PictureBoxCollection;
|
||||
@ -15,7 +19,14 @@ public class FormPlaneCollection {
|
||||
private JButton buttonRemovePlane;
|
||||
private JButton buttonRefreshCollection;
|
||||
private JButton buttonOpenGenerateWindow;
|
||||
private JTextField textFieldStorageName;
|
||||
private JButton buttonAddStorage;
|
||||
private JList listBoxStorages;
|
||||
private JButton buttonRemoveStorage;
|
||||
private JPanel storagesPanel;
|
||||
private JButton buttonShowRemovedPlanes;
|
||||
public DrawingPlane SelectedPlane;
|
||||
Stack<DrawingPlane> _removedPlanes;
|
||||
|
||||
|
||||
public JPanel getPanelWrapper() {
|
||||
@ -24,21 +35,83 @@ public class FormPlaneCollection {
|
||||
|
||||
public FormPlaneCollection() {
|
||||
PictureBoxCollection.setPreferredSize(new Dimension(600, 500));
|
||||
_planes = new PlanesGenericCollection<>(600, 500);
|
||||
_storage = new PlanesGenericStorage(600, 500);
|
||||
_removedPlanes = new Stack<>();
|
||||
buttonAddPlane.addActionListener(this::buttonAddPlaneClicked);
|
||||
buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked);
|
||||
buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked);
|
||||
buttonOpenGenerateWindow.addActionListener(this::buttonOpenGenerateWindowClicked);
|
||||
buttonAddStorage.addActionListener(this::buttonAddStorageClicked);
|
||||
buttonRemoveStorage.addActionListener(this::buttonRemoveStorageClicked);
|
||||
listBoxStorages.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged);
|
||||
buttonShowRemovedPlanes.addActionListener(this::buttonShowRemovedPlanesClicked);
|
||||
}
|
||||
|
||||
private void ReloadObjects() {
|
||||
int index = listBoxStorages.getSelectedIndex();
|
||||
listBoxStorages.setListData(_storage.Keys().toArray());
|
||||
if (listBoxStorages.getModel().getSize() > 0 && (index == -1 || index >= listBoxStorages.getModel().getSize())) {
|
||||
listBoxStorages.setSelectedIndex(0);
|
||||
} else if (listBoxStorages.getModel().getSize() > 0 && index > -1 && index < listBoxStorages.getModel().getSize()) {
|
||||
listBoxStorages.setSelectedIndex(index);
|
||||
}
|
||||
listBoxStorages.invalidate();
|
||||
}
|
||||
|
||||
private void buttonAddStorageClicked(ActionEvent e) {
|
||||
String storageName = textFieldStorageName.getText();
|
||||
if (Objects.equals(storageName, "")) {
|
||||
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
||||
"Введите название",
|
||||
"Ошибка",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
_storage.AddSet(storageName);
|
||||
ReloadObjects();
|
||||
}
|
||||
|
||||
private void listBoxObjectsSelectedIndexChanged(ListSelectionEvent e) {
|
||||
refreshPictureBox();
|
||||
}
|
||||
|
||||
private void buttonRemoveStorageClicked(ActionEvent e) {
|
||||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
Object[] options = {"Да", "Нет"};
|
||||
int n = JOptionPane.showOptionDialog(this.getPanelWrapper(),
|
||||
"Удалить объект?",
|
||||
"Все серьезно",
|
||||
JOptionPane.YES_NO_OPTION,
|
||||
JOptionPane.QUESTION_MESSAGE,
|
||||
null,
|
||||
options,
|
||||
options[0]
|
||||
);
|
||||
if (n == 1) {
|
||||
return;
|
||||
}
|
||||
_storage.DelSet(listBoxStorages.getSelectedValue().toString());
|
||||
ReloadObjects();
|
||||
}
|
||||
|
||||
public void buttonAddPlaneClicked(ActionEvent e) {
|
||||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
|
||||
if (obj == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
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 (_planes.Add(SelectedPlane) > -1) {
|
||||
if (obj.Add(SelectedPlane) > -1) {
|
||||
refreshPictureBox();
|
||||
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
||||
"Объект добавлен",
|
||||
@ -55,6 +128,13 @@ public class FormPlaneCollection {
|
||||
}
|
||||
|
||||
public void buttonRemovePlaneClicked(ActionEvent e) {
|
||||
if (listBoxStorages.getSelectedIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
|
||||
if (obj == null) {
|
||||
return;
|
||||
}
|
||||
int pos;
|
||||
try {
|
||||
pos = Integer.parseInt(textFieldNumber.getText());
|
||||
@ -78,7 +158,9 @@ public class FormPlaneCollection {
|
||||
if (n == 1) {
|
||||
return;
|
||||
}
|
||||
if (_planes.Sub(pos) != null) {
|
||||
DrawingPlane removedPlane = obj.Sub(pos);
|
||||
if (removedPlane != null) {
|
||||
_removedPlanes.push(removedPlane);
|
||||
refreshPictureBox();
|
||||
JOptionPane.showMessageDialog(this.getPanelWrapper(),
|
||||
"Объект удален",
|
||||
@ -104,9 +186,30 @@ 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;
|
||||
}
|
||||
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
|
||||
if (obj == null) {
|
||||
return;
|
||||
}
|
||||
Graphics g = PictureBoxCollection.getGraphics();
|
||||
PictureBoxCollection.paint(g);
|
||||
_planes.ShowPlanes(g);
|
||||
obj.ShowPlanes(g);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="900" height="500"/>
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ProjectStormtrooper;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Stack;
|
||||
|
||||
public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveableObject> {
|
||||
private int _pictureWidth;
|
||||
@ -25,7 +26,11 @@ public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveable
|
||||
}
|
||||
|
||||
public T Sub(int pos) {
|
||||
return _collection.Remove(pos);
|
||||
T obj = _collection.Get(pos);
|
||||
if (obj != null) {
|
||||
_collection.Remove(pos);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
public U GetU(int pos) {
|
||||
@ -55,18 +60,19 @@ public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveable
|
||||
private void DrawObjects(Graphics g) {
|
||||
int placesColumnCount = _pictureHeight / _placeSizeHeight;
|
||||
int placesRowCount = _pictureWidth / _placeSizeWidth;
|
||||
for (int i = 0; i < _collection.Count; i++) {
|
||||
// получение объекта
|
||||
var obj = _collection.Get(i);
|
||||
int i = 0;
|
||||
for (var obj :
|
||||
_collection.GetEnumerator()) {
|
||||
// установка позиции
|
||||
if (obj == null)
|
||||
continue;
|
||||
obj.SetPosition(
|
||||
(placesRowCount - 1) * _placeSizeWidth - (i % placesColumnCount * _placeSizeWidth) + (_placeSizeWidth - obj.GetWidth()) / 2,
|
||||
i / placesColumnCount * _placeSizeHeight + (_placeSizeHeight - obj.GetHeight()) / 2
|
||||
);
|
||||
// прорисовка объекта
|
||||
obj.DrawTransport(g);
|
||||
if (obj != null) {
|
||||
obj.SetPosition(
|
||||
(placesRowCount - 1) * _placeSizeWidth - (i % placesColumnCount * _placeSizeWidth) + (_placeSizeWidth - obj.GetWidth()) / 2,
|
||||
i / placesColumnCount * _placeSizeHeight + (_placeSizeHeight - obj.GetHeight()) / 2
|
||||
);
|
||||
// прорисовка объекта
|
||||
obj.DrawTransport(g);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
42
ProjectStormtrooper/PlanesGenericStorage.java
Normal file
42
ProjectStormtrooper/PlanesGenericStorage.java
Normal file
@ -0,0 +1,42 @@
|
||||
package ProjectStormtrooper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class PlanesGenericStorage {
|
||||
final HashMap<String, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> _planeStorages;
|
||||
|
||||
public List<String> Keys() {
|
||||
return _planeStorages.keySet().stream().toList();
|
||||
}
|
||||
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
|
||||
public PlanesGenericStorage(int pictureWidth, int pictureHeight) {
|
||||
_planeStorages = new HashMap<>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
|
||||
public void AddSet(String name) {
|
||||
_planeStorages.put(name, new PlanesGenericCollection<>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
|
||||
public void DelSet(String name) {
|
||||
_planeStorages.remove(name);
|
||||
}
|
||||
|
||||
public PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> Get(String ind) {
|
||||
if (_planeStorages.containsKey(ind))
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,12 +1,20 @@
|
||||
package ProjectStormtrooper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SetGeneric<T extends DrawingPlane> {
|
||||
private T[] _places;
|
||||
public int Count;
|
||||
private final ArrayList<T> _places;
|
||||
|
||||
public int Count() {
|
||||
return _places.size();
|
||||
}
|
||||
|
||||
private final int _maxCount;
|
||||
|
||||
public SetGeneric(int count) {
|
||||
_places = (T[]) new DrawingPlane[count];
|
||||
Count = count;
|
||||
_maxCount = count;
|
||||
_places = new ArrayList<>(count);
|
||||
}
|
||||
|
||||
public int Insert(T plane) {
|
||||
@ -15,51 +23,44 @@ public class SetGeneric<T extends DrawingPlane> {
|
||||
|
||||
public int Insert(T plane, int position) {
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= Count) {
|
||||
if (position < 0 || position >= _maxCount) {
|
||||
return -1;
|
||||
}
|
||||
// Проверка, что элемент массива по этой позиции пустой
|
||||
if (_places[position] != null) {
|
||||
// Проверка, что после вставляемого элемента в массиве есть пустой элемент
|
||||
int nullIndex = -1;
|
||||
for (int i = position + 1; i < Count; i++) {
|
||||
if (_places[i] == null) {
|
||||
nullIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Если пустого элемента нет, то выходим
|
||||
if (nullIndex < 0) {
|
||||
return -1;
|
||||
}
|
||||
// Сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
|
||||
int j = nullIndex - 1;
|
||||
while (j >= position) {
|
||||
_places[j + 1] = _places[j];
|
||||
j--;
|
||||
}
|
||||
}
|
||||
// Вставка по позиции
|
||||
_places[position] = plane;
|
||||
_places.add(position, plane);
|
||||
return position;
|
||||
}
|
||||
|
||||
public T Remove(int position) {
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= Count) {
|
||||
if (position < 0 || position >= Count()) {
|
||||
return null;
|
||||
}
|
||||
// Удаление объекта из массива, присвоив элементу массива значение null
|
||||
T plane = _places[position];
|
||||
_places[position] = null;
|
||||
T plane = _places.get(position);
|
||||
_places.set(position, null);
|
||||
return plane;
|
||||
}
|
||||
|
||||
public T Get(int position) {
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= Count) {
|
||||
if (position < 0 || position >= Count()) {
|
||||
return null;
|
||||
}
|
||||
return _places[position];
|
||||
return _places.get(position);
|
||||
}
|
||||
|
||||
public void Set(int position, T plane) {
|
||||
// Проверка позиции
|
||||
// Проверка свободных мест в списке
|
||||
if (position < 0 || position >= _maxCount || Count() == _maxCount) {
|
||||
return;
|
||||
}
|
||||
// Вставка в список по позиции
|
||||
_places.set(position, plane);
|
||||
}
|
||||
|
||||
public ArrayList<T> GetEnumerator() {
|
||||
return _places;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user