2023-10-21 19:25:05 +04:00
|
|
|
|
package ProjectElectricLocomotive;
|
|
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
|
|
|
|
public class LocomotiveGenericCollection<T extends DrawingLocomotive,U extends IMoveableObject>
|
|
|
|
|
{
|
|
|
|
|
//ширина/высота окна
|
|
|
|
|
private final int _pictureWidth;
|
|
|
|
|
private final int _pictureHeight;
|
|
|
|
|
//ширина/высота занимаемого места
|
2023-10-21 22:32:22 +04:00
|
|
|
|
private final int _placeSizeWidth = 150;
|
2023-10-21 19:25:05 +04:00
|
|
|
|
private final int _placeSizeHeight = 50;
|
2023-11-06 17:02:05 +04:00
|
|
|
|
|
2023-10-21 19:25:05 +04:00
|
|
|
|
/// Набор объектов
|
|
|
|
|
private final SetGeneric<T> _collection;
|
|
|
|
|
|
|
|
|
|
public LocomotiveGenericCollection(int picWidth, int picHeight)
|
|
|
|
|
{
|
2023-11-06 17:02:05 +04:00
|
|
|
|
// немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло...
|
|
|
|
|
// высчитываем размер массива для setgeneric
|
2023-10-21 19:25:05 +04:00
|
|
|
|
int width = picWidth / _placeSizeWidth;
|
|
|
|
|
int height = picHeight / _placeSizeHeight;
|
|
|
|
|
_pictureWidth = picWidth;
|
|
|
|
|
_pictureHeight = picHeight;
|
|
|
|
|
_collection = new SetGeneric<T>(width*height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Перегрузка оператора сложения
|
|
|
|
|
//да емае, почему в яве все по-другому?...
|
|
|
|
|
|
|
|
|
|
public int AddOverload(T loco){
|
|
|
|
|
if(loco == null){
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return _collection.Insert(loco);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T SubOverload(int pos){
|
|
|
|
|
return _collection.Remove(pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// получение объекта imoveableObj
|
|
|
|
|
public U GetU(int pos)
|
|
|
|
|
{
|
|
|
|
|
return (U)_collection.Get(pos).GetMoveableObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Вывод всего набора объектов
|
|
|
|
|
public void ShowLocomotives(Graphics gr)
|
|
|
|
|
{
|
|
|
|
|
DrawBackground(gr);
|
|
|
|
|
DrawObjects(gr);
|
|
|
|
|
}
|
|
|
|
|
private void DrawBackground(Graphics g)
|
|
|
|
|
{
|
|
|
|
|
Color blackColor = Color.BLACK;
|
|
|
|
|
g.setColor(blackColor);
|
|
|
|
|
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 HeightObjCount = _pictureHeight / _placeSizeHeight;
|
|
|
|
|
int WidthObjCount = _pictureWidth / _placeSizeWidth;
|
|
|
|
|
for (int i = 0; i < _collection.Count(); i++)
|
|
|
|
|
{
|
|
|
|
|
T type = _collection.Get(i);
|
|
|
|
|
if (type != null)
|
|
|
|
|
{
|
|
|
|
|
type.SetPosition(
|
|
|
|
|
(int)(i / HeightObjCount * _placeSizeWidth),
|
|
|
|
|
(HeightObjCount - 1) * _placeSizeHeight - (int)(i % HeightObjCount * _placeSizeHeight)
|
|
|
|
|
);
|
|
|
|
|
type.DrawTransport(g);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|