PIbd-22_Bondarenko_M.S._War.../MapWithSetShipGeneric.java
Макс Бондаренко 30e3d67aeb готово 3
2022-12-15 22:28:43 +04:00

123 lines
4.1 KiB
Java

import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetShipGeneric<T extends IDrawningObject, U extends AbstractMap> {
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 210;
private int _placeSizeHeight = 100;
private SetShipGeneric<T> _setShips;
private U _map;
public MapWithSetShipGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setShips = new SetShipGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int Add(T ship)
{
return _setShips.Insert(ship);
}
public T Delete(int position)
{
return _setShips.Remove(position);
}
public BufferedImage ShowSet()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Graphics g = bmp.getGraphics();
DrawBackground((Graphics2D) g);
DrawShips(g);
return bmp;
}
public BufferedImage ShowOnMap()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Shaking();
for (int i = 0; i < _setShips.getCount(); i++)
{
var ship = _setShips.Get(i);
if (ship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, ship);
}
}
return bmp;
}
public BufferedImage MoveObject(Direction direction)
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
if (_map != null)
{
return _map.MoveObject(direction);
}
return bmp;
}
private void Shaking()
{
int j = _setShips.getCount() - 1;
for (int i = 0; i < _setShips.getCount(); ++i)
{
if (_setShips.Get(i) == null)
{
for (; j > i; --j)
{
var car = _setShips.Get(j);
if (car != null)
{
_setShips.Insert(car, i);
_setShips.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics2D g2d)
{
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, _pictureWidth, _pictureHeight);
g2d.setColor(new Color(150, 75, 0));
for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i)
{
for (int j = 1; j < _pictureHeight / _placeSizeHeight + 1; ++j) //линия рамзетки места
{
g2d.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
for (int k = 0; k < _placeSizeWidth / (30 * 2); ++k)
g2d.drawLine(i * _placeSizeWidth + (k + 1) * 30, j * _placeSizeHeight, i * _placeSizeWidth + (k + 1) * 30, j * _placeSizeHeight + 30);
}
g2d.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawShips(Graphics g)
{
for (int i = 0; i < _setShips.getCount(); ++i)
{
if (_setShips.Get(i) != null )
{
int temp = 0;
if (_setShips.Get(i).GetCurrentPosition()[3] - _setShips.Get(i).GetCurrentPosition()[2] < 75) temp = (int)(_setShips.Get(i).GetCurrentPosition()[3] - _setShips.Get(i).GetCurrentPosition()[2]);
_setShips.Get(i).SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight);
_setShips.Get(i).DrawningObject(g);
}
}
}
}