aspose file tools *
The moose likes Swing / AWT / SWT and the fly likes Overlapping Strings in DrawingPanel (JFrame) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Overlapping Strings in DrawingPanel (JFrame)" Watch "Overlapping Strings in DrawingPanel (JFrame)" New topic
Author

Overlapping Strings in DrawingPanel (JFrame)

Java Smith
Greenhorn

Joined: May 31, 2012
Posts: 1
For my APCS Project, we are trying to generate a wordcloud (google it if you don't know what these are. They're pretty cool.). The challange is, however, to display the words close together, without overlap. no matter what the length of the string is. Right now, we have basically simple hard-code, that is, panel.drawString(words.get(0) * 30 - words.get (0) * 12 + 20 - 122, ....); (given parameter ArrayList<String> words. Can anyone give us an algorithm to print out the words without overlap on a panel?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you'll use FontMetrics (google it if you don't know what these are. They're pretty cool.)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

TextLayout and LineBreakMeasurer can give you a closer fit. But be aware of a 1-pixel bug, don't know whether it's been fixed yet.
https://forums.oracle.com/forums/thread.jspa?threadID=1263979

Why don't you just use a JTextPane/JEditorPane?


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: Overlapping Strings in DrawingPanel (JFrame)