wood burning stoves
The moose likes Swing / AWT / SWT and the fly likes How do I draw Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I draw "Best-Fit" text inside of a rectangle?" Watch "How do I draw "Best-Fit" text inside of a rectangle?" New topic

How do I draw "Best-Fit" text inside of a rectangle?

Austin Henggi

Joined: Jul 08, 2005
Posts: 10
I simply want to draw text inside of a rectangle. Easy. Harder: I would like the text to be as readable (translate: big) as possible with the constraint that it must be contained in the rectangle. I know about getAscent() and getDescent() and how to iterate through font types and sizes to find something that fits. But is there a more efficient / existing code for finding best font size to render a text string in a rectangle so that it is as readable as possible?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10834

Check out FontMetrics#stringWidth(String str)

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Or if you need greater accuracy on the bounding rectangle, look into TextLayout (and LineBreakMeasurer?)

No matter how you approach this, obtaining the font size for the "best fit" will still be an iterative process.

luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link: http://aspose.com/file-tools
subject: How do I draw "Best-Fit" text inside of a rectangle?
It's not a secret anymore!