ホームに戻る
 タイマーの使用


/*
*   タイマーの使用
*/

import com.nttdocomo.ui.*;

public class timer extends IApplication
{
    nmCanvas nmc;

    public void start() {
        nmc = new nmCanvas();
        Display.setCurrent(nmc);
    }

    public class nmCanvas extends Canvas
    {
        String s = "abc";

        ShortTimer timer;
        int timer_id = 1;

        int screen_width;
        int screen_height;

        int position_x;
        int position_y;

        public nmCanvas()
        {
            // ソフトキーの左側を「開始」にする
            setSoftLabel(Frame.SOFT_KEY_1, "開始");
            // ソフトキーの右側を「終了」にする
            setSoftLabel(Frame.SOFT_KEY_2, "終了");

            // スクリーンの幅と高さを取得
            screen_width = getWidth();
            screen_height = getHeight();

            // 文字列の表示位置の初期化
            position_x = 0;
            position_y = screen_height / 2;

            // 解像度1000msのタイマーを作成
            // trueはインターバルタイマー falseでは1度きりのタイマーになる
            timer = ShortTimer.getShortTimer(this, timer_id, 1000, true);
        }

        public void paint(Graphics g)
        {
            // 描画を隠す
            g.lock();
            // 画面のクリア
            g.clearRect(0, 0, screen_width, screen_height);
            // 文字列の描画
            g.drawString(s, position_x, position_y);
            // 描画されたものを表示
            g.unlock(true);
        }

        public void processEvent(int nType, int nParm)
        {
            if(nType == Display.KEY_PRESSED_EVENT) {
                switch(nParm) {
                    case Display.KEY_SOFT1:
                        // タイマーの開始
                        timer.start();
                        break;
                    case Display.KEY_SOFT2:
                        // タイマーの終了
                        timer.stop();
			// 終了
                        IApplication.getCurrentApp().terminate();
                        break;
                }
            }
            if ((nType == Display.TIMER_EXPIRED_EVENT) && (nParm == timer_id)){
                position_x += 1;
            }
            this.repaint();
        }
    }
}


ソフトキー1でタイマーを開始
左から abc という文字列が1秒単位で右に動く

inserted by FC2 system