Writing Java for the Palm

ava continues to make inroads at all levels of computing. Mostrecently, Java has returned to its roots and found a home in the smalldevices for which it was originally designed. Java 2 Micro Editionand its K Virtual Machine (KVM) allow you to write Java applicationsfor handheld devices, including ones that run Palm Computing’sPalm OS. You can download an early version of the KVM fromhttp://java.sun.com/products/kvm.

The KVM includes a reduced set of APIs from the Java 2 platform’sjava.io, java.lang, java.net, and java.util packages. It alsoincludes a package called com.sun.kjava which includes user interfaceand event handling classes for writing applications. At the moment,it only appears to support the Palm OS.

You can write a Palm application by subclassing com.sun.kjava.Spotlet,which provides callbacks for handling events. The KVM manages theevent loop and forwards events to the Spotlet, which are handled bymethods such as keyDown() and penDown(). A Spotlet first needs toregister its event handlers with the register() method before it isable to receive events. This is very different from the traditionalAWT/Swing event model. In fact, none of your AWT or Swing code willport to the current incarnation of the KVM. You unregister yourevent handlers with unregister().

You can compile your KVM apps for the Palm using your regular Javacompiler, setting the classpath to use the KVM classes. But you’llneed to use the utilities included with the KVM distribution in orderto convert your programs into a format you can download to your Palm.These are the palm.database.MakePalmApp program and thepalm.database.MakePalmDB program. MakePalmApp will convert a Javaprogram into a Palm .prc file that will automatically load the KVMto run the program when invoked on the Palm. MakePalmDB allows you toadd classes to the KVM’s class database so that multiple KVMapplications may share classes.

The code below demonstrates a simple HelloWorld Spotlet which displays some text and an exit button:

/*** * To compile adjust the bootclasspath to point to the KVM classes. *   javac -bootclasspath kvmDR4.1_bin/api/classes.zip HelloWorld.java * To make a .prc file add the KVM tools to your classpath. *   java -classpath kvmDR4.1_bin/tools/classes.zip  *        palm.database.MakePalmApp HelloWorld ***/import com.sun.kjava.*;public class HelloWorld extends Spotlet {  private Button __exitButton;  static final String _HELLO_WORLD = "Hello World!";  static final Graphics _GRAPHICS  = Graphics.getGraphics();  public HelloWorld() {    __exitButton = new Button("Exit", 16, 144);    _GRAPHICS.clearScreen();    paint();  }  public void paint() {    __exitButton.paint();    _GRAPHICS.drawString(_HELLO_WORLD, 48, 72);  }  public static void main(String[] args) {    (new HelloWorld()).register(NO_EVENT_OPTIONS);  }  public void penDown(int x, int y) {    if(__exitButton.pressed(x, y))      System.exit(0);  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: