Click to See Complete Forum and Search --> : How to catch the length of string in pixels


k_venkurao
May 6th, 2000, 05:57 AM
hello all,

i have got a doubt. Can any one help me
how to catch the length of a string in pixel.

For example i want to draw an image after printing
a string like this

"HEllo how are you" <Image>

"Hai fine how about you are you ok" <Image>

Can any one tell me a general approach for this

Thanx in advance
k_venkurao

weaver
May 6th, 2000, 07:57 AM
You can use the FontMetrics class to get the length of a string in pixels. You can do this by the following:

FontMetrics metrics = aWindow.getFontMetrics(); // aWindow is a frame
int stringLength = metrics.stringLength( myString );

-------------------------------------------
weaver
icq# 64665116
Please rate this post.
http://weaver.x7.htmlplanet.com

k_venkurao
May 7th, 2000, 11:44 PM
Thank you very much for you reply. Can you tell the same for an applet. Because i have developed a chat server. I will to give an option for images
also.

Thankyou very much
k_venkurao

weaver
May 8th, 2000, 06:20 AM
Ok, here's a version that should work with applets.

Graphics g = getGraphics();
FontMetrics fm = g.getFontMetrics();
int length = fm.stringLength(myString);

-------------------------------------------
weaver
icq# 64665116
Please rate this post.
http://weaver.x7.htmlplanet.com