Реализована базовая лаба, необходимо сделать усложненку

This commit is contained in:
Никита Потапов 2023-10-20 22:43:07 +04:00
parent b04f8401c3
commit e89cbc85a2
11 changed files with 164 additions and 70 deletions

View File

@ -121,6 +121,8 @@ public class DrawingPlane {
}
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(2));
Color bodyColor = EntityPlane.BodyColor;
Color blackColor = Color.BLACK;

View File

@ -13,22 +13,88 @@ public class FormPlaneCollection {
private JTextField textFieldNumber;
private JButton buttonRemovePlane;
private JButton buttonRefreshCollection;
public DrawingPlane SelectedPlane;
public JPanel getPanelWrapper() {
return PanelWrapper;
}
public FormPlaneCollection() {
_planes = new PlanesGenericCollection<>(PictureBoxCollection.getWidth(), PictureBoxCollection.getHeight());
PictureBoxCollection.setPreferredSize(new Dimension(600, 500));
_planes = new PlanesGenericCollection<>(600, 500);
buttonAddPlane.addActionListener(this::buttonAddPlaneClicked);
buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked);
buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked);
}
public void buttonAddPlaneClicked(ActionEvent e) {
//todo
FrameStormtrooper frameStormtrooper = new FrameStormtrooper();
frameStormtrooper.setVisible(true);
frameStormtrooper._formPlaneCollection.buttonSelectPlane.addActionListener(ev -> {
SelectedPlane = frameStormtrooper._formPlaneCollection._drawingPlane;
frameStormtrooper.dispose();
if (SelectedPlane != null) {
if (_planes.Add(SelectedPlane) > -1) {
refreshPictureBox();
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Объект добавлен",
"Успех",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Не удалось добавить объект",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
}
}
});
}
public void buttonRemovePlaneClicked(ActionEvent e) {
//todo
Object[] options = {"Да", "Нет"};
int n = JOptionPane.showOptionDialog(this.getPanelWrapper(),
"Удалить объект?",
"Все серьезно",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]
);
if (n == 1) {
return;
}
try {
int pos = Integer.parseInt(textFieldNumber.getText());
if (_planes.Sub(pos) != null) {
refreshPictureBox();
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Объект удален",
"Успех",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Не удалось удалить объект",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Неверное значение",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
}
}
public void buttonRefreshCollectionClicked(ActionEvent e) {
refreshPictureBox();
}
public void refreshPictureBox() {
Graphics g = PictureBoxCollection.getGraphics();
PictureBoxCollection.paint(g);
_planes.ShowPlanes(g);
}
}

View File

@ -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="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<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">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="900" height="500"/>
@ -21,7 +21,7 @@
</component>
<hspacer id="f9ba0">
<constraints>
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="4" column="3" 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="ce5ea">
@ -31,7 +31,7 @@
</vspacer>
<component id="ac2ff" class="javax.swing.JButton" binding="buttonDown">
<constraints>
<grid row="4" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="4" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -46,7 +46,7 @@
</component>
<component id="b1382" class="javax.swing.JButton" binding="buttonUp">
<constraints>
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="3" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -61,7 +61,7 @@
</component>
<component id="8b2ff" class="javax.swing.JButton" binding="buttonLeft">
<constraints>
<grid row="4" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="4" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -77,7 +77,7 @@
</component>
<component id="25771" class="javax.swing.JButton" binding="buttonRight">
<constraints>
<grid row="4" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<grid row="4" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false">
<minimum-size width="30" height="30"/>
<preferred-size width="30" height="30"/>
<maximum-size width="30" height="30"/>
@ -99,18 +99,26 @@
</component>
<component id="aefae" class="javax.swing.JComboBox" binding="comboBoxStrategy" custom-create="true">
<constraints>
<grid row="0" column="3" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="4" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="c79a6" class="javax.swing.JButton" binding="buttonStep">
<constraints>
<grid row="1" column="3" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="4" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Шаг"/>
</properties>
</component>
<component id="f339a" class="javax.swing.JButton" binding="buttonSelectPlane">
<constraints>
<grid row="4" column="2" 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>
</form>

View File

