ホームに戻る
スレッドの使用
/*
* スレッドの使用
*/
import com.nttdocomo.ui.*;
public class thread extends IApplication
{
nmCanvas nmc;
public void start() {
nmc = new nmCanvas();
Display.setCurrent(nmc);
}
public class nmCanvas extends Canvas implements Runnable
{
String s = "abc";
Thread th;
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のタイマーを作成
th = new Thread(this);
th.start();
}
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 run()
{
for(;;){
// スレッドを100ms止める
try{
Thread.sleep(100);
}
catch(Exception e){}
// キーの状態を得る
int nKeyState = getKeypadState();
// ソフトキー1が押されていたら
if((nKeyState & 1 << Display.KEY_SOFT1) != 0){
position_x += 1;
}
this.repaint();
}
}
}
}
ソフトキー1を押し続けると
左から abc という文字列が0.1秒単位で右に動く