84 lines
2.5 KiB
Java
84 lines
2.5 KiB
Java
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
|
|
public class CatamaranGenericCollection<T extends DrawningCatamaran, U extends IMoveableObject> {
|
|
private final int _pictureWidth;
|
|
|
|
private final int _pictureHeight;
|
|
|
|
private final int _placeSizeWidth = 140;
|
|
|
|
private final int _placeSizeHeight = 90;
|
|
|
|
private final SetGeneric<T> _collection;
|
|
|
|
public CatamaranGenericCollection(int picWidth, int picHeight){
|
|
int width = picWidth / _placeSizeWidth;
|
|
int height = picHeight / _placeSizeHeight;
|
|
_pictureWidth = picWidth;
|
|
_pictureHeight = picHeight;
|
|
_collection = new SetGeneric<T>(width * height);
|
|
}
|
|
|
|
public int Insert(T obj){
|
|
if (obj == null)
|
|
{
|
|
return -1;
|
|
}
|
|
return _collection.Insert(obj);
|
|
}
|
|
|
|
public boolean Remove(int position){
|
|
return _collection.Remove(position);
|
|
}
|
|
|
|
public U GetU(int pos){
|
|
T ans = _collection.Get(pos);
|
|
if(ans == null)
|
|
return null;
|
|
return (U)ans.GetMoveableObject();
|
|
}
|
|
|
|
private void DrawBackground(Graphics g)
|
|
{
|
|
g.setColor(Color.BLACK);
|
|
((Graphics2D) g).setStroke(new BasicStroke(2));
|
|
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);
|
|
}
|
|
((Graphics2D) g).setStroke(new BasicStroke(1));
|
|
}
|
|
|
|
private void DrawObjects(Graphics g)
|
|
{
|
|
for (int i = 0; i < _collection.Count; i++)
|
|
{
|
|
DrawningCatamaran catamaran = _collection.Get(i);
|
|
if (catamaran != null)
|
|
{
|
|
int inRow = _pictureWidth / _placeSizeWidth;
|
|
catamaran.SetPosition(i % inRow * _placeSizeWidth, ((_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight) + 5 );
|
|
catamaran.DrawnCatamaran((Graphics2D) g);
|
|
}
|
|
}
|
|
}
|
|
|
|
public BufferedImage ShowCatamaran()
|
|
{
|
|
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_4BYTE_ABGR);
|
|
Graphics gr = bmp.createGraphics();
|
|
DrawBackground(gr);
|
|
DrawObjects(gr);
|
|
return bmp;
|
|
}
|
|
}
|