PIbd-21_Potapov_N.S._Stormt.../ProjectStormtrooper/PlanesGenericCollection.java

83 lines
2.8 KiB
Java
Raw Normal View History

package ProjectStormtrooper;
import java.awt.*;
import java.util.ArrayList;
import java.util.Stack;
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 ArrayList<T> GetPlanes() {
return _collection.GetEnumerator();
}
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) {
2023-10-24 12:43:15 +04:00
T obj = _collection.Get(pos);
if (obj != null) {
_collection.Remove(pos);
}
return obj;
}
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;
2023-10-24 12:43:15 +04:00
int i = 0;
for (var obj :
_collection.GetEnumerator()) {
// установка позиции
2023-10-24 12:43:15 +04:00
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++;
}
}
}