import javax.microedition.midlet.*; import javax.microedition.rms.*; import javax.microedition.lcdui.*; import java.io.*; public class Salv1 extends MIDlet implements CommandListener { TextField koord1 = new TextField("1. koordinaat", "", 5, TextField.NUMERIC); TextField koord2 = new TextField("2. koordinaat", "", 5, TextField.NUMERIC); Command c1 = new Command("Loe", Command.SCREEN, 1); Command c2 = new Command("Kirjuta", Command.SCREEN, 1); Form f = new Form("Salvesteerija"); public Salv1() { f.append(koord1); f.append(koord2); f.addCommand(c1); f.addCommand(c2); f.setCommandListener(this); Display.getDisplay(this).setCurrent(f); } public void commandAction(Command p1, Displayable p2) { byte[] baidid; int[] indid = new int[2]; try { RecordStore rs = RecordStore.openRecordStore("rs", true); // command == kirjuta if (p1 == c2) { indid[0] = Integer.parseInt(koord1.getString()); indid[1] = Integer.parseInt(koord2.getString()); baidid = i2b(indid); if (rs.getNumRecords()==0) rs.addRecord(baidid, 0, baidid.length); else rs.setRecord(1, baidid, 0, baidid.length); } // command == loe else { if (rs.getNumRecords()==0) indid[0] = indid[1] = 0; else indid = b2i(rs.getRecord(1)); koord1.setString(String.valueOf(indid[0])); koord2.setString(String.valueOf(indid[1])); } rs.closeRecordStore(); } catch(Exception e) e.printStackTrace(); } protected void startApp() throws MIDletStateChangeException {} protected void pauseApp(){} protected void destroyApp(boolean kohustus) throws MIDletStateChangeException{} byte[] i2b(int[] i) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(i[0]); dos.writeInt(i[1]); dos.close(); return bos.toByteArray(); } int[] b2i(byte[] b) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(b); DataInputStream dis = new DataInputStream(bis); int[] i = new int[2]; i[0] = dis.readInt(); i[1] = dis.readInt(); return i; } }