PIbd-21_Bakalskaya_E.D._Ele.../ProjectElectricLocomotive/LocomotiveGenericCollection.java
ekallin e36d02dd75 revert 34f72ac391
revert Lab 3 Ready hard
2023-11-06 17:02:05 +04:00

87 lines
2.9 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ProjectElectricLocomotive;
import java.awt.*;
public class LocomotiveGenericCollection<T extends DrawingLocomotive,U extends IMoveableObject>
{
//ширина/высота окна
private final int _pictureWidth;
private final int _pictureHeight;
//ширина/высота занимаемого места
private final int _placeSizeWidth = 150;
private final int _placeSizeHeight = 50;
/// Набор объектов
private final SetGeneric<T> _collection;
public LocomotiveGenericCollection(int picWidth, int picHeight)
{
// немного странная логика, что-то я пока ее не особо понимаю, зачем нам ААААА дошло...
// высчитываем размер массива для setgeneric
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);
}
}
}
}