From a5112dcbdd83f731af78c8ecddde04f999198a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9?= Date: Mon, 24 Oct 2022 22:36:41 +0400 Subject: [PATCH] Added MapWithSetArtilleriesGeneric --- MapWithSetArtilleriesGeneric.java | 133 ++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 MapWithSetArtilleriesGeneric.java diff --git a/MapWithSetArtilleriesGeneric.java b/MapWithSetArtilleriesGeneric.java new file mode 100644 index 0000000..79fd53b --- /dev/null +++ b/MapWithSetArtilleriesGeneric.java @@ -0,0 +1,133 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +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 (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); + } + } + } +}