I have a Font and a FontMetrics object in my code and I'm drawing a Font onto a Graphics object but I'm not for sure how to find out the dimensions of the font when it's drawn (in pixels). Does anyone know how I could calculate that?
views:
40answers:
2
+1
A:
The FontMetrics object you have should be all you need. getHeight() will give you the height of the text for the font size used to create the FontMetrics object, and stringWidth(String) will tell you the width of any text that you're trying to render.
Note that you probably want to include getAscent() and getDescent() with the height to get a full picture of what the height of the text will be.
Spyder
2010-01-25 01:02:38
A:
An easy way to access FontMetrics is to get it from the graphics context after invoking setFont()
, as seen in this simple example.
trashgod
2010-01-25 03:03:31