This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange !! Facing problem in a simple code

 
Sam Cala
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Vagisha Sharma
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic