PIbd21.LyovushkinaA.A.Conta.../ShipGenericCollection.java
2023-11-04 20:38:51 +04:00

89 lines
2.5 KiB
Java

import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Graphics2D;
public class ShipGenericCollection<T extends DrawingShip, U extends IMoveableObject>
{
private int _pictureWidth;
private int _pictureHeight;
private int _placeSizeWidth = 210;
private int _placeSizeHeight = 90;
private SetGeneric<T> _collection;
public ShipGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public int Add(T obj)
{
if (obj == null)
{
return -1;
}
return _collection.Insert(obj);
}
public T remove(int pos)
{
T obj = _collection.Get(pos);
if (obj != null)
{
_collection.Remove(pos);
}
return obj;
}
public U GetU(int pos)
{
return (U)_collection.Get(pos).GetMoveableObject();
}
public BufferedImage ShowShips()
{
BufferedImage bmp = new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = bmp.createGraphics();
DrawBackground(gr);
DrawObjects(gr);
gr.dispose();
return bmp;
}
private void DrawBackground(Graphics2D gr)
{
gr.setColor(Color.BLACK);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия рамзетки места
gr.drawLine(i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
gr.drawLine(i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
private void DrawObjects(Graphics2D gr)
{
int i = 0;
for (T ship: _collection.GetShips(100))
{
if (ship != null)
{
ship.SetPosition(((_collection._maxCount -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection._maxCount - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
ship.DrawShip(gr);
}
i++;
}
}
}