Added MapWithSetArtilleriesGeneric

This commit is contained in:
Сергей Полевой 2022-10-24 22:36:41 +04:00
parent b481b124a8
commit a5112dcbdd

View File

@ -0,0 +1,133 @@
import java.awt.*;
import java.awt.image.BufferedImage;
public class MapWithSetArtilleriesGeneric<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 SetArtilleriesGeneric<T> _setArtilleries;
private final U _map;
public MapWithSetArtilleriesGeneric(int picWidth, int picHeight, U map) {
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setArtilleries = new SetArtilleriesGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
public int addArtillery(T artillery) {
return _setArtilleries.insert(artillery);
}
public T removeArtilleryAt(int position) {
return _setArtilleries.remove(position);
}
public Image showSet() {
BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) img.getGraphics();
drawBackground(g2d);
drawArtilleries(g2d);
return img;
}
public Image showOnMap() {
shaking();
for (int i = 0; i < _setArtilleries.getCount(); i++)
{
var car = _setArtilleries.get(i);
if (car != null)
{
return _map.createMap(_pictureWidth, _pictureHeight, car);
}
}
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 = _setArtilleries.getCount() - 1;
for (int i = 0; i < _setArtilleries.getCount(); i++)
{
if (_setArtilleries.get(i) == null)
{
for (; j > i; j--)
{
var artillery = _setArtilleries.get(j);
if (artillery != null)
{
_setArtilleries.insert(artillery, i);
_setArtilleries.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.setColor(pen);
g.setStroke(penStroke);
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
g.setColor(box);
g.fillRect(i * _placeSizeWidth + 5, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 - 5, _placeSizeWidth / 3 - 5, _placeSizeHeight / 3 - 5);
g.setColor(pen);
g.setStroke(thinPenStroke);
g.drawLine(i * _placeSizeWidth + 5, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 - 5, i * _placeSizeWidth + _placeSizeWidth / 3, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 + _placeSizeHeight / 3 - 10);
g.drawLine(i * _placeSizeWidth + 5, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 + _placeSizeHeight / 3 - 10, i * _placeSizeWidth + _placeSizeWidth / 3, j * _placeSizeHeight + _placeSizeHeight * 3 / 4 - 5);
g.setColor(flag);
g.fillRect(i * _placeSizeWidth + _placeSizeWidth * 5 / 12, j * _placeSizeHeight + _placeSizeHeight * 5 / 8 - 5, _placeSizeWidth / 5, _placeSizeHeight / 5);
g.setColor(pen);
g.setStroke(thinPenStroke);
g.drawLine(i * _placeSizeWidth + _placeSizeWidth * 5 / 12, j * _placeSizeHeight + _placeSizeHeight - 5, i * _placeSizeWidth + _placeSizeWidth * 5 / 12, j * _placeSizeHeight + _placeSizeHeight * 5 / 8 - 5);
}
g.setColor(pen);
g.setStroke(penStroke);
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
g.setStroke(normalStroke);
}
private void drawArtilleries(Graphics2D g) {
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _setArtilleries.getCount(); i++)
{
var artillery = _setArtilleries.get(i);
if (artillery != null)
{
artillery.setObject(i % width * _placeSizeWidth + 10, (height - 1 - i / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
artillery.drawingObject(g);
}
}
}
}