框架用eclipse开发包含文件如下
其中Game.java是MIDlet类,代码:
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Display;
/*
* Created on 2005-3-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author Magic
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Game extends MIDlet {
public static MIDlet instance;
public static Display display;
/**
*
*/
public Game() {
// TODO Auto-generated constructor stub
display=Display.getDisplay(this);
instance=this;
display.setCurrent(GameCanvas.getInstance());
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp() {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
}
GameCanvas是主Canvas类,代码:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Font;
import java.util.Random;
/*
* Created on 2005-3-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author Magic
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class GameCanvas extends Canvas implements Runnable {
private static GameCanvas gc;
public static int loop;//主循环计数器
public static final int LOGO = 0;//公司标志状态
public static final int MENU = 1;//游戏主菜单状态
public static final int PLAY = 2;//游戏进行状态
public static final int HELP = 3;//游戏帮助状态
public static final int COPY = 4;//公司版权状态
public static int width;//屏幕宽度
public static int height;//屏幕高度
public static Random rdm;//游戏全程用的随机种子
public static int gameState;//游戏状态变量
public static int menuIndex;//菜单索引
private Thread thd;//动画后台线程
private Image imgCompany;//公司标志
private Image imgLogo;//游戏logo
private Image imgBG;//帮助和版权的背景
private Image imgMenu;//游戏菜单
private Image imgMenuCursor;//菜单两边的箭头
private boolean hasSound;//是否有声音
private boolean isStarted;//是否开始游戏
private Font font;
private int helpY;
private String strHelp;
private int linesHelp;
private int lineCharCount;
private int fontHeight;
private int helpMaxY;
synchronized public static GameCanvas getInstance(){//单例模式
if(gc==null){
gc=new GameCanvas();
}
return gc;
}
/**
*
*/
private GameCanvas() {//构造函数
width = getWidth();//width为屏幕宽度
height = getHeight();//height为屏幕高度
rdm=new Random();//生成随机数种子
gameState=LOGO;//初试化游戏状态设置为公司标志
isStarted=false;//游戏未开始
hasSound=true;//默认为有声音
createImageLogo();//创建logo图片
font=Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL);//初始化字体
strHelp="读哈十分首都发火点上多哈,士大夫恒等式反函数的;犯得上副多哈似的哈似的犯得上副多哈似的哈似的犯得上副多哈似的哈似的犯得上副多哈似的哈似的犯得上副多哈似的哈似的犯得上副多哈似的哈似的犯得上副多哈似的哈似的 大撒谎大事记的 大苏打哈时间教授地方艰苦。风格士大夫划时代发挥。";//帮助字符串文字请在次替换
lineCharCount=((3*width)/4)/(font.stringWidth("一"));//当前字体下一行显示中文字符数量
linesHelp=strHelp.length()/lineCharCount+1;//帮助的行数
fontHeight=font.getHeight();//当前字体高度
helpMaxY=linesHelp*fontHeight-6*fontHeight;
thd=new Thread(this);
thd.start();//后台线程开始
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see javax.microedition.lcdui.Displayable#paint(javax.microedition.lcdui.Graphics)
*/
protected void paint(Graphics g) {//paint函数基本不需要改动
// TODO Auto-generated method stub
switch(gameState){
case LOG
drawLogo(g);//画公司标志的方法
break;
case MENU:
drawMenu(g);//画主菜单的方法
break;
case PLAY:
drawPlay(g);//游戏进行绘制屏幕的方法
break;
case HELP:
drawHelp(g);//画帮助画面的方法
break;
case COPY:
drawCopy(g);//画版权画面的方法
break;
}
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {//主循环 基本不需要改动
// TODO Auto-generated method stub
while(true){
long start=System.currentTimeMillis();
loop++;
update();
long end=System.currentTimeMillis();
if(end-start<50){
try{
Thread.sleep(50+start-end);
}catch(Exception e){}
}
repaint();
serviceRepaints();
}
}
public void keyPressed(int keyCode)//按键响应函数
{
switch(gameState){
case LOG//按任意键游戏切换到主菜单
loop=0;
cleanImageLogo();
createImageMenu();
gameState=MENU;
break;
case MENU://主菜单处理逻辑
switch(getGameAction(keyCode)){
case LEFT:
if(menuIndex>0){
if(menuIndex==2&!isStarted){
menuIndex-=2;
}else if(menuIndex==6&&hasSound){
menuIndex-=2;
}else if(menuIndex==5&&!hasSound){
menuIndex-=2;
}else{
menuIndex--;
}
}
break;
case RIGHT:
if(menuIndex<6){
if(menuIndex==0&!isStarted){
menuIndex+=2;
}else if(menuIndex==3&&!hasSound){
menuIndex+=2;
}else if(menuIndex==4&&hasSound){
menuIndex+=2;
}else{
menuIndex++;
}
}
break;
case FIRE:
selectMenuItem();
break;
}
break;
case PLAY://游戏进行的时候代码响应逻辑 请在此填写代码
break;
case HELP:
switch(getGameAction(keyCode)){
case UP:
if(helpY<helpMaxY+fontHeight)
helpY+=font.getHeight();
break;
case DOWN:
if(helpY>0)
helpY-=font.getHeight();
break;
}
break;
case COPY:
break;
}
}
private void drawLogo(Graphics g){//把公司标志画在屏幕中央
g.drawImage(imgCompany,(width-imgCompany.getWidth())/2,(height-imgCompany.getHeight())/2,0);
}
private void drawMenu(Graphics g){//画菜单的代码
g.drawImage(imgLogo,0,0,0);
g.setClip((width-imgMenu.getWidth())/2,(height*3)/4,imgMenu.getWidth(),imgMenu.getHeight()/7);
g.drawImage(imgMenu,(width-imgMenu.getWidth())/2,(height*3)/4-menuIndex*(imgMenu.getHeight()/7),0);
if(loop%4<2){
g.setClip((width-imgMenu.getWidth())/2-imgMenuCursor.getWidth()/2,(height*3)/4+(imgMenu.getHeight()/7-imgMenuCursor.getHeight())/2,imgMenuCursor.getWidth()/2,imgMenuCursor.getHeight());
g.drawImage(imgMenuCursor,(width-imgMenu.getWidth())/2-imgMenuCursor.getWidth()/2,(height*3)/4+(imgMenu.getHeight()/7-imgMenuCursor.getHeight())/2,0);
g.setClip((width-imgMenu.getWidth())/2+imgMenu.getWidth(),(height*3)/4+(imgMenu.getHeight()/7-imgMenuCursor.getHeight())/2,imgMenu.getWidth()/2,imgMenuCursor.getHeight());
g.drawImage(imgMenuCursor,(width-imgMenu.getWidth())/2+imgMenu.getWidth()-imgMenuCursor.getWidth()/2,(height*3)/4+(imgMenu.getHeight()/7-imgMenuCursor.getHeight())/2,0);
}else{
g.setClip((width-imgMenu.getWidth())/2-imgMenuCursor.getWidth()/2-2,(height*3)/4+(imgMenu.getHeight()/7-imgMenuCursor.getHeight())/2,imgMenuCursor.getWidth()/2,imgMenuCursor.getHeight());
g.drawImage(imgMenuCursor,(width-imgMenu.getWidth())/2-imgMenuCursor.getWidth()/2-2,(height*3)/4+(imgMenu.getHeight()/7-imgMenuCursor.getHeight())/2,0);
g.setClip((width-imgMenu.getWidth())/2+imgMenu.getWidth()+2,(height*3)/4+(imgMenu.getHeight()/7-imgMenuCursor.getHeight())/2,imgMenuCursor.getWidth()/2,imgMenuCursor.getHeight());
g.drawImage(imgMenuCursor,(width-imgMenu.getWidth())/2+imgMenu.getWidth()+2-imgMenuCursor.getWidth()/2,(height*3)/4+(imgMenu.getHeight()/7-imgMenuCursor.getHeight())/2,0);
}
g.setClip(0,0,width,height);
}
private void drawPlay(Graphics g){//绘制游戏画面代码请在次填写
}
private void drawCopy(Graphics g){//绘制版权画面的代码
g.drawImage(imgBG,0,0,0);
g.setFont(font);
g.setColor(255,255,255);
g.drawString("版权信息",width/2,height/6,17);
g.drawString("江苏省东方世纪网",width/8,height/6+2*fontHeight,0);
g.drawString("络信息有限公司",width/8,height/6+3*fontHeight,0);
g.drawString("开发制作",width/2,height/6+5*fontHeight,17);
g.drawString("移动数据部",width/8,height/6+7*fontHeight,0);
}
private void drawHelp(Graphics g){//绘制帮助画面的代码
g.drawImage(imgBG,0,0,0);
g.setFont(font);
g.setColor(255,255,255);//帮助文字的颜色
g.drawString("游戏帮助",width/2,height/6,17);
g.setClip(0,height/6+2*fontHeight,width,5*fontHeight);
for(int i=0;i<linesHelp;i++){
if(i<linesHelp-1){
g.drawString(strHelp.substring(lineCharCount*i,lineCharCount*(i+1)),width/2,height/6+2*fontHeight+fontHeight*i-helpY,17);
}else{
g.drawString(strHelp.substring(lineCharCount*i,strHelp.length()),width/8,height/6+2*fontHeight+fontHeight*i-helpY,0);
}
}
g.setClip(0,0,width,height);
if(helpY<helpMaxY+fontHeight&&loop%6>2){
g.setColor(255,255,255);//向下三角形的颜色
g.drawLine(width/2-1,(height*5)/6,width/2+1,(height*5)/6);
g.drawLine(width/2-1,(height*5)/6,width/2,(height*5)/6+3);
g.drawLine(width/2+1,(height*5)/6,width/2,(height*5)/6+3);
}
if(helpY>0&&loop%6>2){
g.setColor(255,255,255);//向上三角形的颜色
g.drawLine(width/2-1,height/6+fontHeight+6,width/2+1,height/6+fontHeight+6);
g.drawLine(width/2-1,height/6+fontHeight+6,width/2,height/6+fontHeight+3);
g.drawLine(width/2+1,height/6+fontHeight+6,width/2,height/6+fontHeight+3);
}
}
private void update(){
switch(gameState){
case LOG//公司标志画面 2秒自动跳到菜单
if(loop>40){
loop=0;
cleanImageLogo();
createImageMenu();
gameState=MENU;
}
break;
case MENU://菜单刷新逻辑 请在此填写代码
break;
case PLAY://游戏刷新逻辑
break;
}
}
private void selectMenuItem(){//这里填写菜单事件代码
switch(menuIndex){
case 0:
isStarted=true;
cleanImageLogo();
createImagePlay();
gameState=PLAY;
break;
case 1:
menuIndex=1;
cleanImageLogo();
createImagePlay();
gameState=PLAY;
break;
case 2:
gameState=HELP;
break;
case 3:
gameState=COPY;
break;
case 4:
hasSound=false;
menuIndex++;
break;
case 5:
hasSound=true;
menuIndex--;
break;
case 6:
Game.instance.notifyDestroyed();
break;
}
}
private void createImageLogo(){//创建公司标志图片
try{
imgCompany=Image.createImage("/company.png");
}catch(Exception e){}
}
private void cleanImageLogo(){//清空内存中的公司标志图片
imgCompany=null;
System.gc();
}
private void createImageMenu(){//创建主菜单画面图片
try{
imgMenu=Image.createImage("/menu.png");
imgMenuCursor=Image.createImage("/menucursor.png");
imgLogo=Image.createImage("/logo.png");
imgBG=Image.createImage("/background.png");
}catch(Exception e){}
}
private void cleanImageMenu(){//清空内存中的主菜单画面图片
imgMenu=null;
imgMenuCursor=null;
imgLogo=null;
imgBG=null;
System.gc();
}
private void createImagePlay(){//创建游戏图片
try{
}catch(Exception e){}
}
private void cleanImagePlay(){//清空内存中的游戏图片
System.gc();
}
public int random(int l,int h){//取l-h随机数的方法
return (rdm.nextInt()>>>1)%(h-l)+l;
}
public int random(int h){//取0-h随机数的方法
return (rdm.nextInt()>>>1)%h;
}
}
希望这段代码对刚开始j2me游戏开发学习的朋友们有所帮助 代码有缺陷之处也请大家指出