From 2a90e0d4b01bdb5f027f5829dcf444585e28319d Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Tue, 1 Nov 2022 20:50:11 +0400 Subject: [PATCH] Generic classes --- src/AbstractMap.java | 2 +- src/DrawingObjectWarship.java | 2 +- src/IDrawingObject.java | 2 +- src/MapWithSetWarshipsGeneric.java | 126 +++++++++++++++++++++++++++++ src/SetWarshipsGeneric.java | 58 +++++++++++++ 5 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 src/MapWithSetWarshipsGeneric.java create mode 100644 src/SetWarshipsGeneric.java diff --git a/src/AbstractMap.java b/src/AbstractMap.java index b494ef9..0677956 100644 --- a/src/AbstractMap.java +++ b/src/AbstractMap.java @@ -139,7 +139,7 @@ public abstract class AbstractMap { } } } - _drawingObject.DrawningObject(gr); + _drawingObject.DrawingObject(gr); return bmp; } diff --git a/src/DrawingObjectWarship.java b/src/DrawingObjectWarship.java index 02d0a4a..3fa8b93 100644 --- a/src/DrawingObjectWarship.java +++ b/src/DrawingObjectWarship.java @@ -25,7 +25,7 @@ public class DrawingObjectWarship implements IDrawingObject { } @Override - public void DrawningObject(Graphics g) { + public void DrawingObject(Graphics g) { _warship.DrawTransport(g); } diff --git a/src/IDrawingObject.java b/src/IDrawingObject.java index f65592b..e32a8d8 100644 --- a/src/IDrawingObject.java +++ b/src/IDrawingObject.java @@ -8,7 +8,7 @@ public interface IDrawingObject { // Изменение направления пермещения объекта void MoveObject(Direction direction); // Отрисовка объекта - void DrawningObject(Graphics g); + void DrawingObject(Graphics g); // Получение текущей позиции объекта // /Left, Right, Top, Bottom) float[] GetCurrentPosition(); diff --git a/src/MapWithSetWarshipsGeneric.java b/src/MapWithSetWarshipsGeneric.java new file mode 100644 index 0000000..b4f55a0 --- /dev/null +++ b/src/MapWithSetWarshipsGeneric.java @@ -0,0 +1,126 @@ +import java.awt.*; +import java.awt.image.BufferedImage; + +public class MapWithSetWarshipsGeneric { + private final int _pictureWidth; + private final int _pictureHeight; + private final int _placeSizeWidth = 120; + private final int _placeSizeHeight = 50; + private final SetWarshipsGeneric _setWarships; + private final U _map; + + public MapWithSetWarshipsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight/_placeSizeHeight; + _setWarships = new SetWarshipsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public int add(T warship) + { + return _setWarships.Insert(warship); + } + + public T remove(int position) + { + return _setWarships.Remove(position); + } + + public Image ShowSet() + { + BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureWidth,BufferedImage.TYPE_INT_RGB); + Graphics gr = bmp.getGraphics(); + DrawBackground(gr); + DrawWarship(gr); + return bmp; + } + + public Image ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setWarships.getCount(); i++) + { + var warship = _setWarships.Get(i); + if (warship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, warship); + } + } + return new BufferedImage(_pictureWidth, _pictureHeight,BufferedImage.TYPE_INT_RGB); + } + + public Image MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new BufferedImage(_pictureWidth, _pictureHeight,BufferedImage.TYPE_INT_RGB); + } + + public void Shaking() + { + int j = _setWarships.getCount() - 1; + for (int i = 0; i < _setWarships.getCount(); i++) + { + if (_setWarships.Get(i) == null) + { + for (; j > i; j--) + { + var warship = _setWarships.Get(j); + if (warship != null) + { + _setWarships.Insert(warship, i); + _setWarships.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics gr) + { + Graphics2D g=(Graphics2D)gr; + + Color brush=Color.BLACK; + Stroke penWide = new BasicStroke(5); + Stroke penThin = new BasicStroke(1); + + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.setColor(brush); + g.setStroke(penWide); + g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight+2, i * _placeSizeWidth + (int)(_placeSizeWidth*0.8), j * _placeSizeHeight+2); + g.drawLine(i * _placeSizeWidth + 20, j * _placeSizeHeight + _placeSizeHeight/2+2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight/2+2); + g.drawLine(i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight/2); + g.drawLine(i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth+ (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight); + g.setStroke(penThin); + } + } + } + + private void DrawWarship(Graphics gr) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + for (int i = 0; i < _setWarships.getCount(); i++) + { + if (_setWarships.Get(i) != null) + { + _setWarships.Get(i).SetObject(i % width * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setWarships.Get(i).DrawingObject(gr); + } + } + } +} diff --git a/src/SetWarshipsGeneric.java b/src/SetWarshipsGeneric.java new file mode 100644 index 0000000..e217ade --- /dev/null +++ b/src/SetWarshipsGeneric.java @@ -0,0 +1,58 @@ +public class SetWarshipsGeneric{ + private T[] _places; + + public int getCount() { + return _places != null ? _places.length : 0; + } + + public SetWarshipsGeneric(int count) + { + _places = (T[]) new Object[count]; + } + + public int Insert(T warship) + { + return Insert(warship,0); + } + + public int Insert(T warship, int position) + { + if (position < 0 || position >= getCount() || _places[position] == null) + return -1; + + int empty = -1; + for (int i = position + 1; i < getCount(); i++) + { + if (_places[i] == null) + empty = i; + } + if (empty == -1) + return 0; + else + { + for (int i = empty; i > position; i--) + _places[i] = _places[i - 1]; + } + _places[position] = warship; + return 1; + } + + + public T Remove(int position) + { + if (position >= getCount() || position < 0 || _places[position] == null) + return null; + + T deleted =_places[position]; + _places[position] = null; + return deleted; + } + + public T Get(int position) + { + if (position >= getCount() || position < 0) + return null; + + return _places[position]; + } +}