aspose file tools*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Word Wrapping in Canvas !!!" Watch "Word Wrapping in Canvas !!!" New topic
Author

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
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
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 ...
Regards,
Manfred.
Greg Harris
Ranch Hand

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


what?
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 ???
Rgds,
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
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.
 
Consider Paul's rocket mass heater.
 
subject: Word Wrapping in Canvas !!!