PIbd-23_Mochalov_D.V._Locom.../MapWithSetLocomotivesGeneric.java

203 lines
6.7 KiB
Java

import java.awt.*;
import java.awt.image.BufferedImage;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
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;
public final SetLocomotivesGeneric<T> _setLocomotives;
// Список удаленных объектов
LinkedList<DrawningObjectLocomotive> deletedLocomotives = new LinkedList<>();
public DrawningObjectLocomotive getDeleted() {
if (deletedLocomotives.isEmpty()) return null;
return deletedLocomotives.removeFirst();
}
/// Карта
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 int Plus(T locomotive)
{
return this._setLocomotives.Insert(locomotive);
}
/// Удаление
public T Minus(int position)
{
T temp = this._setLocomotives.Remove(position);
if (temp == null) return null;
deletedLocomotives.add((DrawningObjectLocomotive) temp);
return temp;
}
/// Вывод всего набора объектов
public BufferedImage ShowSet()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
DrawBackground((Graphics2D)gr);
DrawLocomotives((Graphics2D)gr);
return bmp;
}
/// Просмотр объекта на карте
public BufferedImage ShowOnMap()
{
Shaking();
for (var locomotive : _setLocomotives.GetLocomotives())
{
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(Graphics2D g)
{
g.setColor(Color.WHITE);
g.fillRect(0,0,600, 500);
for (int j = _placeSizeHeight; j < _pictureHeight; j+= _placeSizeHeight)
{
//нижняя линия рельс
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);
}
}
g.setStroke(new BasicStroke(2));
}
/// Метод прорисовки объектов
private void DrawLocomotives(Graphics2D g)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
int curWidth = 0;
int curHeight = 0;
for (var locomotive : _setLocomotives.GetLocomotives())
{
// установка позиции
if (locomotive != null) locomotive.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 18, _pictureWidth, _pictureHeight);
if (locomotive != null) locomotive.DrawningObject(g);
if (curWidth < width) curWidth++;
else
{
curWidth = 0;
curHeight++;
}
}
}
/// Получение данных в виде строки
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)
{
Collections.reverse(Arrays.asList(records));
for (var rec : records)
{
_setLocomotives.Insert((T)DrawningObjectLocomotive.Create(rec));
}
}
}