Pibd-23_Zhelovanov_D.Y._Bat.../MapWithSetBattleshipsGeneric.java

139 lines
4.7 KiB
Java

import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetBattleshipsGeneric <T extends IDrawningObject, U extends AbstractMap>{
/// Ширина окна отрисовки
private final int _pictureWidth;
/// Высота окна отрисовки
private final int _pictureHeight;
/// Размер занимаемого объектом места (ширина)
private final int _placeSizeWidth = 250;
/// Размер занимаемого объектом места (высота)
private final int _placeSizeHeight = 100;
private final SetBattleshipGeneric<T> _setBattleship;
/// Карта
private final U _map;
/// Конструктор
public MapWithSetBattleshipsGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setBattleship = new SetBattleshipGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int Add(T battleship)
{
return this._setBattleship.Insert(battleship);
}
public T Subtraction(int position)
{
return this._setBattleship.Remove(position);
}
public BufferedImage ShowSet()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
Graphics gr = bmp.getGraphics();
DrawBackground((Graphics2D)gr);
DrawBattleship((Graphics2D)gr);
return bmp;
}
public BufferedImage ShowOnMap()
{
Shaking();
for (int i = 0; i < _setBattleship.Count(); i++)
{
var battleship = _setBattleship.Get(i);
if (battleship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, battleship);
}
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
}
/// Перемещение объекта по крате
public BufferedImage MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
}
private void Shaking()
{
int j = _setBattleship.Count() - 1;
for (int i = 0; i < _setBattleship.Count(); i++)
{
if (_setBattleship.Get(i) == null)
{
for (; j > i; j--)
{
var battleship = _setBattleship.Get(j);
if (battleship != null)
{
_setBattleship.Insert(battleship, i);
_setBattleship.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
private void DrawBackground(Graphics2D g)
{
g.setColor(Color.blue);
g.fillRect(0, 0, _pictureWidth, _pictureHeight);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j <= _pictureHeight / _placeSizeHeight; ++j)
{
g.setStroke(new BasicStroke(7));
g.setColor(Color.black);
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i *
_placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
g.setColor(Color.RED);
g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight-5, i * _placeSizeWidth + 20, j * _placeSizeHeight - 23);
g.drawLine( i * _placeSizeWidth + 70, j * _placeSizeHeight - 5, i * _placeSizeWidth + 70, j * _placeSizeHeight - 23);
}
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth,
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
private void DrawBattleship(Graphics2D g)
{
int countColumns = _pictureWidth / _placeSizeWidth - 1;
int countRows = _pictureHeight / _placeSizeHeight - 1;
int currentColumn = 0;
for (int i = 0; i < _setBattleship.Count(); i++)
{
if((_setBattleship.Get(i) != null))
{
_setBattleship.Get(i).SetObject((currentColumn) * _placeSizeWidth + 10, (countRows) * _placeSizeHeight + 15, _pictureWidth, _pictureHeight);
_setBattleship.Get(i).DrawningObject(g);
}
if (currentColumn >= countColumns)
{
currentColumn = 0;
countRows--;
}
else
{
currentColumn++;
}
}
}
}