185 lines
7.1 KiB
Java
185 lines
7.1 KiB
Java
import java.awt.*;
|
||
import java.awt.image.BufferedImage;
|
||
import java.util.LinkedList;
|
||
|
||
public class MapWithSetTracktorGeneric <T extends IDrawningObject, U extends AbstractMap>{
|
||
public final int _pictureWidth;
|
||
public final int _pictureHeight;
|
||
public final int _placeSizeWidth = 150;
|
||
public final int _placeSizeHeight = 100;
|
||
public final SetTracktorGeneric<T> _setTracktor;
|
||
private final U _map;
|
||
|
||
public MapWithSetTracktorGeneric(int picWidth, int picHeight, U map) {
|
||
int width = picWidth / _placeSizeWidth;
|
||
int height = picHeight / _placeSizeHeight;
|
||
_setTracktor = new SetTracktorGeneric<>(width * height);
|
||
_pictureWidth = picWidth;
|
||
_pictureHeight = picHeight;
|
||
_map = map;
|
||
}
|
||
|
||
public U getMap() {
|
||
return _map;
|
||
}
|
||
|
||
public int addTracktor(T tracktor) {
|
||
return _setTracktor.insert(tracktor);
|
||
}
|
||
|
||
public T removeTracktorAt(int position) {
|
||
return _setTracktor.remove(position);
|
||
}
|
||
|
||
public Image showSet() {
|
||
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
||
Graphics2D gr = (Graphics2D)img.getGraphics();
|
||
drawBackground(gr);
|
||
drawTracktor(gr);
|
||
return img;
|
||
}
|
||
|
||
public Image showOnMap() {
|
||
shaking();
|
||
for (var tracktor : _setTracktor.getTracktors())
|
||
{
|
||
if (tracktor != null)
|
||
{
|
||
return _map.CreateMap(_pictureWidth, _pictureHeight, tracktor);
|
||
}
|
||
}
|
||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||
}
|
||
|
||
public Image moveObject(Direction direction) {
|
||
if (_map != null) {
|
||
return _map.MoveObject(direction);
|
||
}
|
||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
||
}
|
||
|
||
private void shaking() {
|
||
int j = _setTracktor.getCount() - 1;
|
||
for (int i = 0; i < _setTracktor.getCount(); i++)
|
||
{
|
||
if (_setTracktor.get(i) == null)
|
||
{
|
||
for (; j > i; j--)
|
||
{
|
||
var tracktor = _setTracktor.get(j);
|
||
if (tracktor != null)
|
||
{
|
||
_setTracktor.insert(tracktor , i);
|
||
_setTracktor.remove(j);
|
||
break;
|
||
}
|
||
}
|
||
if (j <= i)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void drawBackground(Graphics2D g) {
|
||
Color pen = Color.WHITE;
|
||
Stroke penStroke = new BasicStroke(3);
|
||
Color brush = Color.LIGHT_GRAY;
|
||
Color disabledPersonBadgeBrush = Color.WHITE;
|
||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||
{
|
||
for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++)
|
||
{
|
||
// Асфальт
|
||
g.setColor(brush);
|
||
g.fillRect( i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
|
||
// Разметка
|
||
g.setColor(pen);
|
||
g.setStroke(penStroke);
|
||
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, (i * _placeSizeWidth) + (_placeSizeWidth / 2), j * _placeSizeHeight);
|
||
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth, (j * _placeSizeHeight) + _placeSizeHeight);
|
||
g.drawLine(i * _placeSizeWidth, (j + 1) * _placeSizeHeight, (i * _placeSizeWidth) + (_placeSizeWidth / 2), (j + 1) * _placeSizeHeight);
|
||
if (j%2==0)
|
||
{
|
||
// Знак паркинга
|
||
g.setColor(pen);
|
||
g.setStroke(penStroke);
|
||
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + 60, i * _placeSizeWidth + 60, j * _placeSizeHeight + 60);
|
||
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + 60, i * _placeSizeWidth + 20, j * _placeSizeHeight + 40);
|
||
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + 40, i * _placeSizeWidth + 40, j * _placeSizeHeight + 40);
|
||
g.drawLine(i * _placeSizeWidth + 40, j * _placeSizeHeight + 40, i * _placeSizeWidth + 40, j * _placeSizeHeight + 60);
|
||
}
|
||
else
|
||
{
|
||
// Инвалид
|
||
// Колесо
|
||
g.setColor(pen);
|
||
g.setStroke(penStroke);
|
||
g.drawOval(i * _placeSizeWidth + 40, j * _placeSizeHeight + 50, 30, 30);
|
||
// Голова
|
||
g.setColor(disabledPersonBadgeBrush);
|
||
g.setStroke(new BasicStroke(1));
|
||
g.fillOval(i * _placeSizeWidth + 20, j * _placeSizeHeight + 60, 10, 10);
|
||
// Туловище
|
||
g.setColor(pen);
|
||
g.setStroke(penStroke);
|
||
g.drawLine(i * _placeSizeWidth + 25, j * _placeSizeHeight + 65, i * _placeSizeWidth + 55, j * _placeSizeHeight + 60);
|
||
// Рука
|
||
g.drawLine(i * _placeSizeWidth + 32, j * _placeSizeHeight + 65, i * _placeSizeWidth + 38, j * _placeSizeHeight + 55);
|
||
// Нога
|
||
g.drawLine(i * _placeSizeWidth + 55, j * _placeSizeHeight + 60, i * _placeSizeWidth + 60, j * _placeSizeHeight + 40);
|
||
// Голеностоп
|
||
g.drawLine(i * _placeSizeWidth + 60, j * _placeSizeHeight + 40, i * _placeSizeWidth + 65, j * _placeSizeHeight + 38);
|
||
}
|
||
}
|
||
}
|
||
g.setStroke(new BasicStroke(1));
|
||
}
|
||
|
||
private void drawTracktor(Graphics2D g) {
|
||
int width = _pictureWidth / _placeSizeWidth;
|
||
int curWidth = width-1;
|
||
int curHeight = 0;
|
||
|
||
for(var tracktor : _setTracktor.getTracktors()){
|
||
// установка позиции
|
||
// Влево - вниз
|
||
if (tracktor!=null){
|
||
tracktor.setObject(
|
||
curWidth * _placeSizeWidth,
|
||
curHeight * _placeSizeHeight,
|
||
_pictureWidth,
|
||
_pictureHeight);
|
||
tracktor.drawningObject(g);
|
||
if (curWidth > 0)
|
||
{
|
||
curWidth--;
|
||
}
|
||
else
|
||
{
|
||
curWidth = width-1;
|
||
curHeight++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public String getData(char separatorType, char separatorData) {
|
||
StringBuilder data = new StringBuilder(String.format("%s%c", _map.getClass().getSimpleName(), separatorType));
|
||
|
||
for (var tracktor : _setTracktor.getTracktors()) {
|
||
data.append(String.format("%s%c", tracktor.getInfo(), separatorData));
|
||
}
|
||
|
||
return data.toString();
|
||
}
|
||
|
||
@SuppressWarnings("unchecked")
|
||
public void loadData(String[] records) {
|
||
for (int i = records.length - 1; i >= 0; i--) {
|
||
_setTracktor.insert((T) DrawningObjectExcavator.create(records[i]));
|
||
}
|
||
}
|
||
}
|