首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2ME

J2ME 觸控螢幕開發:MIDP 2.0 事件處理與座標判斷

     編輯:J2ME

在 WTK 目錄下的 DefaultColorPhone.properties 中將 touch_screen=false 改為 true 即可啟用模擬器觸控模擬。

MIDP 2.0 三大觸控事件

  1. pointerDragged(int x, int y):拖曳事件
  2. pointerPressed(int x, int y):按下事件,座標為觸控點位置
  3. pointerReleased(int x, int y):釋放事件

實作範例:軟鍵與選單觸控判斷

protected void pointerPressed(int x, int y) {
    switch (status) {
        case Consts.S_MENU:
            int menuWidth = 90;
            int menuItemHeight = 17;
            int menuBarHeight = 16;
            int menuNum = 10;
            if (x < menuWidth && y > (screenHeight - (menuItemHeight * menuNum + menuBarHeight))) {
                int menuIndex = (y - (screenHeight - (menuItemHeight * menuNum + menuBarHeight))) / menuItemHeight;
                doMenuOK(menuIndex);
            }
            break;
            
        case Consts.S_DRAW_DIBIAO_LIST:
        case Consts.S_LOCAL_SEARCH_RESULT:
        case Consts.S_MAP_VIEW:
            // 左右軟鍵區域:左下 40x20、右下 40x20
            if (x < 40 && y > (screenHeight - 20)) {
                doCommandLeft();
            }
            if (x > (screenWidth - 40) && y > (screenHeight - 20)) {
                doCommandRight();
            }
            break;
    }
}

關鍵點:根據遊戲狀態(status)劃分不同互動區域,利用螢幕寬高(screenWidth/screenHeight)計算相對座標,實現虛擬按鍵與選單項目點擊判斷。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved