/* iアプリ, MIDP での不揮発性メモリへの記録の例 Time-stamp: "2005/12/08 Thu 19:45 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.*; import javax.microedition.rms.*; // レコード /* MIDP の TextBox は Form と同列みたいなもの */ /** IApplication または MIDlet を継承するクラス */ public class MemorySample extends MIDlet { /** 起動するときに呼ばれる メソッド. 必須. */ public void startApp(){ MemoryCanvas cc = new MemoryCanvas(this); Display.getDisplay(this).setCurrent(cc); } public void pauseApp(){} public void destroyApp(boolean unconditional){} } /** Canvas を継承するクラス. */ class MemoryCanvas extends Canvas implements CommandListener { MemorySample parent; Form p; private int rgb[] = {0,0,0}; // RGB 値 String tblabel [] = {"R","G","B"}; TextField tbparam[]; String cctitle [] = {"入力"}; String cftitle [] = {"色"}; Command [] cc; Command [] cf; String rsname="color"; /** コンストラクタ. 最初に1回だけ実行される */ MemoryCanvas(MemorySample m){ this.parent=m; RecordStore in=null; try { in = RecordStore.openRecordStore(rsname,true); RecordEnumeration re=in.enumerateRecords(null,null,true); while(re.hasNextElement()){ int id=re.nextRecordId(); int z=Integer.parseInt(new String(in.getRecord(id))); rgb[( z>>8 ) & 0x3 ]=z%256; } } catch (Exception e){ } finally { if( in!=null ){ try { in.closeRecordStore(); } catch ( Exception e){ } } } 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(rgb[0],rgb[1],rgb[2]); g.fillRect(0,0,getWidth(),getHeight()); // 画面を消す } /** Panel/Form を表示するメソッド*/ public void showPanel(){ p = new Form("色の入力"); // 数値入力用 TextBox tbparam = new TextField [tblabel.length]; for(int i=0; i < tbparam.length ; i++){ tbparam[i]= new TextField(tblabel[i],"",3,TextField.NUMERIC); p.append(tbparam[i]); tbparam[i].setString(""+rgb[i]); } /* 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); } // コマンドイベント public void commandAction(Command cx, Displayable s){ if( cx==cc[0] ){ // 値入力 showPanel(); } else if ( cx==cf[0] ){ // 図表示 for(int i=0; i< tbparam.length ; i++){ rgb[i]=Integer.parseInt(tbparam[i].getString()); } RecordStore out=null; try { out = RecordStore.openRecordStore(rsname,true); RecordEnumeration re=out.enumerateRecords(null,null,true); while(re.hasNextElement()){ int id=re.nextRecordId(); out.deleteRecord(id); } for(int i=0; i