2023-10-20 12:17:34 +04:00
|
|
|
package ProjectStormtrooper;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:43:07 +04:00
|
|
|
public int Add(T obj) {
|
2023-10-20 12:17:34 +04:00
|
|
|
if (obj == null) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-10-20 22:43:07 +04:00
|
|
|
return _collection.Insert(obj);
|
2023-10-20 12:17:34 +04:00
|
|
|
}
|
|
|
|
|
2023-10-20 22:43:07 +04:00
|
|
|
public T Sub(int pos) {
|
|
|
|
return _collection.Remove(pos);
|
2023-10-20 12:17:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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:36:12 +04:00
|
|
|
for (int i = 0; i < _collection.Count(); i++) {
|
2023-10-20 12:17:34 +04:00
|
|
|
// получение объекта
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|