Реализована усложненная часть

This commit is contained in:
Никита Потапов 2023-10-22 13:01:12 +04:00
parent 68903479c1
commit 9f8be965e5
8 changed files with 222 additions and 17 deletions

View File

@ -0,0 +1,41 @@
package ProjectStormtrooper;
import java.util.ArrayList;
import java.util.Random;
public class DoubleParametrized<T extends EntityPlane, U extends IDrawingEngines> {
ArrayList<T> _entityPlanes;
ArrayList<U> _drawingEngines;
public DoubleParametrized() {
_entityPlanes = new ArrayList<>();
_drawingEngines = new ArrayList<>();
}
public void Add(T entityPlaneObject) {
_entityPlanes.add(entityPlaneObject);
}
public void Add(U drawingEnginesObject) {
_drawingEngines.add(drawingEnginesObject);
}
public DrawingPlane GeneratePlane(int pictureWidth, int pictureHeight) {
Random random = new Random();
if (_entityPlanes.isEmpty()) {
return null;
}
T entityPlane = _entityPlanes.get(random.nextInt(_entityPlanes.size()));
U drawingEngine = null;
if (!_drawingEngines.isEmpty()) {
drawingEngine = _drawingEngines.get(random.nextInt(_drawingEngines.size()));
}
DrawingPlane drawingPlane;
if (entityPlane instanceof EntityStormtrooper) {
drawingPlane = new DrawingStormtrooper((EntityStormtrooper) entityPlane, drawingEngine, pictureWidth, pictureHeight);
} else {
drawingPlane = new DrawingPlane(entityPlane, drawingEngine, pictureWidth, pictureHeight);
}
return drawingPlane;
}
}

View File

@ -53,6 +53,19 @@ public class DrawingPlane {
_planeHeight = planeHeight; _planeHeight = planeHeight;
} }
protected DrawingPlane(EntityPlane entityPlane, IDrawingEngines drawingEngines, int width, int height) {
EntityPlane = entityPlane;
_drawingEngines = drawingEngines;
_pictureWidth = width;
_pictureHeight = height;
}
protected DrawingPlane(EntityPlane entityPlane, IDrawingEngines drawingEngines, int width, int height, int planeWidth, int planeHeight) {
this(entityPlane, drawingEngines, width, height);
_planeWidth = planeWidth;
_planeHeight = planeHeight;
}
public void SetEnginesCount(int enginesCount) { public void SetEnginesCount(int enginesCount) {
_drawingEngines.SetEnumEnginesCount(enginesCount); _drawingEngines.SetEnumEnginesCount(enginesCount);
} }

View File

@ -12,6 +12,10 @@ public class DrawingStormtrooper extends DrawingPlane {
} }
} }
public DrawingStormtrooper(EntityStormtrooper entityStormtrooper, IDrawingEngines drawingEngines, int width, int height) {
super(entityStormtrooper, drawingEngines, width, height, 140, 90);
}
public void DrawTransport(Graphics g) { public void DrawTransport(Graphics g) {
if (EntityPlane == null) { if (EntityPlane == null) {
return; return;

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectStormtrooper.FormDoubleParametrized">
<grid id="27dc6" binding="PictureBox" layout-manager="GridLayoutManager" row-count="2" column-count="2" 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="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="d5201" class="javax.swing.JButton" binding="buttonGeneratePlane">
<constraints>
<grid row="0" column="1" 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>
<hspacer id="25591">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="1328a">
<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>
</grid>
</form>

View File

@ -0,0 +1,78 @@
package ProjectStormtrooper;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
public class FormDoubleParametrized {
private JPanel PictureBox;
private JButton buttonGeneratePlane;
Random _random;
DrawingPlane _drawingPlane;
DoubleParametrized<EntityPlane, IDrawingEngines> _doubleParametrized;
public JPanel getPictureBox() {
return PictureBox;
}
public FormDoubleParametrized() {
_random = new Random();
_doubleParametrized = new DoubleParametrized<>();
PictureBox.setPreferredSize(new Dimension(400, 400));
buttonGeneratePlane.addActionListener(this::buttonGeneratePlaneClicked);
}
private void addRandomEntityPlane() {
EntityPlane entityPlane;
if (_random.nextBoolean()) {
entityPlane = new EntityPlane(
_random.nextInt(100, 300),
_random.nextInt(1000, 3000),
new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256))
);
} else {
entityPlane = new EntityStormtrooper(
_random.nextInt(100, 300),
_random.nextInt(1000, 3000),
new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)),
new Color(_random.nextInt(256), _random.nextInt(256), _random.nextInt(256)),
_random.nextBoolean(),
_random.nextBoolean()
);
}
_doubleParametrized.Add(entityPlane);
}
private void addRandomDrawingEngine() {
int choice = _random.nextInt(3);
IDrawingEngines drawingEngines;
if (choice == 0) {
drawingEngines = new DrawingEnginesSimple();
} else if (choice == 1) {
drawingEngines = new DrawingEnginesEllipse();
} else {
drawingEngines = new DrawingEnginesPyramid();
}
drawingEngines.SetEnumEnginesCount(_random.nextInt(2, 7));
_doubleParametrized.Add(drawingEngines);
}
public void buttonGeneratePlaneClicked(ActionEvent e) {
addRandomEntityPlane();
addRandomDrawingEngine();
_drawingPlane = _doubleParametrized.GeneratePlane(PictureBox.getWidth(), PictureBox.getHeight());
_drawingPlane.SetPosition((PictureBox.getWidth() - _drawingPlane._planeWidth) / 2, (PictureBox.getHeight() - _drawingPlane._planeHeight) / 2);
Draw();
}
public void Draw() {
if (_drawingPlane == null) {
return;
}
Graphics g = PictureBox.getGraphics();
PictureBox.paint(g);
_drawingPlane.DrawTransport(g);
}
}

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="7" 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="8" 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/>
@ -67,6 +67,14 @@
<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="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"/>
</constraints> </constraints>
</vspacer> </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"/>
</constraints>
<properties>
<text value="Генерировать самолеты"/>
</properties>
</component>
</children> </children>
</grid> </grid>
</children> </children>

