PIbd-12_Karamushko_M.K._Air.../MapWithSetAircraftsGeneric.java
2022-12-15 13:54:34 +03:00

185 lines
5.2 KiB
Java

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ListIterator;
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> _setAircrafts;
private U _map;
public MapWithSetAircraftsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setAircrafts = new SetAircraftsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public U getMap() {
return _map;
}
public String GetData(char separatorType, char separatorData)
{
String data = _map.getClass().getName() + "" + separatorType;
ListIterator<T> iter = _setAircrafts.GetElems();
while (iter.hasNext())
{
data += iter.next().GetInfo() + separatorData;
}
return data;
}
public void ClearMap() {
_setAircrafts.Clear();
}
public void LoadData(String[] records) throws StorageOverflowException {
for (String rec : records)
{
_setAircrafts.Insert((T)DrawingObjectAircraft.Create(rec));
}
}
public int addAircraft(T aircraft) throws StorageOverflowException
{
return _setAircrafts.Insert(aircraft);
}
public T removeAircraft(int position) throws AircraftNotFoundException
{
return _setAircrafts.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 < _setAircrafts.getCount(); i++)
{
var car = _setAircrafts.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;
}
public ListIterator<T> getAircraftsIter() {
return _setAircrafts.GetElems();
}
private void Shaking()
{
int j = _setAircrafts.getCount() - 1;
for (int i = 0; i < _setAircrafts.getCount(); i++)
{
if (_setAircrafts.Get(i) == null)
{
for (; j > i; j--)
{
var car = _setAircrafts.Get(j);
if (car != null)
{
_setAircrafts.Insert(car, i);
_setAircrafts.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 < _setAircrafts.getCount(); i++)
{
int x = i % width;
int y = i / width;
T current =_setAircrafts.Get(i);
if(current == null) continue;
current.SetObject(x * _placeSizeWidth, y * _placeSizeHeight, _pictureWidth, _pictureHeight);
current.DrawningObject(g);
}
}
}