Java Reference
In-Depth Information
// Clear the Canvas.
g.setGrayScale(255);
g.fillRect(0, 0, w - 1, h - 1);
g.setGrayScale(0);
g.drawRect(0, 0, w - 1, h - 1);
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();
}
}
To see this Canvas in action, you'll need a MIDlet that shows it. You could modify Pacer
again, if you wish, or use the following code:
import javax.microedition.lcdui.*;
import javax.microedition.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);
}
Search WWH ::




Custom Search