Java Tutorial/J2ME

Материал из Java эксперт
Перейти к: навигация, поиск

Bold Style Font

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 BoldFontMidlet extends MIDlet {
  public BoldFontMidlet() { // constructor
  }
  public void startApp() {
    Canvas canvas = new FontCanvas();
    Display display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
}
class FontCanvas extends Canvas {
  public void paint(Graphics g) {
    g.setColor(0xffffff);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(0x000000);
    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
    g.drawString("Bold Style", 0, 30, g.LEFT | g.TOP);
  }
}





Font style

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class FontMIDlet extends MIDlet implements CommandListener {
  private FontCanvas mFontCanvas;
  private Command mBoldCommand, mItalicCommand, mUnderlineCommand;
  public FontMIDlet() {
    mFontCanvas = new FontCanvas();
    mBoldCommand = new Command("Bold", Command.SCREEN, 0);
    mItalicCommand = new Command("Italic", Command.SCREEN, 0);
    mUnderlineCommand = new Command("Underline", Command.SCREEN, 0);
    Command exitCommand = new Command("Exit", Command.EXIT, 0);
    mFontCanvas.addCommand(mBoldCommand);
    mFontCanvas.addCommand(mItalicCommand);
    mFontCanvas.addCommand(mUnderlineCommand);
    mFontCanvas.addCommand(exitCommand);
    mFontCanvas.setCommandListener(this);
  }
  public void startApp() {
    Display.getDisplay(this).setCurrent(mFontCanvas);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT) {
      notifyDestroyed();
      return;
    }
    boolean isBold = mFontCanvas.isBold() ^ (c == mBoldCommand);
    boolean isItalic = mFontCanvas.isItalic() ^ (c == mItalicCommand);
    boolean isUnderline = mFontCanvas.isUnderline() ^ (c == mUnderlineCommand);
    int style = (isBold ? Font.STYLE_BOLD : 0) | (isItalic ? Font.STYLE_ITALIC : 0)
        | (isUnderline ? Font.STYLE_UNDERLINED : 0);
    mFontCanvas.setStyle(style);
    mFontCanvas.repaint();
  }
}
class FontCanvas extends Canvas {
  private Font mSystemFont, mMonospaceFont, mProportionalFont;
  public FontCanvas() {
    this(Font.STYLE_PLAIN);
  }
  public FontCanvas(int style) {
    setStyle(style);
  }
  public void setStyle(int style) {
    mSystemFont = Font.getFont(Font.FACE_SYSTEM, style, Font.SIZE_MEDIUM);
    mMonospaceFont = Font.getFont(Font.FACE_MONOSPACE, style, Font.SIZE_MEDIUM);
    mProportionalFont = Font.getFont(Font.FACE_PROPORTIONAL, style, Font.SIZE_MEDIUM);
  }
  public boolean isBold() {
    return mSystemFont.isBold();
  }
  public boolean isItalic() {
    return mSystemFont.isItalic();
  }
  public boolean isUnderline() {
    return mSystemFont.isUnderlined();
  }
  public void paint(Graphics g) {
    int w = getWidth();
    int h = getHeight();
    int x = w / 2;
    int y = 20;
    y += showFont(g, "System", x, y, mSystemFont);
    y += showFont(g, "Monospace", x, y, mMonospaceFont);
    y += showFont(g, "Proportional", x, y, mProportionalFont);
  }
  private int showFont(Graphics g, String s, int x, int y, Font f) {
    g.setFont(f);
    g.drawString(s, x, y, Graphics.TOP | Graphics.HCENTER);
    return f.getHeight();
  }
}





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);
  }
}





Get width and height

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class FontDimensionMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand= new Command("Exit", Command.EXIT, 1);
  Display display;
  public void startApp() {
    Display display = Display.getDisplay(this);
    Displayable d = new FontDimensionCanvas();
    d.addCommand(exitCommand);
    d.setCommandListener(this);
    display.setCurrent(d);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    notifyDestroyed();
  }
}
class FontDimensionCanvas extends Canvas {
  int width;
  int height;
  private Font aFont;
  public FontDimensionCanvas() {
    aFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
  }
  public void paint(Graphics g) {
    width = getWidth();
    height = getHeight();
    String s = "TEXT";
    int stringWidth = aFont.stringWidth(s);
    int stringHeight = aFont.getHeight();
    g.setGrayScale(255);
    g.fillRect(0, 0, width - 1, height - 1);
    g.setGrayScale(0);
    g.drawRect(0, 0, width - 1, height - 1);
    g.setFont(aFont);
    g.drawString(s, 10, 10, Graphics.TOP | Graphics.LEFT);
    g.drawRect(10, 10, stringWidth, stringHeight);
  }
}





Italic Style Font

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 ItalicFontMidlet extends MIDlet {
  public ItalicFontMidlet() { // constructor
  }
  public void startApp() {
    Canvas canvas = new FontCanvas();
    Display display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
}
class FontCanvas extends Canvas {
  public void paint(Graphics g) {
    g.setColor(0xffffff);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(0x000000);
    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC, Font.SIZE_MEDIUM));
    g.drawString("Italic Style", 0, 45, g.LEFT | g.TOP);
  }
}





Medium Sized font

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 MediumFontMidlet extends MIDlet {
  public MediumFontMidlet() { // constructor
  }
  public void startApp() {
    Canvas canvas = new FontCanvas();
    Display display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
}
class FontCanvas extends Canvas {
  public void paint(Graphics g) {
    g.setColor(0xffffff);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(0x000000);
    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
    g.drawString("Medium Size", 0, 15, g.LEFT | g.TOP);
  }
}





System Font

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 SystemFontMidlet extends MIDlet {
  public SystemFontMidlet() { // constructor
  }
  public void startApp() {
    Canvas canvas = new FontCanvas();
    Display display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
}
class FontCanvas extends Canvas {
  public void paint(Graphics g) {
    g.setColor(0xffffff);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(0x000000);
    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
    g.drawString("System Font", 0, 0, g.LEFT | g.TOP);
  }
}





Underlined Style Font

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 UnderlineFontMidlet extends MIDlet {
  public UnderlineFontMidlet() { // constructor
  }
  public void startApp() {
    Canvas canvas = new FontCanvas();
    Display display = Display.getDisplay(this);
    display.setCurrent(canvas);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
}
class FontCanvas extends Canvas {
  public void paint(Graphics g) {
    g.setColor(0xffffff);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(0x000000);
    g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM));
    g.drawString("Underlined Style", 0, 60, g.LEFT | g.TOP);
  }
}