86 lines
3.4 KiB
Java
86 lines
3.4 KiB
Java
|
package Trolleybus;
|
|||
|
|
|||
|
import java.awt.*;
|
|||
|
import javax.swing.*;
|
|||
|
// Параметризованный класс для набора объектов DrawingBus
|
|||
|
public class BusesGenericCollection <T extends DrawingBus, U extends IMoveableObject> {
|
|||
|
private final int _pictureWidth;
|
|||
|
private final int _pictureHeight;
|
|||
|
private final int _placeSizeWidth = 150;
|
|||
|
private final int _placeSizeHeight = 95;
|
|||
|
private final SetGeneric<T> _collection;
|
|||
|
public BusesGenericCollection(int picWidth, int picHeight)
|
|||
|
{
|
|||
|
int width = picWidth / _placeSizeWidth; //width - кол-во помещаемых на PictureBox автобусов по горизонтали
|
|||
|
int height = picHeight / _placeSizeHeight; //height - кол-во помещаемых на PictureBox автобусов по вертикали
|
|||
|
_pictureWidth = picWidth;
|
|||
|
_pictureHeight = picHeight;
|
|||
|
_collection = new SetGeneric<T>(width * height); //width*height - кол-во мест на PictureBox для автобусов; размер массива
|
|||
|
}
|
|||
|
//На Java нельзя перегрузить операторы + и -, поэтому ниже обычные методы
|
|||
|
public int Add(T obj){
|
|||
|
if (obj == null) {
|
|||
|
return -1;
|
|||
|
}
|
|||
|
return _collection.Insert(obj);
|
|||
|
}
|
|||
|
public boolean Remove(int position) {
|
|||
|
T obj = _collection.Get(position);
|
|||
|
if (obj != null) {
|
|||
|
_collection.Remove(position);
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
public U GetU(int pos){
|
|||
|
return (U)_collection.Get(pos).GetMoveableObject();
|
|||
|
}
|
|||
|
//Вывод всех объектов
|
|||
|
public void ShowBuses(JPanel panelToDraw) {
|
|||
|
Graphics gr = panelToDraw.getGraphics();
|
|||
|
//Очистка перед перерисовкой
|
|||
|
panelToDraw.paint(gr);
|
|||
|
DrawBackground(gr);
|
|||
|
DrawObjects(gr);
|
|||
|
}
|
|||
|
//Прорисовка фона (чёрных линий)
|
|||
|
private void DrawBackground(Graphics g) {
|
|||
|
Graphics2D g2d = (Graphics2D)g;
|
|||
|
g2d.setColor(Color.BLACK);
|
|||
|
//вертикальные линии
|
|||
|
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
|
|||
|
{
|
|||
|
g2d.drawLine(i * (_placeSizeWidth + 10), 0, i * (_placeSizeWidth + 10), (_pictureHeight / _placeSizeHeight) * (_placeSizeHeight + 10));
|
|||
|
}
|
|||
|
//горизонтальные линии
|
|||
|
for (int i = 0; i <= _pictureHeight / _placeSizeHeight; i++)
|
|||
|
{
|
|||
|
for (int j = 0; j < _pictureWidth / _placeSizeWidth; j++)
|
|||
|
{
|
|||
|
g2d.drawLine(j * (_placeSizeWidth + 10), i * (_placeSizeHeight + 10), j * (_placeSizeWidth + 10) + _placeSizeWidth / 2, i * (_placeSizeHeight + 10));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
//Прорисовка объектов
|
|||
|
private void DrawObjects(Graphics g) {
|
|||
|
int i = 0;
|
|||
|
int j = _pictureWidth / _placeSizeWidth - 1;
|
|||
|
for (int k = 0; k < _collection.Count; k++)
|
|||
|
{
|
|||
|
DrawingBus bus = _collection.Get(k);
|
|||
|
if (bus != null)
|
|||
|
{
|
|||
|
bus.SetPosition(j * (_placeSizeWidth + 10) + 5, i * (_placeSizeHeight) + 10);
|
|||
|
bus.DrawTransport(g);
|
|||
|
}
|
|||
|
j--;
|
|||
|
//переход на новую строчку
|
|||
|
if (j < 0)
|
|||
|
{
|
|||
|
j = _pictureWidth / _placeSizeWidth - 1;
|
|||
|
i++;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|