PIbd-21_Kryukov_A_I_Excavat.../src/TracktorsGenericCollection.java
Алексей Крюков 7b0ffff8ac лаба 3
2023-12-18 20:31:50 +04:00

57 lines
2.2 KiB
Java

import java.awt.*;
import java.awt.image.BufferedImage;
public class TracktorsGenericCollection<T extends DrawningTracktor, U extends IMoveableObject>{
private int _pictureWidth;
private int _pictureHeight;
private final int _placeSizeWidth = 160;
private final int _placeSizeHeight = 140;
private SetGeneric<T> _collection;
public TracktorsGenericCollection(int pictureWidth, int pictureHeight){
int width = pictureWidth / _placeSizeWidth;
int height = pictureHeight / _placeSizeHeight;
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
_collection = new SetGeneric<T>(width*height);
}
public int Add(T obj){
if(obj == null)
return -1;
return _collection.Insert(obj);
}
public boolean remove(int pos){
T obj = _collection.Get(pos);
if(obj != null)
_collection.Remove(pos);
return false;
}
public U GetU(int pos){
return (U) _collection.Get(pos).GetMoveableObject();
}
public BufferedImage ShowTracktors(){
BufferedImage bitmap = new BufferedImage(_pictureWidth,_pictureHeight,BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = bitmap.createGraphics();
DrawBackground(g);
DrawObjects(g);
g.dispose();
return bitmap;
}
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){
for(int i = 0; i < _collection.Count();i++){
T obj = _collection.Get(i);
if(obj != null){
obj.SetPosition(i %(_pictureWidth/ _placeSizeWidth)*_placeSizeWidth,i/(_pictureWidth/_placeSizeWidth) * _placeSizeHeight);
obj.DrawTransport((Graphics2D) g);
}
}
}
}