下面是在MIDlet中畫圖的一個例子,用到了幾個方法,如drawLine,setColor,drawRect,drawRoundRect,fillRect,其中drawRoundRect的後兩個參數分別表示要畫矩形的園角的橫向和縱向直徑:
import Javax.microedition.lcdui.*;
import Javax.microedition.midlet.*;
public class GraphicsMidlet extends MIDlet
{
Display dis;
MyCanvas c;
public GraphicsMidlet()
{
dis = Display.getDisplay(this);
c = new MyCanvas();
dis.setCurrent(c);
}
public void startApp()
{
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
}
class MyCanvas extends Canvas
{
public void paint(Graphics g)
{
int h = this.getHeight();
int w = this.getWidth();
g.setColor(255,255,255);
g.fillRect(0,0,w,h);
g.setColor(255,0,255);
g.drawLine(0,0,w,h);
g.setColor(0,255,255);
g.drawLine(0,h,w,0);
g.setColor(0,0,0);
g.drawRoundRect(w/2 - 20,h/2 -20,40,40,20,20);
}
}