For our Tetris project we have a 10x20 grid nested inside a 400x800 JPanel. A Tetromino class is created that has a private int[] coordinate
that sets coordinates when a piece is spawned
public void spawnTetromino() {
...
int[] spawnCoordinate = new int[] {Y_VALUE, 5};
fallingTetromino.setCoordinate(spawnCoordinate);
....
if (!gameOver) {
projectTetromino(spawnCoordinate)
projectTetromino()
is highlighted here:
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];
}
}
}
}
A test grid was created that would output something like this:
|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|
My job is to figure out how to implement graphics into this grid. This is the code I currently have
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;
}
}
My end goal is to loop through this graphical grid and fill out tiles that ONLY have a Tetromino occupy it (i.e. non-0 tiles). How would I approach this?