gamemidlet.java
import javax.microedition.midlet.midlet;
import javax.microedition.midlet.midletstatechangeexception;
import javax.microedition.lcdui.display;
public class gamemidlet extends midlet {
game game;
display display;
public gamemidlet() {
game = new game(this);
display = display.getdisplay(this);
}
protected void startapp() throws midletstatechangeexception {
game.setgamestate(game.logo, 30);
game.start();
display.setcurrent(game);
}
protected void pauseapp() {
notifypaused();
}
protected void destroyapp(boolean _boolean) throws midletstatechangeexception {
notifydestroyed();
}
}
game.java
import javax.microedition.lcdui.*;
import com.nokia.mid.ui.*;
/**
* <p>title: </p>
*
* <p>description: </p>
*
* <p>copyright: copyright (c) 2005</p>
*
* <p>company: </p>
*
* @author not attributable
* @version 1.0
*/
public class game extends fullcanvas implements runnable ,commandlistener{
private gamemidlet gamemidlet;
private boolean rungame;//游戏是否启动
private int fps;//频率
private int gamestate = -1; //当前游戏状态
/**
* 游戏状态参数
*/
public final byte logo = 0;
public final byte menu = 1;
public final byte about = 2;
public final byte help = 3;
public final byte wait = 4;
public final byte loginform = 5;
public final byte result = 6;
public final byte loading = 7;
public final byte regist = 8;
public final byte game = 9;
private image bufferedimage;//双缓冲图片
private form form;
private command cmdreg, cmdback; // 注册、登陆、返回、修改按钮对象
private textfield tfaccount, tfpassword; // 昵称、密码,textfield对象
/**
* 默认构造方法
*/
public game(gamemidlet midlet) {
gamemidlet = midlet;
int width = 128;
int height = 128;
// create image buffer:
bufferedimage = image.createimage(width, height);
}
protected void keypressed(int keycode) {
if (keycode == -5) {
setgamestate(loginform, 30);
}
}
/**
*设置当前状态
* @param bgamestate byte 状态值
* @param newfps byte 当前状态下的频率
*/
public void setgamestate(int bgamestate,int newfps){
this.release();
this.gamestate = bgamestate;
this.fps = newfps;
this.init();
}
/**
* 设置当前状态
* @param bgamestate int 状态值
*/
public void setgamestate(int bgamestate){
this.release();
this.gamestate = bgamestate;
this.fps = 30;
this.init();
}
/**
* 得到当前状态
* @return int
*/
public int getgamestate(){
return this.gamestate;
}
/**
* 游戏主线程启动
*/
public void start() {
rungame = true;
thread t = new thread(this);
t.start();
}
/**
* 游戏主线程停止
*/
public void stop() {
rungame = false;
}
/**
* 实现runnable接口
*/
public void run() {
graphics g = bufferedimage.getgraphics();//得到graphics绘图对象
long starttime;
int interval = 1000 / this.fps;
byte count = 2;//垃圾回收次数控制
byte n = count;
thread currentthread = thread.currentthread();
while (rungame && currentthread == thread.currentthread()) {
interval = 1000 / this.fps;
starttime = system.currenttimemillis();
this.input();
this.work();
drawscreen(g);
if (gamestate != loginform) {
repaint();//请求刷新屏幕
servicerepaints();
}
waitsomething(starttime, interval);
if (n == 0) {
runtime.getruntime().gc();
n = count;
}
n--;
}
}
/**
* 线程休眠一段时间
* @param loopstarttime long
* @param interval long
*/
protected void waitsomething(long loopstarttime, long interval) {
long time = system.currenttimemillis() - loopstarttime;
if (time < interval) {
try {
thread.sleep(interval - time);
} catch (interruptedexception e) {}
} else {
thread.yield();
}
}
public void paint(graphics g) {
g.drawimage(this.bufferedimage, 0, 0, graphics.top graphics.left);
}
/**
* 当前状态初始化
*/
protected void init() {
switch (getgamestate()) {
case loginform:
initloginform();
break;
default:
break;
}
}
/**
* 当前状态释放资源
*/
protected void release() {
switch (getgamestate()) {
case loginform:
releaseloginform();
break;
default:
break;
}
runtime.getruntime().gc();
}
/**
* 当前状态下的用户输入
*/
protected void input() {
}
/**
* 当前状态下的逻辑处理
*/
protected void work() {
}
/**
* 当前状态下的绘制
* @param g graphics
*/
protected void drawscreen(graphics g) {
switch (getgamestate()) {
case log
g.drawstring("aaa",10,50,0);
break;
default:
break;
}
system.out.println(runtime.getruntime().freememory());
}
private void initloginform(){
form = new form("用户登陆");
tfaccount = new textfield("帐号:", "", 9, textfield.any);
// tfpassword = new textfield("密码:", "", 9, textfield.password);
cmdreg = new command("注册", command.screen, 2);
cmdback = new command("后退", command.back, 3);
form.append(tfaccount);
// form.append(tfpassword);
form.addcommand(cmdreg);
form.addcommand(cmdback);
form.setcommandlistener(this);
gamemidlet.display.setcurrent(form);
}
public void commandaction(command command, displayable displayable) {
if (command == cmdreg) {
setgamestate(logo, 30);
gamemidlet.display.setcurrent(this);
}
}
private void releaseloginform(){
for (int i = form.size() - 1; i >= 0; i --){
form.delete(i);
}
tfaccount = null;
// tfpassword = null;
cmdback = null;
cmdreg = null;
form = null;
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器