Last active
March 22, 2019 14:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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