×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Jun 2006
    Location
    Beijing
    Posts
    25

    Question How to write string in small font in J2me?

    I used the following code to do the test. And I find that the small font has the same char width as the medium font.

    Font sFONT = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_SMALL);
    Font mFONT = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
    getGraphics().drawString("small width:" + sFONT.charWidth('A'),
    10, 40, Graphics.TOP | Graphics.LEFT);
    getGraphics().drawString("medium width:" + mFONT.charWidth('A'),
    10, 60, Graphics.TOP | Graphics.LEFT);

    I want to use the small font in my J2me application. How can I change the font to small ?

    Thanks.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to write string in small font in J2me?

    Code:
    Graphics.setFont();
    Graham.

  3. #3
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,087

    Re: How to write string in small font in J2me?

    Try this -
    <php>
    Font sFONT = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_SMALL);
    Font mFONT = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
    Graphics.setFont(sFONT);
    getGraphics().drawString("small width:" + sFONT.charWidth('A'),
    10, 40, Graphics.TOP | Graphics.LEFT);
    Graphics.setFont(mFONT );
    getGraphics().drawString("medium width:" + mFONT.charWidth('A'),
    10, 60, Graphics.TOP | Graphics.LEFT);
    </php>

    thanks,
    ~Amitabh

  4. #4
    Registered User
    Join Date
    Jun 2006
    Location
    Beijing
    Posts
    25

    Re: How to write string in small font in J2me?

    thanks. It does work on the WTK's default emulator but it does not work well on the S60 3rd FP2 emulator.

    In the S60 3rd, the small font's char is the same as the medium font char. Any one has the experience on the show small font in the J2me application?


    package com.dh.j2me.font;

    import javax.microedition.lcdui.Canvas;
    import javax.microedition.lcdui.Font;
    import javax.microedition.lcdui.Graphics;

    public class FontCanvas extends Canvas {

    protected void paint(Graphics g) {
    int x = 0;
    int y = 0;
    int w = getWidth();
    int h = getHeight();

    g.setColor(0xFFFFFF);
    g.fillRect(x, y, w, h);

    Font fontSmall = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    Font fontMedium = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
    Font fontLarge = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);

    g.setColor(0x000000);


    g.setFont(fontSmall);
    y = paintTextRow(g, "AAAA" + fontSmall.charWidth('A'), x, y);

    g.setFont(fontMedium);
    y = paintTextRow(g, "AAAA" + fontMedium.charWidth('A'), x, y);

    g.setFont(fontLarge);
    y = paintTextRow(g, "AAAA" + fontLarge.charWidth('A'), x, y);

    }


    private int paintTextRow(Graphics g, String text, int x, int y)
    {
    int w = getWidth();

    Font font = g.getFont();
    for (int j = 0; j < text.length(); j++)
    {
    char c = text.charAt(j);
    int cw = font.charWidth(c);
    if (x + cw > w)
    {
    x = 0;
    y += font.getHeight();
    }
    g.drawChar(c, x, y, Graphics.TOP | Graphics.LEFT);
    x += cw;
    }
    y += font.getHeight();
    return y;
    }

    }









    Quote Originally Posted by im2amit View Post
    Try this -

    Font sFONT = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_SMALL);
    Font mFONT = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
    Graphics.setFont(sFONT);
    getGraphics().drawString("small width:" + sFONT.charWidth('A'),
    10, 40, Graphics.TOP | Graphics.LEFT);
    Graphics.setFont(mFONT );
    getGraphics().drawString("medium width:" + mFONT.charWidth('A'),
    10, 60, Graphics.TOP | Graphics.LEFT);

    thanks,
    ~Amitabh
    why to learn symbian? Do not know well, but do.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Location
    Beijing
    Posts
    25

    Thumbs down Re: How to write string in small font in J2me?

    In S60 5th, we can use the DirectUtils.getFont() (It's nokia's native API) to get/set the font for your preferred font size. But in the S60 3th, no getFont() exists for the DirectUtils class and the J2me's stand Graphics.getFont() for small font does NOT work well.

    It is too strange. The Nokia's future is not so bright for this kind of problem should not existed at all.

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to write string in small font in J2me?

    Code:
    Font smallFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    g.setFont(smallFont);
    g.drawString("this is as small as it gets", 0, 0, Graphics.TOP | Graphics.LEFT);
    This will display the smallest font available from the implementation.

    Note that the MIDP specification does not require implementations to provide different sizes of font. The only rule is that SIZE_SMALL will not give you a larger font than SIZE_MEDIUM (and likewise, SIZE_MEDIUM will not be larger than SIZE_LARGE). On many devices, only one font is available, and all font options will yield the same font. Similarly, face and style options are simply requests... the font you get back might not honour them. You might always get the same font, no matter what arguments you pass.

    Graham.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Location
    Beijing
    Posts
    25

    Re: How to write string in small font in J2me?

    Yes. You are right. Thanks.
    why to learn symbian? Do not know well, but do.

Similar Threads

  1. J2ME File Write Access in the jar file
    By bergteufel in forum Mobile Java General
    Replies: 2
    Last Post: 2010-03-20, 03:40
  2. scroll image and string J2ME
    By mohit28garg in forum Mobile Java General
    Replies: 3
    Last Post: 2010-03-13, 05:36
  3. J2ME or C++ for VoIP on Mobile
    By sandeepkumar03 in forum VoIP
    Replies: 4
    Last Post: 2009-02-02, 10:28
  4. J2ME or C++ for VoIP on Mobile Phone
    By sandeepkumar03 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2008-11-13, 20:14
  5. read and write Chinese menu in J2ME
    By walterzcm in forum Mobile Java General
    Replies: 0
    Last Post: 2002-11-13, 06:22

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×