import Javax.microedition.lcdui.*;
public class MainCanvas extends Canvas { Image image = null; /**代表選中項目的索引號碼*/ int index = 1;
public MainCanvas() { try{ image = Image.createImage("/res/menu.png"); }catch(Exception e){
} }
protected void paint(Graphics g) { //清屏 g.setColor(255,255,255); g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
//繪制圖片 g.drawImage(image,0,0,Graphics.TOP | Graphics.LEFT);
//繪制矩形 g.drawRect(39,24 + (index - 1) * 30,100,27); }
protected void keyPressed(int keyCode){ //按下的是數字鍵2 if(keyCode == Canvas.KEY_NUM2){ //判斷是否移動到第一個選項 if(index == 1){ index = 4; }else{ index--; } } //按下的是數字鍵8 if(keyCode == Canvas.KEY_NUM8){ //判斷是否移動到第四個選項 if(index == 4){ index = 1; }else{ index++; } }
//重新繪制屏幕 repaint();
//如果按下數字鍵5 if(keyCode == Canvas.KEY_NUM5){ //根據選擇的不同顯示不同的界面 switch(index){ case 1:
break; case 2:
break; case 3:
break; case 4: CanvasMIDlet.quitApp(); } }
} }