繼承 Displayable,覆寫 paint(Graphics g) 實作自繪。範例:紅底白字 "Hello World!"。
public class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(0xFF0000); // 紅
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xFFFFFF); // 白
g.drawString("Hello World!", 0, 0, Graphics.TOP | Graphics.LEFT);
}
}
顯示:display.setCurrent(new MyCanvas())。
Display.isColor(), numColors()。Image buffer = Image.createImage(getWidth(), getHeight());Graphics gc = buffer.getGraphics();gc 上繪圖 (動畫每幀)。paint(Graphics g) 中快速拷貝:g.drawImage(buffer, 0, 0, 0);Canvas.isDoubleBuffered() 避免重複實作。Canvas.serviceRepaints() 立即同步呼叫 paint(),**若 paint() 鎖定共享物件會死鎖**。避免在 paint() 同步共享物件。Display.callSerially(Runnable):所有待處理重繪完成後,在事件執行緒執行 run(),適合更新 UI 狀態。Font.getFont(face, style, size) 申請,系統回傳最接近字體。| 組合。getFace(), getSize(), getStyle()。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, Graphics.LEFT | Graphics.TOP);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
g.drawString("Medium Size", 0, 15, Graphics.LEFT | Graphics.TOP);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
g.drawString("Bold Style", 0, 30, Graphics.LEFT | Graphics.TOP);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC, Font.SIZE_MEDIUM));
g.drawString("Italic Style", 0, 45, Graphics.LEFT | Graphics.TOP);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM));
g.drawString("Underlined Style", 0, 60, Graphics.LEFT | Graphics.TOP);
}
}