Созданы generic-классы
This commit is contained in:
parent
a19996af00
commit
a88bc283c7
138
src/main/java/MapWithSetArmoredCarsGeneric.java
Normal file
138
src/main/java/MapWithSetArmoredCarsGeneric.java
Normal file
@ -0,0 +1,138 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class MapWithSetArmoredCarsGeneric<T extends IDrawingObject, U extends AbstractMap> {
|
||||
|
||||
private int _pictureWidth;
|
||||
|
||||
private int _pictureHeight;
|
||||
|
||||
private int _placeSizeWidth = 210;
|
||||
|
||||
private int _placeSizeHeight = 90;
|
||||
|
||||
private SetArmoredCarsGeneric<T> _setCars;
|
||||
|
||||
private U _map;
|
||||
|
||||
public MapWithSetArmoredCarsGeneric(int picWidth, int picHeight, U map)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_setCars = new SetArmoredCarsGeneric<T>(width * height);
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_map = map;
|
||||
}
|
||||
|
||||
public int add(MapWithSetArmoredCarsGeneric<T, U> map, T car)
|
||||
{
|
||||
return map._setCars.Insert(car);
|
||||
}
|
||||
|
||||
public T remove(MapWithSetArmoredCarsGeneric<T, U> map, int position)
|
||||
{
|
||||
return map._setCars.Remove(position);
|
||||
}
|
||||
|
||||
public Image ShowSet()
|
||||
{
|
||||
Image bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_BGR);
|
||||
Graphics gr = bmp.getGraphics();
|
||||
DrawBackground(gr);
|
||||
DrawArmoredCars(gr);
|
||||
return bmp;
|
||||
}
|
||||
|
||||
public Image ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
for (int i = 0; i < _setCars.Count; i++)
|
||||
{
|
||||
var car = _setCars.Get(i);
|
||||
if (car != null)
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
|
||||
}
|
||||
}
|
||||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_BGR);
|
||||
}
|
||||
|
||||
public Image MoveObject(Direction direction)
|
||||
{
|
||||
if (_map != null)
|
||||
{
|
||||
return _map.MoveObject(direction);
|
||||
}
|
||||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_BGR);
|
||||
}
|
||||
|
||||
private void Shaking()
|
||||
{
|
||||
int j = _setCars.Count - 1;
|
||||
for (int i = 0; i < _setCars.Count; i++)
|
||||
{
|
||||
if (_setCars.Get(i) == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var car = _setCars.Get(j);
|
||||
if (car != null)
|
||||
{
|
||||
_setCars.Insert(car, i);
|
||||
_setCars.Remove(j);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j <= i)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawBackground(Graphics g)
|
||||
{
|
||||
Color colorRoad = Color.GRAY;
|
||||
Color colorBox = new Color(99, 99, 99);
|
||||
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
||||
{
|
||||
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||||
{
|
||||
g.setColor(colorRoad);
|
||||
g.fillRect(i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
|
||||
g.drawLine(i * _placeSizeWidth, j * _placeSizeHeight,
|
||||
i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
|
||||
g.setColor(colorBox);
|
||||
int boxSize = _placeSizeWidth / 7;
|
||||
g.fillRect(i * _placeSizeWidth + _placeSizeWidth / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5, boxSize, boxSize);
|
||||
g.drawRect(i * _placeSizeWidth + _placeSizeWidth / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5, boxSize, boxSize);
|
||||
g.fillRect(i * _placeSizeWidth + _placeSizeWidth / 2 - boxSize / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5 + boxSize, boxSize, boxSize);
|
||||
g.drawRect( i * _placeSizeWidth + _placeSizeWidth / 2 - boxSize / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5 + boxSize, boxSize, boxSize);
|
||||
g.fillRect(i * _placeSizeWidth + _placeSizeWidth / 2 + boxSize / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5 + boxSize, boxSize, boxSize);
|
||||
g.drawRect( i * _placeSizeWidth + _placeSizeWidth / 2 + boxSize / 2,
|
||||
j * _placeSizeHeight + _placeSizeHeight / 5 + boxSize, boxSize, boxSize);
|
||||
}
|
||||
g.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth,
|
||||
(_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawArmoredCars(Graphics g)
|
||||
{
|
||||
int width = _pictureWidth / _placeSizeWidth;
|
||||
for (int i = 0; i < _setCars.Count; i++)
|
||||
{
|
||||
if (_setCars.Get(i) != null) {
|
||||
_setCars.Get(i).SetObject(i % width * _placeSizeWidth + 5, i / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
|
||||
_setCars.Get(i).DrawingObject((Graphics2D) g);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
80
src/main/java/SetArmoredCarsGeneric.java
Normal file
80
src/main/java/SetArmoredCarsGeneric.java
Normal file
@ -0,0 +1,80 @@
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SetArmoredCarsGeneric<T> {
|
||||
private T[] _places;
|
||||
public int Count = _places.length;
|
||||
|
||||
public SetArmoredCarsGeneric(int count)
|
||||
{
|
||||
_places = (T[]) new Object[count];
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
/// </summary>
|
||||
/// <param name="armoredCar">Добавляемый автомобиль</param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T armoredCar)
|
||||
{
|
||||
return Insert(armoredCar, 0);
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
/// </summary>
|
||||
/// <param name="armoredCar">Добавляемый автомобиль</param>
|
||||
/// <param name="position">Позиция</param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T armoredCar, int position)
|
||||
{
|
||||
if (position < 0 || position >= Count)
|
||||
return -1;
|
||||
|
||||
if (!(_places[position] == null))
|
||||
{
|
||||
int index_empty = -1;
|
||||
// поиск первого пустого элемента
|
||||
for (int i = position + 1; i < Count; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
index_empty = i;
|
||||
}
|
||||
}
|
||||
if (index_empty == -1)
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
for (int i = index_empty; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
_places[position] = armoredCar;
|
||||
return position;
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление объекта из набора с конкретной позиции
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position < 0 || position >= Count)
|
||||
return null;
|
||||
T armoredCar = _places[position];
|
||||
_places[position] = null;
|
||||
return armoredCar;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение объекта из набора по позиции
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position < 0 || position >= Count)
|
||||
return null;
|
||||
return _places[position];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user