• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Word wrapping in Canvas !!!!

 
Sam Cala
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.
 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Figure out how many characters will fit on the width of your canvas (using FontMetrics would be easiest), then print that many characters on each line.
 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm sorry as I really dont know how to use FontMetrics class. Can u give me some guidance ???
Thanx in advance.
Regards,
 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link to the FontMetrics docs:
http://java.sun.com/j2se/1.3/docs/api/java/awt/FontMetrics.html
You can get a FontMetrics object by calling the method getFontMetrics(Font font) on your canvas. There are functions in the class that return values for the height and width of characters or strings for that font.
For example, to find the width of a string in Arial 12pt plain text:

 
Vagisha Sharma
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,
Can't you use a TextArea instead of a Canvas? That should solve your problem.

-Vagisha
 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vagisha,
Sorry dear !!, As I have already done it using Canvas !! Conversion of canvas to textarea would be a long process for me.
Thanks Angela for your guidance !!
Regards,
 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TextArea doesn't have word wrapping and can't display different fonts and colors, so that wouldn't work anyway.
 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela,
I think word wrapping in textarea is possible if you remove the horizontal scrollbar. But yes, writing in diffrent colors, I assume would be a problem. But then you can always change the Foreground color of a text you are writing. But if you have to draw an image on a textarea, I dont know how will you ? Many chat applets are doing this including yahoo.com and rediff.com.
If possible , do tell me what they are using , a textarea or a canvas, because they are drawing images also. I doubt if images can be drawn on a textarea.
Regards and thanks,
 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right about the word wrapping, my mistake.
I can't say for sure what Yahoo uses for their chat applet, since I haven't seen the source code, but I'm pretty sure it must be a custom Canvas component. TextArea can't draw images and will only show text in one font and color. And if they were using JTextArea, which does support images and different fonts and colors, then the applet would require the JRE plugin.
 
Sam Cala
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Angela,
You're right ! Canvas would the right component to use.
Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic