SemesterFirstLabThirdBomberHard

This commit is contained in:
VanyaAlekseev 2023-12-24 22:52:28 +04:00
parent 5d4d809278
commit dc25d1861e
17 changed files with 586 additions and 37 deletions

View File

@ -0,0 +1,41 @@
package ProjectBomber;
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 EntityBomber) {
drawingPlane = new DrawingBomber((EntityBomber) entityPlane, drawingEngine, pictureWidth, pictureHeight);
} else {
drawingPlane = new DrawingPlane(entityPlane, drawingEngine, pictureWidth, pictureHeight);
}
return drawingPlane;
}
}

View File

@ -11,7 +11,9 @@ public class DrawingBomber extends DrawingPlane {
EntityPlane = new EntityBomber(speed, weight, bodyColor, additionalColor, fuelTanks, bombs);
}
}
public DrawingBomber(EntityBomber entityBomber, IDrawingEngines drawingEngines, int width, int height) {
super(entityBomber, drawingEngines, width, height, 140, 90);
}
public void DrawTransport(Graphics g) {
if (EntityPlane == null) {
return;

View File

@ -52,6 +52,18 @@ public class DrawingPlane {
_planeWidth = planeWidth;
_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) {
_drawingEngines.SetEnumEnginesCount(enginesCount);
}
@ -119,7 +131,7 @@ public class DrawingPlane {
return;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(2));
Color bodyColor = EntityPlane.BodyColor;
Color blackColor = Color.BLACK;
@ -179,4 +191,10 @@ public class DrawingPlane {
bodyHeight
);
}
public IMoveableObject GetMoveableObject() {
return new DrawingObjectPlane(this);
}
;
}

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="ProjectBomber.FormBomber">
<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"/>
@ -26,7 +26,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"/>
@ -41,7 +41,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"/>
@ -56,7 +56,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"/>
@ -72,7 +72,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"/>
@ -94,23 +94,31 @@
</component>
<hspacer id="80ae3">
<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>
<component id="9ddde" 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="848b4" 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="ddb72" 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

