PIbd-21_Kouvshinoff_T._A._W.../laba1Loco/TrainsGenericCollection.java

137 lines
4.4 KiB
Java
Raw Normal View History

2023-10-22 22:05:13 +04:00
package laba1Loco;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class TrainsGenericCollection<T extends DrawingTrain, U extends IMoveableObject>{
/// <summary>
/// Получение объектов коллекции
/// </summary>
public Iterable<T> getTrains(final Integer maxTrains) { return _collection.GetTrains(maxTrains); }
2023-10-22 22:05:13 +04:00
/// <summary>
/// Ширина окна прорисовки
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна прорисовки
/// </summary>
private int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private int _placeSizeWidth = 210;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private int _placeSizeHeight = 90;
/// <summary>
/// Набор объектов
/// </summary>
private SetGeneric<T> _collection;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public TrainsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
2023-10-23 18:05:55 +04:00
public int Add(T obj)
2023-10-22 22:05:13 +04:00
{
if (obj == null)
{
2023-10-23 18:05:55 +04:00
return -1;
2023-10-22 22:05:13 +04:00
}
2023-10-23 18:05:55 +04:00
return _collection.Insert(obj);
2023-10-22 22:05:13 +04:00
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public T remove(int pos)
{
T obj = _collection.Get(pos);
if (obj != null)
{
_collection.Remove(pos);
}
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U GetU(int pos)
{
return (U)_collection.Get(pos).GetMoveableObject();
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public BufferedImage ShowTrains()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bmp.createGraphics();
DrawBackground(g);
DrawObjects(g);
g.dispose();
return bmp;
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics2D g)
{
g.setColor(Color.BLACK);
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);
}
}
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics2D g)
{
int i = 0;
for (T train : _collection.GetTrains(100))
2023-10-22 22:05:13 +04:00
{
if (train != null)
2023-10-22 22:05:13 +04:00
{
2023-11-05 16:29:18 +04:00
train._pictureHeight = _pictureHeight;
train._pictureWidth = _pictureWidth;
train.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (train instanceof DrawingLoco)
((DrawingLoco)train).DrawTransport(g);
2023-10-22 22:05:13 +04:00
else
train.DrawTransport(g);
2023-10-22 22:05:13 +04:00
}
i++;
2023-10-22 22:05:13 +04:00
}
}
public void clear(){ _collection.clear(); }
2023-10-22 22:05:13 +04:00
}