J 2 MEタッチパネル処理


J 2 MEタッチ処理
 
一、MIDP Canvas類にはタッチ技術に関する類が入っていて、そのまま使えばいいのですが、簡単です.
 

java.lang.Object 

|_  javax.microedition.lcdui.Displayable 

      |  _  
javax.microedition.lcdui.Canvas 


 
二、相関関数:
1、判断類
hasPointerEventsを使用してデバイスがタッチをサポートしているかどうかを検出し、サポートされている場合はtrueを返します.
public boolean hasPointerEvents ()
Checks if the platform supports pointer press and release events.
Returns:
true if the device supports pointer events
 
hasPointerMotionEventsを使用してタッチ動作がサポートされているかどうかを検出するには、タッチスクリーンをドラッグし、サポートされている場合はtrueに戻ります.
public boolean hasPointerMotionEvents ()
Checks if the platform supports pointer motion events (pointer dragged). Applications may use this method to determine if the platform is capable of supporting motion events.
Returns:
true if the device supports pointer motion events
 
2,座標類(指やタッチペンなどタッチスクリーン上の位置座標)を取得する
protected void pointerPressed(int x,int y)/タッチポイントの座標
protected void pointerReleased(int x,int y)/タッチポイントの座標を解放
protected void pointerDragged(int x,int y)/タッチスクリーンでドラッグした座標は、指がタッチスクリーンに押されて位置を移動し、その座標が更新され続けます
 
三、どのように使うか
我々のUIクラスはCanvasクラスを直接継承すればよいが,これらの関数を実現し,タッチ座標を取得する
プログラムを処理させます.たとえば、次のようにします.
import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
public class CMyApp extends Canvas{
    //position for touch
public int point_x = 0;
public int point_y = 0;
 
private void mainLoop(){
    if(point_x<50 && point_y<50)  {
        System.out.println("You point on the left top of screen!");
}
}
 
 
protected void pointerPressed( int x, int y){
    point_x = x;
    point_y = y;
}
 
protected void pointerReleased( int x, int y){
    point_x = x;
    point_y = y;
}   
protected void pointerDragged( int x, int y){
    point_x = x;
    point_y = y;
}
 
}
 
 
転載は以下の情報を保持してください:作者(Author):smilelance時間(Time):2009.02出典(From):http://blog.csdn.net/smilelance