import java.awt.*; import java.awt.image.BufferedImage; import java.util.Iterator; public class MapWithSetArtilleriesGeneric { public final int _pictureWidth; public final int _pictureHeight; public final int _placeSizeWidth = 210; public final int _placeSizeHeight = 90; private final SetArtilleriesGeneric _setArtilleries; private final U _map; public MapWithSetArtilleriesGeneric(int picWidth, int picHeight, U map) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _setArtilleries = new SetArtilleriesGeneric<>(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 (T artillery : _setArtilleries.getArtilleries()) { return _map.createMap(_pictureWidth, _pictureHeight, artillery); } 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; int index = 0; for (T artillery : _setArtilleries.getArtilleries()) { artillery.setObject(index % width * _placeSizeWidth + 10, (height - 1 - index / width) * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); artillery.drawingObject(g); index++; } } }