Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Last active March 22, 2019 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/9a5d6bd5ced9ee1d97c1dfa120f1e95b to your computer and use it in GitHub Desktop.
Save ssaurel/9a5d6bd5ced9ee1d97c1dfa120f1e95b to your computer and use it in GitHub Desktop.
Draw Grid for the Game of Fifteen tutorial on the SSaurel's Channel
private void drawGrid(Graphics2D g) {
for (int i = 0; i < tiles.length; i++) {
// we convert 1D coords to 2D coords given the size of the 2D Array
int r = i / size;
int c = i % size;
// we convert in coords on the UI
int x = margin + c * tileSize;
int y = margin + r * tileSize;
// check special case for blank tile
if(tiles[i] == 0) {
if (gameOver) {
g.setColor(FOREGROUND_COLOR);
drawCenteredString(g, "\u2713", x, y);
}
continue;
}
// for other tiles
g.setColor(getForeground());
g.fillRoundRect(x, y, tileSize, tileSize, 25, 25);
g.setColor(Color.BLACK);
g.drawRoundRect(x, y, tileSize, tileSize, 25, 25);
g.setColor(Color.WHITE);
drawCenteredString(g, String.valueOf(tiles[i]), x , y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment