PIbd-22_Bondarenko_M.S._War.../MapWithSetShipGeneric.java

137 lines
4.3 KiB
Java
Raw Normal View History

2022-12-15 22:28:43 +04:00
import java.awt.*;
import java.awt.image.BufferedImage;
2022-12-15 22:31:51 +04:00
import java.util.LinkedList;
import java.util.Queue;
2022-12-15 22:28:43 +04:00
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;
2022-12-15 22:31:51 +04:00
private Queue<T> _deletedShips;
2022-12-15 22:28:43 +04:00
public MapWithSetShipGeneric(int picWidth, int picHeight, U map)
{
2022-12-15 22:31:51 +04:00
_deletedShips = new LinkedList<>();
2022-12-15 22:28:43 +04:00
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)
{
2022-12-15 22:31:51 +04:00
T temp = _setShips.Remove(position);
_deletedShips.add(temp);
return temp;
2022-12-15 22:28:43 +04:00
}
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()
{
2022-12-15 22:31:51 +04:00
//BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
2022-12-15 22:28:43 +04:00
Shaking();
2022-12-15 22:31:51 +04:00
for (var ship : _setShips)
2022-12-15 22:28:43 +04:00
{
2022-12-15 22:31:51 +04:00
return _map.CreateMap(_pictureWidth,_pictureHeight,ship);
2022-12-15 22:28:43 +04:00
}
2022-12-15 22:31:51 +04:00
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
}
public T GetSelectedShip(int ind){
return _setShips.Get(ind);
}
public T GetShipsDeleted()
{
if(_deletedShips.isEmpty())
{
return null;
}
return _deletedShips.remove();
2022-12-15 22:28:43 +04:00
}
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)
{
2022-12-15 22:31:51 +04:00
int i = 0;
for (var ship: _setShips)
2022-12-15 22:28:43 +04:00
{
2022-12-15 22:31:51 +04:00
int temp = 0;
if (ship.GetCurrentPosition()[3] - ship.GetCurrentPosition()[2] < 75) temp = (int)(ship.GetCurrentPosition()[3] - ship.GetCurrentPosition()[2]);
ship.SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight);
ship.DrawningObject(g);
++i;
2022-12-15 22:28:43 +04:00
}
}
}