File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes Word Wrapping in Canvas !!! 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 » Applets
Bookmark "Word Wrapping in Canvas !!!" Watch "Word Wrapping in Canvas !!!" New topic

Word Wrapping in Canvas !!!

Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
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.

Best wishes,
Dave Turner
Ranch Hand

Joined: Mar 13, 2001
Posts: 60
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
Paul Bailey
Ranch Hand

Joined: Oct 20, 2000
Posts: 91

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?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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 ...
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
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

Joined: Jan 03, 2001
Posts: 147
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 ???
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
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.
I agree. Here's the link:
subject: Word Wrapping in Canvas !!!
Similar Threads
Screen not getting refreshed
Breaking a word in a TD
Word wrapping in Canvas !!!!
SWT Text word wrap with GridLayout and horizontal grab