OK so take a look at this vid (it’s 3 seconds long..) showing the problem:
See how the images flicker when you first enter the room?
Here’s how I have the images set up (they are pulled from tiles folder in the .jar file)
for (int i = 0; i < 522; i++) {
tiles[i] = tk.getImage(this.getClass().getResource(String.format("tiles/t%d.png", i)));
}
and here’s how they are painting on:
for (row = 0; row < board.length; row++) {
for (col = 0; col < board[row].length; col++) {
int index = board[row][col];
g.drawImage(tiles[index], 32 * col, 32 * row, this);
}
}
How do I stop the flicker…
The
Toolkit.getImage(URL)method is asynchronous and 522 images might take a ‘few moments’ to load. Either add them to aMediaTrackeror useImageIO.read(URL)instead.