I'm trying to draw strings character by character to add lighting effects to shapes composed of text.
while (i != line.length()) {
c = line.substring(i, i + 1);
cWidth = g.getFontMetrics().stringWidth(c);
g.drawString(c, xx += cWidth, yy);
i++;
}
The problem is, the width of a character isn't the actual distance it's drawn from another character when those two characters are printed as a string. Is there any way to get the correct distance in graphics2d?
The answer by Lukas Baran solves the main problem that was probably causing your output to look bad. However, the more subtle problem that you can't replicate kerning in this way remains. How much of a problem this is may depend on the font you're using. To get the kerning right, too, you could do something like this:
That should place each character where the kerning would have placed it.
The problem is that kerning defines the spacing of pairs of letters, and iterating over a string char-by-char gives the kerning system no chance to kick in. I think you'll have to use either a fixed-width font or rework your lighting effect so it works with full strings instead of single chars.
I'm not sure if I understood your problem correctly. However, I have tested your code and indeed some string characters overlapped each other.
The problem was in a way you're incrementing
xx
value (you were incrementing it before drawing a character).Here's corrected version of your code:
Uncomment and adjust
xx += 2
line to increase space between characters.