Skip to main content

font.substringWidth() to find the length of a string in pixels

3 replies [Last post]
mmc2
Offline
Joined: 2010-02-09
Points: 0

I want to find the length of a string in pixels.

System.out.println(font.substringWidth(s, 0, 10)); (font with a small f)
brings up the message "Cannot find symbol symbol variable font"

System.out.println(Font.substringWidth(s, 0, 10)); (Font with a capital F)
brings up the message "non-static method... cannot be referenced from a static context"

Can someone please show me how to do this?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15
Points: 0

System.out.println("Font Width="+Font.getDefaultFont().substringWidth(s,0,10));

Rules of OOP. You must have an instance of an object to make a method call (Instance Methods) on it, unless it's a static method (Class Methods).

-Shawn

mmc2
Offline
Joined: 2010-02-09
Points: 0

Thank you very much for your help. I feel that I have some competence in JME but I know that there are some gaps. Could you please point me to some documentation that would explain the issues here.

sfitzjava
Offline
Joined: 2003-06-15
Points: 0

Bruce Eckel's "Thinking in Java", will provide a good understanding of the items that you may be needing a deeper understanding in. Then Jonathan Knudsen's "Kicking Butt with MIDP and MSA" will have j2me knowledge detail of the UI, and major portions.

Both excellent reads.

-Shawn