九九热最新网址,777奇米四色米奇影院在线播放,国产精品18久久久久久久久久,中文有码视频,亚洲一区在线免费观看,国产91精品在线,婷婷丁香六月天

歡迎來到裝配圖網(wǎng)! | 幫助中心 裝配圖網(wǎng)zhuangpeitu.com!
裝配圖網(wǎng)
ImageVerifierCode 換一換
首頁(yè) 裝配圖網(wǎng) > 資源分類 > PPT文檔下載  

J2ME的低級(jí)用戶界面編程.ppt

  • 資源ID:6357337       資源大?。?span id="24d9guoke414" class="font-tahoma">981.55KB        全文頁(yè)數(shù):77頁(yè)
  • 資源格式: PPT        下載積分:14.9積分
快捷下載 游客一鍵下載
會(huì)員登錄下載
微信登錄下載
三方登錄下載: 微信開放平臺(tái)登錄 支付寶登錄   QQ登錄   微博登錄  
二維碼
微信掃一掃登錄
下載資源需要14.9積分
郵箱/手機(jī):
溫馨提示:
用戶名和密碼都是您填寫的郵箱或者手機(jī)號(hào),方便查詢和重復(fù)下載(系統(tǒng)自動(dòng)生成)
支付方式: 支付寶    微信支付   
驗(yàn)證碼:   換一換

 
賬號(hào):
密碼:
驗(yàn)證碼:   換一換
  忘記密碼?
    
友情提示
2、PDF文件下載后,可能會(huì)被瀏覽器默認(rèn)打開,此種情況可以點(diǎn)擊瀏覽器菜單,保存網(wǎng)頁(yè)到桌面,就可以正常下載了。
3、本站不支持迅雷下載,請(qǐng)使用電腦自帶的IE瀏覽器,或者360瀏覽器、谷歌瀏覽器下載即可。
4、本站資源下載后的文檔和圖紙-無(wú)水印,預(yù)覽文檔經(jīng)過壓縮,下載后原文更清晰。
5、試題試卷類文檔,如果標(biāo)題沒有明確說明有答案則都視為沒有答案,請(qǐng)知曉。

J2ME的低級(jí)用戶界面編程.ppt

