/* iアプリ, MIDP の Canvas クラスでのキーイベント処理の例 Time-stamp: "2004/10/13 Wed 20:04 hig" Saburo Higuchi 2004 http://www.math.ryukoku.ac.jp/~hig/ プログラム解説等 http://sparrow.math.ryukoku.ac.jp/~hig/course/juniors_2004/03/ */ #if DOJA import com.nttdocomo.ui.*; #elif MIDP import javax.microedition.midlet.*; import javax.microedition.lcdui.*; #endif #if DOJA #elif MIDP // DoJa/MIDP Builder を使われる方へ: // まず, プログラムの先頭で, #define DOJA // などと定義する必要があります. // 以下の define 文は, #if - #endif の中に書かれていますので, // DoJa/MIDP Builder のプリプロセッサでは処理できません. // そこで, #if - #endif はやめて, // いらない方をJava の意味でコメント文にしてください. #endif #if DOJA #define MYALIGN +0 #define MYAP IApplication #define MYSTART start #define MYBLACK Graphics.getColorOfName(Graphics.BLACK) #define MYWHITE Graphics.getColorOfName(Graphics.WHITE) #define MYBLUE Graphics.getColorOfName(Graphics.BLUE) #define MYRED Graphics.getColorOfName(Graphics.RED) #define MYGREEN Graphics.getColorOfName(Graphics.GREEN) #define MYYELLOW Graphics.getColorOfName(Graphics.YELLOW) #define MYMAGENTA Graphics.getColorOfName(Graphics.MAROON) #define MYCYAN Graphics.getColorOfName(Graphics.AQUA) #define MYDIALOG Dialog #define MYLABEL Label #define MYADD add #define MYTEXT Text #elif MIDP #define MYALIGN ,Graphics.LEFT|Graphics.BOTTOM #define MYAP MIDlet #define MYSTART startApp #define MYBLACK 0 #define MYWHITE ( (255<<16) + (255<<8) + (255)) #define MYBLUE ( (0<<16) + (0<<8) + (255)) #define MYRED ( (255<<16) + (0<<8) + (0)) #define MYGREEN ( (0<<16) + (255<<8) + (0)) #define MYYELLOW ( (255<<16) + (255<<8) + (0)) #define MYMAGENTA ( (255<<16) + (0<<8) + (255)) #define MYCYAN ( (0<<16) + (255<<8) + (255)) #define MYDIALOG Alert #define MYLABEL StringItem #define MYADD append #define MYTEXT String #endif /** Canvas の例(イベント処理あり) */ public class KeyEventSample extends MYAP { /** 起動するときに呼ばれる メソッド. 必須. */ public void MYSTART(){ MyKeyCanvas mc = new MyKeyCanvas(this); #if DOJA Display.setCurrent(mc); #elif MIDP Display.getDisplay(this).setCurrent(mc); #endif } #if MIDP /* 以下は, 空でも定義しておくことが必要 */ public void pauseApp(){} public void destroyApp(boolean unconditional){} #endif /* 自分で好きなだけメソッドを定義してよい */ } /** Canvas を用いたグラフィックス */ class MyKeyCanvas extends Canvas #ifdef MIDP implements CommandListener // このクラスでコマンドイベントを処理 #endif { /** コンストラクタ. 配列の初期化のみ行う */ public MyKeyCanvas(MYAP ap){ this.parent=ap; // これは樋口の癖. 必須ではない. #ifdef DOJA // ソフトキーのラベルを表示する. setSoftLabel(Frame.SOFT_KEY_1, "ソ1"); setSoftLabel(Frame.SOFT_KEY_2, "ソ2"); #else MIDP // コマンドキーを登録. 第1引数は画面表示ラベル, 最後の引数は優先順位 for(int i=0; i