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

75 lines
2.7 KiB
Java

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);
}
public int Add(PlanesGenericCollection<T, U> collect, T obj) {
if (obj == null) {
return -1;
}
if (collect != null)
return collect._collection.Insert(obj);
return -1;
}
public T Sub(PlanesGenericCollection<T, U> collect, int pos) {
return collect._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);
}
}
}