Đa số thì game nào cũng vậy, mở ra sẽ có phần giới thiệu này nọ, sơ lược cốt truyện, chọn sound...Logo game...Nhưng không thể nào thiếu một menu, nơi sẽ có các tùy chọn như continue, new game, high score, help, exit...
Sau đây sẽ là một menu đơn giản:
//Midlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Midlet extends MIDlet{
public void startApp() {
Display.getDisplay(this).setCurrent(newcv(this));
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}
//cv.java
import javax.microedition.lcdui.*;
public class cv extends Canvas{
String[] s={"play","help","score","exit"};
int w, h, fh, in=0, mo=0;
Midlet m;
Font f=Font.getDefaultFont();
public cv(Midlet m){
this.m=m;
setFullScreenMode(true);
w=getWidth();
h=getHeight();
fh=f.getHeight();
}
public void paint(Graphics g){
g.setColor(0);
g.fillRect(0,0,w,h);
g.setColor(0xf0f0f0);
switch(mo){
case0:
//ve menu
//ve index
g.fillRect(0, (h-fh*4)/2+in*fh,w,fh);
for(int i=0;i< s.length;i++){
if(i==in){
g.setColor(0);
}else{
g.setColor(0xf0f0f0);
}
g.drawString(s[i],w/2,(h-fh*4)/2+i*fh,Graphics.HCENTER|Graphics.TOP);
}
break;
case1:
//ve play
g.drawString(s[0],w/2,h/2,Graphics.BASELINE|Graphics.HCENTER);
break;
case2:
//ve help
g.drawString(s[1],w/2,h/2,Graphics.BASELINE|Graphics.HCENTER);
break;
case3:
//ve about
g.drawString(smenu[2],w/2,h/2,Graphics.BASELINE|Graphics.HCENTER);
break;
case4:
//ve score
g.drawString(s[3],w/2,h/2,Graphics.BASELINE|Graphics.HCENTER);
break;
}
//ve cac lenh
g.setColor(0xf0f0f0);
g.drawString("exit",0,h,Graphics.LEFT|Graphics.BOTTOM);
g.drawString("back",w,h,Graphics.RIGHT|Graphics.BOTTOM);
repaint();
}
public void keyPressed(int k){
//di chuyen index khi nhan phim
if(k==-1)in--;
if(k==-2)in++;
if(in< 0)in=s.length-1;
if(in==s.length)in=0;
//lua chon menu se ve tuong ung
if(k==-5){
if (in==s.length-1)m.notifyDestroyed();
mo=in+1;
}
//lenh exit va back
if(k==-7)mo=0;
if(k==-6)m.notifyDestroyed();
}
}