版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
《基于移動平臺的JAVA實訓》實訓報告動畫和簡單游戲姓名:班級:學號:指導教師:成績:完成時間:設計目的在本次實訓中,針對畫布類和畫筆類進行一些低級界面的開發(fā),聯(lián)系前邊學過的知識,進行一次綜合的實訓,學習一些控制技巧。這次實訓包括三個小項目,第一個是彈跳的小球,幫助同學們掌握一些動畫開發(fā)過程的一些技巧,特別是線程控制技巧;第二個是卡通時鐘,在這個項目中將制作界面豐富的時鐘,主要復習定時器和畫圖,以及分塊畫圖的一些技巧;第三個是拼圖游戲,復習鍵盤控制和一些開發(fā)策略。通過本次實訓,我們了解了一些開發(fā)技巧,熟悉了低級界面的開發(fā)過程和其中的基本內(nèi)容。功能介紹(一)彈跳的小球:界面上有個小紅球,要求能夠慢慢掉下來,讓后彈起,為了逼真,當球在上方是比較大,球落下時,慢慢變小,在界面的右下角有一個“暫?!卑粹o,可以讓動畫暫停,動畫暫停結束之后又可以讓小球繼續(xù)運動。(二)卡通時鐘:在界面畫出當前時間,每隔一秒,重新獲取當前時間畫到界面上。該問題可以用多線程實現(xiàn),也可以用定時器實現(xiàn),本節(jié)中選用定時器安裝。(三)拼圖游戲:制作一個拼圖游戲,該系統(tǒng)有1個界面組成,用戶通過上、下、左、右控制小塊的運動,當用戶長按選擇的鍵時界面將會打印出拼好的圖片,選擇鍵松開,又恢復到打亂的狀態(tài)。概要設計(一)彈跳的小球1自定義類說明在這個系統(tǒng)中,定義了兩個類,分別是VideoCanvas,用于用于繼承Canvas類和實現(xiàn)Runnable接口以及CommandLinstener接口,類VideoMIDlet,用于繼承MIDlet類,這個類是手機必不可少的類。2方法定義說明在VideoCanvas類中,有一個publicVideoCanvas()構造方法方法,用于實例化對象;publicvoidcommandAction(Commandc,Displayabled)方法是CommandLinstener接口必須重寫的方法,用于響應鍵盤事件;publicvoidpaint(Graphicsg)方法是類Canvas中必須重寫的方法,用于繪制圖畫;publicvoidrun()是接口Runnable必須重寫的方法,用于控制小球的運動。(二)卡通時鐘1自定義類說明在這個項目中,有如下幾個類:ClockCanvas類,用來繼承Canvas類,實現(xiàn)繪制圖片的功能,控制顯示的數(shù)字;ClockMIDlet類,用于繼承MIDlet類,實例化ClockCanvas類的對象,并把給對象設置為當前頁面。2方法定義說明 在ClockCanvas類中,有publicClockCanvas()構造方法,還有publicvoidpaint(Graphicsg)是類Canvas中必須重寫的方法,publicvoidpaint(Graphicsg)用于繪制當前時間,publicvoiddrawNumber(Graphicsg,intnumber,intlocation)方法用于從數(shù)字圖片上截取一個數(shù)字。(三)拼圖游戲1自定義類說明在這個游戲中,有如下幾個類:PPuzzleCanvas,用于繼承Canvas類和實現(xiàn)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("繼續(xù)",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("繼續(xù)",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("恭喜您,您已經(jīng)順利完成!還要繼續(xù)嗎?",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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 16 太陽 教案 統(tǒng)編版五年級語文上冊
- 2024年九年級道德與法治下冊 第一單元 我們共同的世界 第一課 同住地球村 第2框 復雜多變的關系說課稿 新人教版
- 2 學會寬容 第一課時 說課稿-2023-2024學年道德與法治六年級下冊統(tǒng)編版
- 2025如何寫農(nóng)村土地承包合同范文
- 2025服裝代理商合同協(xié)議書范本
- 2《花的學?!氛f課稿-2024-2025學年統(tǒng)編版語文三年級上冊
- 隧道拆除專項施工方案
- 2024年五年級數(shù)學上冊 二 小數(shù)乘法 2小數(shù)的乘法第2課時 小數(shù)乘小數(shù)說課稿 冀教版
- 軍訓訓合同范例
- 黔江辦公室鋁扣板施工方案
- 2025年浙江省交通投資集團財務共享服務中心招聘2名高頻重點提升(共500題)附帶答案詳解
- 做投標文件培訓
- 9.4+跨學科實踐:制作簡易活塞式抽水機課件+-2024-2025學年人教版物理八年級下冊
- 建筑工程工作計劃
- 2025年中國國際投資促進中心限責任公司招聘管理單位筆試遴選500模擬題附帶答案詳解
- 瓶裝液化氣送氣工培訓
- 外科護理課程思政課程標準
- 船舶航行安全
- 道德經(jīng)全文完整版本
- 9.2溶解度(第1課時飽和溶液不飽和溶液)+教學設計-2024-2025學年九年級化學人教版(2024)下冊
- 2024年審計局公務員招錄事業(yè)單位招聘考試招錄139人完整版附答案【研優(yōu)卷】
評論
0/150
提交評論