源码:
/*
* Created on 2004-6-20
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package cn.edu.xtu.tilepuzzle.ui;
import cn.edu.xtu.tilepuzzle.GameDB;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class MainMenuUI extends Canvas implements CommandListener{
static final int lowColor = 0x000000FF;
static final int highColor = 0x00FF0000;
static final int highBGColor = 0x00CCCCCC;
public int width;
public int height;
static int startHeight;
static final int spacing = GameDB.MONOSPACE_BOLD_LARGE_Font.getHeight() ;
static String[] mainMenu;
private MainUIController mainUIController;
private int lineHeight ;
static int menuIdx;
private Command ok;
private Command cancel;
public MainMenuUI(MainUIController mainUIController) {
this.mainUIController=mainUIController;
//this.setFullScreenMode(true);
this.width = getWidth();
this.height = getHeight();
//System.out.println("屏幕的宽,高:"+width+","+height);
mainMenu=GameDB.menu;
startHeight = GameDB.MONOSPACE_BOLD_LARGE_Font.getHeight();
//startHeight = (height - startHeight) ;
menuIdx = 0;
ok = new Command("选择", Command.OK, 1);
cancel = new Command("退出", Command.CANCEL, 0);
lineHeight=this.height/mainMenu.length;
addCommand(ok);
addCommand(cancel);
setCommandListener(this);
// System.out.println("=2");
this.mainUIController.setOPtions(this);
}
protected void paint(Graphics g) {
// TODO Auto-generated method stub
if(mainUIController.getBoardModel().getGameState()==GameDB.PLAYING){
mainMenu[0] = "恢复游戏";
mainMenu[1] = "新游戏";
}else {
mainMenu[0] = "新游戏";
mainMenu[1] = "设置图片";
}
g.setColor(255, 255, 190);
g.fillRect(0, 0, width, height);
//System.out.println("屏幕的宽,高:"+width+","+height);
for (int i = 0; i < mainMenu.length; i++) {
if (i == menuIdx) {
g.setColor(highBGColor);
g.fillRect(0, i * lineHeight , width, lineHeight);
g.setFont(GameDB.MONOSPACE_BOLD_LARGE_Font);
g.setColor(highColor);
g.drawString(mainMenu[i], (width - GameDB.MONOSPACE_BOLD_LARGE_Font.stringWidth(mainMenu[i])) / 2, i * lineHeight+(lineHeight-GameDB.MONOSPACE_BOLD_LARGE_Font.getHeight())/2 , Graphics.LEFT | Graphics.TOP);
} else {
g.setFont(GameDB.MONOSPACE_PLAIN_MEDIUM_Font);
g.setColor(lowColor);
g.drawString(mainMenu[i], (width - GameDB.MONOSPACE_PLAIN_MEDIUM_Font.stringWidth(mainMenu[i])) / 2,i * lineHeight+(lineHeight-GameDB.MONOSPACE_PLAIN_MEDIUM_Font.getHeight())/2 , Graphics.LEFT | Graphics.TOP);
}
/*
if (i == menuIdx) {
g.setColor(highBGColor);
g.fillRect(0, startHeight + (i * GameDB.MONOSPACE_BOLD_LARGE_Font.getHeight()*3/2) , width, startHeight*3/2);
g.setFont(GameDB.MONOSPACE_BOLD_LARGE_Font);
g.setColor(highColor);
g.drawString(mainMenu[i], (width - GameDB.MONOSPACE_BOLD_LARGE_Font.stringWidth(mainMenu[i])) / 2, startHeight + (i * startHeight*3/2)+startHeight/4 , Graphics.LEFT | Graphics.TOP);
} else {
g.setFont(GameDB.MONOSPACE_PLAIN_MEDIUM_Font);
g.setColor(lowColor);
g.drawString(mainMenu[i], (width - GameDB.MONOSPACE_PLAIN_MEDIUM_Font.stringWidth(mainMenu[i])) / 2, startHeight + (i * startHeight*3/2+startHeight/4) , Graphics.LEFT | Graphics.TOP);
}*/
}
}
protected void keyPressed(int code) {
//System.out.println(code);
if(code==-5||code==5)
mainUIController.handleCOMMAND_ID(menuIdx);
if (getGameAction(code) == Canvas.UP ) {
if(menuIdx - 1 >= 0)
menuIdx--;
else
menuIdx=mainMenu.length-1;
} else if (getGameAction(code) == Canvas.DOWN ) {
if(menuIdx + 1 < mainMenu.length)
menuIdx++;
else
menuIdx=0;
} else;
//System.out.println("menuIdx = " + (menuIdx)+":"+mainMenu[menuIdx]);
repaint();
}
public void commandAction(Command c, Displayable arg1) {
// TODO Auto-generated method stub
if(c==ok){
mainUIController.handleCOMMAND_ID(menuIdx);
}else if(c==cancel){
mainUIController.handleCOMMAND_ID(GameDB.COMMAND_ID_EXIT);
}
}
}
界面显示如图: