111 lines
3.4 KiB
Java
111 lines
3.4 KiB
Java
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
|
|
public class MapWithSetShipsGeneric<T extends IDrawingObject, U extends AbstractMap> {
|
|
public final int _pictureWidth;
|
|
public final int _pictureHeight;
|
|
public final int _placeSizeWidth = 210;
|
|
public final int _placeSizeHeight = 90;
|
|
private final SetShipsGeneric<T> _setShips;
|
|
private final U _map;
|
|
|
|
public MapWithSetShipsGeneric(int picWidth, int picHeight, U map) {
|
|
int width = picWidth / _placeSizeWidth;
|
|
int height = picHeight / _placeSizeHeight;
|
|
_setShips = new SetShipsGeneric<T>(width * height);
|
|
_pictureWidth = picWidth;
|
|
_pictureHeight = picHeight;
|
|
_map = map;
|
|
}
|
|
|
|
public int addShip(T ship) {
|
|
return _setShips.insert(ship);
|
|
}
|
|
|
|
public T removeShipAt(int position) {
|
|
return _setShips.remove(position);
|
|
}
|
|
|
|
public Image showSet() {
|
|
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
|
Graphics2D g2d = (Graphics2D) img.getGraphics();
|
|
drawBackground(g2d);
|
|
drawShips(g2d);
|
|
return img;
|
|
}
|
|
|
|
public Image showOnMap() {
|
|
shaking();
|
|
for (int i = 0; i < _setShips.getCount(); i++)
|
|
{
|
|
var ship = _setShips.get(i);
|
|
if (ship != null)
|
|
{
|
|
return _map.createMap(_pictureWidth, _pictureHeight, ship);
|
|
}
|
|
}
|
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
|
}
|
|
|
|
public Image moveObject(Direction direction) {
|
|
if (_map != null) {
|
|
return _map.moveObject(direction);
|
|
}
|
|
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
|
}
|
|
|
|
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 ship = _setShips.get(j);
|
|
if (ship != null)
|
|
{
|
|
_setShips.insert(ship, i);
|
|
_setShips.remove(j);
|
|
break;
|
|
}
|
|
}
|
|
if (j <= i)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void drawBackground(Graphics2D g) {
|
|
Color pen = Color.black;
|
|
Color box = new Color(0, 100, 0, 255);
|
|
Color flag = Color.red;
|
|
Stroke normalStroke = new BasicStroke(1);
|
|
Stroke penStroke = new BasicStroke(3);
|
|
Stroke thinPenStroke = new BasicStroke(2);
|
|
|
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
|
{
|
|
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {
|
|
}
|
|
}
|
|
g.setStroke(normalStroke);
|
|
}
|
|
|
|
private void drawShips(Graphics2D g) {
|
|
int width = _pictureWidth / _placeSizeWidth;
|
|
int height = _pictureHeight / _placeSizeHeight;
|
|
|
|
for (int i = 0; i < _setShips.getCount(); i++)
|
|
{
|
|
var ship = _setShips.get(i);
|
|
if (ship != null)
|
|
{
|
|
ship.setObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
|
ship.drawingObject(g);
|
|
}
|
|
}
|
|
}
|
|
} |