/* スレッドによるアニメーションと, タイマーイベントとの組み合わせ Time-stamp: "2007-11-15 Thu 14:18 JST hig" Saburo Higuchi 2003,2007 http://www.math.ryukoku.ac.jp/~hig/ プログラム解説等 http://www.a.math.ryukoku.ac.jp/~hig/course/juniors_2007/06/ */ //#define 1 //#define MIDP import com.nttdocomo.ui.*; import com.nttdocomo.util.*; // Timer を使う import java.util.Random; /** シングルスレッドのサンプル */ public class TimerEventSample extends IApplication { /** 起動するときに呼ばれる メソッド. 必須. */ public void start(){ TimerEventCanvas cc=new TimerEventCanvas(this); Display.setCurrent(cc); } } /** キャンバスを継承するクラス */ class TimerEventCanvas extends Canvas implements Runnable,TimerListener // timer のイベントをこのクラスの timerExpired で処理 { TimerEventSample parent; private int width; private int height; private Image [] im; private int mx; private int my; private int mt; private int ssize=5; private int s0x,s0y,s0dx; private int s1x,s1y,s1dx; Random r; private int t; // 0,1 で背景の色が変化 String imagefiles[]={"pose0.gif","pose1.gif"}; /** コンストラクタ. 最初に1回だけ実行される */ TimerEventCanvas(TimerEventSample m){ this.parent=m; width=getWidth(); height=getHeight(); /* 人を作る */ mx=width/2; my=height/2; mt=0; s0x=width/2; s0y=height/10; s0dx=2; s1x=width/2; s1y=height/4; s1dx=8; r = new Random( System.currentTimeMillis() ); // イメージの読み込み im=new Image[imagefiles.length]; MediaImage mi[]; mi=new MediaImage[imagefiles.length]; for(int i=0; i< imagefiles.length; i++){ mi[i]=MediaManager.getImage("resource:///" + imagefiles[i]); // アプリのダウンロード後に Web サーバからイメージをダウンロード // ADF 設定で UseNetwork: http // mi[i]=MediaManager.getImage("http://somehost/somepath/" + imagefiles[i]); } try{ for(int i=0; i< imagefiles.length; i++){ mi[i].use(); im[i]=mi[i].getImage(); } } catch (Exception e){ //"gif を読み込むことに失敗" } /* このクラスの run に制御を移す */ Thread th=new Thread(this); th.start(); Timer ti=new Timer(); // Timer を作る. ti.setTime(10000); // 10000ミリ秒=10秒で鳴るタイマー ti.setRepeat(true); // 繰り返し鳴るタイマー ti.setListener(this); // このクラス cc の timerExpired を使用. ti.start(); // カウントダウン開始. 一時停止は, ti.stop(); } public void run(){ while(true){ // 無限ループ // 決り文句. ループの回る速さを決める mt=(mt+1)%2; s0y=(s0y+1)%100; s1y=(s1y+1)%100; if((r.nextInt() & 0x4) == 0x4){ s0x=(s0x+s0dx)%100; } else { s0x=(s0x-s0dx+100)%100; } if((r.nextInt() & 0x4) == 0x4){ s1x=(s1x+s1dx)%100; } else { s1x=(s1x-s1dx+100)%100; } try { Thread.sleep(300); } catch ( Exception e ) { // 割り込みがあっても何もしない. } repaint(); } } public void paint(Graphics g){ if( t==0 ){ g.setColor(Graphics.getColorOfName(Graphics.WHITE)); } else { g.setColor(Graphics.getColorOfName(Graphics.BLUE)); } g.fillRect(0,0,width,height); // 画面を消す g.setColor(Graphics.getColorOfName(Graphics.BLACK)); g.fillRect(s0x,s0y,ssize,ssize); // 4角形を描く g.fillRect(s1x,s1y,ssize,ssize); // 4角形を描く g.drawImage(im[mt],mx,my +0); // 絵を貼る } public void changeColor(){ t=(t+1)%2; repaint(); } /** キーイベントを拾う */ public void processEvent(int type, int param){ if( type==Display.KEY_PRESSED_EVENT ){ switch( param ){ case Display.KEY_UP: my-=5; break; case Display.KEY_DOWN: my+=5; break; case Display.KEY_LEFT: mx-=5; break; case Display.KEY_RIGHT: mx+=5; break; default: } } } /** Timer が 0 になったとき呼ばれるメソッド implements TimerListner を宣言したらこれが必要 */ public void timerExpired(Timer source){ this.changeColor(); } } /* Local Variables: mode: java End: */