Pibd-22_Emelyanov_A.S._Airb.../MapWithSetPlanesGeneric.java
2022-11-30 19:25:43 +04:00

139 lines
4.2 KiB
Java

import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends AbstractMap>
{
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 210;
private int _placeSizeHeight = 90;
private SetPlanesGeneric<T> _setPlanes;
private U _map;
public MapWithSetPlanesGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setPlanes = new SetPlanesGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int addPlane(T planes)
{
return _setPlanes.Insert(planes);
}
public T removePlane(int position)
{
return _setPlanes.Remove(position);
}
public BufferedImage ShowSet()
{
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = (Graphics2D) img.getGraphics();
DrawBackground(gr);
DrawPlanes(gr);
return img;
}
public BufferedImage ShowOnMap()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Shaking();
for (int i = 0; i < _setPlanes.Count(); i++)
{
var ship = _setPlanes.Get(i);
if (ship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
}
return bmp;
}
public BufferedImage MoveObject(Direction direction)
{
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
if (_map != null)
{
_map.MoveObject(direction);
}
return img;
}
private void Shaking()
{
int j = _setPlanes.Count() - 1;
for (int i = 0; i < _setPlanes.Count(); i++)
{
if (_setPlanes.Get(i) == null)
{
for (; j > i; j--)
{
var plane = _setPlanes.Get(j);
if (plane != null)
{
_setPlanes.Insert(plane, i);
_setPlanes.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.GRAY);
g.fillRect(0, 0, _pictureWidth, _pictureHeight);
g2d.setColor(Color.WHITE);
for (int y = 0; y < _pictureHeight; y+=80)
{
g.fillRect(_pictureWidth / 2 - 10, y, 20, 70);
}
int lastLine = 0;
for (int y = 50; y < _pictureHeight; y += _pictureHeight/3 - 50)
{
g.fillRect(_pictureWidth / 3 - 10, y, 20, 70);
g.fillRect(_pictureWidth - _pictureWidth / 3 - 10, y, 20, 70);
lastLine = y;
}
g.fillRect(_pictureWidth / 3 - 40, lastLine, 20, 70);
g.fillRect(_pictureWidth - _pictureWidth / 3 + 20, lastLine, 20, 70);
g2d.setColor(Color.BLACK);
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);
}
}
private void DrawPlanes(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setPlanes.Count(); i++)
{
if(_setPlanes.Get(i) != null)
{
_setPlanes.Get(i).SetObject((width - i % width - 1) * _placeSizeWidth,(height - i / width - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setPlanes.Get(i).DrawningObject(g);
}
}
}
}