import com.nttdocomo.io.ConnectionException; import com.nttdocomo.ui.*; import com.nttdocomo.util.Timer; import com.nttdocomo.util.TimerListener; public class KitchenTimer extends IApplication implements ComponentListener,SoftKeyListener,TimerListener { private Panel panel; private TextBox time; private Button start; private Label message; private AudioPresenter sound; private MediaSound mediaSound; private Timer timer; // 初期化 public KitchenTimer() { time=new TextBox("",5,1,TextBox.DISPLAY_ANY); time.setInputMode(TextBox.NUMBER); start=new Button("開始"); message=new Label(""); panel=new Panel(); panel.add(new Label("キッチンタイマー",Label.LEFT)); panel.add(new Label("時間:",Label.LEFT)); panel.add(time); panel.add(new Label("分",Label.LEFT)); panel.add(start); panel.add(message); panel.setComponentListener(this); panel.setSoftLabel(Panel.SOFT_KEY_1,"終了"); panel.setSoftKeyListener(this); Display.setCurrent(panel); // 実機で実行する時は .mld ファイルを使う // mediaSound=loadSound("/alarm.mld"); mediaSound=loadSound("/alarm.mid"); sound=AudioPresenter.getAudioPresenter(); sound.setSound(mediaSound); } private MediaSound loadSound(String file) { MediaSound mediaSound; mediaSound=MediaManager.getSound("resource://"+file); try { mediaSound.use(); } catch (ConnectionException e) { e.printStackTrace(); } return mediaSound; } // 開始 public void start() { } // 定時処理 public void timerExpired(Timer source) { Dialog dialog; sound.play(); dialog=new Dialog(Dialog.DIALOG_INFO,"時間です"); dialog.setText("時間ですよ"); dialog.show(); start.setEnabled(true); message.setText(""); } // イベント public void componentAction(Component source,int type,int param) { int count; if (source==start && type==ComponentListener.BUTTON_PRESSED) { try { count=Integer.parseInt(time.getText()); start.setEnabled(false); message.setText("少し待ってね"); timer=new Timer(); timer.setListener(this); timer.setTime(count*60*1000); timer.start(); } catch (NumberFormatException e) {} } } public void softKeyPressed(int softKey) { if (softKey==Panel.SOFT_KEY_1) { timer.stop(); timer.dispose(); mediaSound.unuse(); mediaSound.dispose(); terminate(); } } public void softKeyReleased(int softKey) { } }