views:

222

answers:

1

Is there an way to estimate the number of pixels used by a proportional font? I am writing software that creates an image in SVG and transform that to PNG in Java. In this image I am using a text with a proportional font (size 16). I can sometimes fit 26 characters in the picture and sometimes only 19. This is because 'WWW' takes much more space as 'li1'. How can I estimate the number of pixels a String needs?

+2  A: 

Have a look at the FontMetrics class - the stringWidth() method in particular.

If you have a graphics object g you are using for painting

g.getFontMetrics(f).stringWidth(message)
Nick Fortescue
Thanks. This worked well.
Edwin