@ -4,9 +4,10 @@ import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.ActionListener;
import javax.swing.JColorChooser;
public class FormBomber {
DrawingPlane _drawingPlane;
public class FormBomber extends JDialog{
public DrawingPlane _drawingPlane;
AbstractStrategy _abstractStrategy;
private JButton buttonCreateBomber;
private JPanel pictureBox;
@ -17,6 +18,7 @@ public class FormBomber {
private JButton buttonCreatePlane;
private JComboBox comboBoxStrategy;
private JButton buttonStep;
public JButton buttonSelectPlane;
public JPanel getPictureBox() {
return pictureBox;
@ -32,11 +34,14 @@ public class FormBomber {
buttonCreateBomber.addActionListener(e -> {
Random random = new Random();
Color color = JColorChooser.showDialog(this.pictureBox, "Выберите цвет", Color.BLACK);
Color additionalColor = JColorChooser.showDialog(this.pictureBox, "Выберите дополнительный цвет", Color.BLACK);
_drawingPlane = new DrawingBomber(
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(),
@ -51,11 +56,11 @@ public class FormBomber {
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()
);

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="ProjectBomber.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 ProjectBomber;
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 EntityBomber(
_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

@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ProjectBomber.FormPlaneCollection">
<grid id="27dc6" binding="PanelWrapper" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<xy x="20" y="20" width="666" height="454"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="5f693" binding="PictureBoxCollection" layout-manager="GridLayoutManager" row-count="1" 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="Center"/>
<properties/>
<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">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="East"/>
<properties/>
<border type="none"/>
<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"/>
</constraints>
<properties>
<text value="Добавить самолет"/>
</properties>
</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"/>
</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">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</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"/>
</constraints>
<properties>
<text value="Удалить самолет"/>
</properties>
</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"/>
</constraints>
<properties>
<text value="Обновить коллекцию"/>
</properties>
</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"/>
</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"/>
</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"/>
</constraints>
<properties>
<text value="Генерировать самолеты"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@ -0,0 +1,112 @@
package ProjectBomber;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class FormPlaneCollection {
PlanesGenericCollection<DrawingPlane, DrawingObjectPlane> _planes;
FrameDoubleParametrized _frameDoubleParametrized;
private JPanel PanelWrapper;
private JPanel GroupBoxInstruments;
private JPanel PictureBoxCollection;
private JButton buttonAddPlane;
private JTextField textFieldNumber;
private JButton buttonRemovePlane;
private JButton buttonRefreshCollection;
private JButton buttonOpenGenerateWindow;
public DrawingPlane SelectedPlane;
public JPanel getPanelWrapper() {
return PanelWrapper;
}
public FormPlaneCollection() {
PictureBoxCollection.setPreferredSize(new Dimension(600, 500));
_planes = new PlanesGenericCollection<>(600, 500);
buttonAddPlane.addActionListener(this::buttonAddPlaneClicked);
buttonRemovePlane.addActionListener(this::buttonRemovePlaneClicked);
buttonRefreshCollection.addActionListener(this::buttonRefreshCollectionClicked);
buttonOpenGenerateWindow.addActionListener(this::buttonOpenGenerateWindowClicked);
}
public void buttonAddPlaneClicked(ActionEvent e) {
FrameBomber frameBomber = new FrameBomber();
frameBomber.setVisible(true);
frameBomber._formPlaneCollection.buttonSelectPlane.addActionListener(ev -> {
SelectedPlane = frameBomber._formPlaneCollection._drawingPlane;
frameBomber.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) {
int pos;
try {
pos = Integer.parseInt(textFieldNumber.getText());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Неверное значение",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
return;
}
Object[] options = {"Да", "Нет"};
int n = JOptionPane.showOptionDialog(this.getPanelWrapper(),
"Удалить объект?",
"Все серьезно",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]
);
if (n == 1) {
return;
}
if (_planes.Sub(pos) != null) {
refreshPictureBox();
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Объект удален",
"Успех",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this.getPanelWrapper(),
"Не удалось удалить объект",
"Ошибка",
JOptionPane.ERROR_MESSAGE);
}
}
public void buttonRefreshCollectionClicked(ActionEvent e) {
refreshPictureBox();
}
public void buttonOpenGenerateWindowClicked(ActionEvent e) {
if (_frameDoubleParametrized != null) {
_frameDoubleParametrized.dispose();
}
_frameDoubleParametrized = new FrameDoubleParametrized();
_frameDoubleParametrized.setVisible(true);
}
public void refreshPictureBox() {
Graphics g = PictureBoxCollection.getGraphics();
PictureBoxCollection.paint(g);
_planes.ShowPlanes(g);
}
}

View File

@ -0,0 +1,17 @@
package ProjectBomber;
import javax.swing.*;
public class FrameBomber extends JFrame {
public FormBomber _formPlaneCollection;
public FrameBomber() {
super();
setTitle("Бомбардировщик");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
_formPlaneCollection = new FormBomber();
setContentPane(_formPlaneCollection.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();
}
}

View File

@ -0,0 +1,17 @@
package ProjectBomber;
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();
}
}

View File

@ -0,0 +1,17 @@
package ProjectBomber;
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

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

View File

@ -1,5 +1,8 @@
package ProjectBomber;
public class Main {
public static void main(String[] args) {MainFrameBomber mainFrameBomber = new MainFrameBomber();}
public static void main(String[] args) {
FramePlaneCollection framePlaneCollection = new FramePlaneCollection();
framePlaneCollection.setVisible(true);
}
}

View File

@ -1,19 +0,0 @@
package ProjectBomber;
import javax.swing.*;
public class MainFrameBomber extends JFrame {
private FormBomber _formBomber;
public MainFrameBomber() {
super();
setTitle("Бомбардировщик");
setDefaultCloseOperation(EXIT_ON_CLOSE);
_formBomber = new FormBomber();
setContentPane(_formBomber.getPictureBox());
setDefaultLookAndFeelDecorated(false);
setLocation(300, 100);
pack();
setVisible(true);
}
}

View File

@ -0,0 +1,72 @@
package ProjectBomber;
import java.awt.*;
public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveableObject> {
private int _pictureWidth;
private int _pictureHeight;
private final int _placeSizeWidth = 160;
private final int _placeSizeHeight = 120;
private SetGeneric<T> _collection;
public PlanesGenericCollection(int picWidth, int picHeight) {
int horizontalObjectsCount = picWidth / _placeSizeWidth;
int verticalObjectsCount = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(horizontalObjectsCount * verticalObjectsCount);
}
public int Add(T obj) {
if (obj == null) {
return -1;
}
return _collection.Insert(obj);
}
public T Sub(int pos) {
return _collection.Remove(pos);
}
public U GetU(int pos) {
if (_collection.Get(pos) != null)
return (U) _collection.Get(pos).GetMoveableObject();
return null;
}
public void ShowPlanes(Graphics g) {
DrawBackground(g);
DrawObjects(g);
}
private void DrawBackground(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(3));
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
// Линия разметки места
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
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);
// установка позиции
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);
}
}
}

View File

@ -0,0 +1,65 @@
package ProjectBomber;
public class SetGeneric<T extends DrawingPlane> {
private T[] _places;
public int Count;
public SetGeneric(int count) {
_places = (T[]) new DrawingPlane[count];
Count = count;
}
public int Insert(T plane) {
return Insert(plane, 0);
}
public int Insert(T plane, int position) {
// Проверка позиции
if (position < 0 || position >= Count) {
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;
return position;
}
public T Remove(int position) {
// Проверка позиции
if (position < 0 || position >= Count) {
return null;
}
// Удаление объекта из массива, присвоив элементу массива значение null
T plane = _places[position];
_places[position] = null;
return plane;
}
public T Get(int position) {
// Проверка позиции
if (position < 0 || position >= Count) {
return null;
}
return _places[position];
}
}