// ==================================================================== // iAppli PROGRAM BASE SOURCE BakuretuKen // サンプルソース(画面をくまさんが歩き回るiあぷり) // ==================================================================== import com.nttdocomo.ui.*; import com.nttdocomo.io.*; // ========================================== // ========== CanvasTestクラス ========== // ========================================== public class SampleiApp extends IApplication { public void start() { // MyCanvasクラスを画面に表示 MyCanvas c = new MyCanvas(); Display.setCurrent(c); } } // End of Class // ========================================== // ========== 自作キャンバスクラス ========== // ========================================== class MyCanvas extends Canvas { // === グローバル変数 === int x, y; Image img[]; char map[][]; boolean isK = true; // === コンストラクタ === MyCanvas() { x=20; y=20; img = new Image[5]; // GIF画像読込み for(int i=0; i<5; i++){ MediaImage mImg = MediaManager.getImage("resource:///"+i+".gif"); try { mImg.use(); } catch (com.nttdocomo.io.ConnectionException e) { System.out.println("Image Read Error !"); } img[i] = mImg.getImage(); } // マップ初期宣言 map = new char[50][50]; for(int xx=0; xx<50; xx++) for(int yy=0; yy<50; yy++) map[xx][yy]=3; for(int xx=5; xx<45; xx++) for(int yy=5; yy<45; yy++) map[xx][yy]=1; // ランダムで岩山を配置(200個) java.util.Random rnd; rnd = new java.util.Random() ; for(int xx=0; xx<200; xx++){ int r1 = rnd.nextInt(); if(r1<0) r1=-r1; int r2 = rnd.nextInt(); if(r2<0) r2=-r2; map[r1%40+5][r2%40+5]=2; } // ファンクションキー設定(Exit) setSoftLabel(Frame.SOFT_KEY_1, "Exit"); } // Constructor // === イベント処置 processEvent() === public void processEvent(int type, int param) { if (Display.KEY_RELEASED_EVENT == type) { // ファンクションキー(Exit)が押されたら終了 if (param == Display.KEY_SOFT1){ IApplication.getCurrentApp().terminate(); } // 移動前のくまの座標を ox、oy に保存 int ox=x, oy=y; // 方向キーが押されていたら、くまの座標を変更 if (param == Display.KEY_UP) y--; if (param == Display.KEY_DOWN) y++; if (param == Display.KEY_RIGHT) x++; if (param == Display.KEY_LEFT) x--; // もしも移動先が野原以外だったら、移動目の座標に戻す if(map[x][y]!=1){ x=ox; y=oy; return;} // 再作画 repaint(); } } // processEvent() // === 作画 paint() === public void paint(Graphics g) { // GCロック g.lock(); // マップ作画(くまの座標を中心に背景の作画) for(int _x=0; _x<5 ;_x++) { for(int _y=0; _y<5 ;_y++) { g.drawImage( img[map[_x+x-2][_y+y-2]], _x*20, _y*20); } } // くま作画 if(isK){ g.drawImage( img[0], 2*20, 2*20); } else { g.drawImage( img[4], 2*20, 2*20); } // くま状態変更 isK = !isK; // GCアンロック g.unlock(true); } // paint() } // End of Class