File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Overlapping Strings in DrawingPanel (JFrame)" Watch "Overlapping Strings in DrawingPanel (JFrame)" New topic

Overlapping Strings in DrawingPanel (JFrame)

Java Smith

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

Joined: May 03, 2008
Posts: 5098

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.

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:
subject: Overlapping Strings in DrawingPanel (JFrame)
It's not a secret anymore!