Реализована базовая лаба

This commit is contained in:
Никита Потапов 2023-10-24 14:17:48 +04:00
parent 88eea507fe
commit deab7a4c3f
3 changed files with 144 additions and 16 deletions

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="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="10" 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/>
@ -22,7 +22,7 @@
<children> <children>
<component id="c9ec0" class="javax.swing.JButton" binding="buttonAddPlane"> <component id="c9ec0" class="javax.swing.JButton" binding="buttonAddPlane">
<constraints> <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> </constraints>
<properties> <properties>
<text value="Добавить самолет"/> <text value="Добавить самолет"/>
@ -30,12 +30,12 @@
</component> </component>
<vspacer id="82bd6"> <vspacer id="82bd6">
<constraints> <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> </constraints>
</vspacer> </vspacer>
<component id="17d80" class="javax.swing.JTextField" binding="textFieldNumber"> <component id="17d80" class="javax.swing.JTextField" binding="textFieldNumber">
<constraints> <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"/> <preferred-size width="150" height="-1"/>
</grid> </grid>
</constraints> </constraints>
@ -43,7 +43,7 @@
</component> </component>
<component id="639f2" class="javax.swing.JButton" binding="buttonRemovePlane"> <component id="639f2" class="javax.swing.JButton" binding="buttonRemovePlane">
<constraints> <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> </constraints>
<properties> <properties>
<text value="Удалить самолет"/> <text value="Удалить самолет"/>
@ -51,7 +51,7 @@
</component> </component>
<component id="86611" class="javax.swing.JButton" binding="buttonRefreshCollection"> <component id="86611" class="javax.swing.JButton" binding="buttonRefreshCollection">
<constraints> <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> </constraints>
<properties> <properties>
<text value="Обновить коллекцию"/> <text value="Обновить коллекцию"/>
@ -59,22 +59,69 @@
</component> </component>
<vspacer id="75ae2"> <vspacer id="75ae2">
<constraints> <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> </constraints>
</vspacer> </vspacer>
<vspacer id="7b32d"> <vspacer id="7b32d">
<constraints> <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> </constraints>
</vspacer> </vspacer>
<component id="a5cad" class="javax.swing.JButton" binding="buttonOpenGenerateWindow"> <component id="a5cad" class="javax.swing.JButton" binding="buttonOpenGenerateWindow">
<constraints> <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> </constraints>
<properties> <properties>
<text value="Генерировать самолеты"/> <text value="Генерировать самолеты"/>
</properties> </properties>
</component> </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>
</children> </children>
</grid> </grid>
</children> </children>

View File

@ -1,11 +1,13 @@
package ProjectStormtrooper; package ProjectStormtrooper;
import javax.swing.*; import javax.swing.*;
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;
public class FormPlaneCollection { public class FormPlaneCollection {
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> _planes; final PlanesGenericStorage _storage;
FrameDoubleParametrized _frameDoubleParametrized; FrameDoubleParametrized _frameDoubleParametrized;
private JPanel PanelWrapper; private JPanel PanelWrapper;
private JPanel GroupBoxInstruments; private JPanel GroupBoxInstruments;
@ -15,6 +17,11 @@ public class FormPlaneCollection {
private JButton buttonRemovePlane; private JButton buttonRemovePlane;
private JButton buttonRefreshCollection; private JButton buttonRefreshCollection;
private JButton buttonOpenGenerateWindow; private JButton buttonOpenGenerateWindow;
private JTextField textFieldStorageName;
private JButton buttonAddStorage;
private JList listBoxStorages;
private JButton buttonRemoveStorage;
private JPanel storagesPanel;
public DrawingPlane SelectedPlane; public DrawingPlane SelectedPlane;
@ -24,21 +31,81 @@ public class FormPlaneCollection {
public FormPlaneCollection() { public FormPlaneCollection() {
PictureBoxCollection.setPreferredSize(new Dimension(600, 500)); PictureBoxCollection.setPreferredSize(new Dimension(600, 500));
_planes = new PlanesGenericCollection<>(600, 500); _storage = new PlanesGenericStorage(600, 500);
buttonAddPlane.addActionListener(this::buttonAddPlaneClicked); buttonAddPlane.addActionListener(this::buttonAddPlaneClicked);
buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked); buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked);
buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked); buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked);
buttonOpenGenerateWindow.addActionListener(this::buttonOpenGenerateWindowClicked); buttonOpenGenerateWindow.addActionListener(this::buttonOpenGenerateWindowClicked);
buttonAddStorage.addActionListener(this::buttonAddStorageClicked);
buttonRemoveStorage.addActionListener(this::buttonRemoveStorageClicked);
listBoxStorages.addListSelectionListener(this::listBoxObjectsSelectedIndexChanged);
}
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) { 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 frameStormtrooper = new FrameStormtrooper();
frameStormtrooper.setVisible(true); frameStormtrooper.setVisible(true);
frameStormtrooper._formPlaneCollection.buttonSelectPlane.addActionListener(ev -> { frameStormtrooper._formPlaneCollection.buttonSelectPlane.addActionListener(ev -> {
SelectedPlane = frameStormtrooper._formPlaneCollection._drawingPlane; SelectedPlane = frameStormtrooper._formPlaneCollection._drawingPlane;
frameStormtrooper.dispose(); frameStormtrooper.dispose();
if (SelectedPlane != null) { if (SelectedPlane != null) {
if (_planes.Add(SelectedPlane) > -1) { if (obj.Add(SelectedPlane) > -1) {
refreshPictureBox(); refreshPictureBox();
JOptionPane.showMessageDialog(this.getPanelWrapper(), JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Объект добавлен", "Объект добавлен",
@ -55,6 +122,13 @@ public class FormPlaneCollection {
} }
public void buttonRemovePlaneClicked(ActionEvent e) { public void buttonRemovePlaneClicked(ActionEvent e) {
if (listBoxStorages.getSelectedIndex() == -1) {
return;
}
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
if (obj == null) {
return;
}
int pos; int pos;
try { try {
pos = Integer.parseInt(textFieldNumber.getText()); pos = Integer.parseInt(textFieldNumber.getText());
@ -78,7 +152,7 @@ public class FormPlaneCollection {
if (n == 1) { if (n == 1) {
return; return;
} }
if (_planes.Sub(pos) != null) { if (obj.Sub(pos) != null) {
refreshPictureBox(); refreshPictureBox();
JOptionPane.showMessageDialog(this.getPanelWrapper(), JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Объект удален", "Объект удален",
@ -105,8 +179,15 @@ public class FormPlaneCollection {
} }
public void refreshPictureBox() { public void refreshPictureBox() {
if (listBoxStorages.getSelectedIndex() == -1) {
return;
}
var obj = _storage.Get(listBoxStorages.getSelectedValue().toString());
if (obj == null) {
return;
}
Graphics g = PictureBoxCollection.getGraphics(); Graphics g = PictureBoxCollection.getGraphics();
PictureBoxCollection.paint(g); PictureBoxCollection.paint(g);
_planes.ShowPlanes(g); obj.ShowPlanes(g);
} }
} }

View File

@ -7,8 +7,8 @@ import java.util.List;
public class PlanesGenericStorage { public class PlanesGenericStorage {
final HashMap<String, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> _planeStorages; final HashMap<String, PlanesGenericCollection<DrawingPlane, DrawingObjectPlane>> _planeStorages;
public ArrayList<String> Keys() { public List<String> Keys() {
return (ArrayList<String>) _planeStorages.keySet().stream().toList(); return _planeStorages.keySet().stream().toList();
} }
private final int _pictureWidth; private final int _pictureWidth;