File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets 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 » Applets
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,
Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
Please, I mean no disrespect, but this is honestly THE most confusing quastion I've ever heard asked. What exactly is it that you want answered. I hope this doesn't come across as ethnocentric. Your English is terrific, made I had a difficult time making heads or tails of this....too many levels of indirection. Okay, as best I could make of it...
Maybe you should try to print what you want to print in a TextField or a Label, or does this not suit your purposes?
If you could a little more clearly state your problem, I think I could help. No disrespect intended, I've written many a confusing question.
Matt
Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
Some people really dont have a sense of understanding. People who do not want to understand may never get anything.
Its no disrespect, I can tell you.
With warm regards - my dear friend,
Dave Turner
Ranch Hand

Joined: Mar 13, 2001
Posts: 60
You need to use the FontMetrics Class (Graphics.getFontMetrics()) to determine the length of your String in pixels using the current Font. In particular youll need to use the stringWidth() method which returns the string parameter's width in pixels.
Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
Hi,
Yes, I'm now done with the same. But it still its giving me problem. Assume that I have a string and I wana divide it from where the ':' is. Now I really dont know how much pixels (i.e., string width) are there before ':'. If I say :
public void paint(Graphics g)
{
g.drawString(s,25,25);
g.drawString(s1,25+fm.stringWidth(),25);
}
}
where fm is a FontMetrics object then also it gives me problem when there are more(say around 30) characters before ':'
Regards and thank u very much,
Paul Bailey
Ranch Hand

Joined: Oct 20, 2000
Posts: 91
Originally posted by Sam Cala:
Hi,
Yes, I'm now done with the same. But it still its giving me problem. Assume that I have a string and I wana divide it from where the ':' is. Now I really dont know how much pixels (i.e., string width) are there before ':'. If I say :
public void paint(Graphics g)
{
g.drawString(s,25,25);
g.drawString(s1,25+fm.stringWidth(),25);
}
}
where fm is a FontMetrics object then also it gives me problem when there are more(say around 30) characters before ':'
Regards and thank u very much,

the FontMetrics class, in the java 1.3 standard edition api http://java.sun.com/j2se/1.3/docs/api/java/awt/FontMetrics.html
requires a String, so your code, as above, should not compile. Are you sure that this is the code you are using?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange !! Facing problem in a simple code !!
 
Similar Threads
Haw to display painting in Panel
Strange !! Facing problem in a simple code
Problem in UDP client/server application
Problem in UDP client/server application....
Custom java.awt.TextArea enhancement