20 40 1 第8章J2ME的低級(jí)用戶界面編程 20 40 2 預(yù)備知識(shí) 高級(jí)界面就是界面上的效果都是由控件組成的 與此對(duì)應(yīng)的是低級(jí)界面 就是界面效果都是通過編程 在畫布上畫出來的 Displayable的子類可以充滿整個(gè)界面 其直接子類有兩個(gè) Canvas和Screen 其中 Screen的子類就是以前講解高級(jí)界面 而Canvas就提供了低級(jí)界面開發(fā)的支持 本章主要講解低級(jí)界面開發(fā) 20 40 3 本章知識(shí)點(diǎn) Canvas類 1 圖形繪制與Graphics類 2 文字繪制與Font類 3 圖像繪制與Image類 4 Canvas事件處理 低級(jí) 5 動(dòng)畫實(shí)現(xiàn) 6 游戲設(shè)計(jì)中的基本方法 7 20 40 4 8 1Canvas類 Canvas類代表了設(shè)備屏幕的低級(jí)抽象繪圖表面 畫布 它可以用于繪制直線和曲線以及顯示圖像等圖形操作 是游戲屏幕的基礎(chǔ) 所有的低級(jí)API都是由javax microedition lcdui Canvas類開始 只要繼承Canvas 就可以建立一個(gè)能夠顯示在屏幕上的畫面 Canvas是一個(gè)抽象類 其中只有一個(gè)必須要實(shí)現(xiàn)的抽象方法paint 每當(dāng)屏幕需要重繪時(shí) 就會(huì)產(chǎn)生重繪事件 系統(tǒng)會(huì)自動(dòng)調(diào)用paint 并傳入一個(gè)Graphics的引用 由此 整個(gè)畫面描繪出來 20 40 5 8 1Canvas類 當(dāng)屏幕需要重繪時(shí) 就會(huì)產(chǎn)生重繪事件 系統(tǒng)將自動(dòng)調(diào)用paint 方法 實(shí)現(xiàn)整個(gè)屏幕畫的繪制 畫布開發(fā)的基本結(jié)構(gòu) importjavax microedition midlet importjavax microedition lcdui publicclassMyMidletextendsMIDlet publicMyMidlet publicvoidstartApp Canvascanvas newMC Displayd Display getDisplay this d setCurrent canvas publicvoidpauseApp publicvoiddestroyApp booleanu importjavax microedition lcdui 畫布類publicclassMCextendsCanvas publicvoidpaint Graphicsg 在畫布上畫圖 20 40 6 8 1Canvas類 Canvas類的方法 20 40 7 8 1Canvas類 Canvas類的方法 20 40 8 8 1Canvas類 Canvas類的方法 20 40 9 8 1Canvas類 如果用戶想要調(diào)用paint 方法 只能通過repaint 方法 repaint 方法叫重繪 用于請(qǐng)求屏幕重新繪圖 repaint 有兩個(gè)重載方法 repaint 不帶參數(shù)的方法用于重畫整個(gè)屏幕 repaint intx inty intpw intph 帶參數(shù)的方法用于重繪指定的矩形區(qū)域 其中 x y 是重繪區(qū)域的左上角坐標(biāo) pw ph為寬和高 重畫整個(gè)屏幕的方法 編程實(shí)現(xiàn)較簡(jiǎn)單 但效率比較低 一般 指定重畫區(qū)的繪圖方法會(huì)比較有效率 20 40 10 8 1Canvas類 調(diào)用repaint 之后會(huì)立刻返回 調(diào)用paint 回調(diào)函數(shù)的工作則由另外一個(gè)專門處理UI的線程來完成 如果我們希望等到paint 完成后再返回 那么可以在repaint 之后調(diào)用serviceRepaints serviceRepaints 的功能 用來強(qiáng)制隊(duì)列中的重繪事件快點(diǎn)做完 如果隊(duì)列中根本沒有重繪事件 那么serviceRepaints 什么事也不做 為了確保每個(gè)重繪事件盡快完成 在調(diào)用repaint 之后通常都會(huì)伴隨一個(gè)serviceRepaints 20 40 11 8 1Canvas類 showNotify 在Canvas對(duì)象顯示到屏幕上之前 AMS 應(yīng)用程序管理器 先調(diào)用showNotify Canvas類的showNotify 方法缺省實(shí)現(xiàn)為空 應(yīng)用程序可以重寫該方法 以完成畫布顯示前的初始化工作 hideNotify 方法在Canvas對(duì)象從顯示屏幕上刪除之后 AMS會(huì)自動(dòng)調(diào)用hideNotify Canvas類的hideNotify 方法缺省實(shí)現(xiàn)為空 應(yīng)用程序可以重寫該方法 在Canvas對(duì)象刪除后執(zhí)行一些清除工作 20 40 12 8 1Canvas類 熟悉低級(jí)用戶界面編程基本思路 importjavax microedition midlet importjavax microedition lcdui publicclassMyMIDletextendsMIDlet publicvoidstartApp Display getDisplay this setCurrent newMyCanvas publicvoidpauseApp publicvoiddestroyApp booleanun importjavax microedition lcdui publicclassMyCanvasextendsCanvas publicvoidshowNotify System out println showNotify 方法被調(diào)用 publicvoidhideNotify System out println hideNotify 方法被調(diào)用 publicvoidpaint Graphicsg g setColor 255 0 0 將畫筆設(shè)置為紅顏色g fillRect 0 0 getWidth getHeight 填充矩形g setColor 0 0 0 將畫筆設(shè)置為黑顏色g drawString CanvasTest 20 120 g BOTTOM g LEFT 在指定位置繪制文本 20 40 13 8 2圖形繪制與Graphics類 Graphics類 畫筆類 javax microedition lcdui Graphics類Canvas的實(shí)例對(duì)象代表一塊畫布 要在畫布上繪制圖形需要另一個(gè)重要的圖形類Graphics類 Graphics類是虛擬繪圖設(shè)備的抽象 類似一支畫筆 在Canvas實(shí)例中 Graphics對(duì)象只能在paint 方法中操作 在低級(jí)用戶界面API中 取得Graphics對(duì)象的方法有兩種 在Canvas類的paint 方法傳入的參數(shù)中獲取用Image類的getGraphics 方法獲取兩者的區(qū)別是用getGraphics 方法取得的Graphics對(duì)象不能在畫布上繪圖Graphics類為二維圖形繪制提供了豐富的方法 20 40 14 Graphics類的方法 8 2圖形繪制與Graphics類 20 40 15 Graphics類的方法 8 2圖形繪制與Graphics類 20 40 16 Graphics類的方法 8 2圖形繪制與Graphics類 20 40 17 Graphics類的方法 8 2圖形繪制與Graphics類 20 40 18 畫筆的顏色操作設(shè)置畫筆顏色兩種方法 publicvoidsetColor intred intgreen intblue publicvoidsetColor intRRGGBB 練習(xí)熟悉Graphics對(duì)象的顏色操作 8 2圖形繪制與Graphics類 20 40 19 8 2圖形繪制與Graphics類 坐標(biāo)系和像素在畫布上繪制圖形和文本 需要坐標(biāo)定位 在MIDP中繪圖 所使用的屏幕坐標(biāo) 注 由于手機(jī)屏幕包括標(biāo)題區(qū)域 內(nèi)容區(qū)域和按鈕區(qū)域等多個(gè)區(qū)域 因此實(shí)際繪制原點(diǎn)是指內(nèi)容區(qū)域的左上角點(diǎn) 20 40 20 8 2圖形繪制與Graphics類 像素 Pixel 像素是指在顯示器屏幕上按 行 與 列 排列的點(diǎn)陣元素 像素 是一個(gè)面積的概念 每個(gè)像素的面積大小由生產(chǎn)工藝決定 工藝越高 每個(gè)像素面積越小 每平方英寸屏幕上就能容納更多的像素 分辨率就越高 圖像就越精細(xì) 數(shù)字圖像是由許多像素構(gòu)成的 每個(gè)像素都包含顏色信息和位置信息 它們決定了圖像的形態(tài)與色彩 Canvas類代表的畫布被劃分成若干個(gè)虛擬柵格 每個(gè)柵格代表一個(gè)像素 20 40 21 8 2圖形繪制與Graphics類 圖形裁剪實(shí)現(xiàn)圖形裁剪的兩種方法 1 使用repaint方法2 使用專門設(shè)置剪切區(qū)的方法 publicvoidsetClip intx inty intwidth intheight 參數(shù)意義 x y為剪切區(qū)左上角的坐標(biāo) width為剪切區(qū)的寬度 height為剪切區(qū)的高度 注意 通過該方法設(shè)置的剪切區(qū) 在更新圖形時(shí) 畫圖就只對(duì)該剪切區(qū)起作用了 20 40 22 8 2圖形繪制與Graphics類 幾何圖形繪制繪制直線publicvoiddrawLine intx1 inty1 intx2 inty2 參數(shù)說明x1 y1 線段的起點(diǎn)坐標(biāo)x2 y2 線段的終點(diǎn)坐標(biāo)實(shí)例片段 在畫布上繪制 十 字線classMyCanvasextendsCanvas publicvoidpaint Graphicsg g drawLine 0 getHeight 2 getWidth getHeight 2 g drawLine getWidth 2 0 getWidth 2 getHeight 20 40 23 8 2圖形繪制與Graphics類 幾何圖形繪制填充三角形publicvoidfillTriangle intx1 inty1 intx2 inty2 intx3 inty3 參數(shù)說明x1 y1 x2 y2 x3 y3分別為三角形三個(gè)頂點(diǎn)的坐標(biāo)實(shí)例片段 在畫布的中央畫填充三角形classMyCanvasextendsCanvas publicvoidpaint Graphicsg intx getWidth 2 y getHeight 2 g fillTriangle x y 30 x 30 y 30 x 30 y 30 注 MIDP沒有提供繪制三角形的功能 20 40 24 8 2圖形繪制與Graphics類 幾何圖形繪制繪制直角矩形publicvoiddrawRect intx inty intwidth intheight 填充直角矩形publicvoidfillRect intx inty intwidth intheight 參數(shù)說明x y 矩形框左上角坐標(biāo)width height 矩形框的寬度和高度實(shí)例片段 填充與畫布尺寸相同的直角矩形classMyCanvasextendsCanvas publicvoidpaint Graphicsg g fillRect 0 0 getWidth getHeight 清屏 20 40 25 8 2圖形繪制與Graphics類 練習(xí)A在畫布中央繪制指定大小的直角矩形 畫游戲中的邊框 classMyCanvasextendsCanvas intboardWidth getWidth 24 intboardHeight getHeight 12 publicvoidpaint Graphicsg g setColor 0 0 0 g fillRect 0 0 getWidth getHeight 清屏g setColor 255 255 255 intboardX getWidth boardWidth 2 intboardY getHeight boardHeight 2 g drawRect boardX boardY boardWidth boardHeight 20 40 26 8 2圖形繪制與Graphics類 幾何圖形繪制繪制圓角矩形publicvoiddrawRoundRect intx inty intwidth intheight intarcWidth intarcHeight 填充圓角矩形publicvoidfillRoundRect intx inty intwidth intheight intarcWidth intarcHeight 參數(shù)說明x y 矩形框左上角坐標(biāo)width height 矩形框的寬度和高度arcWidth 圓角的水平直徑arcHeight 圓角的垂直直徑 20 40 27 8 2圖形繪制與Graphics類 RectTestCanvas畫布類importjavax microedition lcdui importjavax microedition midlet publicclassRectTestCanvasextendsCanvas publicvoidpaint Graphicsg clear g g setColor 255 0 0 g drawRect 5 5 100 20 g setColor 0 255 0 g fillRect 5 30 100 20 g setColor 0 0 255 g drawRoundRect 5 55 100 20 20 20 g setColor 255 0 255 g fillRoundRect 5 80 100 20 20 20 publicvoidclear Graphicsg 把屏幕清成白色g setColor 255 255 255 g fillRect 0 0 getWidth getHeight 20 40 28 8 2圖形繪制與Graphics類 幾何圖形繪制繪制弧形publicvoiddrawArc intx inty intwidth intheight intstartAngle intarcAngle 填充弧形publicvoidfillArc intx inty intwidth intheight intstartAngle intarcAngle 參數(shù)說明x y 創(chuàng)建弧的橢圓的邊界矩形的左上角坐標(biāo)width height 創(chuàng)建弧的橢圓的邊界矩形的寬度和高度startAngle 起始角度arcAngle 終止角度 是相對(duì)于起始角度的增量值 20 40 29 8 2圖形繪制與Graphics類 在邊框中央畫一個(gè)卡通形象classMyCanvasextendsCanvas intboardWidth getWidth 24 intboardHeight getHeight 12 intr 40 intdarc 22 在線程中改變這個(gè)值 嘴就會(huì)動(dòng)了intxc getWidth r 2 yc getHeight r 2 在線程中改變值 就能移動(dòng)了publicvoidpaint Graphicsg g setColor 0 0 0 g fillRect 0 0 getWidth getHeight 清屏g setColor 255 255 255 intboardX getWidth boardWidth 2 邊框左上角intboardY getHeight boardHeight 2 g drawRect boardX boardY boardWidth boardHeight g setColor 0 x00FFFF00 g fillArc xc yc r r 0 315 darc g setColor 0 x00000000 g fillArc xc 2 r 3 yc r 4 r 7 r 7 0 360 20 40 30 8 2圖形繪制與Graphics類 改變線條的樣式publicvoidsetStrokeStyle intstyle 參數(shù)說明style 只能取值Griphics SOLID或Griphics DOTTED兩種繪制模式實(shí)例片段 繪制實(shí)線與虛線classMyCanvasextendsCanvas publicvoidpaint Graphicsg g setColor 255 255 255 g fillRect 0 0 getWidth getHeight 清屏g setColor 255 0 0 g setStrokeStyle Graphics SOLID g drawLine 10 50 80 50 g setStrokeStyle Graphics DOTTED g drawLine 10 80 80 80 20 40 31 8 2圖形繪制與Graphics類 坐標(biāo)變換publicvoidtranslate intx inty 功能 將坐標(biāo)原點(diǎn)移到 X Y 的位置 具有積累效果 publicclassMyCanvasextendsCanvas publicvoidpaint Graphicsg for inti 0 i 2 i g drawLine 50 50 150 50 g translate 0 50 垂直向下移動(dòng)坐標(biāo) 20 40 32 8 3文字繪制與Font類 Graphics中文字繪制的相關(guān)方法繪制字符串的使用方法 publicvoiddrawString Stringstr intx inty intanchor publicvoiddrawSubstring Stringstr intoffset intlen intx inty intanchor 繪制字符的使用方法 publicvoiddrawChar charcharacter intx inty intanchor publicvoiddrawChars char data intoffset intlength intx inty intanchor 20 40 33 8 3文字繪制與Font類 錨點(diǎn)的定義共有7種 分別是Graphics TOP Graphics BOTTOM Graphics LEFT Graphics RIGHT Graphics HCENTER Graphics VCENTER 文本除外 Graphics BASELINE 20 40 34 8 3文字繪制與Font類 關(guān)于錨點(diǎn)的實(shí)例說明g drawString 游戲暫停 50 50 Graphic TOP Graphics LEFT g drawString 游戲暫停 50 50 Graphic BOTTOM Graphics RIGHT 20 40 35 8 3文字繪制與Font類 Font類獲取代表系統(tǒng)缺省所使用字體的Font對(duì)象 Font getDefaultFont 獲取代表特定字體的對(duì)象publicstaticFontgetFont intface intstyle intsize 參數(shù)意義 外觀 face 選項(xiàng) Font FACE MONOSPACE Font FACE PROPORTIONAL Font FACE SYSTEM 樣式 style 選項(xiàng) Font STYLE BOLD Font STYLE ITALIC Font STYLE PLAIN Font STYLE UNDERLINED 尺寸 size 選項(xiàng) Font SIZE SMALL Font SIZE MEDIUM Font SIZE LARGE 20 40 36 8 3文字繪制與Font類 獲取系統(tǒng)用來顯示輸入用的字體以及一般常用的字體 publicstaticFontgetFont intfontSpecifier getFont 只有一個(gè)參數(shù) 只有FONT INPUT TEXT和FONT STATIC TEXT兩種可供選擇 Graphics中設(shè)置字體的方法 publicvoidsetFont Fontfont 20 40 37 8 3文字繪制與Font類 在畫布上繪制文本classMyCanvasextendsCanvas intboardWidth getWidth 24 boardHeight getHeight 12 publicvoidpaint Graphicsg g setColor 0 0 0 g fillRect 0 0 getWidth getHeight 清屏g setColor 255 255 255 intboardX getWidth boardWidth 2 邊框左上角intboardY getHeight boardHeight 2 g drawRect boardX boardY boardWidth boardHeight g translate boardX 2 boardY 2 showInfo g 游戲暫停 privatevoidshowInfo Graphicsg Stringstr g setColor 0 x0080FFFF intww boardWidth 4 12 inthh ww 2 intxx boardWidth ww 2 yy boardHeight hh 2 g fillRect xx yy ww hh g setColor 0 x00FF0000 g setFont Font getFont Font FACE SYSTEM Font STYLE PLAIN Font SIZE LARGE g drawString str boardWidth 2 boardHeight 2 Graphics HCENTER Graphics BASELINE 20 40 38 8 4圖像繪制與Image類 繪制圖片publicvoiddrawImage Imageimg intx inty intanchor 參數(shù)說明x y 文本顯示坐標(biāo)位置img 顯示的圖片對(duì)象anchor 錨點(diǎn) 用于計(jì)算圖片放置的位置 打開圖片文件privateImageimg null try img Image createImage gameStart png catch IOExceptione Image類提供了多種創(chuàng)建圖像對(duì)象的方法 參見 javax microedition lcdui Image 20 40 39 8 4圖像繪制與Image類 創(chuàng)建Image對(duì)象在游戲中因?yàn)閮?nèi)存的限制 通常需要經(jīng)常載入圖片和釋放圖片 每次都要多寫一次trycatch語(yǔ)句很麻煩 為了方便 我們可以重寫createImage函數(shù) 根據(jù)png圖片名創(chuàng)建圖片publicImagecreateImage StringpngName Imageimage null try image Image createImage pngName png returnimage catch Exceptione returnimage 20 40 40 8 4圖像繪制與Image類 創(chuàng)建不變圖像從命名資源創(chuàng)建publicstaticImagecreateImage Stringname 從字節(jié)數(shù)組中創(chuàng)建publicstaticImagecreateImage byte imageData intoffset intlength 從其他圖像創(chuàng)建publicstaticImagecreateImage Imagesource 從像素?cái)?shù)據(jù)創(chuàng)建publicstaticImagecreateImage Imageimage intx inty intwidth intheight inttranasform 從ARGB值創(chuàng)建publicstaticImagecreateImage int rgb intwidth intheight booleanprocessAlpha 20 40 41 8 4圖像繪制與Image類 創(chuàng)建可變圖像創(chuàng)建publicstaticImagecreateImage intwidth intheight 檢測(cè)可變性publicbooleanisMutable 20 40 42 8 4圖像繪制與Image類 可變圖像與不可變圖像之間的轉(zhuǎn)換可變圖像轉(zhuǎn)換為不可變圖像ImagemImg Image createImage 120 120 mImg可變圖像ImageiImg Image createImage mImg iImg不可變圖像不可變圖像轉(zhuǎn)換為可變圖像 source為不可變圖像Imagesource Image createImage logo png copy為可變圖像Imagecopy Image createImage source getWidth source getHeight Graphicsgm copy getGraphics gm drawImage source 0 0 gm TOP gm LEFT 至此 copy圖像的內(nèi)容完全與源圖像一樣 并可以更改 20 40 43 8 4圖像繪制與Image類 創(chuàng)建動(dòng)態(tài)圖像 在圖像中繪一組直線importjava io IOException importjavax microedition lcdui Canvas importjavax microedition lcdui Graphics importjavax microedition lcdui Image publicclassMyCanvasextendsCanvas protectedvoidpaint Graphicsg Imageimage Image createImage this getWidth this getHeight 創(chuàng)建空動(dòng)態(tài)圖像Graphicsgh image getGraphics 得到圖像畫筆for inti 0 i this getWidth 15 i gh drawLine this getWidth 2 this getHeight i 15 0 畫直線 g drawImage image 0 0 Graphics TOP Graphics LEFT 把圖像繪到屏幕上 20 40 44 8 5Canvas事件處理 低級(jí) Canvas是個(gè)低級(jí)組件 為開發(fā)人員提供了完全的界面控制能力 并提供了鍵盤事件和觸摸屏事件等低級(jí)事件的響應(yīng)能力 設(shè)備支持哪些系統(tǒng)事件 必須由硬件的支持程度來判斷 Canvas提供了一些幫助我們判斷硬件支持程度的方法 20 40 45 8 5Canvas事件處理 低級(jí) 屏幕事件處理Canvas組件是個(gè)可顯示組件 具有兩種屏幕狀態(tài) 普通狀態(tài) 默認(rèn)狀態(tài) 全屏幕狀態(tài)利用setFullScreenMode 可以設(shè)定屏幕狀態(tài) 普通狀態(tài)全屏幕狀態(tài) 20 40 46 8 5Canvas事件處理 低級(jí) 編程示例 CanvasTest javaimportjavax microedition lcdui publicclassCanvasTestextendsCanvasimplementsCommandListener publicCanvasTest setTitle 全屏幕測(cè)試 setTicker newTicker 走馬燈 addCommand newCommand 全屏幕 Command SCREEN 1 addCommand newCommand 正常 Command SCREEN 1 setCommandListener this publicvoidpaint Graphicsg g setColor 125 125 125 g fillRect 0 0 getWidth getHeight g setColor 0 0 0 g drawLine 10 10 150 10 20 40 47 8 5Canvas事件處理 低級(jí) publicvoidcommandAction Commandc Displayables Stringcmd c getLabel if cmd equals 全屏幕 setFullScreenMode true elseif cmd equals 正常 setFullScreenMode false protectedvoidsizeChanged intw inth System out println 改變后的寬度 w System out println 改變后的高度 h protectedvoidhideNotify System out println 屏幕被系統(tǒng)遮蔽 protectedvoidshowNotify System out println 屏幕顯示在屏幕上 20 40 48 8 5Canvas事件處理 低級(jí) 鍵盤事件處理與鍵盤響應(yīng)當(dāng)Canvas的派生類成為當(dāng)前畫面時(shí) 只要按下任何按鈕 都會(huì)引發(fā)keyPressed 方法調(diào)用 當(dāng)放開按鈕時(shí) 會(huì)調(diào)用keyReleased 方法 如果傳入小于0的數(shù)值 通常代表不合法的keycode 按鈕按下時(shí)被調(diào)用 voidkeyPressed intkeyCode 按鈕連發(fā)時(shí)被調(diào)用 voidkeyRepeated intkeyCode 按鈕放開時(shí)被調(diào)用 voidkeyReleased intkeyCode keyCode的值如果小于0 說明按鍵不合法keyReapeated 方法JTWI并沒有做硬性規(guī)定 因此在編程時(shí)要用Canvas hasRepeatedEvents 方法進(jìn)行偵測(cè) JTWI規(guī)定MIDP2 0的目標(biāo)設(shè)備必須支持ITU T 國(guó)際電信聯(lián)盟 的電話鍵盤 即必須使用數(shù)字0到9 20 40 49 8 5Canvas事件處理 低級(jí) 理解鍵盤事件處理 為了在Canvas中處理按鍵事件 必須覆寫Canvas的keyPressed keyReleased 和keyReapeated 這三個(gè)方法 20 40 50 8 5Canvas事件處理 低級(jí) 在這三個(gè)函數(shù)中 都有一個(gè)整型參數(shù)keyCode 表示鍵盤上事件發(fā)生時(shí)按鍵所對(duì)應(yīng)的代碼值 如當(dāng)某個(gè)按鍵被按下時(shí) keyPressed方法的參數(shù)就變成那個(gè)鍵所對(duì)應(yīng)的鍵值 各個(gè)按鍵及其對(duì)應(yīng)的值如下表所示 20 40 51 8 5Canvas事件處理 低級(jí) 20 40 52 8 5Canvas事件處理 低級(jí) 常見的功能鍵 其值如下表所示 20 40 53 8 5Canvas事件處理 低級(jí) 編程示例importjavax microedition lcdui importjavax microedition midlet publicclassKeyEventTestCanvasextendsCanvas booleanpressed false publicvoidpaint Graphicsg g setColor 125 125 125 g fillRect 0 0 getWidth getHeight if pressed g setColor 0 0 0 g drawLine 20 20 120 20 g drawLine 20 20 20 100 g setColor 255 255 255 g drawLine 120 20 120 100 g drawLine 20 100 120 100 else g setColor 255 255 255 g drawLine 20 20 120 20 g drawLine 20 20 20 100 g setColor 0 0 0 g drawLine 120 20 120 100 g drawLine 20 100 120 100 protectedvoidkeyPressed intkeycode pressed true repaint protectedvoidkeyReleased intkeycode pressed false repaint 20 40 54 8 5Canvas事件處理 低級(jí) 鍵盤響應(yīng)Canvas類中定義了ITU T 國(guó)際電信聯(lián)盟 標(biāo)準(zhǔn)電話鍵盤上每個(gè)鍵的代碼常量 KEY NUM0 KEY NUM1 KEY NUM2 KEY NUM3 KEY NUM4 KEY NUM5 KEY NUM6 KEY NUM7 KEY NUM8 KEY NUM9 KEY STAR KEY POUND共12個(gè) 分別代表0 9的數(shù)字鍵 星號(hào)鍵以及井號(hào)鍵 可以利用此常量來判定事件處理函數(shù)所傳入的keyCode 借以了解哪個(gè)按鈕被按下 20 40 55 8 5Canvas事件處理 低級(jí) 游戲動(dòng)作Canvas類定義了幾個(gè)與游戲動(dòng)作相關(guān)的常量 UP DOWN LEFT RIGHT FIRE GAME A GAME B GAME C GAME D 因?yàn)橛辛艘粚映橄笮?在移植的時(shí)候比較方便 MIDP里提供兩種方法 讓程序在處理鍵盤時(shí)更有移植性 intgetGameAction intkeyCode intgetKeyCode intGameAction 20 40 56 8 5Canvas事件處理 低級(jí) getGameAction intkeyCode 該函數(shù)傳入keyCode 函數(shù)會(huì)回傳所代表的Game鍵盤代碼 其用法如下 publicvoidkeyPressed intkeyCode switch getGameAction keyCode caseCanvas LEFT moveLeft break caseCanvas FIRE goFire break 略 20 40 57 8 5Canvas事件處理 低級(jí) getKeyCode intGameAction 該函數(shù)傳入Game鍵盤代碼 函數(shù)會(huì)回傳所代表的keyCode 其用法如下 publicvoidkeyPressed intkeyCode if KeyCode getKeyCode Canvas LEFT moveLeft if KeyCode getKeyCode Canvas RIGHT moveRight if KeyCode getKeyCode Canvas UP moveUp if KeyCode getKeyCode Canvas DOWN moveDown if KeyCode getKeyCode Canvas FIRE goFire 20 40 58 8 5Canvas事件處理 低級(jí) 觸控屏幕事件處理protectedvoidpointerPressed intx inty 手寫筆在組件上按下時(shí)調(diào)用 參數(shù)x y手寫筆在當(dāng)前組件中的坐標(biāo) protectedvoidpointerReleased intx inty 手寫筆在組件上釋放時(shí)調(diào)用 參數(shù)x y手寫筆在當(dāng)前組件中的坐標(biāo) protectedvoidpointerDragged intx inty 手寫筆在組件上按下移動(dòng)時(shí)調(diào)用 參數(shù)x y手寫筆在當(dāng)前組件中的坐標(biāo) 使用Canvas類之中的hasPointerEvents 函數(shù)查詢系統(tǒng)是否支持觸控筆事件 使用Canvas類之中的hasPointerMotionEvents 函數(shù)詢問系統(tǒng)是否支持觸控筆拖曳事件 20 40 59 8 6動(dòng)畫實(shí)現(xiàn) 處理低級(jí)事件的運(yùn)行模式 20 40 60 8 6動(dòng)畫實(shí)現(xiàn) 假設(shè)我們希望系統(tǒng)可以同時(shí)響應(yīng)用戶的輸入 在屏幕上繪出十字標(biāo) 同時(shí)間又想在屏幕的右上角不斷地繪制一個(gè)會(huì)動(dòng)的圖形時(shí) 就會(huì)導(dǎo)致兩難的境地 1 想要不斷繪制一個(gè)會(huì)動(dòng)的圖形 paint 就要進(jìn)入一個(gè)無(wú)窮循環(huán) 2 想要接受用戶的輸入 keyPressed 和keyReleased 必須要能被調(diào)用 要完成我們的目的 就必須引入線程 20 40 61 8 6動(dòng)畫實(shí)現(xiàn) 引入線程后的Canvas運(yùn)行模式 20 40 62 8 6動(dòng)畫實(shí)現(xiàn) Canvas多線程編程 實(shí)現(xiàn)Runnable接口importjavax microedition lcdui importjavax microedition midlet publicclassMyGameextendsCanvasimplementsRunnable publicMyGame Threadt newThread this t start publicvoidpaint Graphicsg clear g paintAnimation g 100 10 r paintCross g x y length publicvoidclear Graphicsg 把屏幕清成白色g setColor 255 255 255 g fillRect 0 0 getWidth getHeight 20 40 63 8 6動(dòng)畫實(shí)現(xiàn) Canvas多線程編程 實(shí)現(xiàn)Runnable接口intr 0 publicvoidpaintAnimation Graphicsg intx inty intl g setColor 0 0 0 g drawRect x y l l intx 50 inty 50 intlength 5 publicvoidpaintCross Graphicsg intx inty intlength g setColor 255 0 0 g drawLine x length y x length y g drawLine x y length x y length publicvoidrun while true r if r 10 r 0 repaint 20 40 64 8 6動(dòng)畫實(shí)現(xiàn) Canvas多線程編程 實(shí)現(xiàn)Runnable接口protectedvoidkeyPressed intkeycode switch getGameAction keycode caseCanvas UP y y 2 break caseCanvas DOWN y y 2 break caseCanvas LEFT x x 2 break caseCanvas RIGHT x x 2 break 20 40 65 8 6動(dòng)畫實(shí)現(xiàn) Canvas多線程編程 實(shí)現(xiàn)Runnable接口 運(yùn)行結(jié)果 當(dāng)鍵盤交互時(shí) 十字架移動(dòng) 而右上方的方塊不斷變化 存在的問題 程序退出后 線程仍然在運(yùn)行 想一想改進(jìn)方法 20 40 66 8 6動(dòng)畫實(shí)現(xiàn) 線程的控制 使線程能夠結(jié)束J2ME的Thread類沒有提供結(jié)束線程的方法 當(dāng)需要終止線程運(yùn)行時(shí) 可以通過設(shè)置while的循環(huán)變量來進(jìn)行控制實(shí)現(xiàn) booleanconti true while conti 執(zhí)行線程 publicvoidstop conti false 20 40 67 8 6動(dòng)畫實(shí)現(xiàn) 改進(jìn)后程序importjavax microedition lcdui importjavax microedition midlet publicclassMyGameCanStopextendsCanvasimplementsRunnable CommandListener Commandstart newCommand 開始 Command OK 1 Commandstop newCommand 停止 Command STOP 1 publicMyGameCanStop addCommand start setCommandListener this publicvoidpaint Graphicsg clear g paintAnimation g 100 10 r paintCross g x y length publicvoidclear Graphicsg 把屏幕清成白色g setColor 255 255 255 g fillRect 0 0 getWidth getHeight 20 40 68 8 6動(dòng)畫實(shí)現(xiàn) 改進(jìn)后程序intr 0 publicvoidpaintAnimation Graphicsg intx inty intl g setColor 0 0 0 g drawRect x y l l intx 50 inty 50 intlength 5 publicvoidpaintCross Graphicsg intx inty intlength g setColor 255 0 0 g drawLine x length y x length y g drawLine x y length x y length 20 40 69 8 6動(dòng)畫實(shí)現(xiàn) 改進(jìn)后程序booleanconti true publicvoidcommandAction Commandc Displayables Stringcmd c getLabel if cmd equals 停止 conti false removeCommand stop addCommand start elseif cmd equals 開始 removeCommand start addCommand stop conti true Threadt newThread this t start 20 40 70 8 6動(dòng)畫實(shí)現(xiàn) 改進(jìn)后程序publicvoidrun while conti r if r 10 r 0 repaint 20 40 71 8 6動(dòng)畫實(shí)現(xiàn) 改進(jìn)后程序protectedvoidkeyPressed intkeycode switch getGameAction keycode caseCanvas UP y y 2 break caseCanvas DOWN y y 2 break caseCanvas LEFT x x 2 break caseCanvas RIGHT x x 2 break repaint 20 40 72 8 6動(dòng)畫實(shí)現(xiàn) 改進(jìn)后結(jié)果按開始命令conti true newThread this start 啟動(dòng)線程 按停止命令conti false 結(jié)束線程 存在的問題右上角的動(dòng)畫繪制速度隨著機(jī)器的速度變化 如果游戲的話 那就可能出現(xiàn)在有的機(jī)器上運(yùn)行快 有的機(jī)器運(yùn)行慢 想一想改進(jìn)方法 20 40 73 8 6動(dòng)畫實(shí)現(xiàn) 時(shí)間控制s System currentTimeMillis r if r 10 r 0 e System currentTimeMillis diff e s System out println diff if diff rate System out println Sleep rate diff try Thread sleep rate diff catch Exceptionexc 20 40 74 8 6動(dòng)畫實(shí)現(xiàn) 改進(jìn)的部分為intrate 50 每1 20秒畫一次publicvoidrun longs 0 longe 0 longdiff 0 while conti s System currentTimeMillis r if r 10 r 0 e System currentTimeMillis diff e s System out println diff if diff rate System out println Sleep rate diff try Thread sleep rate diff catch Exceptionexc repaint 20 40 75 8 7游戲設(shè)計(jì)中的基本方法 程序退出的方法兩種退出程序的方法 使用直接用戶輸入使用命令事件直接用戶輸入包括用戶按下MID的按鍵和移動(dòng)MID的指針 按下MID的按鍵使用方法 keyPressed keyRepeatedkeyReleased 移動(dòng)MID的指針使用方法 pointerPressed pointerDragged pointerReleased 20 40 76 8 7游戲設(shè)計(jì)中的基本方法 使用直接用戶輸入退出程序publicvoidexit destroyApp true notifyDestroyed protectedvoidkeyPressed intkeyCode exit 使用命令事件退出程序privateCommandexitCommand newCommand Exit Command SCREEN 1 publicvoidcommandAction Commandc Displayabled if c exitCommand exit 20 40 77 8 7游戲設(shè)計(jì)中的基本方法 清屏的方法在MIDP的低級(jí)用戶界面設(shè)計(jì)中 并沒有提供清屏功能 采用的方法 在屏幕上畫一個(gè)矩形將整個(gè)屏幕遮住 同時(shí)將矩形填充為屏幕的底色 例 使用白色繪制一個(gè)屏幕大小的矩形覆蓋整個(gè)屏幕 g setColor 255 255 255 g fillRect 0 0 getWidth getHeight

注意事項(xiàng)

本文(J2ME的低級(jí)用戶界面編程.ppt)為本站會(huì)員(max****ui)主動(dòng)上傳,裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。 若此文所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng)(點(diǎn)擊聯(lián)系客服),我們立即給予刪除!

溫馨提示:如果因?yàn)榫W(wǎng)速或其他原因下載失敗請(qǐng)重新下載,重復(fù)下載不扣分。




關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!