aspose file tools*
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
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
Author

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>
</applet>*/
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() {
s1=s.substring(i+1);
s=s.substring(0,i+1);
repaint();
}
public void paint(Graphics g)
{
g.drawString(s,25,25);
g.drawString(s1,25+s.length(),25);
}
}

Best wishes,
Vagisha Sharma
Greenhorn

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:
<code>
public void paint(Graphics g)
{
FontMetrics fm = g.getFontMetrics();
g.drawString(s,25,25);
g.drawString(s1, 25 + fm.stringWidth(s),25);
}
</code>
 
 
subject: Strange !! Facing problem in a simple code