import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
public class MainCanvas extends Canvas implements Runnable
{
private Image imgBuffer;
private Graphics graBuffer;
private boolean Running;
protected Thread thread;
protected GameMIDlet midlet;
protected int keyCode;
public static final int KEY_SOFTKEY1 = -6;
public static final int KEY_SOFTKEY2 = -7;
public static final int UP=-1;
public static final int NUM2=50;
public static final int DOWN=-2;
public static final int NUM8=56;
public static final int LEFT=-3;
public static final int NUM4=52;
public static final int RIGHT=-4;
public static final int NUM6=54;
public static final int NUM_3=51;
public static final int FIRE=-5;
public static final int NUM_5=53;
protected static final byte GS_LOGO = 0; //LOGO显示状态
protected static final byte GS_MENU = 1; //主菜单状态
protected static final byte GS_ENGINE = 2; //游戏中状态(正式进入游戏中)
protected static final byte GS_GAME_END = 3; //游戏结束
protected static final byte GS_CONTROL = 4; //游戏控制
protected static final byte GS_ABOUT = 5; //游戏关于信息
private byte nowGameState;
private byte preGameState;
protected static final int SCREENWIDTH=240;
protected static final int SCREENHEIGHT=320;
protected boolean init;
protected boolean release;
protected MenuCanvas menu;
protected EngineCanvas engine;
public MainCanvas(GameMIDlet midlet)
{
this.midlet=midlet;
setFullScreenMode(true);
imgBuffer=Image.createImage(SCREENWIDTH,SCREENHEIGHT);
graBuffer=imgBuffer.getGraphics();
nowGameState=GS_MENU;
init=true;
release=false;
Running=true;
}
protected void startThread()
{
if(thread==null)
{
thread=new Thread(this);
}
thread.start();
}
protected void mainLogic()
{
switch(nowGameState)
{
case GS_MENU:
switch(menu.menuLogic(keyCode))
{
case 0:
init=true;
release=true;
preGameState=nowGameState;
nowGameState=GS_ENGINE;
break;
case 1:
break;
case 2:
break;
}
break;
case GS_ENGINE:
switch(engine.engineLogic(keyCode))
{
case 0:
init=true;
release=true;
preGameState=nowGameState;
nowGameState=GS_MENU;
break;
case 1:
break;
case 2:
break;
}
break;
}
}
protected void maininit()
{
switch(nowGameState)
{
case GS_MENU:
menu=new MenuCanvas();
break;
case GS_ENGINE:
engine=new EngineCanvas();
break;
}
init=false;
}
protected void mainrelease()
{
switch(preGameState)
{
case GS_MENU:
menu.release();
menu=null;
break;
case GS_ENGINE:
engine.release();
engine=null;
break;
}
release=false;
}
protected void paint(Graphics g)
{
g.setColor(0);
g.fillRect(0,0,SCREENWIDTH,SCREENHEIGHT);
//lanlin: offPaint(graBuffer);
g.drawImage(imgBuffer,0,0,0);
//lanlin: for(int i=0;i<=320;i=i+4)
//lanlin: {
//lanlin: for(j=0;j<=240;j=j+4)
//lanlin: g.setClip(1,1);
//lanlin: }
}
protected void offPaint(Graphics g)
{
// if(!(release|| init))
// {
switch(nowGameState)
{
case GS_MENU:
menu.menuPaint(g);
break;
case GS_ENGINE:
engine.enginePaint(g);
break;
}
// }
}
public void run()
{
while(Running)
{
if(init)
maininit();
if(release)
{mainrelease();}
long startTime=System.currentTimeMillis();
// offPaint(graBuffer);
mainLogic();
if(release|| init)
continue;
offPaint(graBuffer);
repaint();
serviceRepaints();
System.gc();
long passTime=System.currentTimeMillis()-startTime;
// System.out.println(passTime);
if(passTime<100)
{
try{
Thread.sleep(100-passTime);
}catch(Exception e)
{
}
}
}
}
protected void keyPressed(int key)
{
switch(nowGameState)
{
case GS_MENU:
keyCode=key;
break;
case GS_ENGINE:
keyCode=key;
break;
}
}
protected void keyReleased(int key)
{
switch(nowGameState)
{
case GS_MENU:
keyCode=0;
break;
case GS_ENGINE:
keyCode=0;
break;
}
}
}