J2ME 拼图游戏 快速开发 全过程 (二)——游戏开始界面设计
2012-04-14 13:24
351 查看
源码:
/*
* 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);
}
}
}
界面显示如图:
/*
* 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);
}
}
}
界面显示如图:
相关文章推荐
- J2ME 拼图游戏 快速开发 全过程 (三)——新游戏界面
- J2ME 拼图游戏 快速开发 全过程 (四)——游戏选项设置界面
- J2ME 拼图游戏 快速开发 全过程 (五)——游戏帮助界面
- J2ME 拼图游戏 快速开发 全过程 (六)——图片设置界面设计
- J2ME 拼图游戏 快速开发 全过程 (一)——最佳成绩显示界面
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(6 )——欢迎界面、游戏结束后的等待界面与增加用户名字界面
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(2 )——游戏数据库与游戏模型开发
- J2ME 拼图游戏 快速开发 全过程 (七)——设置图片后的图片预览界面
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(3 )——拼图的方格类、个人信息类、选择背景拼图的列表类
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(4 )——图片操作类、图片预览后处理类
- J2ME 拼图游戏 快速开发 全过程 (一)——开发环境及游戏菜单
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(5 )——记录集操作类、序列的奇偶性检查类
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(1 )——程序入口与控制器开发
- [wxWidgets]_[初级]_[使用wxFormBuilder设计XRC文件快速开发界面]
- [wxWidgets]_[初级]_[使用wxFormBuilder设计XRC文件快速开发界面]
- Android开发教程:游戏过程中按Home键后返回游戏界面
- 从拼图游戏开始(五)_Android版功能及界面设计
- [Cocos2d塔防游戏开发]Cocos2dx-3.X完成塔防游戏《王国保卫战》--游戏开始界面
- Html5 Egret游戏开发 成语大挑战(六)游戏界面构建和设计
- Html5 Egret游戏开发 成语大挑战(三)开始界面