• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strange !! Facing problem in a simple code !!

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic