2022-11-21 21:34:45 +04:00
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
2022-11-22 00:21:46 +04:00
|
|
|
|
import java.util.LinkedList;
|
2022-11-21 21:34:45 +04:00
|
|
|
|
|
|
|
|
|
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;
|
2022-11-22 00:21:46 +04:00
|
|
|
|
public final SetTracktorGeneric<T> _setTracktor;
|
2022-11-21 21:34:45 +04:00
|
|
|
|
private final U _map;
|
|
|
|
|
|
|
|
|
|
public MapWithSetTracktorGeneric(int picWidth, int picHeight, U map) {
|
|
|
|
|
int width = picWidth / _placeSizeWidth;
|
|
|
|
|
int height = picHeight / _placeSizeHeight;
|
2022-11-22 00:21:46 +04:00
|
|
|
|
_setTracktor = new SetTracktorGeneric<>(width * height);
|
2022-11-21 21:34:45 +04:00
|
|
|
|
_pictureWidth = picWidth;
|
|
|
|
|
_pictureHeight = picHeight;
|
|
|
|
|
_map = map;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 15:01:05 +04:00
|
|
|
|
public U getMap() {
|
|
|
|
|
return _map;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 21:34:45 +04:00
|
|
|
|
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();
|
2022-11-22 00:21:46 +04:00
|
|
|
|
for (var tracktor : _setTracktor.getTracktors())
|
2022-11-21 21:34:45 +04:00
|
|
|
|
{
|
|
|
|
|
if (tracktor != null)
|
|
|
|
|
{
|
|
|
|
|
return _map.CreateMap(_pictureWidth, _pictureHeight, tracktor);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-22 00:21:46 +04:00
|
|
|
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
2022-11-21 21:34:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2022-11-22 00:21:46 +04:00
|
|
|
|
|
|
|
|
|
for(var tracktor : _setTracktor.getTracktors()){
|
2022-11-21 21:34:45 +04:00
|
|
|
|
// установка позиции
|
|
|
|
|
// Влево - вниз
|
2022-11-22 00:21:46 +04:00
|
|
|
|
if (tracktor!=null){
|
|
|
|
|
tracktor.setObject(
|
2022-11-21 21:34:45 +04:00
|
|
|
|
curWidth * _placeSizeWidth,
|
|
|
|
|
curHeight * _placeSizeHeight,
|
|
|
|
|
_pictureWidth,
|
|
|
|
|
_pictureHeight);
|
2022-11-22 00:21:46 +04:00
|
|
|
|
tracktor.drawningObject(g);
|
2022-11-21 21:34:45 +04:00
|
|
|
|
if (curWidth > 0)
|
|
|
|
|
{
|
|
|
|
|
curWidth--;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
curWidth = width-1;
|
|
|
|
|
curHeight++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-06 15:01:05 +04:00
|
|
|
|
|
|
|
|
|
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]));
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-21 21:34:45 +04:00
|
|
|
|
}
|