实训报告模板 javaME 动画和简单游戏_第1页
实训报告模板 javaME 动画和简单游戏_第2页
实训报告模板 javaME 动画和简单游戏_第3页
实训报告模板 javaME 动画和简单游戏_第4页
实训报告模板 javaME 动画和简单游戏_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

《基于移动平台的JAVA实训》实训报告动画和简单游戏姓名:班级:学号:指导教师:成绩:完成时间:设计目的在本次实训中,针对画布类和画笔类进行一些低级界面的开发,联系前边学过的知识,进行一次综合的实训,学习一些控制技巧。这次实训包括三个小项目,第一个是弹跳的小球,帮助同学们掌握一些动画开发过程的一些技巧,特别是线程控制技巧;第二个是卡通时钟,在这个项目中将制作界面丰富的时钟,主要复习定时器和画图,以及分块画图的一些技巧;第三个是拼图游戏,复习键盘控制和一些开发策略。通过本次实训,我们了解了一些开发技巧,熟悉了低级界面的开发过程和其中的基本内容。功能介绍(一)弹跳的小球:界面上有个小红球,要求能够慢慢掉下来,让后弹起,为了逼真,当球在上方是比较大,球落下时,慢慢变小,在界面的右下角有一个“暂停”按钮,可以让动画暂停,动画暂停结束之后又可以让小球继续运动。(二)卡通时钟:在界面画出当前时间,每隔一秒,重新获取当前时间画到界面上。该问题可以用多线程实现,也可以用定时器实现,本节中选用定时器安装。(三)拼图游戏:制作一个拼图游戏,该系统有1个界面组成,用户通过上、下、左、右控制小块的运动,当用户长按选择的键时界面将会打印出拼好的图片,选择键松开,又恢复到打乱的状态。概要设计(一)弹跳的小球1自定义类说明在这个系统中,定义了两个类,分别是VideoCanvas,用于用于继承Canvas类和实现Runnable接口以及CommandLinstener接口,类VideoMIDlet,用于继承MIDlet类,这个类是手机必不可少的类。2方法定义说明在VideoCanvas类中,有一个publicVideoCanvas()构造方法方法,用于实例化对象;publicvoidcommandAction(Commandc,Displayabled)方法是CommandLinstener接口必须重写的方法,用于响应键盘事件;publicvoidpaint(Graphicsg)方法是类Canvas中必须重写的方法,用于绘制图画;publicvoidrun()是接口Runnable必须重写的方法,用于控制小球的运动。(二)卡通时钟1自定义类说明在这个项目中,有如下几个类:ClockCanvas类,用来继承Canvas类,实现绘制图片的功能,控制显示的数字;ClockMIDlet类,用于继承MIDlet类,实例化ClockCanvas类的对象,并把给对象设置为当前页面。2方法定义说明 在ClockCanvas类中,有publicClockCanvas()构造方法,还有publicvoidpaint(Graphicsg)是类Canvas中必须重写的方法,publicvoidpaint(Graphicsg)用于绘制当前时间,publicvoiddrawNumber(Graphicsg,intnumber,intlocation)方法用于从数字图片上截取一个数字。(三)拼图游戏1自定义类说明在这个游戏中,有如下几个类:PPuzzleCanvas,用于继承Canvas类和实现CommandListener接口,PPuzzleMIDlet类用来继承MIDlet。2方法定义说明在PPuzzleCanvas中有如下方法:publicPPuzzleCanvas(Imageimg,MIDletparent)是构造方法,voidinitMap()是把一幅图分成均等的16份之后打乱顺序,publicvoidpaint(Graphicsg)是Canvas类必须重写的方法,显示打乱顺序之后的图片和判断是否完成拼图,publicvoidcommandAction(Commandcmd,Displayabledis)是接口CommandListener类必须重写的方法,publicbooleanisSuccess()是判断是否完成拼图的方法,protectedvoidkeyPressed(intkeyCode)是响应键盘事件的方法,详细设计弹跳的小球关键代码packageproj11_1;importjavax.microedition.lcdui.Canvas;importjavax.microedition.lcdui.Command;importjavax.microedition.lcdui.CommandListener;importjavax.microedition.lcdui.Displayable;importjavax.microedition.lcdui.Graphics;publicclassVideoCanvasextendsCanvasimplementsRunnable,CommandListener{ privateintleft=50; privateinttop=50; privateintd=100; privateintDIR=1; privateCommandcmdPause=newCommand("暂停",Command.SCREEN,1); privateCommandcmdResume=newCommand("继续",Command.SCREEN,1); privateThreadth; privatebooleanRUN=true; publicVideoCanvas() { this.addCommand(cmdPause); this.setCommandListener(this); th=newThread(this); th.start(); //TODOAuto-generatedconstructorstub } publicvoidcommandAction(Commandc,Displayabled) { if(c==cmdPause) { this.removeCommand(cmdPause); this.addCommand(cmdResume); RUN=false; th=null; } elseif(c==cmdResume) { this.removeCommand(cmdResume); this.addCommand(cmdPause); RUN=true; th=newThread(this); th.start(); } } publicvoidpaint(Graphicsg) { g.setColor(255,255,255); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.setColor(255,0,0); g.fillArc(left,top,d,d,0,360); } publicvoidrun() { while(RUN) { if(DIR==1) { top+=3; d--; if(top>=this.getHeight()-d) { DIR=2; } } if(DIR==2) { top-=3; d++; if(top<=50) { DIR=1; } } repaint(); try { Thread.currentThread().sleep(10); }catch(Exceptionex){} } } }运行截图(二)卡通时钟1、关键代码packagepro11_2;importjava.util.Calendar;importjava.util.Timer;importjava.util.TimerTask;importjavax.microedition.lcdui.Canvas;importjavax.microedition.lcdui.Graphics;importjavax.microedition.lcdui.Image;importjavax.microedition.lcdui.game.Sprite;publicclassClockCanvasextendsCanvas{ privateinthour; privateintminute; privateintsecond; privateImageimg=null; privateTimertimer=newTimer(); privateintwidthOfNumber; privateintheightOfNumber; publicClockCanvas() { try { img=Image.createImage("/number.jpg"); }catch(Exceptionex) { ex.printStackTrace(); } widthOfNumber=img.getWidth()/11; heightOfNumber=img.getHeight(); Tasktask=newTask(); timer.schedule(task,0,1000); } publicvoidpaint(Graphicsg) { intnum1=hour/10; intnum2=hour%10; this.drawNumber(g,num1,0); this.drawNumber(g,num2,1); this.drawNumber(g,10,2); intnum3=minute/10; intnum4=minute%10; this.drawNumber(g,num3,3); this.drawNumber(g,num4,4); this.drawNumber(g,10,5); intnum5=second/10; intnum6=second%10; this.drawNumber(g,num5,6); this.drawNumber(g,num6,7); } publicvoiddrawNumber(Graphicsg,intnumber,intlocation) { intx_src=widthOfNumber*number; inty_src=0; intwidth=widthOfNumber; intheight=heightOfNumber; intx_des=location*widthOfNumber; inty_dest=0; g.drawRegion(img,x_src,y_src,width,height, Sprite.TRANS_NONE,x_des,y_dest,Graphics.LEFT|Graphics.TOP); } classTaskextendsTimerTask { publicvoidrun() { Calendarcalendar=Calendar.getInstance(); hour=calendar.get(Calendar.HOUR_OF_DAY); minute=calendar.get(Calendar.MINUTE); second=calendar.get(Calendar.SECOND); repaint(); } }}运行截图(三)拼图游戏1、关键代码packagepro11_3;importjava.util.Random;importjavax.microedition.lcdui.Canvas;importjavax.microedition.lcdui.Command;importjavax.microedition.lcdui.CommandListener;importjavax.microedition.lcdui.Displayable;importjavax.microedition.lcdui.Graphics;importjavax.microedition.lcdui.Image;importjavax.microedition.lcdui.game.GameCanvas;importjavax.microedition.lcdui.game.Sprite;importjavax.microedition.midlet.MIDlet;publicclassPPuzzleCanvasextendsCanvasimplementsCommandListener{ privateCommandcmdResume=newCommand("继续",Command.SCREEN,1); privateCommandcmdClose=newCommand("关闭",Command.CANCEL,1); privateImageimg; privateintedge; int[][]map={{00,01,02,03}, {10,11,12,13}, {20,21,22,23}, {30,31,32,33}}; privateGraphicsgra=null; privateMIDletparent; publicPPuzzleCanvas(Imageimg,MIDletparent) { this.img=img; this.parent=parent; edge=img.getWidth()/4; this.setCommandListener(this); this.initMap(); } voidinitMap() { Randomrnd=newRandom(); inttemp,x1,y1,x2,y2; for(inti=0;i<100;i++) { x1=rnd.nextInt(4); x2=rnd.nextInt(4); y1=rnd.nextInt(4); y2=rnd.nextInt(4); temp=map[x1][y1]; map[x1][y1]=map[x2][y2]; map[x2][y2]=temp; } this.repaint(); } publicvoidpaint(Graphicsg) { gra=g; g.setColor(255,255,255); g.fillRect(0,0,this.getWidth(),this.getHeight()); for(intx=0;x<4;x++) { for(inty=0;y<4;y++) { if(map[x][y]!=33) { intxSegment=map[x][y]/10; intySegment=map[x][y]%10; g.drawRegion(img,xSegment*edge,ySegment*edge,edge,edge,Sprite.TRANS_NONE,x*edge,y*edge,Graphics.LEFT|Graphics.TOP); } } } if(isSuccess()) { g.setColor(0,0,0); g.drawString("恭喜您,您已经顺利完成!还要继续吗?",this.getWidth()/2,this.getHeight()-60,Graphics.HCENTER|Graphics.TOP); this.addCommand(cmdResume); this.addCommand(cmdClose); } } publicvoidcommandAction(Commandcmd,Displayabledis) { if(cmd==cmdClose) { parent.notifyDestroyed(); }elseif(cmd==cmdResume) { this.initMap(); this.removeCommand(cmdResume); this.removeCommand(cmdClose); } } publicbooleanisSuccess() { for(intx=0;x<4;x++) { for(inty=0;y<4;y++) { intxSegment=map[x][y]/10; intySegment=map[x][y%10]; if(xSegment!=x||ySegment!=y) { returnfalse; } } } returntrue; } protectedvoidkeyPressed(intkeyCode) { intaction=this.getGameAction(keyCode); intxOf33=-1,yOf33=-1; for(intx=0;x<4;x++) { for(inty=0;y<4;y++) { if(map[x][y]==33) { xOf33=x; yOf33=y; break; } } } switch(action) { caseGameCanvas.UP: if(yOf33!=3) { this.swap(xOf33,yOf33,xOf33,yOf33+1); }break; caseGameCanvas.DOWN: if(yOf33!=0) { th

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论