Eliseev E.E. LabWork03 #3
205
Project/src/MapWithSetPlanesGeneric.java
Normal file
205
Project/src/MapWithSetPlanesGeneric.java
Normal file
@ -0,0 +1,205 @@
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class MapWithSetPlanesGeneric <T extends IDrawningObject, U extends AbstractMap>
|
||||
{
|
||||
//ширина окна отрисовки
|
||||
private final int _pictureWidth;
|
||||
|
||||
//высота окна отрисовки
|
||||
private final int _pictureHeight;
|
||||
|
||||
//размер занимаемого объектом места (ширина)
|
||||
private final int _placeSizeWidth = 210;
|
||||
|
||||
//размер занимаемого объектом места (высота)
|
||||
private final int _placeSizeHeight = 90;
|
||||
|
||||
//набор объектов
|
||||
private final SetPlanesGeneric<T> _setPlanes;
|
||||
|
||||
//карта
|
||||
private final U _map;
|
||||
|
||||
//конструктор
|
||||
public MapWithSetPlanesGeneric(int picWidth, int picHeight, U map)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_setPlanes = new SetPlanesGeneric<T>(width * height);
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_map = map;
|
||||
}
|
||||
|
||||
/*
|
||||
//пеергрузка оператора сложения
|
||||
public static int operator +(MapWithSetPlanesGeneric<T, U> map, T plane)
|
||||
{
|
||||
return map._setPlanes.Insert(plane);
|
||||
}
|
||||
|
||||
//перегрузка оператора вычитания
|
||||
public static T operator -(MapWithSetPlanesGeneric<T, U> map, int position)
|
||||
{
|
||||
return map._setPlanes.Remove(position);
|
||||
}
|
||||
*/
|
||||
|
||||
//вывод всего набора объектов
|
||||
public BufferedImage ShowSet()
|
||||
{
|
||||
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics gr = bmp.getGraphics();
|
||||
DrawBackground(gr);
|
||||
DrawPlanes(gr);
|
||||
return bmp;
|
||||
}
|
||||
|
||||
//просмотр объекта на карте
|
||||
public BufferedImage ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
|
||||
for(int i = 0; i < _setPlanes.Count(); i++)
|
||||
{
|
||||
var plane = _setPlanes.Get(i);
|
||||
|
||||
if(plane != null)
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, plane);
|
||||
}
|
||||
}
|
||||
|
||||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||
}
|
||||
|
||||
//пеермещение объекта по карте
|
||||
public BufferedImage MoveObject(Direction direction)
|
||||
{
|
||||
if(_map != null)
|
||||
{
|
||||
return _map.MoveObject(direction);
|
||||
}
|
||||
|
||||
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_RGB);
|
||||
}
|
||||
|
||||
//"взламываем" набор, чтобы все элементы оказались в начале
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//метод отрисовки фона
|
||||
public void DrawBackground(Graphics g)
|
||||
{
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
//заливаем область в цвет бетона
|
||||
g2d.setPaint(Color.LIGHT_GRAY);
|
||||
g.fillRect(0, 0, _pictureWidth, _pictureHeight);
|
||||
|
||||
g2d.setStroke(new BasicStroke(3));
|
||||
g2d.setColor(Color.BLACK);
|
||||
for(int i = 0; i < _pictureWidth / _placeSizeWidth - 1; i++)
|
||||
{
|
||||
for(int j = 2; j < _pictureHeight / _placeSizeHeight + 1; ++j)
|
||||
{//линия разметки места
|
||||
g2d.drawLine(i * _placeSizeWidth + 5, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 5, j * _placeSizeHeight);
|
||||
}
|
||||
|
||||
g2d.drawLine(i * _placeSizeWidth + 5, _placeSizeHeight * 2, i * _placeSizeWidth + 5, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
|
||||
}
|
||||
|
||||
//отрисовка разметки взлётной полосы
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(_placeSizeWidth * 2 + 30, 0, 185, _pictureHeight);
|
||||
g2d.fillRect(_placeSizeWidth * 2 + 35, 0, 175, _pictureHeight);
|
||||
|
||||
g2d.setStroke(new BasicStroke(5));
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawLine(_placeSizeWidth * 2 + 210, 0, _placeSizeWidth * 2 + 210, _pictureHeight);
|
||||
g2d.drawLine(_placeSizeWidth * 2 + 35, 0, _placeSizeWidth * 2 + 35, _pictureHeight);
|
||||
g2d.drawLine(_placeSizeWidth * 2 + 215, 0, _placeSizeWidth * 2 + 215, _pictureHeight);
|
||||
g2d.drawLine(_placeSizeWidth * 2 + 30, 0, _placeSizeWidth * 2 + 30, _pictureHeight);
|
||||
|
||||
g2d.setStroke(new BasicStroke(5));
|
||||
g2d.setColor(new Color(0xFF, 0x45, 0x00));
|
||||
for (int i = 0; i < _pictureHeight / _placeSizeHeight; ++i)
|
||||
{
|
||||
g2d.drawLine(_placeSizeWidth * 2 + 125, 20 + i * _placeSizeHeight, _placeSizeWidth * 2 + 125, (i + 1) * _placeSizeHeight - 20);
|
||||
}
|
||||
|
||||
for(int i = 0; i < _pictureHeight / 20; i++)
|
||||
{
|
||||
g2d.drawLine(_placeSizeWidth * 2 + 15, 20 + i * _placeSizeHeight / 2, _placeSizeWidth * 2 + 15, (i + 1) * _placeSizeHeight / 2 - 20);
|
||||
g2d.drawLine(_placeSizeWidth * 3 + 20, 20 + i * _placeSizeHeight / 2, _placeSizeWidth * 3 + 20, (i + 1) * _placeSizeHeight / 2 - 20);
|
||||
}
|
||||
|
||||
//отрисовка сочков
|
||||
for(int i = 1; i < 6; i++)
|
||||
{
|
||||
g2d.setColor(new Color(0xFF, 0xB6, 0xC1));
|
||||
int[] xPoint = {(i * 70 - 10) + 45, i * 70 - 10, i * 70 - 10};
|
||||
int[] yPoint = {30, 50, 10};
|
||||
g.fillPolygon(xPoint, yPoint, xPoint.length);
|
||||
|
||||
g2d.setStroke(new BasicStroke(3));
|
||||
g2d.setColor(Color.BLACK);
|
||||
g2d.drawLine(i * 70 - 10, 10, i * 70 - 10, 80);
|
||||
g2d.drawLine(i * 70 - 10, 10, (i * 70 - 10) + 45, 30);
|
||||
g2d.drawLine(i * 70 - 10, 50, (i * 70 - 10) + 45, 30);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//метод прорисовки объеков
|
||||
public void DrawPlanes(Graphics g)
|
||||
{
|
||||
int position = 0;
|
||||
int currentWidth = 1;
|
||||
int currentHeight = 5;
|
||||
|
||||
for (int i = 0; i < _setPlanes.Count(); i++)
|
||||
{
|
||||
_setPlanes.Get(i).SetObject(currentWidth * _placeSizeWidth + 20, currentHeight * _placeSizeHeight + 20, _pictureWidth, _pictureHeight);
|
||||
_setPlanes.Get(i).DrawningObject(g);
|
||||
|
||||
if(position % 2 == 0)
|
||||
{
|
||||
position++;
|
||||
currentWidth--;
|
||||
}
|
||||
else
|
||||
{
|
||||
position = 0;
|
||||
currentWidth = 1;
|
||||
currentHeight--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
93
Project/src/SetPlanesGeneric.java
Normal file
93
Project/src/SetPlanesGeneric.java
Normal file
@ -0,0 +1,93 @@
|
||||
public class SetPlanesGeneric<T extends Object>
|
||||
{
|
||||
//массив объектов, которые храним
|
||||
private final Object[] _places;
|
||||
|
||||
//количество объектов в массиве
|
||||
public int Count()
|
||||
{
|
||||
return _places.length;
|
||||
}
|
||||
|
||||
//конструктор
|
||||
public SetPlanesGeneric(int count)
|
||||
{
|
||||
_places = new Object[count];
|
||||
}
|
||||
|
||||
//добавление объекта в набор
|
||||
public int Insert(T plane)
|
||||
{
|
||||
return Insert(plane, 0);
|
||||
}
|
||||
|
||||
//добавление объекта в набор на конкретную позицию
|
||||
public int Insert(T plane, int position)
|
||||
{
|
||||
// проверка позиции
|
||||
if (position >= _places.length || position < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
//проверка, что элемент массива по этой позиции пустой, если нет, то
|
||||
if (_places[position] == null)
|
||||
{
|
||||
_places[position] = plane;
|
||||
return position;
|
||||
}
|
||||
|
||||
//проверка, что после вставляемого элемента в массиве есть пустой элемент
|
||||
int findEmptyPos = -1;
|
||||
|
||||
for (int i = position + 1; i < Count(); i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
findEmptyPos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (findEmptyPos < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// вставка по позиции
|
||||
_places[position] = plane;
|
||||
|
||||
return position;
|
||||
}
|
||||
|
||||
//удаление объекта из набора с конкретной позиции
|
||||
public T Remove(int position)
|
||||
{
|
||||
// проверка позиции
|
||||
if (position >= _places.length || position < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// удаление объекта из массива, присовив элементу массива значение null
|
||||
T temp = (T)_places[position];
|
||||
_places[position] = null;
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
//получение объекта из набора по позиции
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position >= _places.length || position < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (_places[position] == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return (T)_places[position];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user