2022-10-21 16:24:45 +04:00
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.image.BufferedImage;
|
2022-11-05 18:49:21 +04:00
|
|
|
import java.util.LinkedList;
|
2022-10-21 16:24:45 +04:00
|
|
|
|
|
|
|
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;
|
2022-11-05 18:49:21 +04:00
|
|
|
public final SetLocomotivesGeneric<T> _setLocomotives;
|
|
|
|
|
|
|
|
// Список удаленных объектов
|
|
|
|
LinkedList<DrawningObjectLocomotive> deletedLocomotives = new LinkedList<>();
|
|
|
|
|
|
|
|
public DrawningObjectLocomotive getDeleted() {
|
|
|
|
if (deletedLocomotives.isEmpty()) return null;
|
|
|
|
return deletedLocomotives.removeFirst();
|
|
|
|
}
|
2022-10-21 16:24:45 +04:00
|
|
|
/// Карта
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Добавление
|
2022-10-21 21:59:22 +04:00
|
|
|
public int Plus(T locomotive)
|
2022-10-21 16:24:45 +04:00
|
|
|
{
|
2022-10-21 21:27:07 +04:00
|
|
|
return this._setLocomotives.Insert(locomotive);
|
2022-10-21 16:24:45 +04:00
|
|
|
}
|
|
|
|
/// Удаление
|
2022-10-21 21:59:22 +04:00
|
|
|
public T Minus(int position)
|
2022-10-21 16:24:45 +04:00
|
|
|
{
|
2022-11-05 18:49:21 +04:00
|
|
|
T temp = this._setLocomotives.Remove(position);
|
|
|
|
if (temp == null) return null;
|
2022-11-08 16:50:09 +04:00
|
|
|
deletedLocomotives.add((DrawningObjectLocomotive) temp);
|
2022-11-05 18:49:21 +04:00
|
|
|
return temp;
|
2022-10-21 16:24:45 +04:00
|
|
|
}
|
|
|
|
/// Вывод всего набора объектов
|
|
|
|
public BufferedImage ShowSet()
|
|
|
|
{
|
|
|
|
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
|
Graphics gr = bmp.getGraphics();
|
2022-10-21 21:27:07 +04:00
|
|
|
DrawBackground((Graphics2D)gr);
|
|
|
|
DrawLocomotives((Graphics2D)gr);
|
2022-10-21 16:24:45 +04:00
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Просмотр объекта на карте
|
|
|
|
public BufferedImage ShowOnMap()
|
|
|
|
{
|
|
|
|
Shaking();
|
2022-11-05 13:03:36 +04:00
|
|
|
for (var locomotive : _setLocomotives.GetLocomotives())
|
2022-10-21 16:24:45 +04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Метод отрисовки фона
|
2022-10-21 21:27:07 +04:00
|
|
|
private void DrawBackground(Graphics2D g)
|
2022-10-21 16:24:45 +04:00
|
|
|
{
|
2022-10-21 21:27:07 +04:00
|
|
|
g.setColor(Color.WHITE);
|
|
|
|
g.fillRect(0,0,600, 500);
|
|
|
|
for (int j = _placeSizeHeight; j < _pictureHeight; j+= _placeSizeHeight)
|
2022-10-21 16:24:45 +04:00
|
|
|
{
|
2022-10-21 21:27:07 +04:00
|
|
|
//нижняя линия рельс
|
|
|
|
g.setColor(Color.BLACK);
|
|
|
|
g.setStroke(new BasicStroke(5));
|
|
|
|
|
|
|
|
g.drawLine(0, j, _pictureWidth, j);
|
|
|
|
for (int i = 0; i < _pictureWidth; i+=20)
|
|
|
|
{
|
|
|
|
g.drawLine(i, j, i, j + 10);
|
|
|
|
}
|
|
|
|
g.drawLine(0, j + 10, _pictureWidth, j + 10);
|
|
|
|
|
|
|
|
//верхняя линия рельс
|
|
|
|
|
|
|
|
g.setColor(Color.GRAY);
|
|
|
|
|
|
|
|
g.drawLine(0, j - 20, _pictureWidth, j - 20);
|
|
|
|
for (int i = 0; i < _pictureWidth; i += 20)
|
|
|
|
{
|
|
|
|
g.drawLine(i, j - 20, i, j - 10);
|
|
|
|
}
|
|
|
|
g.drawLine(0, j - 10, _pictureWidth, j - 10);
|
|
|
|
|
|
|
|
//фонари
|
|
|
|
for (int i = _placeSizeWidth; i < _pictureWidth; i += _placeSizeWidth)
|
|
|
|
{
|
|
|
|
g.setColor(Color.BLACK);
|
|
|
|
g.setStroke(new BasicStroke(10));
|
|
|
|
g.drawLine(i, j - _placeSizeHeight + 20, i, j);
|
|
|
|
g.setColor(Color.YELLOW);
|
|
|
|
g.setStroke(new BasicStroke(20));
|
|
|
|
g.drawLine(i, j - _placeSizeHeight + 18, i, j - _placeSizeHeight + 38);
|
2022-10-21 16:24:45 +04:00
|
|
|
}
|
|
|
|
}
|
2022-10-21 21:27:07 +04:00
|
|
|
g.setStroke(new BasicStroke(2));
|
2022-10-21 16:24:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Метод прорисовки объектов
|
2022-10-21 21:27:07 +04:00
|
|
|
private void DrawLocomotives(Graphics2D g)
|
2022-10-21 16:24:45 +04:00
|
|
|
{
|
|
|
|
int width = _pictureWidth / _placeSizeWidth;
|
|
|
|
int height = _pictureHeight / _placeSizeHeight;
|
|
|
|
|
|
|
|
int curWidth = 0;
|
|
|
|
int curHeight = 0;
|
|
|
|
|
2022-11-05 13:03:36 +04:00
|
|
|
for (var locomotive : _setLocomotives.GetLocomotives())
|
2022-10-21 16:24:45 +04:00
|
|
|
{
|
|
|
|
// установка позиции
|
2022-11-05 13:03:36 +04:00
|
|
|
if (locomotive != null) locomotive.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight);
|
|
|
|
if (locomotive != null) locomotive.DrawningObject(g);
|
2022-10-21 16:24:45 +04:00
|
|
|
if (curWidth < width) curWidth++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curWidth = 0;
|
|
|
|
curHeight++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 14:06:39 +04:00
|
|
|
/// Получение данных в виде строки
|
|
|
|
public String GetData(char separatorType, char separatorData)
|
|
|
|
{
|
|
|
|
//string data = $"{_map.GetType().Name}{separatorType}";
|
|
|
|
String data = ""+ _map.getClass() + separatorType;
|
|
|
|
for (var locomotive : _setLocomotives.GetLocomotives())
|
|
|
|
{
|
|
|
|
data += "" + locomotive.getInfo() + separatorData;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Загрузка списка из массива строк
|
|
|
|
public void LoadData(String[] records)
|
|
|
|
{
|
|
|
|
for (var rec : records)
|
|
|
|
{
|
|
|
|
_setLocomotives.Insert((T)DrawningObjectLocomotive.Create(rec));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 16:24:45 +04:00
|
|
|
}
|