
import com.nttdocomo.ui.*;

public class TimerTest extends IApplication
{
	TestCanvas testCanvas;

    public void start()
	{
		testCanvas = new TestCanvas();
        Display.setCurrent(testCanvas);
		testCanvas.start();
    }
}


class TestCanvas extends Canvas
{
	//文字表示座標
	int x = 10;
	int y = 10;

	//キャンバスのサイズ
	int screenWidth  = getWidth();
	int screenHeight = getHeight();

	int count;

	//タイマー
	static int TIMER_ID = 0;
	ShortTimer timer;

	//コンストラクタ
	TestCanvas()
	{
	}
	
	//スタート
	public void start()
	{
		//0.1秒単位のショートタイマーをセットしてスタート
		timer = ShortTimer.getShortTimer(this, TIMER_ID, 1000/10, true);
		timer.start();
	}


	public void paint(Graphics g)
	{
		g.lock();	//ロック

		g.clearRect(0, 0, screenWidth, screenHeight);	//クリア
		g.drawString("count=" + count, x, y);

		g.unlock(true);	//アンロック
	}

	public void processEvent(int type, int param)
	{
		if( (type == Display.TIMER_EXPIRED_EVENT) && (param == TIMER_ID) )//タイマーイベント？
		{
			timerLoop();
		}
	}

	//タイマーイベントの処理
	public void timerLoop()
	{
		count += 1;

		//キーの押し下げ状態により移動
		int key = getKeypadState();
		if( (key & (1<<Display.KEY_DOWN  ) ) != 0 )	y += 4;
		if( (key & (1<<Display.KEY_UP    ) ) != 0 )	y += (-4);
		if( (key & (1<<Display.KEY_LEFT  ) ) != 0 )	x += (-4);
		if( (key & (1<<Display.KEY_RIGHT ) ) != 0 )	x += 4;

		repaint();
	}
}

