PIbd-23_Minhasapov_R.H._Exc.../MapWithSetTracktorGeneric.java

165 lines
6.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import java.awt.*;
import java.awt.image.BufferedImage;
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;
private 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<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = 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 (int i = 0; i < _setTracktor.getCount(); i++)
{
var tracktor = _setTracktor.get(i);
if (tracktor != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, tracktor);
}
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
}
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 (int i = 0; i < _setTracktor.getCount(); i++)
{
// установка позиции
// Влево - вниз
if (_setTracktor.get(i) != null){
_setTracktor.get(i).setObject(
curWidth * _placeSizeWidth,
curHeight * _placeSizeHeight,
_pictureWidth,
_pictureHeight);
_setTracktor.get(i).drawningObject(g);
if (curWidth > 0)
{
curWidth--;
}
else
{
curWidth = width-1;
curHeight++;
}
}
}
}
}