@ -2,11 +2,12 @@ package ProjectStormtrooper;
import javax.swing.*;
import java.awt.*;
import javax.swing.JColorChooser;
import java.util.Random;
import java.awt.event.ActionListener;
public class FormStormtrooper {
DrawingPlane _drawingPlane;
public class FormStormtrooper extends JDialog {
public DrawingPlane _drawingPlane;
AbstractStrategy _abstractStrategy;
private JButton buttonCreateStormtrooper;
private JPanel pictureBox;
@ -17,6 +18,7 @@ public class FormStormtrooper {
private JButton buttonCreatePlane;
private JComboBox comboBoxStrategy;
private JButton buttonStep;
public JButton buttonSelectPlane;
public JPanel getPictureBox() {
return pictureBox;
@ -32,11 +34,15 @@ public class FormStormtrooper {
buttonCreateStormtrooper.addActionListener(e -> {
Random random = new Random();
Color color = JColorChooser.showDialog(this.pictureBox, "Выберите цвет", Color.BLACK);
Color additionalColor = JColorChooser.showDialog(this.pictureBox, "Выберите дополнительный цвет", Color.BLACK);
_drawingPlane = new DrawingStormtrooper(
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)),
color,
additionalColor,
random.nextBoolean(),
random.nextBoolean(),
pictureBox.getWidth(),
@ -52,10 +58,12 @@ public class FormStormtrooper {
buttonCreatePlane.addActionListener(e -> {
Random random = new Random();
Color color = JColorChooser.showDialog(this.pictureBox, "Выберите цвет", Color.BLACK);
_drawingPlane = new DrawingPlane(
random.nextInt(100, 300),
random.nextInt(1000, 3000),
new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)),
color,
pictureBox.getWidth(),
pictureBox.getHeight()
);
@ -92,6 +100,7 @@ public class FormStormtrooper {
}
});
ActionListener buttonMoveClickedListener = e -> {
String buttonName = ((JButton) e.getSource()).getName();

View File

@ -0,0 +1,17 @@
package ProjectStormtrooper;
import javax.swing.*;
public class FramePlaneCollection extends JFrame {
FormPlaneCollection _formPlaneCollection;
public FramePlaneCollection() {
super();
setTitle("Набор самолетов");
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formPlaneCollection = new FormPlaneCollection();
setContentPane(_formPlaneCollection.getPanelWrapper());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();
}
}

View File

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

View File

@ -16,6 +16,8 @@ public interface IDrawingEngines {
}
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(2));
int engineWidth = 20;
int engineHeight = 6;

View File

@ -2,6 +2,7 @@ package ProjectStormtrooper;
public class Main {
public static void main(String[] args) {
MainFrameStormtrooper mainFrameStormtrooper = new MainFrameStormtrooper();
FramePlaneCollection framePlaneCollection = new FramePlaneCollection();
framePlaneCollection.setVisible(true);
}
}

View File

@ -1,19 +0,0 @@
package ProjectStormtrooper;
import javax.swing.*;
public class MainFrameStormtrooper extends JFrame {
private FormStormtrooper _formStormtrooper;
public MainFrameStormtrooper() {
super();
setTitle("Штурмовик");
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formStormtrooper = new FormStormtrooper();
setContentPane(_formStormtrooper.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();
setVisible(true);
}
}

View File

@ -17,17 +17,15 @@ public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveable
_collection = new SetGeneric<T>(horizontalObjectsCount * verticalObjectsCount);
}
public int Add(PlanesGenericCollection<T, U> collect, T obj) {
public int Add(T obj) {
if (obj == null) {
return -1;
}
if (collect != null)
return collect._collection.Insert(obj);
return -1;
return _collection.Insert(obj);
}
public T Sub(PlanesGenericCollection<T, U> collect, int pos) {
return collect._collection.Remove(pos);
public T Sub(int pos) {
return _collection.Remove(pos);
}
public U GetU(int pos) {
@ -57,7 +55,7 @@ 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++) {
for (int i = 0; i < _collection.Count; i++) {
// получение объекта
var obj = _collection.Get(i);
// установка позиции

View File

@ -2,44 +2,39 @@ package ProjectStormtrooper;
public class SetGeneric<T extends DrawingPlane> {
private T[] _places;
public int Count() {return _places.length;}
public SetGeneric(int count)
{
public int Count;
public SetGeneric(int count) {
_places = (T[]) new DrawingPlane[count];
Count = count;
}
public int Insert(T plane)
{
public int Insert(T plane) {
return Insert(plane, 0);
}
public int Insert(T plane, int position)
{
public int Insert(T plane, int position) {
// Проверка позиции
if (position < 0 || position >= Count())
{
if (position < 0 || position >= Count) {
return -1;
}
// Проверка, что элемент массива по этой позиции пустой
if (_places[position] != null)
{
if (_places[position] != null) {
// Проверка, что после вставляемого элемента в массиве есть пустой элемент
int nullIndex = -1;
for (int i = position + 1; i < Count(); i++)
{
if (_places[i] == null)
{
for (int i = position + 1; i < Count; i++) {
if (_places[i] == null) {
nullIndex = i;
break;
}
}
// Если пустого элемента нет, то выходим
if (nullIndex < 0)
{
if (nullIndex < 0) {
return -1;
}
// Сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
int j = nullIndex - 1;
while (j >= position)
{
while (j >= position) {
_places[j + 1] = _places[j];
j--;
}
@ -48,11 +43,10 @@ public class SetGeneric<T extends DrawingPlane> {
_places[position] = plane;
return position;
}
public T Remove(int position)
{
public T Remove(int position) {
// Проверка позиции
if (position < 0 || position >= Count())
{
if (position < 0 || position >= Count) {
return null;
}
// Удаление объекта из массива, присвоив элементу массива значение null
@ -60,11 +54,10 @@ public class SetGeneric<T extends DrawingPlane> {
_places[position] = null;
return plane;
}
public T Get(int position)
{
public T Get(int position) {
// Проверка позиции
if (position < 0 || position >= Count())
{
if (position < 0 || position >= Count) {
return null;
}
return _places[position];