aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 9990

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: 4522

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:
subject: How do I draw "Best-Fit" text inside of a rectangle?
Similar Threads
Swing related
creating a String Shape of specified size
Text surrounded by a rectangle
How to create a new image file?
Is it possilbe to create Gantt chart without any tool