Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Tetris - Set new color for each piece
    text
    copied!<p>I'm trying to set a new random color for each new piece (composed of 4 tiles). To draw the full piece to the board, I have a paint component in the Board class:</p> <pre><code>public void paintComponent(Graphics g) { g.setColor(Color.black); g.fillRect(0, 0, getWidth(), getHeight()); for(int row = 0; row &lt; grid.length; row++) { for(int col = 0; col &lt; grid[row].length; col++) { if(grid[row][col] != null) { //if there is a non-null space, that is a Tetris piece.. fill it red g.setColor(color); g.fillRect(row * tilesize, col * tilesize, tilesize, tilesize); g.setColor(Color.WHITE); g.drawString("(" + row + ", " + col + ")", row * tilesize, col * tilesize+10); } } } } </code></pre> <p>You can see that <code>g.setColor()</code> is given a global variable <code>color</code></p> <p>As defined in the Board constructor:</p> <p><code>color = setColor();</code></p> <p><strong>setColor():</strong></p> <pre><code>public Color setColor() { Random rand = new Random(); float r = rand.nextFloat(); float g = rand.nextFloat(); float b = rand.nextFloat(); Color randomColor = new Color(r, g, b); return randomColor; } </code></pre> <p>And when there is a collision, a new piece is generated, which overwrites the <code>color</code> global variable with a new random color...</p> <pre><code>public void collisionCheck() { if (newPiece.isCollision()){ newPiece = new Piece(this, randomPiece()); color = setColor(); } } </code></pre> <p>This gives me this result:</p> <p>All shapes are set to the same color... not what I want <img src="https://i.stack.imgur.com/NIrgc.jpg" alt="enter image description here"></p> <p>Then if a new piece is generated, the color for all of them changes... again, not what I want. <img src="https://i.stack.imgur.com/BmUdO.jpg" alt="enter image description here"></p> <p>I know <em>what</em> the issue is... it's that I shouldn't be overwriting the global color variable... but if I don't assign a color from the board class... but instead get the color from the tile class, as such:</p> <p><code>g.setColor(grid[row][col].getColor());</code></p> <pre><code>public void paintComponent(Graphics g) { g.setColor(Color.black); g.fillRect(0, 0, getWidth(), getHeight()); for(int row = 0; row &lt; grid.length; row++) { for(int col = 0; col &lt; grid[row].length; col++) { if(grid[row][col] != null) { //if there is a non-null space, that is a Tetris piece.. fill it red g.setColor(grid[row][col].getColor()); g.fillRect(row * tilesize, col * tilesize, tilesize, tilesize); g.setColor(Color.WHITE); g.drawString("(" + row + ", " + col + ")", row * tilesize, col * tilesize+10); } } } } </code></pre> <p>Then each individual tile will have a new color generated each time the tiles are repainted...</p> <p><img src="https://i.stack.imgur.com/1b5cf.jpg" alt="enter image description here"> </p> <p>My goal is to give a single piece (composed of 4 tiles) a random color... then when a new piece is generated, that first piece retains its color... and the new piece retains its color...</p> <p>Any thoughts?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload