105 lines
3.1 KiB
Java
105 lines
3.1 KiB
Java
package Generics;
|
|
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import Drawnings.DrawningAirbus;
|
|
import MovementStrategy.IMoveableObject;
|
|
|
|
public class AirbusGenericCollection<T extends DrawningAirbus, U extends IMoveableObject> {
|
|
private int _pictureWidth;
|
|
private int _pictureHeight;
|
|
private int _placeSizeWidth = 124;
|
|
private int _placeSizeHeight = 44;
|
|
private SetGeneric<T> _collection;
|
|
|
|
public AirbusGenericCollection(int picWidth, int picHeight)
|
|
{
|
|
int width = picWidth / _placeSizeWidth;
|
|
int height = picHeight / _placeSizeHeight;
|
|
_pictureWidth = picWidth;
|
|
_pictureHeight = picHeight;
|
|
|
|
_collection = new SetGeneric<T>(width * height);
|
|
}
|
|
// сложение
|
|
public int Add(T obj)
|
|
{
|
|
if (obj == null)
|
|
return -1;
|
|
return _collection.Insert(obj);
|
|
}
|
|
// вычитание
|
|
public boolean Remove(int pos)
|
|
{
|
|
if (_collection.Get(pos) == null)
|
|
return false;
|
|
return _collection.Remove(pos);
|
|
}
|
|
|
|
// получение объекта IMoveableObjecr
|
|
public U GetU(int pos)
|
|
{
|
|
if (_collection.Get(pos) != null)
|
|
return (U)_collection.Get(pos).GetMoveableObject();
|
|
return null;
|
|
}
|
|
|
|
// вывод всего набора
|
|
public BufferedImage ShowAirbus()
|
|
{
|
|
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
|
Graphics2D gr = bmp.createGraphics();
|
|
DrawBackground(gr);
|
|
DrawObjects(gr);
|
|
return bmp;
|
|
}
|
|
|
|
// прорисовка фона
|
|
private void DrawBackground(Graphics gr)
|
|
{
|
|
gr.setColor(Color.BLACK);
|
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i)
|
|
{
|
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
|
{
|
|
// линия разметки
|
|
gr.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
|
gr.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
|
|
}
|
|
}
|
|
}
|
|
|
|
// прорисовка объекта
|
|
private void DrawObjects(Graphics2D gr)
|
|
{
|
|
// координаты
|
|
int x = _pictureWidth / _placeSizeWidth - 1;
|
|
int y = _pictureHeight / _placeSizeHeight - 1;
|
|
|
|
for (int i = 0; i < _collection.Count; ++i)
|
|
{
|
|
DrawningAirbus _airbus = _collection.Get(i);
|
|
if (_airbus == null)
|
|
{
|
|
--x;
|
|
if (x < 0)
|
|
{
|
|
x = _pictureWidth / _placeSizeWidth - 1;
|
|
--y;
|
|
}
|
|
continue;
|
|
}
|
|
_airbus.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y);
|
|
_airbus.DrawTransport(gr);
|
|
--x;
|
|
// новая строка
|
|
if (x < 0)
|
|
{
|
|
x = _pictureWidth / _placeSizeWidth - 1;
|
|
--y;
|
|
}
|
|
}
|
|
}
|
|
}
|