J2ME 拼图游戏 快速开发 全过程 之代码祥解(6 )——欢迎界面、游戏结束后的等待界面与增加用户名字界面
2012-04-14 14:20
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 java.io.IOException;
import javax.microedition.lcdui.*;
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class WelcomeUI extends Canvas
{
public int width;
public int height;
//private Image image;
//private String message = DataBase.author;
private String message = GameDB.author;
private String school = GameDB.school;
public WelcomeUI()
{
this.setFullScreenMode(true);
width = this.getWidth();
height = this.getHeight();
//System.out.println(width+","+height);
//image = createImage("/welcome.png");
}
public Image createImage(String name)
{
Image image = null;
try
{
image = Image.createImage(name);
}
catch(IOException e)
{
}
return image;
}
public void setMessage(String message)
{
this.message = message;
this.repaint();
}
public void setSchool(String school)
{
this.school = school;
this.repaint();
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.Displayable#paint(javax.microedition.lcdui.Graphics)
*/
protected void paint(Graphics g)
{
//System.out.println("WelcomeUI 开始绘图 ......");
// TODO Auto-generated method stub
//arg0.setColor(128, 128, 255);
//arg0.drawRect(5, 5, width - 10, height - 10);
/*
if(image!=null)
{
arg0.drawImage(image,(width-image.getWidth())/2,
(height-image.getHeight())/2,Graphics.TOP|Graphics.LEFT);
}
*/
g.setColor(255, 255, 190);
g.fillRect(0, 0, width, height);
g.setColor(255,0,0);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE ));
if (message != null)
{
g.drawString(message, width / 2, height / 3, Graphics.BOTTOM | Graphics.HCENTER);
}
if (school != null)
{
g.drawString(school, width / 2, height * 2 / 3, Graphics.BOTTOM | Graphics.HCENTER);
}
}
}
游戏结束后的等待界面,如图:
代码:
package cn.edu.xtu.tilepuzzle.ui;
import java.util.*;
import javax.microedition.lcdui.*;
import cn.edu.xtu.tilepuzzle.GameDB;
import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
import cn.edu.xtu.tilepuzzle.model.BoardModel;
public class WaitCanvasUI extends Canvas implements CommandListener{
MainUIController mainUIController;
Display display;
BoardModel boardModel;
Displayable displayable;
private int mCount, mMaximum;
private int mInterval;
private int mWidth, mHeight, mX, mY, mRadius;
private String mMessage;
Command ok;
Command cancel;
public WaitCanvasUI(MainUIController mainUIController,BoardModel boardModel,Display display,Displayable db) {
this.boardModel=boardModel;
this.display=display;
this.displayable=db;
this.mainUIController=mainUIController;
mCount = 0;
mMaximum = 36;
mInterval = 100;
mWidth = getWidth();
mHeight = getHeight();
// Calculate the radius.
int halfWidth = (mWidth - mRadius) / 2;
int halfHeight = (mHeight - mRadius) / 2;
mRadius = Math.min(halfWidth, halfHeight);
// Calculate the location.
mX = halfWidth - mRadius / 2;
mY = halfHeight - mRadius / 2;
ok=new Command("添加",Command.OK,1);
cancel=new Command("返回",Command.CANCEL,0);
this.addCommand(ok);
this.addCommand(cancel);
setCommandListener(this);
}
public void setFlag(boolean run){
// Create a Timer to update the display.
if (run) {
TimerTask task = new TimerTask() {
public void run() {
mCount = (mCount + 1) % mMaximum;
repaint();
}
};
Timer timer = new Timer();
timer.schedule(task, 0, mInterval);
}
}
public void paint(Graphics g) {
int theta = -(mCount * 360 / mMaximum);
// Clear the whole screen.
g.setColor(255, 255, 190);
g.fillRect(0, 0, mWidth, mHeight);
// Now draw the pinwheel.
g.setColor(128, 128, 255);
g.drawArc(mX, mY, mRadius, mRadius, 0, 360);
g.fillArc(mX, mY, mRadius, mRadius, theta + 90, 90);
g.fillArc(mX, mY, mRadius, mRadius, theta + 270, 90);
// Draw the message, if there is a message.
if (mMessage != null) {
g.drawString(mMessage, mWidth / 2, mHeight, Graphics.BOTTOM
| Graphics.HCENTER);
}
g.setColor(255,0,0);
g.setFont(GameDB.MONOSPACE_BOLD_LARGE_Font);
//绘制一个手机全屏幕填充颜色的矩形
//g.fillRect(0,0,getWidth(),getHeight());
//System.out.println("胜利时屏幕宽度= "+getWidth());
//System.out.println("恭喜你,过关了!,字符串宽度="+tempFont.stringWidth("恭喜你,过关了!"));
g.drawString((boardModel.cheated ? "CHEATER!" : "恭喜你,过关了!"), (getWidth()-GameDB.MONOSPACE_BOLD_LARGE_Font.stringWidth("恭喜你,过关了!"))/2 , getHeight()*1/12 , Graphics.LEFT | Graphics.TOP);
g.setFont(GameDB.MONOSPACE_BOLD_MEDIUM_Font);
//System.out.println("共用时:"+boardModel.sumtTime);
String timeString="共用时:"+BoardModel.getTimeStringByS(boardModel.sumTime);
g.drawString(timeString,(getWidth()- GameDB.MONOSPACE_BOLD_MEDIUM_Font.stringWidth(timeString))/2, getHeight()*2/12,Graphics.LEFT | Graphics.TOP);
g.setFont(GameDB.MONOSPACE_PLAIN_MEDIUM_Font);
String messageString="最否将你的名字添加到游戏排名?";
g.drawString(messageString,(getWidth()- GameDB.MONOSPACE_PLAIN_MEDIUM_Font.stringWidth(messageString))/2, getHeight()*10/12,Graphics.LEFT | Graphics.TOP);
}
public void commandAction(Command c, Displayable arg1) {
if(c==ok){
//System.out.println("OK");
AddUserNameUI addUserNameUI=new AddUserNameUI(mainUIController, boardModel, display, displayable);
display.setCurrent(addUserNameUI);
//mainUIController.handleCOMMAND_ID(menuIdx);
}else if(c==cancel){
//System.out.println("CANCEL");
this.display.setCurrent(mainUIController.mainMenuUI);
//mainUIController.handleCOMMAND_ID(6);
}
}
}
增加用户名字界面,如图:
代码:
package cn.edu.xtu.tilepuzzle.ui;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import cn.edu.xtu.tilepuzzle.GameDB;
import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
import cn.edu.xtu.tilepuzzle.model.BoardModel;
import cn.edu.xtu.tilepuzzle.model.RecordStoreOperations;
public class AddUserNameUI extends Canvas implements CommandListener{
MainUIController mainUIController;
Display display;
BoardModel boardModel;
Displayable db;
Image image;
TextBox nameTextBox;
private Command ok;
private Command cancel;
public AddUserNameUI(MainUIController mainUIController,BoardModel boardModel,Display display,Displayable db) {
//this.setFullScreenMode(true);
this.boardModel=boardModel;
this.display=display;
this.db=db;
this.mainUIController=mainUIController;
init();
}
public void init(){
nameTextBox=new TextBox("请输入你的名字",null,255,TextField.ANY);
ok = new Command("确定", Command.OK, 1);
cancel = new Command("退出", Command.CANCEL, 0);
nameTextBox.addCommand(ok);
nameTextBox.addCommand(cancel);
nameTextBox.setCommandListener(this);
}
protected void paint(Graphics g) {
this.display.setCurrent(nameTextBox);
}
public void commandAction(Command c, Displayable db) {
if(c==ok){
try {
String strString=nameTextBox.getString()+";"+(boardModel.columns+"x"+boardModel.rows).toString()+";"+boardModel.sumTime/1000;
RecordStoreOperations.addStrToRecordStroe(strString ,GameDB.recordStorePeopleResult);
mainUIController.showPeopleInfoListUI.init();
mainUIController.showPeopleInfoListUI.repaint();
this.display.setCurrent(mainUIController.mainMenuUI);
} catch (Exception e) {
e.printStackTrace();
}
}else if(c==cancel){
this.display.setCurrent(mainUIController.mainMenuUI);
//mainUIController.handleCOMMAND_ID(6);
//System.out.println("Cancel");
}
}
}
代码:
/*
* 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 java.io.IOException;
import javax.microedition.lcdui.*;
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class WelcomeUI extends Canvas
{
public int width;
public int height;
//private Image image;
//private String message = DataBase.author;
private String message = GameDB.author;
private String school = GameDB.school;
public WelcomeUI()
{
this.setFullScreenMode(true);
width = this.getWidth();
height = this.getHeight();
//System.out.println(width+","+height);
//image = createImage("/welcome.png");
}
public Image createImage(String name)
{
Image image = null;
try
{
image = Image.createImage(name);
}
catch(IOException e)
{
}
return image;
}
public void setMessage(String message)
{
this.message = message;
this.repaint();
}
public void setSchool(String school)
{
this.school = school;
this.repaint();
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.Displayable#paint(javax.microedition.lcdui.Graphics)
*/
protected void paint(Graphics g)
{
//System.out.println("WelcomeUI 开始绘图 ......");
// TODO Auto-generated method stub
//arg0.setColor(128, 128, 255);
//arg0.drawRect(5, 5, width - 10, height - 10);
/*
if(image!=null)
{
arg0.drawImage(image,(width-image.getWidth())/2,
(height-image.getHeight())/2,Graphics.TOP|Graphics.LEFT);
}
*/
g.setColor(255, 255, 190);
g.fillRect(0, 0, width, height);
g.setColor(255,0,0);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE ));
if (message != null)
{
g.drawString(message, width / 2, height / 3, Graphics.BOTTOM | Graphics.HCENTER);
}
if (school != null)
{
g.drawString(school, width / 2, height * 2 / 3, Graphics.BOTTOM | Graphics.HCENTER);
}
}
}
游戏结束后的等待界面,如图:
代码:
package cn.edu.xtu.tilepuzzle.ui;
import java.util.*;
import javax.microedition.lcdui.*;
import cn.edu.xtu.tilepuzzle.GameDB;
import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
import cn.edu.xtu.tilepuzzle.model.BoardModel;
public class WaitCanvasUI extends Canvas implements CommandListener{
MainUIController mainUIController;
Display display;
BoardModel boardModel;
Displayable displayable;
private int mCount, mMaximum;
private int mInterval;
private int mWidth, mHeight, mX, mY, mRadius;
private String mMessage;
Command ok;
Command cancel;
public WaitCanvasUI(MainUIController mainUIController,BoardModel boardModel,Display display,Displayable db) {
this.boardModel=boardModel;
this.display=display;
this.displayable=db;
this.mainUIController=mainUIController;
mCount = 0;
mMaximum = 36;
mInterval = 100;
mWidth = getWidth();
mHeight = getHeight();
// Calculate the radius.
int halfWidth = (mWidth - mRadius) / 2;
int halfHeight = (mHeight - mRadius) / 2;
mRadius = Math.min(halfWidth, halfHeight);
// Calculate the location.
mX = halfWidth - mRadius / 2;
mY = halfHeight - mRadius / 2;
ok=new Command("添加",Command.OK,1);
cancel=new Command("返回",Command.CANCEL,0);
this.addCommand(ok);
this.addCommand(cancel);
setCommandListener(this);
}
public void setFlag(boolean run){
// Create a Timer to update the display.
if (run) {
TimerTask task = new TimerTask() {
public void run() {
mCount = (mCount + 1) % mMaximum;
repaint();
}
};
Timer timer = new Timer();
timer.schedule(task, 0, mInterval);
}
}
public void paint(Graphics g) {
int theta = -(mCount * 360 / mMaximum);
// Clear the whole screen.
g.setColor(255, 255, 190);
g.fillRect(0, 0, mWidth, mHeight);
// Now draw the pinwheel.
g.setColor(128, 128, 255);
g.drawArc(mX, mY, mRadius, mRadius, 0, 360);
g.fillArc(mX, mY, mRadius, mRadius, theta + 90, 90);
g.fillArc(mX, mY, mRadius, mRadius, theta + 270, 90);
// Draw the message, if there is a message.
if (mMessage != null) {
g.drawString(mMessage, mWidth / 2, mHeight, Graphics.BOTTOM
| Graphics.HCENTER);
}
g.setColor(255,0,0);
g.setFont(GameDB.MONOSPACE_BOLD_LARGE_Font);
//绘制一个手机全屏幕填充颜色的矩形
//g.fillRect(0,0,getWidth(),getHeight());
//System.out.println("胜利时屏幕宽度= "+getWidth());
//System.out.println("恭喜你,过关了!,字符串宽度="+tempFont.stringWidth("恭喜你,过关了!"));
g.drawString((boardModel.cheated ? "CHEATER!" : "恭喜你,过关了!"), (getWidth()-GameDB.MONOSPACE_BOLD_LARGE_Font.stringWidth("恭喜你,过关了!"))/2 , getHeight()*1/12 , Graphics.LEFT | Graphics.TOP);
g.setFont(GameDB.MONOSPACE_BOLD_MEDIUM_Font);
//System.out.println("共用时:"+boardModel.sumtTime);
String timeString="共用时:"+BoardModel.getTimeStringByS(boardModel.sumTime);
g.drawString(timeString,(getWidth()- GameDB.MONOSPACE_BOLD_MEDIUM_Font.stringWidth(timeString))/2, getHeight()*2/12,Graphics.LEFT | Graphics.TOP);
g.setFont(GameDB.MONOSPACE_PLAIN_MEDIUM_Font);
String messageString="最否将你的名字添加到游戏排名?";
g.drawString(messageString,(getWidth()- GameDB.MONOSPACE_PLAIN_MEDIUM_Font.stringWidth(messageString))/2, getHeight()*10/12,Graphics.LEFT | Graphics.TOP);
}
public void commandAction(Command c, Displayable arg1) {
if(c==ok){
//System.out.println("OK");
AddUserNameUI addUserNameUI=new AddUserNameUI(mainUIController, boardModel, display, displayable);
display.setCurrent(addUserNameUI);
//mainUIController.handleCOMMAND_ID(menuIdx);
}else if(c==cancel){
//System.out.println("CANCEL");
this.display.setCurrent(mainUIController.mainMenuUI);
//mainUIController.handleCOMMAND_ID(6);
}
}
}
增加用户名字界面,如图:
代码:
package cn.edu.xtu.tilepuzzle.ui;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import cn.edu.xtu.tilepuzzle.GameDB;
import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
import cn.edu.xtu.tilepuzzle.model.BoardModel;
import cn.edu.xtu.tilepuzzle.model.RecordStoreOperations;
public class AddUserNameUI extends Canvas implements CommandListener{
MainUIController mainUIController;
Display display;
BoardModel boardModel;
Displayable db;
Image image;
TextBox nameTextBox;
private Command ok;
private Command cancel;
public AddUserNameUI(MainUIController mainUIController,BoardModel boardModel,Display display,Displayable db) {
//this.setFullScreenMode(true);
this.boardModel=boardModel;
this.display=display;
this.db=db;
this.mainUIController=mainUIController;
init();
}
public void init(){
nameTextBox=new TextBox("请输入你的名字",null,255,TextField.ANY);
ok = new Command("确定", Command.OK, 1);
cancel = new Command("退出", Command.CANCEL, 0);
nameTextBox.addCommand(ok);
nameTextBox.addCommand(cancel);
nameTextBox.setCommandListener(this);
}
protected void paint(Graphics g) {
this.display.setCurrent(nameTextBox);
}
public void commandAction(Command c, Displayable db) {
if(c==ok){
try {
String strString=nameTextBox.getString()+";"+(boardModel.columns+"x"+boardModel.rows).toString()+";"+boardModel.sumTime/1000;
RecordStoreOperations.addStrToRecordStroe(strString ,GameDB.recordStorePeopleResult);
mainUIController.showPeopleInfoListUI.init();
mainUIController.showPeopleInfoListUI.repaint();
this.display.setCurrent(mainUIController.mainMenuUI);
} catch (Exception e) {
e.printStackTrace();
}
}else if(c==cancel){
this.display.setCurrent(mainUIController.mainMenuUI);
//mainUIController.handleCOMMAND_ID(6);
//System.out.println("Cancel");
}
}
}
相关文章推荐
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(5 )——记录集操作类、序列的奇偶性检查类
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(1 )——程序入口与控制器开发
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(2 )——游戏数据库与游戏模型开发
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(3 )——拼图的方格类、个人信息类、选择背景拼图的列表类
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(4 )——图片操作类、图片预览后处理类
- J2ME 拼图游戏 快速开发 全过程 (一)——最佳成绩显示界面
- J2ME 拼图游戏 快速开发 全过程 (四)——游戏选项设置界面
- J2ME 拼图游戏 快速开发 全过程 (五)——游戏帮助界面
- J2ME 拼图游戏 快速开发 全过程 (六)——图片设置界面设计
- J2ME 拼图游戏 快速开发 全过程 (七)——设置图片后的图片预览界面
- J2ME 拼图游戏 快速开发 全过程 (二)——游戏开始界面设计
- J2ME 拼图游戏 快速开发 全过程 (三)——新游戏界面
- 微信快速开发框架(八)-- V2.3--增加语音识别及网页获取用户信息,代码已更新至Github
- J2ME 拼图游戏 快速开发 全过程 (一)——开发环境及游戏菜单
- 微信快速开发框架V2.3--增加语音识别及网页获取用户信息(八),代码已更新至Github
- iOS开发UI篇—以微博界面为例使用纯代码自定义cell程序编码全过程(一)
- Asp.net 2.0 自定义控件开发专题讲解[为用户控件增加DataSource属性, 能够自动识别不同数据源](示例代码下载)
- .NET快速信息化系统开发框架 V3.2 -> “用户管理”主界面使用多表头展示、增加打印功能
- Asp.net 2.0 自定义控件开发专题讲解[为用户控件增加DataSource属性, 能够自动识别不同数据源](示例代码下载)
- Asp.net 2.0 自定义控件开发专题讲解[为用户控件增加DataSource属性, 能够自动识别不同数据源](示例代码下载)