Java进阶篇:RMS从入门到精通系列(1)(2)
中华网科技 http://tech.china.com
2005-09-21 11:45:41
|
|
|
import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Display; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet. MIDletStateChangeException; import javax.microedition. rms.RecordStoreException;
public class RMSAnalyzer extends MIDlet {
private Display display; private CounterCanvas counterCanvas; private Alert alert;
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(RMSAnalyzer.this); alert = new Alert("错误提示"); try { String interval = this.getAppProperty("INTER"); int t = Integer.parseInt(interval); counterCanvas = new CounterCanvas(t, 1, this); } catch (RecordStoreException e) { this.showAlertError(e.getMessage()); } display.setCurrent(counterCanvas);
}
public Display getDisplay() { return display; }
protected void pauseApp() {
}
protected void destroyApp (boolean arg0) throws MIDletStateChangeException {
}
public void showAlertError(String message) { alert.setString(message); alert.setType(AlertType.ERROR); alert.setTimeout(3000); display.setCurrent(alert);
}
}
import java.util.Timer; import java.util.TimerTask;
import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Graphics; import javax.microedition.midlet. MIDletStateChangeException; import javax.microedition.rms.*;
public class CounterCanvas extends Canvas implements CommandListener {
private RMSModel model; private RMSAnalyzer RMSanalyzer; private int interTime; private int counter; private boolean go = true; public static Command backCommand = new Command("退出", Command.EXIT, 3); public static final int INC = 1; public final Timer timer = new Timer();
public CounterCanvas(int interTime, int base, RMSAnalyzer rmsa) throws RecordStoreException { this.interTime = interTime; this.counter = base; this.RMSanalyzer = rmsa; model = new RMSModel(base, RMSanalyzer); this.addCommand(backCommand); this.setCommandListener(this);
TimerTask timerTask = new TimerTask() { public void run() {
try { model.writeRecord(INC); counter++; } catch (RecordStoreFullException e) { go = false; model.deleteRMS(); timer.cancel(); } catch (RecordStoreException e) { model.deleteRMS(); RMSanalyzer.showAlertError(e.getMessage()); timer.cancel(); } repaint();
} };
timer.schedule(timerTask, 1000, interTime);
}
public void setCounter(int counter) { this.counter = counter; }
public void setInterTime(int interTime) { this.interTime = interTime; }
protected void paint(Graphics arg0) {
int SCREEN_WIDTH = this.getWidth(); int SCREEN_HEIGHT = this.getHeight(); arg0.drawRect(SCREEN_WIDTH / 10, SCREEN_HEIGHT / 2, SCREEN_WIDTH * 4 / 5, 10); if(RMSanalyzer.getDisplay().isColor()) { arg0.setColor(128, 128, 255); } arg0.fillRect(SCREEN_WIDTH / 10, SCREEN_HEIGHT / 2, counter, 10); if (!go) arg0.drawString("最大值: " + counter+"K字节", 0, 0, Graphics.TOP | Graphics.LEFT);
}
public void commandAction (Command arg0, Displayable arg1) {
if (arg0 == backCommand) { try { model.deleteRMS(); RMSanalyzer.destroyApp(false); RMSanalyzer.notifyDestroyed(); } catch (MIDletStateChangeException e) {
} }
}
}
import javax.microedition.rms.*;
public class RMSModel { public static final int K = 1024; private RecordStore rs; private int baseCount; private RMSAnalyzer RMSanalyzer; public static final String name = "test";
public RMSModel(int baseCount, RMSAnalyzer rmsa) throws RecordStoreException { this.baseCount = baseCount; this.RMSanalyzer = rmsa; if (rs == null) { rs = RecordStore. openRecordStore(name, true); writeRecord(baseCount); } }
public void writeRecord(int count) throws RecordStoreException { byte[] data = new byte[count * K]; for (int i = 0; i < count; i++) { data[i] = 1; } rs.addRecord(data, 0, count * K); }
public void deleteRMS() { try { rs.closeRecordStore(); RecordStore.deleteRecordStore(name); } catch (RecordStoreException e) { RMSanalyzer.showAlertError (e.getMessage()); } } }
RMSAnalyzer.jad
MIDlet-Jar-Size: 5293 MIDlet-1: RMSAnalyzer,,RMSAnalyzer MIDlet-Jar-URL: RMSAnalyzer.jar MicroEdition-Configuration: CLDC-1.0 MIDlet-Version: 1.0.0 MIDlet-Name: RMSAnalyzer MIDlet-Data-Size: 8192 MIDlet-Vendor: Midlet Suite Vendor MicroEdition-Profile: MIDP-1.0 INTER: 100
首页 上页 | 1 | 2 | 下页 尾页 共 2 页
|