Pibd-22_Presnyakova.V.V_Cat.../MapWithSetBoatsGeneric.java
2022-12-23 10:22:06 +04:00

133 lines
4.1 KiB
Java

import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetBoatsGeneric <T extends IDrawningObject, U extends AbstractMap>
{
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 210;
private int _placeSizeHeight = 90;
private SetBoatsGeneric<T> _setBoats;
private U _map;
public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBoats = new SetBoatsGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int addBoat(T boat)
{
return _setBoats.Insert(boat);
}
public T removeBoat(int position)
{
return _setBoats.Remove(position);
}
public BufferedImage ShowSet()
{
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = (Graphics2D) img.getGraphics();
DrawBackground(gr);
DrawBoats(gr);
return img;
}
public BufferedImage ShowOnMap()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Shaking();
for (int i = 0; i < _setBoats.Count(); i++)
{
var ship = _setBoats.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 = _setBoats.Count() - 1;
for (int i = 0; i < _setBoats.Count(); i++)
{
if (_setBoats.Get(i) == null)
{
for (; j > i; j--)
{
var boat = _setBoats.Get(j);
if (boat != null)
{
_setBoats.Insert(boat, i);
_setBoats.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
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);
for (int k = 0; k < 7; k++)
{
g.drawOval( i * _placeSizeWidth + 15 * k, j * _placeSizeHeight, 15, 15);
g.drawOval( i * _placeSizeWidth + 15 * k, j * _placeSizeHeight - 15, 15, 15);
}
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
for (int k = 0; k < 6 * 4; k++)
{
g.drawOval( i * _placeSizeWidth, 0 + 15 * k, 15, 15);
}
}
}
private void DrawBoats(Graphics g)
{
int numberOfSeatsInWidth = _pictureWidth / _placeSizeWidth;
int numberOfSeatsInHeight = _pictureHeight / _placeSizeHeight;
int bottomLine = (numberOfSeatsInHeight - 1) * _placeSizeHeight;
for (int i = 0; i < _setBoats.Count(); i++)
{
if(_setBoats.Get(i) != null)
{
_setBoats.Get(i).SetObject(i % numberOfSeatsInWidth * _placeSizeWidth, bottomLine - i / numberOfSeatsInWidth * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setBoats.Get(i).DrawningObject(g);
}
}
}
}