/* iアプリ, MIDP での Panel/Form の使用例 Time-stamp: "2005/12/08 Thu 19:51 hig" Saburo Higuchi 2003-2005 http://www.math.ryukoku.ac.jp/~hig/ プログラム解説等 http://www.a.math.ryukoku.ac.jp/~hig/course/juniors_2005/09/ */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /* MIDP の TextBox は Form と同列みたいなもの */ /** IApplication または MIDlet を継承するクラス */ public class PanelSample extends MIDlet { /** 起動するときに呼ばれる メソッド. 必須. */ public void startApp(){ PanelCanvas cc = new PanelCanvas(this); Display.getDisplay(this).setCurrent(cc); } public void pauseApp(){} public void destroyApp(boolean unconditional){} } /** Canvas を継承するクラス. */ class PanelCanvas extends Canvas implements CommandListener { PanelSample parent; Form p; private int x [] = {10,20,30,40}; // 四角形の x,y 座標と 幅 w 高さ h String tblabel [] = {"x","y","w","h"}; TextField tbparam[]; TextField tb1; // 出力用 TextField tb2; // 入力用 ChoiceGroup li1; // 選択 // 色の種類 int [] color={0, ( (0<<16) + (0<<8) + (255)), ( (255<<16) + (0<<8) + (0)), ( (0<<16) + (255<<8) + (0)), ( (255<<16) + (255<<8) + (0)), ( (255<<16) + (0<<8) + (255)), ( (0<<16) + (255<<8) + (255))}; String [] colname={"黒","青","赤","緑","黄","マゼンタ","シアン"}; int curcolor=0; // コマンドキー Command cc []; Command cf []; // コマンドキーラベル String cctitle [] = {"入力","ダミー"};// キャンバスでのコマンドキー String cftitle [] = {"図", "反映"}; // フォームでのコマンドキー /** コンストラクタ. 最初に1回だけ実行される */ PanelCanvas(PanelSample m){ this.parent=m; // ソフトキー, コマンドキーの設定 cc = new Command [ cctitle.length ]; for(int i=0; i< cctitle.length; i++){ cc[i]=new Command(cctitle[i],Command.SCREEN,i); addCommand(cc[i]); } setCommandListener(this); } public void paint(Graphics g){ // 画面を消す g.setColor(( (255<<16) + (255<<8) + (255))); g.fillRect(0,0,getWidth(),getHeight()); // 特定の色の長方形 g.setColor(color[curcolor]); g.fillRect(x[0],x[1],x[2],x[3]); } /** Panel/Form を表示するメソッド*/ public void showPanel(){ p = new Form("入力してね"); // StringItem を追加 StringItem lb1=new StringItem("ラベル","テキスト"); p.append(lb1); // Ticker を追加. 追加される場所が異なる. Ticker tc1=new Ticker("これがTickerの例です。右から左に文字列が流れていきます。"); p.setTicker(tc1); // 画像つきラベル // 画像ファイルの読み込みはコンストラクタでやっておいてもよい try{ Image img=Image.createImage("/pose0.png"); ImageItem il=new ImageItem("題名",img,ImageItem.LAYOUT_CENTER,"代用テキスト"); p.append(il); } catch (Exception e){ p.append(new StringItem("報告","読み込み失敗")); } // (表示用) TextBox を追加 tb1 = new TextField("イベント表示","",16*3,TextField.ANY); tb1.setString("イベント表示領域。これは最初に表示される内容。"); // 1 では, TextField は編集禁止にできない. // 編集禁止にしたいなら StringItem を使う. p.append(tb1); // (入力用) TextBox を追加 tb2 = new TextField("タイトル","",16*3,TextField.ANY); p.append(tb2); // 数値入力用 TextBox tbparam = new TextField [x.length]; for(int i=0; i < x.length ; i++){ tbparam[i]= new TextField(tblabel[i],"",3,TextField.NUMERIC); p.append(tbparam[i]); tbparam[i].setString(""+x[i]); } // ListBox を追加 li1 = new ChoiceGroup("色",Choice.EXCLUSIVE); for(int i=0; i< colname.length ; i++){ li1.append(colname[i],null); } li1.setSelectedIndex(curcolor,true); p.append(li1); // リスナーの登録 /* MIDP では component Listener はない.*/ /* MIDP ではコマンドキーの扱いは Canvas と同じ方法でよい.*/ cf = new Command [cftitle.length]; for(int i=0; i < cf.length; i++){ cf[i] = new Command(cftitle[i], Command.SCREEN,i); p.addCommand(cf[i]); } p.setCommandListener(this); // Panel/Form p に制御を移す Display.getDisplay(parent).setCurrent(p); } // Canvas/Form でのコマンドイベント public void commandAction(Command cx, Displayable s){ if( cx==cc[0] ){ // 値入力 showPanel(); } else if ( cx==cf[0] ){ // 図表示 for(int i=0; i< x.length ; i++){ x[i]=Integer.parseInt(tbparam[i].getString()); } curcolor=li1.getSelectedIndex(); Display.getDisplay(parent).setCurrent(this); } else if ( cx==cf[1] ){ curcolor=li1.getSelectedIndex(); tb1.setString( "色は" + colname[curcolor] + " " + "入力は" + tb2.getString()); } } protected synchronized void keyPressed(int keyCode){ } protected synchronized void keyReleased(int keycode){ } protected synchronized void keyRepeated(int keycode){ } } /* Local Variables: mode: java End: */