Question

I want it to look symmetrical, but I can't work out why this doesn't work.

Font scoreFont = new Font("Impact", Font.PLAIN, 72);
    g.setFont(scoreFont);
    FontMetrics scoreFontMetrics = g.getFontMetrics();
    g.drawString("" + playerOneScore, SCREEN_WIDTH / 2
            - 30 - scoreFontMetrics.charWidth(playerOneScore),
            SCREEN_HEIGHT / 2);
    g.drawString("" + playerTwoScore,
            SCREEN_WIDTH / 2 + 30,
            SCREEN_HEIGHT / 2);

}

Thanks in advance.

Me

Was it helpful?

Solution

The charWidth in FontMetrics gets a char as a parameter. If playerOneScore is an int than it is actually transformed to its char value and then that is applied to the method. You would be better using stringWidth(playerOneScore + "");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top