开发者

How can I find the dimensions of a font drawn on a Graphics object in Java?

开发者 https://www.devze.com 2022-12-18 04:20 出处:网络
I have a Font an开发者_运维技巧d 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 pi

I have a Font an开发者_运维技巧d 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?


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.


An easy way to access FontMetrics is to get it from the graphics context after invoking setFont(), as seen in this simple example.

0

精彩评论

暂无评论...
验证码 换一张
取 消