import com.nttdocomo.ui.*; import com.nttdocomo.util.*; /** * sampleクラス */ public class sample extends IApplication { /** * アプリ起動の処理 */ public void start() { //最初はMainCanvasクラスをインスタンス化 Display.setCurrent((Frame)(new MainCanvas())); } } /** * MainCanvasクラス */ //TimerListenerを実装しないとタイマーイベントが活用できません class MainCanvas extends Canvas implements TimerListener{ int count; //Timerクラス こいつがないとタイマーイベントを使えません Timer tm = new Timer(); //これは今STARTが押されているときかSTOPが押されているときかを判断するやつ int flag = 0; /** * 一番最初に表示される文字列をセット */ String message = "☆ただのタイムウォッチ☆"; /** * 準備する処理 */ MainCanvas() { //画面左下のボタン setSoftLabel(SOFT_KEY_1, "END"); //画面右下のボタン setSoftLabel(SOFT_KEY_2, "START"); //背景色の設定 setBackground(Graphics.getColorOfName(Graphics.BLUE)); } /** * 描写をする処理 */ public void paint(Graphics g) { g.lock(); g.clearRect(0, 0, Display.getWidth(), Display.getHeight()); //色は白にセット! g.setColor(Graphics.getColorOfName(Graphics.WHITE)); //文字を書き込みます! g.drawString(message, Display.getWidth() / 4, Display.getHeight() / 2); g.unlock(true); } /** * ボタンが押されると呼ばれる処理 */ public void processEvent(int type, int param) { if (type == Display.KEY_RELEASED_EVENT) { //こっちのボタンが押されたら・・・ if (param == Display.KEY_SOFT1) { //アプリ終了 (IApplication.getCurrentApp()).terminate(); //こっちのボタンが押されたら・・・ }else if(param == Display.KEY_SOFT2){ //STARTかSTOPか判断してから・・・ if(flag == 0){ flag = 1; setSoftLabel(SOFT_KEY_2, "STOP"); count = 0; message = count + "秒"; repaint(); //1000ミリ秒(つまりは1秒)ごとに働いてね!という設定 tm.setTime(1000); tm.setRepeat(true); tm.setListener((TimerListener)this); //タイムカウント開始! tm.start(); }else{ //タイムカウント終了! tm.stop(); flag = 0; //ボタンの表記を元に戻してっと setSoftLabel(SOFT_KEY_2, "START"); } } } } /** * 1秒経つごとに行われる処理 */ public void timerExpired(Timer arg0) { //ひとーつ・・・ふたーつ・・・ count++; //秒という文字をくっつけまして message = count + "秒"; //再描写! repaint(); } }