<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    騎豬闖天下

    J2ME隨筆,記錄成長的腳步

    統計

    留言簿(3)

    閱讀排行榜

    評論排行榜

    [J2ME]觸摸屏功能實現代碼一


    觸摸屏功能的實現大致來說要在三處添加代碼:
    1.在Screen類中重寫觸摸功能;
    2.建立一個觸摸屏對象,使要實現觸摸功能的控件以對象的方式保存在Vector中;
    3.在獲取頁面時,把要實現的觸摸控件保存在Vector中;

    以項目中的源碼為例,這三個部分的代碼如下:

    1.Screen類:
    (本源碼最后兩個方法為:字符占屏幕寬度及高度的計算方法)

    /**
     * Data:2010.02.25
     * Time:15:18:20
     
    */

    package com.xuudoo.ewl;

    import
     java.util.Vector;

    import
     javax.microedition.lcdui.Canvas;
    import
     javax.microedition.lcdui.Command;
    import
     javax.microedition.lcdui.CommandListener;
    import
     javax.microedition.lcdui.Display;
    import
     javax.microedition.lcdui.Displayable;
    import
     javax.microedition.lcdui.Font;
    import
     javax.microedition.lcdui.Graphics;
    import
     javax.microedition.lcdui.Image;
    import
     javax.microedition.midlet.MIDlet;

    import
     com.xuudoo.booking.BizWindow;
    import
     com.xuudoo.booking.BookingMidlet;
    import
     com.xuudoo.booking.BusinessWindow;
    import
     com.xuudoo.booking.FirstPage;
    import
     com.xuudoo.booking.IOManager;
    import
     com.xuudoo.booking.UIManager;

    public class EWLScreen extends Canvas // FullCanvas

            implements CommandListener {

        
    public static final int SCREEN_WIDTH =  360;//屏寬

        public static final int SCREEN_HEIGHT =  640;//屏高
        
        
    public static final int KEY_CODE_LEFT_SOFT_KEY = -6;//左軟鍵

        public static final int KEY_CODE_RIGHT_SOFT_KEY = -7;//右軟鍵
        
        
    public static EWLScreen instance = new
     EWLScreen();
        
    public static
     Display display;
        
    protected
     EWLWindow activeWindow;
        
    protected
     EWLWindow popupWindow;
        
        
    public static Image splashImg = null;//初始化圖片

        private static String splashImgPath = "/LogoXD.png";//初始化圖片地址
        public static Image main_bg = null;//背景圖片
        private static String main_bgPath = "/bg.png";//背景圖片地址
        public static boolean connectAgain = false;//用cmnet聯網失敗后再連接
        public static boolean connectPrompt = true;//第一此聯網詢問時是否連接網絡
        public static boolean phoneNoMoney = false;//手機欠費標志位
        
        
    public EWLScreen() 
    {
            setFullScreenMode(
    true
    );
        }


        
    public static void init(MIDlet midlet) {
            display 
    =
     Display.getDisplay(midlet);
            instance.iniLogoPic();
        }


        
    public int getHeight() {
            
    return
     SCREEN_HEIGHT;
        }


        
    public int getWidth() {
            
    return
     SCREEN_WIDTH;
        }


        
    /**
         * 第一次登陸,畫Logo登錄 圖片
         
    */

        
    public void iniLogoPic() {
            
    //圖片資源

            try{
                splashImg 
    =
     Image.createImage(splashImgPath);
                main_bg 
    =
     Image.createImage(main_bgPath);
            }
    catch(Exception e)
            
    {
                
    if
    (BookingMidlet.DEBUG)
                    e.printStackTrace();
            }

        }

        
        
    //調試時顯示信息之用
        public static String prompt1 = null;
        
    public static String prompt2 = null
    ;
        
    public static String prompt3 = null
    ;
        
    protected void paint(Graphics g) 
    {
            
    //登陸時的提示界面

            if(UIManager.state == UIManager.STATE_LOGIN){
                
    if(splashImg != null
    )
    //
                    g.drawImage(splashImg, (EWLScreen.SCREEN_WIDTH - splashImg.getWidth()) / 2,
    //                        (EWLScreen.SCREEN_HEIGHT - splashImg.getHeight()) / 2, 20);

                    g.drawImage(splashImg, 0,020);
                drawConnectPrompt(g);
    //畫提示信息

                String leftSoftStr="登陸";
                
    if
    (connectAgain)
                    leftSoftStr
    =null
    ;
                
    this.drawSoftKeys(g, leftSoftStr, "退出"0xff0000);//畫提示界面左右軟鍵

                return;
            }
    else{
    //
                if(main_bg != null)
    //                g.drawImage(main_bg,0,0,20);

                if (activeWindow != null){
                    activeWindow.paint(g);
                }

                
    if (popupWindow != null){
                    popupWindow.paint(g);
                }

            }

            
    //-----------------------調試信息-----------------------------
    //
                g.setColor(0xff0000);
    //
                if(prompt1 != null )
    //
                    g.drawString(prompt1, 10, 260, 0);
    //
                if(prompt2 != null )
    //
                    g.drawString(prompt2, 10, 280, 0);
    //
                if(prompt3 != null )
    //                g.drawString(prompt3, 10, 240, 0);

        }


        
    /**
         * 畫聯網提示,并兼容原來系統 的字體和顏色
         
    */

        
    private void drawConnectPrompt(Graphics g){
            
    int nowColor =
     g.getColor();
            Font nowFont 
    =
     g.getFont();
            g.setColor(
    0x000000
    );
            g.setFont(UIManager.my_smallFont);
            
            String httpConMode
    =""
    ;
            String promptStr1 
    =""
    ;
            String promptStr2 
    =""
    ;
            
    if(BookingMidlet.useProxy)
    {
                promptStr1 
    ="友情提示:請選用移動夢網"
    ;
                promptStr2 
    ="連接方式。"
    ;
                httpConMode
    ="移動夢網"
    ;
            }
    else{
                promptStr1 
    ="友情提示:請選用CMNET"
    ;
                promptStr2 
    ="或WLAN連接方式。"
    ;
                httpConMode
    ="CMNET或WLAN"
    ;
            }

            
            
    if(connectAgain){
                promptStr1
    ="友情提示:請重新登錄"
    ;
                
    if(!connectPrompt)
    {
                    promptStr2
    ="并同意使用網絡"
    ;
                }
    else{
                    promptStr2
    ="選用"+
    httpConMode;
                    
    if
    (phoneNoMoney)
                        promptStr2
    ="確認網絡信號或手機話費"
    ;
                }

            }

            
    int promptStr_X= (EWLScreen.SCREEN_WIDTH-getStringWidth(g.getFont(),promptStr1))/2;
            
    int promptStr_Y= EWLScreen.SCREEN_HEIGHT*3/5
    ;
            g.drawString(promptStr1, promptStr_X, promptStr_Y, 
    0
    );
            g.drawString(promptStr2, promptStr_X, promptStr_Y
    +g.getFont().getHeight(), 0
    );
            g.setColor(nowColor);
            g.setFont(nowFont);
        }

        
    /**
         * 繪制左右軟鍵
         *
         * 
    @param lSoftKeyName 左軟鍵名稱 如果null則不繪制
         * 
    @param
     rSoftKeyName 右軟鍵名稱 如果null則不繪制
         * 
    @param
     color        繪制軟鍵的顏色
         * 
         
    */

        
    private void drawSoftKeys(Graphics g, String lSoftKeyName,
                String rSoftKeyName, 
    int
     color)
        
    {
            
    if (lSoftKeyName != null
    )
            
    {// 左軟鍵
                int x = FirstPage.LRSoft_LeftAndTopPosition[0][0]+ FirstPage.LRSoft_length_high[0]/2;
                
    int y = FirstPage.LRSoft_LeftAndTopPosition[0][1]+ FirstPage.LRSoft_length_high[1]/2
    ;
    //            g.setClip(x, y, getStringWidth(g.getFont(), lSoftKeyName), g.getFont().getHeight());

                g.drawString( lSoftKeyName, x, y, Graphics.HCENTER|Graphics.BASELINE);
            }

            
    if (rSoftKeyName != null)
            
    {// 右軟鍵
                int x = FirstPage.LRSoft_LeftAndTopPosition[1][0]+ FirstPage.LRSoft_length_high[0]/2;
                
    int y = FirstPage.LRSoft_LeftAndTopPosition[1][1]+ FirstPage.LRSoft_length_high[1]/2
    ;
    //            g.setClip(x, y, getStringWidth(g.getFont(), rSoftKeyName), g.getFont().getHeight());

                g.drawString(rSoftKeyName, x, y, Graphics.HCENTER|Graphics.BASELINE);
            }

            
        }

        
        
        
    /**
         * 初始化提示界面時的交互處理
         * 
    @param keyCode
         
    */

        
    private void stateLoginKeyPressed(int keyCode){
            
    int gameCode = 0
    ;
            
    try
    {
                gameCode 
    = this
    .getGameAction(keyCode);
            }
    catch(Exception e){
                System.out.println(
    "get gameaction key error:"+
    keyCode);
            }

            
    if(gameCode==Canvas.UP || gameCode==Canvas.LEFT
                    
    || gameCode==Canvas.RIGHT || gameCode==
    Canvas.DOWN
                    
    || gameCode==
    Canvas.FIRE)
            
    {
                ;
            }

            
    else if (keyCode == EWLScreen.KEY_CODE_LEFT_SOFT_KEY
                    
    || keyCode == EWLScreen.KEY_CODE_RIGHT_SOFT_KEY) 
    {
                gameCode 
    =
     keyCode;
            }

            
    else if(keyCode==21||keyCode==-21)
                gameCode 
    =
     EWLScreen.KEY_CODE_LEFT_SOFT_KEY;
            
    else if(keyCode==22||keyCode==-22
    )
                gameCode 
    =
     EWLScreen.KEY_CODE_RIGHT_SOFT_KEY;
            
    else

                
    return;
            
            
    switch (gameCode) 
    {
                
    case
     Canvas.FIRE:
                
    case EWLScreen.KEY_CODE_LEFT_SOFT_KEY://
     左軟鍵
                    
    //向服務器驗證身份,打開首頁面

                    UIManager.state=UIManager.STATE_LOGIN_httpConnecting;
                    BizWindow bw 
    = new
     BizWindow();
                    bw.commitUserAuth();
                    
    //在用戶向服務器發起請求返回成功后再顯示首頁面。

                    break;
                
    case EWLScreen.KEY_CODE_RIGHT_SOFT_KEY://
     右軟鍵
                    
    //退出。

                    UIManager.state=UIManager.STATE_DEFAULE;
                    UIManager._flag 
    = 0
    ;
                    UIManager._instance.doLoop();
                    
    break
    ;
            }

        }

        
        
    protected void keyPressed(int keyCode) {
            
    //處理初始化時的提示界面

            if(UIManager.state == UIManager.STATE_LOGIN){
                
    if(!connectAgain)
    {
                    stateLoginKeyPressed(keyCode);
                }
    else
    {
                    
    if(keyCode == -7 || keyCode == -22 || keyCode == 22
    )
                        UIManager._flag 
    = 0
    ;
                        UIManager._instance.doLoop();
                        
    return
    ;
                }

            }

            
    else if(UIManager.state == UIManager.STATE_LOGIN_httpConnecting){
                
    //只對返回鍵做處理

                if(keyCode == -7 || keyCode == -22 || keyCode == 22){
                    connectAgain 
    = true
    ;
                    connectPrompt 
    = true
    ;
                    UIManager.state
    =
    UIManager.STATE_LOGIN;
                    hidePopup();
                }

            }
    else if(UIManager.state == UIManager.STATE_DEFAULE){
                
    try 
    {
                    
    if (popupWindow != null
    )
                        popupWindow.keyPressed(keyCode);
                    
    else if (activeWindow != null
    )
                        activeWindow.keyPressed(keyCode);
                }
     catch (Throwable e) {
                    e.printStackTrace();
                }

            }

        }


        
    protected void keyReleased(int keyCode) {
            
    if(UIManager.state == UIManager.STATE_LOGIN ||

               UIManager.state 
    == UIManager.STATE_LOGIN_httpConnecting ){
                
    return
    ;
            }

            
    try {
                
    if (popupWindow != null
    )
                    popupWindow.keyReleased(keyCode);
                
    else if (activeWindow != null
    )
                    activeWindow.keyReleased(keyCode);
            }
     catch (Throwable e) {
                e.printStackTrace();
            }

        }


        
    protected void keyRepeated(int keyCode) {
            
    if(UIManager.state == UIManager.STATE_LOGIN ||

               UIManager.state 
    == UIManager.STATE_LOGIN_httpConnecting ){
                
    return
    ;
            }

            
    try {
                
    if (popupWindow != null
    )
                    popupWindow.keyRepeated(keyCode);
                
    else if (activeWindow != null
    )
                    activeWindow.keyRepeated(keyCode);
            }
     catch (Throwable e) {
                e.printStackTrace();
            }

        }


        
    /**
         * 筆觸 點擊
         * 
    @param a_x: x 坐標
         * 
    @param
     a_y: y 坐標
         
    */

        
    protected void pointerPressed(int a_x,int a_y)
        
    {
            
    //對九宮格界面處理

            if(UIManager._flag == 20){
                
    boolean clickFocus =
     getFocusFromPointer( a_x, a_y);
                
    if(clickFocus)
    {
                    keyReleased(
    -5
    );
                    
    return
    ;
                }
    else{
                    repaint();
                }

    //            return;
            }

            
    //對左右軟件處理
            String clickLRSoft = getFocusFromPointer_LRSoft( a_x, a_y);
            
    if(!clickLRSoft.equals(""))
    {
                
    if(clickLRSoft.equals("LEFTSOFT"))
    {
                    keyPressed(
    -6
    );
                    keyReleased(
    -6
    );
                }

                
    else if(clickLRSoft.equals("RIGHTSOFT")){
                    keyPressed(
    -7
    );;
                    keyReleased(
    -7
    );
                }

                
    return;
            }

            
    //對觸摸屏對象處理
            if(UIManager._instance.i_clipArea != null){
                
    boolean browseClicpFocus =
     browseClip(a_x, a_y);
                
    if(browseClicpFocus)
    {
                    keyPressed(
    -5
    );;
                    keyReleased(
    -5
    );
                }
    else{
                    repaint();
                }

            }

        }

        
        
    protected void pointerReleased(int x, int y)
        
    {
            
    //在pointerPressed中處理

        }

        
        
    /**
         * 筆觸 拖拉
         * x : x 坐標
         * y : y 坐標
         
    */

        
    protected void pointerDragged(int x,int y)
        
    {
            
    //暫未實現

        }

        
        
    /**
         * 列表界面的觸摸屏判斷
         * 
    @param a_x
         * 
    @param
     a_y
         * 
    @return

         
    */

        
    protected boolean browseClip(int a_x, int a_y){
            
    boolean clickAreaIsFocus = false
    ;
            Vector i_clipArea 
    =
     UIManager._instance.i_clipArea;
            
    if(i_clipArea==null
    )
                
    return false
    ;
            ClipRegion a_clipRegion 
    = null
    ;
            
    for (int i = 0; i < i_clipArea.size(); i++
    {
                a_clipRegion 
    =
     (ClipRegion) i_clipArea.elementAt(i);
                
    boolean findClipArea =
     a_clipRegion.inClipRegion(a_x, a_y);
                
    if(findClipArea)
    {//點擊區域
                    clickAreaIsFocus = activeWindow.pointerPressSetFocus(a_x ,i);
                    
    break
    ;
                }

            }

            i_clipArea
    =null;
            
    return
     clickAreaIsFocus;
        }

        
        
        
    /**
         * 通過x,y坐標,獲得筆觸的焦點值
         * 
    @param a_x:筆觸的x
         * 
    @param
     a_y:筆觸的y
         * 
    @param
     focus_X:筆觸所在九宮格的起始x
         * 
    @param
     focus_Y:筆觸所在九宮格的起始y
         * 
    @return

         
    */

        
    protected boolean getFocusFromPointer(int a_x, int a_y){
            
    int focus_X = 0
    ;
            
    int focus_Y = 0
    ;
            
    boolean clickAreaIsFocus = false
    ;
            
    for(int i=0; i<9; i++
    )
            
    {
                focus_X 
    = FirstPage.itemsPosition[i][0
    ];
                focus_Y 
    = FirstPage.itemsPosition[i][1
    ];
                
                
    if(focus_X <= a_x &&

                   focus_Y 
    <= a_y &&
                   focus_X 
    + FirstPage.VSize >= a_x &&
                   focus_Y 
    + FirstPage.HSize >= a_y)
                
    {//筆觸所在的九宮格
                    if(i==FirstPage.currentItem)
                        clickAreaIsFocus 
    = true
    ;
                    FirstPage.currentItem 
    =
     i;
                    
    break
    ;                
                }

            }

            
    return clickAreaIsFocus;
            
        }

        
        
    /**
         * 通過x,y坐標,獲得筆觸的焦點值
         * 
    @param a_x:筆觸的x
         * 
    @param
     a_y:筆觸的y
         * 
    @param
     focus_X:左、右軟件的起始x
         * 
    @param
     focus_Y:左、右軟件的起始y
         * 
    @return

         
    */

        
    protected String getFocusFromPointer_LRSoft(int a_x, int a_y){
            
    int focus_X = 0
    ;
            
    int focus_Y = 0
    ;
            String pointSoftKey
    =""
    ;
            
    for(int i=0; i<2; i++
    )
            
    {
                focus_X 
    = FirstPage.LRSoft_LeftAndTopPosition[i][0
    ];
                focus_Y 
    = FirstPage.LRSoft_LeftAndTopPosition[i][1
    ];
                
                
    if(focus_X <= a_x &&

                   focus_Y 
    <= a_y &&
                   focus_X 
    + FirstPage.LRSoft_length_high[0>= a_x &&
                   focus_Y 
    + FirstPage.LRSoft_length_high[1>= a_y)
                
    {//筆觸所在的九宮格
                    if(i==0)
                        pointSoftKey
    ="LEFTSOFT"
    ;
                    
    else

                        pointSoftKey
    ="RIGHTSOFT";
                    
    break
    ;                
                }

            }

            
    return pointSoftKey;
            
        }

        
    public void commandAction(Command c, Displayable d) {
            
    try 
    {
                
    if (d == this
    )
                    
    if (instance.popupWindow != null
    )
                        instance.popupWindow.onCommand(c);
                    
    else if (instance.activeWindow != null
    )
                        instance.activeWindow.onCommand(c);
            }
     catch (Throwable e) {
                e.printStackTrace();
            }

        }


        
    public static EWLWindow getTopWindow() {
            
    if (instance.popupWindow != null
    )
                
    return
     instance.popupWindow;
            
    else

                
    return instance.activeWindow;
        }


        
    public static EWLWindow getCurrent() {
            
    return
     instance.activeWindow;
        }


        
    public static void setCurrent(EWLWindow window) {
            instance.activeWindow 
    =
     window;
            instance.repaint();
        }


        
    public static EWLWindow getPopup() {
            
    return
     instance.popupWindow;
        }


        
    public static void popup(EWLWindow window) {
            instance.popupWindow 
    =
     window;
            instance.repaint();
        }


        
    public static void hidePopup() {
            
    if (instance.popupWindow instanceof
     BusinessWindow)
                IOManager._interrupt 
    = true
    ;
            instance.popupWindow 
    = null
    ;
            instance.repaint();
        }


        
    public void clearScreen() {
            repaint();
            serviceRepaints();
        }


        
    /***************************************************************************
         * 獲取String屏幕寬度操作 開始 ***********************************************
         *************************************************************************
    */

        
    private static byte[] i_smallWidth = null;
        
    private static byte[] i_mediumWidth = null
    ;
        
    private static byte[] i_largeWidth = null
    ;
        
    public static byte getCharWidth(Font a_font, char
     a_c)
        
    {
            initFontWidth();
            
    int type =
     a_font.getSize();
            
    byte
    [] widthType;
            
    if(type ==
     Font.SIZE_SMALL)
                widthType 
    =
     i_smallWidth;
            
    else if(type ==
     Font.SIZE_MEDIUM)
                widthType 
    =
     i_mediumWidth;
            
    else if(type ==
     Font.SIZE_LARGE)
                widthType 
    =
     i_largeWidth;
            
    else

                widthType 
    = i_smallWidth;
            
    if(a_c<0 || a_c>=128
    )
                
    return widthType[128
    ];
            
    else

            
    {
                
    return
     widthType[a_c];
            }

        }

        
        
    /**
         * 獲取字符的寬度
         * 
    @param a_font
         * 
    @param
     a_str
         * 
    @return

         
    */

        
    public static int getStringWidth(Font a_font, String a_str)
        
    {
            
    if(a_str ==null
    )
                
    return 0
    ;
            
    int width = 0
    ;
            
    char
     currChar;
            
    for(int i=0;i<a_str.length();i++
    )
            
    {
                currChar 
    =
     a_str.charAt(i);
                width 
    +=
     getCharWidth(a_font, currChar);
            }

            
    return width;
        }

        
    private static void initFontWidth()
        
    {
            
    if(i_smallWidth!=null
    )
                
    return
    ;

            i_smallWidth 
    = new byte[129
    ];
            i_mediumWidth 
    = new byte[129
    ];
            i_largeWidth 
    = new byte[129
    ];

            Font i_fontSmall 
    =
     Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
            Font i_fontMedium 
    =
     Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
            Font i_fontLarge 
    =
     Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
            
    for(int i=0;i<128;i++
    )
            
    {
                i_smallWidth[i] 
    = (byte)i_fontSmall.charWidth( ((char
    )i) );
                i_mediumWidth[i] 
    = (byte)i_fontMedium.charWidth( ((char
    )i) );
                i_largeWidth[i] 
    = (byte)i_fontLarge.charWidth( ((char
    )i) );
            }


            i_smallWidth[
    128= (byte)i_fontSmall.charWidth( '' );
            i_mediumWidth[
    128= (byte)i_fontMedium.charWidth( ''
     );
            i_largeWidth[
    128= (byte)i_fontLarge.charWidth( ''
     );
        }

        
    /***************************************************************************
         * 獲取String屏幕寬度操作結束************************************************
         *************************************************************************
    */

    }

    posted on 2010-02-25 15:23 騎豬闖天下 閱讀(804) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 西西人体44rt高清亚洲 | 中国国产高清免费av片| 国产精品一区二区三区免费| 中文字幕永久免费| 99re免费99re在线视频手机版| 成年人免费的视频| 国产又大又长又粗又硬的免费视频| 国产成人在线观看免费网站| 国产亚洲精品激情都市| 国产成人精品久久免费动漫| 四虎成人精品一区二区免费网站| 亚洲欧洲国产成人综合在线观看| 国产亚洲一区二区精品| 亚洲一级片在线观看| 曰批全过程免费视频免费看| 大地资源网高清在线观看免费| 国产91色综合久久免费分享| 亚洲AV无码一区二三区 | 中文字幕久久亚洲一区 | 日韩精品无码永久免费网站| 久久久国产精品福利免费| 青青久在线视频免费观看| 亚洲精品无码专区2| 亚洲最大中文字幕| 黄床大片30分钟免费看| 一级毛片在线免费观看| 日产乱码一卡二卡三免费| 亚洲第一极品精品无码久久 | 久久99国产亚洲精品观看| 亚洲色大成网站www永久男同 | 18禁无遮挡无码国产免费网站| 日韩一品在线播放视频一品免费| 亚洲精品夜夜夜妓女网| 亚洲一线产区二线产区区| A级毛片高清免费视频在线播放| 最近的中文字幕大全免费版| 亚洲小说区图片区另类春色| 亚洲欧美成人一区二区三区| 精品免费tv久久久久久久| 国产青草视频在线观看免费影院| 亚洲avav天堂av在线不卡|