102 lines
3.0 KiB
Java
Raw Permalink Normal View History

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;
private final int _placeSizeHeight = 50;
/// Набор объектов
private final SetGeneric<T> _collection;
public LocomotiveGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width*height);
}
2023-12-05 00:57:00 +04:00
public Iterable<T> GetLocomotives(){
return _collection.GetEnumerator();
}
/// Перегрузка оператора сложения
//да емае, почему в яве все по-другому?...
public int AddOverload(T obj){
if(obj == null){
return -1;
}
return _collection.Insert(obj);
}
public T SubOverload(int pos)
{
T loco = _collection.Get(pos);
if (loco != null)
{
_collection.Remove(pos);
}
2023-12-19 14:15:51 +04:00
else{
throw new LocoNotFoundException(pos);
}
return loco;
}
// получение объекта 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 i = 0;
for (var obj : _collection.GetEnumerator())
{
if (obj != null)
{
obj.SetPosition(
(int)(i / HeightObjCount * _placeSizeWidth),
(HeightObjCount - 1) * _placeSizeHeight - (int)(i % HeightObjCount * _placeSizeHeight)
);
obj.DrawTransport(g);
}
i++;
}
}
2023-12-19 12:47:07 +04:00
public void clear() {
_collection.clear();
}
}