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

129 lines
4.0 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>
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)
{
2023-10-24 02:19:14 +04:00
for (int i = 0; i < _collection.Count(); i++)
2023-10-22 22:05:13 +04:00
{
T t = _collection.Get(i);
if (t != null)
{
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t instanceof DrawingLoco)
((DrawingLoco) t).DrawTransport(g);
else
t.DrawTransport(g);
}
}
}
}