2022-12-17 13:01:40 +03:00

169 lines
4.8 KiB
Java

import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetPlane<T extends IDrawingObject, U extends AbstractMap>
{
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 210;
private int _placeSizeHeight = 170;
private SetPlaneGeneric<T> _setPlane;
private U _map;
public MapWithSetPlane(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setPlane = new SetPlaneGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int addAirPlane(T airplane)
{
return _setPlane.Insert(airplane);
}
public T removeAirPlane(int position)
{
return _setPlane.Remove(position);
}
public Image ShowSet()
{
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = (Graphics2D) img.getGraphics();
DrawBackground(gr);
DrawPlanes(gr);
return img;
}
public Image ShowOnMap()
{
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) img.getGraphics();
Shaking();
for (int i = 0; i < _setPlane.getCount(); i++)
{
var car = _setPlane.Get(i);
if (car != null)
{
_map.CreateMap(_pictureWidth, _pictureHeight, car);
_map.DrawMapWithObject(g);
return img;
}
}
return img;
}
public Image MoveObject(Direction direction)
{
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) img.getGraphics();
if (_map != null)
{
_map.MoveObject(direction);
_map.DrawMapWithObject(g);
}
return img;
}
private void Shaking()
{
int j = _setPlane.getCount() - 1;
for (int i = 0; i < _setPlane.getCount(); i++)
{
if (_setPlane.Get(i) == null)
{
for (; j > i; j--)
{
var car = _setPlane.Get(j);
if (car != null)
{
_setPlane.Insert(car, i);
_setPlane.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics2D g)
{
g.setPaint(new Color(160, 160, 160));
g.fillRect(0, 0, _pictureWidth , _pictureHeight );
g.setPaint(Color.black);
g.setStroke(new BasicStroke(3));
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i *
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.drawLine( i * _placeSizeWidth, 0, i * _placeSizeWidth,
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
g.setStroke(new BasicStroke(1));
}
private void DrawPlanes(Graphics2D g)
{
int CountWidth = _pictureWidth / _placeSizeWidth;
int x = _pictureWidth - _placeSizeWidth - _placeSizeWidth/2 - _placeSizeWidth / 4;
int y = _placeSizeHeight/4;
for (int k = 0; k < _setPlane.getCount(); k++)
{
if (_setPlane.Get(k) != null)
{
if ((k+1) % CountWidth != 0 || k ==0)
{
_setPlane.Get(k).SetObject(x, y, _pictureWidth, _pictureHeight);
_setPlane.Get(k).DrawningObject(g);
x -= _placeSizeWidth;
}
else
{
_setPlane.Get(k).SetObject(x, y, _pictureWidth, _pictureHeight);
_setPlane.Get(k).DrawningObject(g);
x = _pictureWidth - _placeSizeWidth - _placeSizeWidth / 2 - _placeSizeWidth / 4;
y += _placeSizeHeight ;
}
}
if (_setPlane.Get(k) == null)
{
if ((k + 1) % CountWidth != 0 || k ==0)
x -= _placeSizeWidth;
else
{
x = _pictureWidth - _placeSizeWidth - _placeSizeWidth / 2 - _placeSizeWidth / 4;
y += _placeSizeHeight;
}
}
}
}
public T getPlane(int pos){
return _setPlane.Get(pos);
}
}