Get font width : Font « J2ME « Java Tutorial






Get font width
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

public class FontWidthGraphicsMIDlet extends MIDlet {
  private Display display;
  protected void startApp() {
    Canvas canvas = new LineCanvas();
    display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}

class LineCanvas extends Canvas {
  public void paint(Graphics g) {
    int width = getWidth();
    int height = getHeight();
    g.setColor(0);
    g.setStrokeStyle(Graphics.SOLID);

        Font font = g.getFont();
        int x = font.stringWidth("asdf");
        g.setColor(0x00ff00);
        g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD | Font.STYLE_ITALIC, Font.SIZE_MEDIUM));
        g.drawString("and multi-color", x, height/2, Graphics.LEFT | Graphics.BASELINE);
  }
}








31.30.Font
31.30.1.Get font widthGet font width
31.30.2.Font styleFont style
31.30.3.System FontSystem Font
31.30.4.Medium Sized fontMedium Sized font
31.30.5.Bold Style FontBold Style Font
31.30.6.Italic Style FontItalic Style Font
31.30.7.Underlined Style FontUnderlined Style Font
31.30.8.Get width and heightGet width and height