October 23, 2016
Hot Topics:

Java 2 Micro Edition and the Mobile Information Device Profile

  • November 15, 2000
  • By David Fox
  • Send Email »
  • More Articles »

To hit the 0 through 9, #, *, left, right, up and down keys you can use your keypad or simply click on the desired key with your mouse. Since HelloWorld doesn't do anything with user input, hitting these keys will have no effect.

If you happened to own an iDen phone, you could plug it into your PC and use the phone's linking software to install the MIDlet directly into the phone's memory, similarly to the way you might install an application on the Palm Pilot. In the very near future, Motorola and other cell phone manufacturers may make it easy for users to download applets right to their phones, on demand.

Now let's blaze ahead a bit and talk about the MIDP specification. A Display can consist of either a Screen object or a Canvas. Every Screen has an optional title (usually at the top) and Ticker (usually running along the bottom). Specific types of Screens include:

  • List — A row of elements that a user may choose from, perfect for a menu.
  • Alert — A "dialog box" that appears and shows some text and/or an Image to the user and then, after a set amount of time, disappears. This is perfect for important messages.
  • Form — A screen for user input which may contain all or some of the following MIDP objects: ImageItem, StringItem, TextField, DateField, Gauge, or ChoiceGroup.
  • TextBox — A box that allows the user to type in or edit some text.

Using the ease and convenience of Screens, creating quick MIDlets is a snap. If you want to customize a component, capture user input, or create more detailed graphics, however, you should use a Canvas object. A Canvas must define the paint() method. The Graphics object allows for simple renderings using familiar Java methods such as drawArc(), drawImage(), drawLine(), drawRect(), drawString(), fillRect(), etc.

You should always create your own Canvas subclass. For example:

class SillyCanvas extends Canvas 
     public void paint(Graphics g) 
        g.drawString("Hello World", getWidth() / 2, 0,                           
            Graphics.HCENTER | Graphics.TOP);

This Canvas contains a rectangle bordering the entire screen with the string "Hello World" at the top, horizontal center. You could drop this canvas into the Display by creating it in your MIDlet:

private SillyCanvas aCanvas = new SillyCanvas();

And then, in the MIDlet's startApp() method or anywhere else that made sense, you could set the current Display to show the special canvas:


Note that a cell phone's Display can only show one Screen or Canvas at a time. A typical application starts with a List as a main menu of choices, branching into other types of Screens depending on the user's selection. An application such as a game is more likely to paint the game screen using a Canvas, redrawing the screen as often as possible to achieve animation.

Of course the real fun of having an application on a cell phone is networking. The MIDP specification, especially Motorola's implementation, makes it extremely easy to pass data back and forth using Datagrams. Every iDen phone has its own static IP address, so having one phone communicate with another is only a matter of simple peer-to-peer networking.

In addition, the phone can connect to any outside server machine. This server can be used as a gateway for pretty much any type of network communication imaginable. For instance, a gateway can be set up to access an entire database of sports scores, and then stream only the latest requested scores to an MIDlet.

To connect to a server, simply have the MIDlet use the Connector class:

Datagram dgram = dc.newDatagram(message, msglength,

The remote server could, of course, be another iDen phone. Just create an endless loop that listens to a port and waits for some data:

DatagramConnection dc = ( DatagramConnection)Connector.open
while (true) 
   dgram = dc.newDatagram(dc.getMaximumLength());
   reply = new String(dgram.getData(), 0,dgram.getLength());

Since the Datagram protocol is standard, one could write a dastardly-simple server component in Java Standard Edition (or any other language), running on any PC. For instance:

DatagramSocket receiveSocket = null; 
DatagramPacket receivePacket = new DatagramPacket(bytesReceived, bytesReceived.length);

A simple game can be written with relative ease. For instance, NumberPick.java

is a MIDlet that allows a user to pick a number between 0 and 9, as shown in Figure 2. It then sends this number to a server (at a fictitious machine with the host name of myserver.com) Note that for the sake of testing, it's a good idea to set the server to 'localhost' — that way you can run the server and the client from the comfort of your desktop machine.

Figure 2.

Running on the myserver.com machine is NumberServer.java

, which simply sits there and waits for a message on port 9000. If the number is 4, then the server sends back a simple one-byte "Y" message. Otherwise it sends back an "N". Maybe not as compelling a game as Unreal or Quake III, but it's a start.

Scan the MotoSDK\docs\api directory for the complete CLDC and MIDP API. And for additional sample code, check out the applications in the MotoSDK\demo\midlets\com\mot\j2me\midlets directory.

About the Author

David Fox is vice president of Games at PlayLink, Inc. He's also the author of numerous books and articles about cyberculture and technology.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel