import java.awt.*; import java.awt.image.BufferedImage; import java.util.LinkedList; import java.util.Queue; public class MapWithSetShipGeneric { private int _pictureWidth; private int _pictureHeight; private int _placeSizeWidth = 210; private int _placeSizeHeight = 100; private SetShipGeneric _setShips; private U _map; private Queue _deletedShips; public MapWithSetShipGeneric(int picWidth, int picHeight, U map) { _deletedShips = new LinkedList<>(); int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _setShips = new SetShipGeneric(width * height); _pictureWidth = picWidth; _pictureHeight = picHeight; _map = map; } public int Add(T ship) { return _setShips.Insert(ship); } public T Delete(int position) { T temp = _setShips.Remove(position); _deletedShips.add(temp); return temp; } public BufferedImage ShowSet() { BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); Graphics g = bmp.getGraphics(); DrawBackground((Graphics2D) g); DrawShips(g); return bmp; } public BufferedImage ShowOnMap() { //BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); Shaking(); for (var ship : _setShips) { return _map.CreateMap(_pictureWidth,_pictureHeight,ship); } return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); } public T GetSelectedShip(int ind){ return _setShips.Get(ind); } public T GetShipsDeleted() { if(_deletedShips.isEmpty()) { return null; } return _deletedShips.remove(); } public BufferedImage MoveObject(Direction direction) { BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB); if (_map != null) { return _map.MoveObject(direction); } return bmp; } 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 car = _setShips.Get(j); if (car != null) { _setShips.Insert(car, i); _setShips.Remove(j); break; } } if (j <= i) { return; } } } } private void DrawBackground(Graphics2D g2d) { g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, _pictureWidth, _pictureHeight); g2d.setColor(new Color(150, 75, 0)); for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i) { for (int j = 1; j < _pictureHeight / _placeSizeHeight + 1; ++j) //линия рамзетки места { g2d.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); for (int k = 0; k < _placeSizeWidth / (30 * 2); ++k) g2d.drawLine(i * _placeSizeWidth + (k + 1) * 30, j * _placeSizeHeight, i * _placeSizeWidth + (k + 1) * 30, j * _placeSizeHeight + 30); } g2d.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); } } private void DrawShips(Graphics g) { int i = 0; for (var ship: _setShips) { int temp = 0; if (ship.GetCurrentPosition()[3] - ship.GetCurrentPosition()[2] < 75) temp = (int)(ship.GetCurrentPosition()[3] - ship.GetCurrentPosition()[2]); ship.SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight + temp, _pictureWidth, _pictureHeight); ship.DrawningObject(g); ++i; } } }