This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Word Wrapping in Canvas !!!

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I have a canvas in a chat applet where all the messages are being drawn/displayed send by different users. Now, the problem is - the width of a canvas is fixed. Now, the problem is when the user types a message the length of which is more than the canvas width, the message gets cuts when the canvas width exceeds. I want the canvas when the width of message exceeds, the rest of the message should be displayed on the next line. Also, I want the word wrapping feature ?? What should I do ?
I hope there is of course someone to help me !!
Regards and thanx in advance.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
You will need to use the FontMetrics class to determine the length of strings, using stringWidth(). For each message you will need to split it into sections that are smaller than the width of the canvas and display each one on a new line.
hope this helps
dave
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi, I tried using the fond metrics class and found out that it doesn't always return the right metrics! which is to say that sometimes the font doesn't display the way the metrics class claims it will. what to do?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
For fixed sized fonts (i.e., Courier) the font metrics return the correct character sizes. But for proportional fonts (i.e., Helvetica) the font metrics return average character sizes. Since each character is a different width it is impossible to give you an exact answer.
My advice, use fixed sized fonts if your application depends on word wrapping or exact string widths ...
Regards,
Manfred.
 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam,
i see that this post was resurrected from the dead... i hope you see my reply. i just wrote a bit of code that takes input from the user and displays it in an applet window... if the line is too long, the function searches for the closest " " from the end of the line, cuts the string and then displays the input on 2 lines... my code is pretty rough, but it works. i hacked this out in about an hour, so just use it as a reference.

this code just splits the string one ( 1 ) time, so you will need to modify it if multiple lines are possible. mine does not work perfectly yet, but check here to see what it does... Click here...
feel free to email me with any suggestions!
 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Greg, The stuff you mentioned has already been done.
thanks for your inputs.
Dear Manfred Leonhardt,
Whats the way if I'm using Arial as a font, how do I get exact string width ???
Rgds,
 
Greg Harris
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam,
i am using arial in the example above. the fontMetrics( font ) function returns the width in pixles of the current font no matter what the font is.
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!