Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overlapping Strings in DrawingPanel (JFrame)

 
Java Smith
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you'll use FontMetrics (google it if you don't know what these are. They're pretty cool.)
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic