-1

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?

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Nathan Natindim
  • 147
  • 2
  • 5
  • 1
    if more code is necessary, I would be happy to provide it – Nathan Natindim Apr 25 '19 at 18:19
  • 2
    We don't need more code, we need a [mcve], please read the link, if you have questions after that, let me know and I'll be glad to explain whatever you don't understand – Frakcool Apr 25 '19 at 18:26

1 Answers1

0

If you want to implement this into graphics, you will need a general game loop in your main game method. Because you try to move objects they have to be reinitialized(or update their position).

My guess is to install Java Editor and have a look at several objects available. This programm will provide you with a graphic interface and live code updating, which you just need to copy in your normal IDE. My suggestion would be to draw polygons or use a small object (1 pixel x 1 pixel) and then color it.

Java Editor: Download here