J2ME - drawString font related question

Discussion in 'Java' started by Dave Ekhaus, May 8, 2005.

  1. Dave Ekhaus

    Dave Ekhaus Guest

    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
     
    Dave Ekhaus, May 8, 2005
    #1
    1. Advertising

  2. Dave Ekhaus wrote:
    > 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.

    --
    Darryl L. Pierce <>
    Visit my homepage: http://mcpierce.multiply.com
    "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
     
    Darryl Pierce, May 8, 2005
    #2
    1. Advertising

  3. Dave Ekhaus

    Dave Ekhaus Guest

    On 2005-05-08 05:19:45 -0700, Darryl Pierce <> said:

    > Dave Ekhaus wrote:
    >> 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.


    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 ...
    >>
    >> 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.



    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.
     
    Dave Ekhaus, May 9, 2005
    #3
  4. Dave Ekhaus wrote:
    >>> 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.

    >
    > 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.

    >>> 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.

    >
    > 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?

    --
    Darryl L. Pierce <>
    Visit my homepage: http://mcpierce.multiply.com
    "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
     
    Darryl Pierce, May 10, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonah Olsson

    Special characters with DrawString?!

    Jonah Olsson, Oct 1, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,387
    Dmitriy Lapshin [C# / .NET MVP]
    Oct 3, 2003
  2. Xiaolei Li

    No Focused View on DrawString

    Xiaolei Li, Apr 16, 2004, in forum: Java
    Replies:
    3
    Views:
    403
    Xiaolei Li
    Apr 18, 2004
  3. Chris Smith
    Replies:
    1
    Views:
    3,318
    Wolfgang
    May 31, 2004
  4.  opalinski from opalpaweb
    Replies:
    9
    Views:
    2,645
    opalinski from opalpaweb
    Mar 1, 2006
  5. mttc
    Replies:
    2
    Views:
    2,415
    Roedy Green
    Jul 3, 2009
Loading...

Share This Page