143 lines
4.8 KiB
Java
143 lines
4.8 KiB
Java
import java.awt.*;
|
||
import java.awt.image.BufferedImage;
|
||
|
||
public class MapWithSetLocomotivesGeneric
|
||
<T extends IDrawningObject, U extends AbstractMap>
|
||
{
|
||
/// Ширина окна отрисовки
|
||
private final int _pictureWidth;
|
||
/// Высота окна отрисовки
|
||
private final int _pictureHeight;
|
||
/// Размер занимаемого объектом места (ширина)
|
||
private final int _placeSizeWidth = 210;
|
||
/// Размер занимаемого объектом места (высота)
|
||
private final int _placeSizeHeight = 90;
|
||
|
||
/// Набор объектов
|
||
private final SetLocomotivesGeneric<T> _setLocomotives;
|
||
/// Карта
|
||
private final U _map;
|
||
/// Конструктор
|
||
public MapWithSetLocomotivesGeneric(int picWidth, int picHeight, U map)
|
||
{
|
||
int width = picWidth / _placeSizeWidth;
|
||
int height = picHeight / _placeSizeHeight;
|
||
_setLocomotives = new SetLocomotivesGeneric<T>(width * height);
|
||
_pictureWidth = picWidth;
|
||
_pictureHeight = picHeight;
|
||
_map = map;
|
||
}
|
||
|
||
/// Добавление
|
||
public boolean Plus(MapWithSetLocomotivesGeneric<T, U> map, T locomotive)
|
||
{
|
||
return map._setLocomotives.Insert(locomotive);
|
||
}
|
||
/// Удаление
|
||
public boolean Minus(MapWithSetLocomotivesGeneric<T, U> map, int position)
|
||
{
|
||
return map._setLocomotives.Remove(position);
|
||
}
|
||
|
||
/// Вывод всего набора объектов
|
||
public BufferedImage ShowSet()
|
||
{
|
||
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||
Graphics gr = bmp.getGraphics();
|
||
DrawBackground(gr);
|
||
DrawLocomotives(gr);
|
||
return bmp;
|
||
}
|
||
|
||
/// Просмотр объекта на карте
|
||
public BufferedImage ShowOnMap()
|
||
{
|
||
Shaking();
|
||
for (int i = 0; i < _setLocomotives.Count(); i++)
|
||
{
|
||
var locomotive = _setLocomotives.Get(i);
|
||
if (locomotive != null)
|
||
{
|
||
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
|
||
}
|
||
}
|
||
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 = _setLocomotives.Count() - 1;
|
||
for (int i = 0; i < _setLocomotives.Count(); i++)
|
||
{
|
||
if (_setLocomotives.Get(i) == null)
|
||
{
|
||
for (; j > i; j--)
|
||
{
|
||
var locomotive = _setLocomotives.Get(j);
|
||
if (locomotive != null)
|
||
{
|
||
_setLocomotives.Insert(locomotive, i);
|
||
_setLocomotives.Remove(j);
|
||
break;
|
||
}
|
||
}
|
||
if (j <= i)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Метод отрисовки фона
|
||
private void DrawBackground(Graphics 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);
|
||
}
|
||
}
|
||
|
||
/// Метод прорисовки объектов
|
||
private void DrawLocomotives(Graphics g)
|
||
{
|
||
int width = _pictureWidth / _placeSizeWidth;
|
||
int height = _pictureHeight / _placeSizeHeight;
|
||
|
||
int curWidth = 0;
|
||
int curHeight = 0;
|
||
|
||
for (int i = 0; i < _setLocomotives.Count(); i++)
|
||
{
|
||
// установка позиции
|
||
if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).SetObject(curWidth * _placeSizeWidth, curHeight * _placeSizeHeight, _pictureWidth, _pictureHeight);
|
||
if (_setLocomotives.Get(i) != null) _setLocomotives.Get(i).DrawningObject(g);
|
||
if (curWidth < width) curWidth++;
|
||
else
|
||
{
|
||
curWidth = 0;
|
||
curHeight++;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|