PIbd-12_Karamushko_M.K._Air.../MapWithSetAircraftsGeneric.java
2022-11-15 17:20:34 +03:00

154 lines
4.3 KiB
Java

import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetAircraftsGeneric<T extends IDrawingObject, U extends AbstractMap>
{
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 210;
private int _placeSizeHeight = 170;
private SetAircraftsGeneric<T> _setCars;
private U _map;
public MapWithSetAircraftsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setCars = new SetAircraftsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int addAircraft(T aircraft)
{
return _setCars.Insert(aircraft);
}
public T removeAircraft(int position)
{
return _setCars.Remove(position);
}
public Image ShowSet()
{
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = (Graphics2D) img.getGraphics();
DrawBackground(gr);
DrawCars(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 < _setCars.getCount(); i++)
{
var car = _setCars.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 = _setCars.getCount() - 1;
for (int i = 0; i < _setCars.getCount(); i++)
{
if (_setCars.Get(i) == null)
{
for (; j > i; j--)
{
var car = _setCars.Get(j);
if (car != null)
{
_setCars.Insert(car, i);
_setCars.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics2D g)
{
Polygon angar = new Polygon();
angar.addPoint(0, _pictureHeight );
angar.addPoint(0, _pictureHeight / 4 );
angar.addPoint(_pictureWidth / 2 , 9);
angar.addPoint(_pictureWidth, _pictureHeight / 4 );
angar.addPoint(_pictureWidth, _pictureHeight );
g.setPaint(new Color(211, 136, 84));
g.fillPolygon(angar);
g.setPaint(new Color(160, 160, 160));
g.fillRect(_pictureWidth / 6, (_pictureHeight * 5) / 12, (_pictureWidth * 2) / 3, (_pictureHeight * 7) / 12);
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 DrawCars(Graphics2D g)
{
int width = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _setCars.getCount(); i++)
{
int x = i % width;
int y = i / width;
T current =_setCars.Get(i);
if(current == null) continue;
current.SetObject(x * _placeSizeWidth, y * _placeSizeHeight, _pictureWidth, _pictureHeight);
current.DrawningObject(g);
}
}
}