Created
March 22, 2019 14:24
-
-
Save ssaurel/9de6961e42721c3e0f37a51b845043e9 to your computer and use it in GitHub Desktop.
Paint Component method 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 drawStartMessage(Graphics2D g) { | |
if (gameOver) { | |
g.setFont(getFont().deriveFont(Font.BOLD, 18)); | |
g.setColor(FOREGROUND_COLOR); | |
String s = "Click to start new game"; | |
g.drawString(s, (getWidth() - g.getFontMetrics().stringWidth(s)) / 2, | |
getHeight() - margin); | |
} | |
} | |
private void drawCenteredString(Graphics2D g, String s, int x, int y) { | |
// center string s for the given tile (x,y) | |
FontMetrics fm = g.getFontMetrics(); | |
int asc = fm.getAscent(); | |
int desc = fm.getDescent(); | |
g.drawString(s, x + (tileSize - fm.stringWidth(s)) / 2, | |
y + (asc + (tileSize - (asc + desc)) / 2)); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
Graphics2D g2D = (Graphics2D) g; | |
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
drawGrid(g2D); | |
drawStartMessage(g2D); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment