J2ME - drawString font related question

D

Dave Ekhaus

hi

i'm trying to write a J2ME app and i'm having some trouble placing
text precisely. the problem is that the font and subsequent font
metrics on two different phones are different. specifically the
getBaselinePosition() and getHeight() methods of the Font object return
different values on the different phones and i'm trying to use the
drawString method to display text in the exact same relative position
on each respective phone. here's what i'm trying to do ...

Font font = Font.getFont (Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_SMALL) ;

int width = getWidth () ;

int refX = 10 ;
int refY = 15 ;


// draw reference line that subsequent text drawing will be relative to ...
g.setColor (0x00,0x99,0xCC) ;
g.drawLine (0, refY + 1, width, refY + 1) ;

StringBuffer buf = new StringBuffer () ;

buf.append ("H = ") ;
buf.append (font.getHeight()) ;
buf.append ("BLP = ") ;
buf.append (font.getBaselinePosition()) ;

g.drawString (buf.toString(),
refX,
refY - font.getBaselinePosition(),
Graphics.LEFT | Graphics.TOP) ;


my goal is to have the text drawn by the 'drawString' method above
drawn in the same relative position to the 'reference' line draw above
on all phones. i thought that making the anchor the 'TOP' and
subtracting the amount of the 'baseline' from the position of the
reference line - would result in the text being drawn the same distance
from the reference line on ANY phone. what i'm seeing (on two different
phones) is different number of pixels between the bottom the text and
my reference line.

any help the group can provide would be greatly appreciated.

thanks
dave
 
D

Darryl Pierce

Dave said:
hi

i'm trying to write a J2ME app and i'm having some trouble placing
text precisely. the problem is that the font and subsequent font
metrics on two different phones are different. specifically the
getBaselinePosition() and getHeight() methods of the Font object return
different values on the different phones and i'm trying to use the
drawString method to display text in the exact same relative position on
each respective phone.

Then we can safely assume that the two handsets are not using the same
font. There's no requirement in the MIDP specification that platforms
use fonts with exactly the same metrics for FACE_MONOSPACE,
FACE_PROPORTIONAL or FACE_SYSTEM. You have to adapt your application so
that it will create its display without depend on any specific font
metrics value.


here's what i'm trying to do ...
Font font = Font.getFont (Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_SMALL) ;

int width = getWidth () ;

int refX = 10 ;
int refY = 15 ;


// draw reference line that subsequent text drawing will be relative to ...
g.setColor (0x00,0x99,0xCC) ;
g.drawLine (0, refY + 1, width, refY + 1) ;

StringBuffer buf = new StringBuffer () ;

buf.append ("H = ") ;
buf.append (font.getHeight()) ;
buf.append ("BLP = ") ;
buf.append (font.getBaselinePosition()) ;

g.drawString (buf.toString(),
refX,
refY - font.getBaselinePosition(),
Graphics.LEFT | Graphics.TOP) ;


my goal is to have the text drawn by the 'drawString' method above drawn
in the same relative position to the 'reference' line draw above on all
phones.

Then you need to set the value for refY programmatically rather than
statically. As you can see, the fonts are different on each handset, so
you'll have to change your code for drawing the line.
 
D

Dave Ekhaus

Then we can safely assume that the two handsets are not using the same
font. There's no requirement in the MIDP specification that platforms
use fonts with exactly the same metrics for FACE_MONOSPACE,
FACE_PROPORTIONAL or FACE_SYSTEM. You have to adapt your application so
that it will create its display without depend on any specific font
metrics value.

i am aware that the two phones are using different fonts.

using the 'baseline position' of the font is an attempt to adapt the
app to each phone.
here's what i'm trying to do ...

Then you need to set the value for refY programmatically rather than
statically. As you can see, the fonts are different on each handset, so
you'll have to change your code for drawing the line.


i am changing the 'y offset' used and making it sensitive to the font
used on each respective phone by using the 'baseline position' of each
font. 'refY' is a reference and should not have to change to
accomodate the differences of one font to another.
 
D

Darryl Pierce

Dave said:
i am aware that the two phones are using different fonts.

using the 'baseline position' of the font is an attempt to adapt the app
to each phone.

What I'm saying is that you should adjust the location where you want
the baseline, basing it on the phone's font rather than on a static value.
i am changing the 'y offset' used and making it sensitive to the font
used on each respective phone by using the 'baseline position' of each
font. 'refY' is a reference and should not have to change to
accomodate the differences of one font to another.

What if the font is taller than 15 pixels?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top