PIbd21.LyovushkinaA.A.Conta.../ShipGenericCollection.java

89 lines
2.5 KiB
Java
Raw Permalink Normal View History

2023-10-24 01:06:14 +04:00
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)
{
2023-11-04 20:38:51 +04:00
int i = 0;
2023-11-04 19:41:05 +04:00
for (T ship: _collection.GetShips(100))
2023-10-24 01:06:14 +04:00
{
2023-11-04 20:38:51 +04:00
2023-11-04 19:41:05 +04:00
if (ship != null)
2023-10-24 01:06:14 +04:00
{
2023-11-04 19:41:05 +04:00
ship.SetPosition(((_collection._maxCount -i - 1) % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (_collection._maxCount - i-1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
ship.DrawShip(gr);
2023-10-24 01:06:14 +04:00
}
2023-11-04 19:41:05 +04:00
i++;
2023-10-24 01:06:14 +04:00
}
}
}