This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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: 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:

 
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,
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite 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!