Right now, I'm working on making a version of Tetris. To test out my Graphics class, I'm first trying to draw 200 gray squares (10x20) before I worry about drawing the piece. I have two 2D arrays called myRectangles (which has coordinates, height, width), and myColors, which stores the color of each square. Right now it's just all Color.DARK_GRAY.
In my constructor, I set up the arrays like this:
for(int i = 0; i < width; i++)
{
for(int j = 0; j < height; j++)
{
myColors[i][j] = Color.DARK_GRAY;
myRectangles[i][j] = new Rectangle2D.Double(INITIAL_X + (i * SQUARE_SIDE),
INITIAL_Y + (j * SQUARE_SIDE),
SQUARE_SIDE, SQUARE_SIDE);
}
}
Then, I have a separate drawBoard method, because I'll want to be able to call this multiple times when I actually start drawing pieces on the board. That method looks like this (note that I have some test code in the method):
public void drawBoard()
{
Graphics g = myGame.getGraphics();
Graphics2D g2 = (Graphics2D) g;
int counter = 0;
for(int i = 0; i < TetrisBoard.MAX_WIDTH; i++)
{
for(int j = 0; j < TetrisBoard.MAX_HEIGHT; j++)
{
g2.setPaint(myColors[i][j]);
g2.fill(myRectangles[i][j]);
System.out.println(counter);
System.out.println("Drawing rectangle at " + myRectangles[i][j].getX() + " " + myRectangles[i][j].getY());
System.out.println("Color " + myColors[i][j]);
System.out.println();
counter++;
}
}
System.out.println(counter);
myGame.setVisible(true);
myWindow.setVisible(true);
}
Typically, when I run the code, it misses a few squares in the first column. When I use the debugger, it appears correctly.