How do you draw a string centered vertically in Java?

Posted by Paul Alexander on Stack Overflow See other posts from Stack Overflow or by Paul Alexander
Published on 2009-06-28T21:42:18Z Indexed on 2010/05/16 10:20 UTC
Read the original article Hit count: 248

Filed under:
|

I know it's a simple concept but I'm struggling with the font metrics. Centering horizontally isn't too hard but vertically seems a bit difficult.

I've tried using the FontMetrics getAscent, getLeading, getXXXX methods in various combinations but no matter what I've tried the text is always off by a few pixels. Is there a way to measure the exact height of the text so that it is exactly centered.

© Stack Overflow or respective owner

Related posts about java

Related posts about graphics