Skip to main content

Finding out Rect size of Text

1 reply [Last post]
jammerie
Offline
Joined: 2005-11-14
Points: 0

Hi,

I'm using Graphics.drawString(String str,int x,int y) to draw my text on the Panel. Is there any way to find out how much space the text occupies. I need this info because I want to draw a box around the text.

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
loubs001
Offline
Joined: 2006-01-05
Points: 0

There's a bunch of ways. The simplest is
FontMetrics fm = g.getFontMetrics();
Rectangle2D bounds = fm.getStringBounds(text, g);