File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Strange !! Facing problem in a simple code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Strange !! Facing problem in a simple code" Watch "Strange !! Facing problem in a simple code" New topic

Strange !! Facing problem in a simple code

Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
Hi Guys,
Following is the code in which I am facing problem. There is a string which is initialized to 'John Levis says : This is something exciting'. Now I have to print this string on my applet by dividing it. So I took a substring and trying to first display until ':'. Then I have made up another string using substring, now I want to display that string on the same line where the earlier string was but it should be displayed where the previous string ended up. So I said - g.drawString(s1,25+s.length(),25);. The target output would be something like this : 'John Levis says : This is something exciting' but not by simply painting s. I have to split s first till ':', take rest of the string in some other string and then painting that another string.
For this I said - g.drawString(s1,25+s.length(),25);, but it seems drawstring method doesn't relies on length but on pixels. So tell me guys what can I do to solve this ?? Please help me. I would be obliged if someone can put up a code to do this !!
Thanks and regards,
/*<applet code="Test" height=200 width=200>
import java.awt.*;
import java.applet.*;
public class Mitali extends Applet {
String s = "John Levis says : This is something exciting";
int i=s.indexOf(":");
String s1="";
public void init() {
public void paint(Graphics g)

Best wishes,
Vagisha Sharma

Joined: Mar 01, 2001
Posts: 12
Use the stringWidth method of class FontMetrics.
public int stringWidth(String str) returns the advance width of the characters in the specified string.
try the following code:
public void paint(Graphics g)
FontMetrics fm = g.getFontMetrics();
g.drawString(s1, 25 + fm.stringWidth(s),25);
I agree. Here's the link:
subject: Strange !! Facing problem in a simple code
It's not a secret anymore!