PIbd-21_KozyrevSS_GasolineT.../CarsGenericCollection.java

68 lines
2.2 KiB
Java
Raw Permalink Normal View History

import java.awt.*;
import java.awt.image.BufferedImage;
public class CarsGenericCollection <T extends DrawTanker, U extends IMoveableObject> {
private final int _pictureWidth;
private final int _pictureHeight;
private final int _placeSizeWidth = 110;
private final int _placeSizeHeight = 80;
private final SetGeneric<T> _collection;
public CarsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public int plus(T gasTanker)
{
return _collection.Insert(gasTanker);
}
public boolean minus(int position)
{
return _collection.Remove(position);
}
public U GetU(int pos)
{
return (U)_collection.Get(pos).GetMoveableObject();
}
public Image ShowCars()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics g = bmp.getGraphics();
DrawBackground(g);
DrawObjects(g);
return bmp;
}
private void DrawBackground(Graphics 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);
}
}
}
private void DrawObjects(Graphics g) {
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count(); i++) {
DrawTanker tank = _collection.Get(i);
if (tank == null)
continue;
tank.SetPosition(i % width * _placeSizeWidth, (height - i / height - 1) * _placeSizeHeight);
tank.DrawTransport(g);
}
}
}