Generic classes
This commit is contained in:
parent
4c43d95862
commit
2a90e0d4b0
@ -139,7 +139,7 @@ public abstract class AbstractMap {
|
||||
}
|
||||
}
|
||||
}
|
||||
_drawingObject.DrawningObject(gr);
|
||||
_drawingObject.DrawingObject(gr);
|
||||
return bmp;
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,7 @@ public class DrawingObjectWarship implements IDrawingObject {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void DrawningObject(Graphics g) {
|
||||
public void DrawingObject(Graphics g) {
|
||||
_warship.DrawTransport(g);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
126
src/MapWithSetWarshipsGeneric.java
Normal file
126
src/MapWithSetWarshipsGeneric.java
Normal file
@ -0,0 +1,126 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class MapWithSetWarshipsGeneric <T extends IDrawingObject,U extends AbstractMap> {
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
private final int _placeSizeWidth = 120;
|
||||
private final int _placeSizeHeight = 50;
|
||||
private final SetWarshipsGeneric<T> _setWarships;
|
||||
private final U _map;
|
||||
|
||||
public MapWithSetWarshipsGeneric(int picWidth, int picHeight, U map)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight/_placeSizeHeight;
|
||||
_setWarships = new SetWarshipsGeneric<T>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
58
src/SetWarshipsGeneric.java
Normal file
58
src/SetWarshipsGeneric.java
Normal file
@ -0,0 +1,58 @@
|
||||
public class SetWarshipsGeneric<T>{
|
||||
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];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user