diff --git a/AbstractMap.java b/AbstractMap.java index cf2e1b3..2f797e6 100644 --- a/AbstractMap.java +++ b/AbstractMap.java @@ -83,7 +83,7 @@ public abstract class AbstractMap { return true; } - private BufferedImage DrawMapWithObject() + BufferedImage DrawMapWithObject() { BufferedImage bmp = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); if (_drawningObject == null || _map == null) diff --git a/MapWithSetPlanesGeneric.java b/MapWithSetPlanesGeneric.java new file mode 100644 index 0000000..d49924b --- /dev/null +++ b/MapWithSetPlanesGeneric.java @@ -0,0 +1,141 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetPlanesGeneric +{ + private int _pictureWidth; + private int _pictureHeight; + private int _placeSizeWidth = 210; + private int _placeSizeHeight = 90; + private SetPlanesGeneric _setPlanes; + private U _map; + + public MapWithSetPlanesGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setPlanes = new SetPlanesGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int addPlane(T planes) + { + return _setPlanes.Insert(planes); + } + + public T removePlane(int position) + { + return _setPlanes.Remove(position); + } + + public BufferedImage ShowSet() + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D gr = (Graphics2D) img.getGraphics(); + DrawBackground(gr); + DrawPlanes(gr); + return img; + } + + public BufferedImage ShowOnMap() + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = (Graphics2D) img.getGraphics(); + Shaking(); + for (int i = 0; i < _setPlanes.Count(); i++) + { + var car = _setPlanes.Get(i); + if (car != null) + { + _map.CreateMap(_pictureWidth, _pictureHeight, car); + _map.DrawMapWithObject(); + return img; + } + } + return img; + } + + public BufferedImage MoveObject(Direction direction) + { + BufferedImage img = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB); + if (_map != null) + { + _map.MoveObject(direction); + _map.DrawMapWithObject(); + } + return img; + } + + private void Shaking() + { + int j = _setPlanes.Count() - 1; + for (int i = 0; i < _setPlanes.Count(); i++) + { + if (_setPlanes.Get(i) == null) + { + for (; j > i; j--) + { + var plane = _setPlanes.Get(j); + if (plane != null) + { + _setPlanes.Insert(plane, i); + _setPlanes.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics g) + { + Graphics2D g2d = (Graphics2D) g; + g2d.setColor(Color.GRAY); + g.fillRect(0, 0, _pictureWidth, _pictureHeight); + g2d.setColor(Color.WHITE); + for (int y = 0; y < _pictureHeight; y+=80) + { + g.fillRect(_pictureWidth / 2 - 10, y, 20, 70); + } + int lastLine = 0; + for (int y = 50; y < _pictureHeight; y += _pictureHeight/3 - 50) + { + g.fillRect(_pictureWidth / 3 - 10, y, 20, 70); + g.fillRect(_pictureWidth - _pictureWidth / 3 - 10, y, 20, 70); + lastLine = y; + } + g.fillRect(_pictureWidth / 3 - 40, lastLine, 20, 70); + g.fillRect(_pictureWidth - _pictureWidth / 3 + 20, lastLine, 20, 70); + + g2d.setColor(Color.BLACK); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + + private void DrawPlanes(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < _setPlanes.Count(); i++) + { + if(_setPlanes.Get(i) != null) + { + _setPlanes.Get(i).SetObject((width - i % width - 1) * _placeSizeWidth,(height - i / width - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setPlanes.Get(i).DrawningObject(g); + } + } + } +} diff --git a/SetPlanesGeneric.java b/SetPlanesGeneric.java new file mode 100644 index 0000000..7eb7932 --- /dev/null +++ b/SetPlanesGeneric.java @@ -0,0 +1,44 @@ +public class SetPlanesGeneric { + private T[] _places; + + public int Count() { + return _places.length; + } + + public SetPlanesGeneric(int count) { + _places = (T[]) (new Object[count]); + } + + public int Insert(T plane) { + for (int i = 0; i < _places.length; i++) { + if (_places[i] == null) { + _places[i] = plane; + return i; + } + } + return -1; + } + + public int Insert(T plane, int position) { + int index = position; + + while (_places[index] != null && index < _places.length) index++; + + if (index == _places.length) return -1; + for (int i = index; i > position; --i) _places[i] = _places[i - 1]; + + _places[position] = plane; + return position; + } + + public T Remove(int position) { + if (position >= _places.length) return null; + T res = _places[position]; + _places[position] = null; + return res; + } + + public T Get(int position) { + return _places[position]; + } +} \ No newline at end of file