欢迎来到天天文库
浏览记录
ID:34723833
大小:48.35 KB
页数:3页
时间:2019-03-10
《java手机游戏开发实训(手机上下左右键设置代码)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、KeyCode.java(第一个文件)importjavax.microedition.midlet.*;importjavax.microedition.lcdui.*;//importMyCanvas.java;publicclassKeyCodeextendsMIDlet{privateDisplaydisplay;privateMyCanvascanvas;//定义canvas属性,引用的是用户自定义类//MyCanvascanvas=newcanvas();publicKeyCode(){display=Display
2、.getDisplay(this);canvas=newMyCanvas(this);//实例化canvas}protectedvoidstartApp(){display.setCurrent(canvas);//当程序启动时将canvas注册到程序中}protectedvoidpauseApp(){}protectedvoiddestroyApp(booleanunconditional){}publicvoidexitMIDlet()//退出方法{destroyApp(true);notifyDestroyed();}}M
3、yCanvas.java(第二个文件)importjavax.microedition.midlet.*;importjavax.microedition.lcdui.*;publicclassMyCanvasextendsCanvasimplementsCommandListener{privateCommandexit;privateStringdirection;//定义String型方向变量privateKeyCodekeyCodeExample;publicMyCanvas(KeyCodekeyCodeExample)
4、{direction="2=up8=dn4=lt6=rt";this.keyCodeExample=keyCodeExample;exit=newCommand("Exit",Command.EXIT,1);//定义退出命令对象addCommand(exit);setCommandListener(this);}protectedvoidpaint(Graphicsgraphics)//定义绘画方法{graphics.setColor(255,255,255);//设置颜色为红色graphics.fillRect(0,0,get
5、Width(),getHeight());//填充的起始点为(0,0)graphics.setColor(255,0,0);graphics.drawString(direction,0,0,Graphics.TOP
6、Graphics.LEFT);//开始绘画}publicvoidcommandAction(Commandcommand,Displayabledisplayable){keyCodeExample.exitMIDlet();//定义退出事件}protectedvoidkeyPressed(intkey)//定义按
7、键{switch(key){caseKEY_NUM2:direction="up";break;caseKEY_NUM8:direction="down";break;caseKEY_NUM4:direction="left";break;caseKEY_NUM6:direction="right";break;case-1:direction="上键";break;case-2:direction="下键";break;case-3:direction="左键";break;case-4:direction="右键";brea
8、k;case-5:direction="确定";break;case-10:direction="拨号键";break;case42:direction="*号键";break;case35:direction="#键";break;}repaint();//重绘组件}}两个文件放在一个文件夹下,文件夹名为KeyCode.java生成启动
此文档下载收益归作者所有