View File

@ -6,6 +6,7 @@ import java.awt.event.ActionEvent;
public class FormPlaneCollection { public class FormPlaneCollection {
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> _planes; PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> _planes;
FrameDoubleParametrized _frameDoubleParametrized;
private JPanel PanelWrapper; private JPanel PanelWrapper;
private JPanel GroupBoxInstruments; private JPanel GroupBoxInstruments;
private JPanel PictureBoxCollection; private JPanel PictureBoxCollection;
@ -13,6 +14,7 @@ public class FormPlaneCollection {
private JTextField textFieldNumber; private JTextField textFieldNumber;
private JButton buttonRemovePlane; private JButton buttonRemovePlane;
private JButton buttonRefreshCollection; private JButton buttonRefreshCollection;
private JButton buttonOpenGenerateWindow;
public DrawingPlane SelectedPlane; public DrawingPlane SelectedPlane;
@ -26,6 +28,7 @@ public class FormPlaneCollection {
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);
} }
public void buttonAddPlaneClicked(ActionEvent e) { public void buttonAddPlaneClicked(ActionEvent e) {
@ -52,6 +55,16 @@ public class FormPlaneCollection {
} }
public void buttonRemovePlaneClicked(ActionEvent e) { public void buttonRemovePlaneClicked(ActionEvent e) {
int pos;
try {
pos = Integer.parseInt(textFieldNumber.getText());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Неверное значение",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
return;
}
Object[] options = {"Да", "Нет"}; Object[] options = {"Да", "Нет"};
int n = JOptionPane.showOptionDialog(this.getPanelWrapper(), int n = JOptionPane.showOptionDialog(this.getPanelWrapper(),
"Удалить объект?", "Удалить объект?",
@ -65,23 +78,15 @@ public class FormPlaneCollection {
if (n == 1) { if (n == 1) {
return; return;
} }
try { if (_planes.Sub(pos) != null) {
int pos = Integer.parseInt(textFieldNumber.getText()); refreshPictureBox();
if (_planes.Sub(pos) != null) {
refreshPictureBox();
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Объект удален",
"Успех",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Не удалось удалить объект",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this.getPanelWrapper(), JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Неверное значение", "Объект удален",
"Успех",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Не удалось удалить объект",
"Ошибка", "Ошибка",
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
} }
@ -91,6 +96,14 @@ public class FormPlaneCollection {
refreshPictureBox(); refreshPictureBox();
} }
public void buttonOpenGenerateWindowClicked(ActionEvent e) {
if (_frameDoubleParametrized != null) {
_frameDoubleParametrized.dispose();
}
_frameDoubleParametrized = new FrameDoubleParametrized();
_frameDoubleParametrized.setVisible(true);
}
public void refreshPictureBox() { public void refreshPictureBox() {
Graphics g = PictureBoxCollection.getGraphics(); Graphics g = PictureBoxCollection.getGraphics();
PictureBoxCollection.paint(g); PictureBoxCollection.paint(g);

View File

@ -0,0 +1,17 @@
package ProjectStormtrooper;
import javax.swing.*;
public class FrameDoubleParametrized extends JFrame {
public FormDoubleParametrized _formDoubleParametrized;
public FrameDoubleParametrized() {
super();
setTitle("Генерация самолетов");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
_formDoubleParametrized = new FormDoubleParametrized();
setContentPane(_formDoubleParametrized.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();
}
}