addGenerics
This commit is contained in:
parent
e14664fe07
commit
c1a5d23a36
@ -7,6 +7,8 @@ import javax.swing.Timer;
|
||||
import java.awt.event.*;
|
||||
|
||||
public class DrawingTrain {
|
||||
public IMoveableObject GetMoveableObject() { return new DrawningObjectTrain(this);}
|
||||
|
||||
protected IWheelDrawing wheelDrawing;
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
|
90
laba1Loco/SetGeneric.java
Normal file
90
laba1Loco/SetGeneric.java
Normal file
@ -0,0 +1,90 @@
|
||||
package laba1Loco;
|
||||
|
||||
public class SetGeneric<T extends Object> {
|
||||
/// <summary>
|
||||
/// Массив объектов, которые храним
|
||||
/// </summary>
|
||||
private Object[] _places;
|
||||
/// <summary>
|
||||
/// Количество объектов в массиве
|
||||
/// </summary>
|
||||
public int Count;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = new Object[count];
|
||||
Count = _places.length;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор
|
||||
/// </summary>
|
||||
/// <param name="train">Добавляемый поезд</param>
|
||||
/// <returns></returns>
|
||||
public int Insert(T train)
|
||||
{
|
||||
int i = 0;
|
||||
for (;i < _places.length; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
break;
|
||||
}
|
||||
if (i == _places.length)
|
||||
return -1;
|
||||
for (; i > 0; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
_places[i] = train;
|
||||
return i;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
/// </summary>
|
||||
/// <param name="train">Добавляемый поезд</param>
|
||||
/// <param name="position">Позиция</param>
|
||||
/// <returns></returns>
|
||||
public boolean Insert(T train, int position)
|
||||
{
|
||||
if (position < 0 || position >= _places.length)
|
||||
return false;
|
||||
for (; position < _places.length; position++)
|
||||
{
|
||||
if (_places[position] == null)
|
||||
break;
|
||||
}
|
||||
if (position == _places.length)
|
||||
return false;
|
||||
for (; position > 0; position--)
|
||||
{
|
||||
_places[position] = _places[position - 1];
|
||||
}
|
||||
_places[position] = train;
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление объекта из набора с конкретной позиции
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public boolean Remove(int position)
|
||||
{
|
||||
if (position < 0 || position >= _places.length)
|
||||
return false;
|
||||
_places[position] = null;
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение объекта из набора по позиции
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <returns></returns>
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position < 0 || position >= _places.length)
|
||||
return null;
|
||||
return (T)_places[position];
|
||||
}
|
||||
}
|
128
laba1Loco/TrainsGenericCollection.java
Normal file
128
laba1Loco/TrainsGenericCollection.java
Normal file
@ -0,0 +1,128 @@
|
||||
package laba1Loco;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class TrainsGenericCollection<T extends DrawingTrain, U extends IMoveableObject>{
|
||||
/// <summary>
|
||||
/// Ширина окна прорисовки
|
||||
/// </summary>
|
||||
private int _pictureWidth;
|
||||
/// <summary>
|
||||
/// Высота окна прорисовки
|
||||
/// </summary>
|
||||
private int _pictureHeight;
|
||||
/// <summary>
|
||||
/// Размер занимаемого объектом места (ширина)
|
||||
/// </summary>
|
||||
private int _placeSizeWidth = 210;
|
||||
/// <summary>
|
||||
/// Размер занимаемого объектом места (высота)
|
||||
/// </summary>
|
||||
private int _placeSizeHeight = 90;
|
||||
/// <summary>
|
||||
/// Набор объектов
|
||||
/// </summary>
|
||||
private SetGeneric<T> _collection;
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="picWidth"></param>
|
||||
/// <param name="picHeight"></param>
|
||||
public TrainsGenericCollection(int picWidth, int picHeight)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
int height = picHeight / _placeSizeHeight;
|
||||
_pictureWidth = picWidth;
|
||||
_pictureHeight = picHeight;
|
||||
_collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
/// <summary>
|
||||
/// Перегрузка оператора сложения
|
||||
/// </summary>
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public void Add(T obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
_collection.Insert(obj);
|
||||
}
|
||||
/// <summary>
|
||||
/// Перегрузка оператора вычитания
|
||||
/// </summary>
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public T remove(int pos)
|
||||
{
|
||||
T obj = _collection.Get(pos);
|
||||
if (obj != null)
|
||||
{
|
||||
_collection.Remove(pos);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение объекта IMoveableObject
|
||||
/// </summary>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public U GetU(int pos)
|
||||
{
|
||||
return (U)_collection.Get(pos).GetMoveableObject();
|
||||
}
|
||||
/// <summary>
|
||||
/// Вывод всего набора объектов
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public BufferedImage ShowTrains()
|
||||
{
|
||||
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g = bmp.createGraphics();
|
||||
DrawBackground(g);
|
||||
DrawObjects(g);
|
||||
g.dispose();
|
||||
return bmp;
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод отрисовки фона
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
private void DrawBackground(Graphics2D g)
|
||||
{
|
||||
g.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);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод прорисовки объектов
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
private void DrawObjects(Graphics2D g)
|
||||
{
|
||||
for (int i = 0; i < _collection.Count; i++)
|
||||
{
|
||||
T t = _collection.Get(i);
|
||||
if (t != null)
|
||||
{
|
||||
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
|
||||
if (t instanceof DrawingLoco)
|
||||
((DrawingLoco) t).DrawTransport(g);
|
||||
else
|
||||
t.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user