142 lines
4.9 KiB
Java
142 lines
4.9 KiB
Java
|
import java.awt.*;
|
|||
|
import java.awt.image.BufferedImage;
|
|||
|
|
|||
|
public class MapWithSetAirbusGeneric <T extends IDrawingObject, U extends AbstractMap>
|
|||
|
{
|
|||
|
/// Ширина окна отрисовки
|
|||
|
private final int _pictureWidth;
|
|||
|
/// Высота окна отрисовки
|
|||
|
private final int _pictureHeight;
|
|||
|
/// Размер занимаемого объектом места (ширина)
|
|||
|
private final int _placeSizeWidth = 210;
|
|||
|
/// Размер занимаемого объектом места (высота)
|
|||
|
private final int _placeSizeHeight = 90;
|
|||
|
|
|||
|
/// Набор объектов
|
|||
|
private final SetAirbusGeneric<T> _setAirbus;
|
|||
|
/// Карта
|
|||
|
private final U _map;
|
|||
|
/// Конструктор
|
|||
|
public MapWithSetAirbusGeneric(int picWidth, int picHeight, U map)
|
|||
|
{
|
|||
|
int width = picWidth / _placeSizeWidth;
|
|||
|
int height = picHeight / _placeSizeHeight;
|
|||
|
_setAirbus = new SetAirbusGeneric<T>(width * height);
|
|||
|
_pictureWidth = picWidth;
|
|||
|
_pictureHeight = picHeight;
|
|||
|
_map = map;
|
|||
|
}
|
|||
|
|
|||
|
/// Добавление
|
|||
|
public int Plus(T airbus)
|
|||
|
{
|
|||
|
return this._setAirbus.Insert(airbus);
|
|||
|
}
|
|||
|
/// Удаление
|
|||
|
public T Minus(int position)
|
|||
|
{
|
|||
|
return this._setAirbus.Remove(position);
|
|||
|
}
|
|||
|
/// Вывод всего набора объектов
|
|||
|
public BufferedImage ShowSet()
|
|||
|
{
|
|||
|
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
|||
|
Graphics gr = bmp.getGraphics();
|
|||
|
DrawBackground((Graphics2D)gr);
|
|||
|
DrawAirbus((Graphics2D)gr);
|
|||
|
return bmp;
|
|||
|
}
|
|||
|
|
|||
|
/// Просмотр объекта на карте
|
|||
|
public BufferedImage ShowOnMap()
|
|||
|
{
|
|||
|
Shaking();
|
|||
|
for (int i = 0; i < _setAirbus.Count(); i++)
|
|||
|
{
|
|||
|
var airbus = _setAirbus.Get(i);
|
|||
|
if (airbus != null)
|
|||
|
{
|
|||
|
return _map.CreateMap(_pictureWidth, _pictureHeight, airbus);
|
|||
|
}
|
|||
|
}
|
|||
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
|||
|
}
|
|||
|
|
|||
|
/// Перемещение объекта по крате
|
|||
|
public BufferedImage MoveObject(Direction direction)
|
|||
|
{
|
|||
|
if (_map != null)
|
|||
|
{
|
|||
|
return _map.MoveObject(direction);
|
|||
|
}
|
|||
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
|||
|
}
|
|||
|
|
|||
|
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
|
|||
|
private void Shaking()
|
|||
|
{
|
|||
|
int j = _setAirbus.Count() - 1;
|
|||
|
for (int i = 0; i < _setAirbus.Count(); i++)
|
|||
|
{
|
|||
|
if (_setAirbus.Get(i) == null)
|
|||
|
{
|
|||
|
for (; j > i; j--)
|
|||
|
{
|
|||
|
var airbus = _setAirbus.Get(j);
|
|||
|
if (airbus != null)
|
|||
|
{
|
|||
|
_setAirbus.Insert(airbus, i);
|
|||
|
_setAirbus.Remove(j);
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
if (j <= i)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// Метод отрисовки фона
|
|||
|
private void DrawBackground(Graphics2D g)
|
|||
|
{
|
|||
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
|||
|
{
|
|||
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
|||
|
{
|
|||
|
g.setColor(Color.BLACK);
|
|||
|
g.drawLine( i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight);
|
|||
|
g.setColor(Color.GRAY);
|
|||
|
g.fillRect(i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
|
|||
|
g.setColor(Color.YELLOW);
|
|||
|
g.drawLine( i * _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2);
|
|||
|
}
|
|||
|
g.setColor(Color.BLACK);
|
|||
|
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
|||
|
}
|
|||
|
g.setStroke(new BasicStroke(2));
|
|||
|
}
|
|||
|
|
|||
|
// Метод прорисовки объектов
|
|||
|
private void DrawAirbus(Graphics2D g)
|
|||
|
{
|
|||
|
int curWidth = _pictureWidth / _placeSizeWidth - 1;
|
|||
|
int curHeight = _pictureHeight / _placeSizeHeight - 1;
|
|||
|
|
|||
|
for (int i = 0; i < _setAirbus.Count(); i++)
|
|||
|
{
|
|||
|
if((_setAirbus.Get(i) != null))
|
|||
|
{
|
|||
|
_setAirbus.Get(i).SetObject(curWidth * _placeSizeWidth + 30, curHeight * _placeSizeHeight + 40, _pictureWidth, _pictureHeight);
|
|||
|
_setAirbus.Get(i).DrawingObject(g);
|
|||
|
}
|
|||
|
if (curWidth <= 0) {
|
|||
|
curWidth = _pictureWidth / _placeSizeWidth - 1;
|
|||
|
curHeight--;
|
|||
|
}
|
|||
|
else curWidth--;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|