Для нашего проекта Tetris у нас есть сетка 10x20, вложенная в JPanel 400x800.Создается класс Tetromino с private int[] coordinate
, который устанавливает координаты при порождении фигуры.
public void spawnTetromino() {
...
int[] spawnCoordinate = new int[] {Y_VALUE, 5};
fallingTetromino.setCoordinate(spawnCoordinate);
....
if (!gameOver) {
projectTetromino(spawnCoordinate)
projectTetromino()
выделено здесь:
public void projectTetromino(int[] coordinate) {
// projects the shape of the tetromino onto the board based on its coordinate
// only projects the non-0 (the filled) indices of the shape
int[][] shape = fallingTetromino.getShape();
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
if (shape[y][x] != 0) {
grid[coordinate[0]+y][coordinate[1]+x] = shape[y][x];
}
}
}
}
Была создана тестовая сетка, котораябудет выводить что-то вроде этого:
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 1 1 1 0 0 0|
|0 0 0 0 0 1 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
|0 0 0 0 0 0 0 0 0 0|
Моя задача - выяснить, как внедрить графику в эту сетку.Это код, который у меня есть на данный момент
public void paintComponent(Graphics g) {
super.paintComponent(g);
tile = new Rectangle(tileX,tileY,tileWidth, tileHeight);
int[] tileCoordinate = {tileX, tileY};
Graphics2D g2 = (Graphics2D) g;
g2.setBackground(Color.BLACK);
//filling out the tiles
for (int x = 0; x <= 10; x++) {
tile.y = 0;
for (int y = 0; y <=20; y++) {
//check if the tetronimo shape is 0
tile.y = tile.y + 40;
int[] shape = fallingTetromino.getCoordinate();
//somehow check if tetromino is inside one of the tile????
g2.fill(tile);
}
tile.x = tile.x + 40;
}
}
Моя конечная цель состоит в том, чтобы перебрать эту графическую сетку и заполнить плитки, которые занимают ТОЛЬКО Tetromino (то есть плитки, отличные от 0).Как бы я подошел к этому?