GUI组件(4)
时间:2012-01-03
让我们再看一个例子:示例3中代码子类化Canvas 类。在此,绘图颜色设置为白色,并用该色画出一个矩形,然后把绘图色置为黑色。代码剩下的部分在设备屏幕上绘制系统字体,如图2所示。
图2.在设备显示屏上画出系统字体
示例3:使用字体
import javax.microedition.lcdui.*;
public 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);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,Font.SIZE_MEDIUM));
g.drawString("Medium Size", 0, 15, g.LEFT | g.TOP);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,Font.SIZE_MEDIUM));
g.drawString("Bold Style", 0, 30, g.LEFT | g.TOP);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC,Font.SIZE_MEDIUM));
g.drawString("Italic Style", 0, 45, g.LEFT | g.TOP);
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM));
g.drawString("Underlined Style", 0, 60, g.LEFT | g.TOP);
}
}
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class FontMidlet extends MIDlet {
public FontMidlet( ) {}
public void startApp( ) {
Canvas canvas = new FontCanvas( );
Display display = Display.getDisplay(this);
display.setCurrent(canvas);
}
public void pauseApp( ) {}
public void destroyApp(boolean unconditional) {}
}
|