By Qusay Mahmoud
Price: $34.95 USD
£24.95 GBP
Cover | Table of Contents | Colophon
http://java.sun.com/products/j2mewtoolkit.
ktoolbar program, just ignore it for the
moment.) However, we need to do a few more things before we can get
started with our examples.
HelloMidlet.java, shown in Example 1-1, creates a text box and then prints the
archetypal "Hello World" in a text box.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloMidlet extends MIDlet {
// The display for this MIDlet
private Display display;
// TextBox to display text
TextBox box = null;
public HelloMidlet( ) {
}
public void startApp( ) {
display = Display.getDisplay(this);
box = new TextBox("Simple Example", "Hello World", 20, 0);
display.setCurrent(box);
}
/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp( ) {
}
/**
* Destroy must cleanup everything not handled by the garbage
* collector. In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
}
}
MIDlet class found in
javax.microedition.midlet
. This superclass forms
the base of all MIDlets in J2ME. Our HelloMidlet
class contains a constructor, as well as the
startApp()
, pauseApp(
)
, and destroyApp(
)
methods that have been inherited from
the MIDlet
class. Note
that there is no main() method in this program.
Instead, the startApp(), pauseApp(
), and destroyApp( ) methods are called
by the underlying framework to start up the MIDlet, to pause it, or
to destroy it.
http://java.sun.com/products/kvm.
|
Directory
|
Description
|
|---|---|
api
|
The Java classes and source code for the CLDC
|
bin
|
http://www.jcp.org/.
http://java.sun.com/products/midp.
javax.microedition.midlet
package, which contains classes and methods for starting, pausing,
and destroying applications in the host environment.
javax.microedition.lcdui
packages, which include classes and interfaces for creating GUI
components for applications.
javax.microedition.midlet.MIDlet
abstract class and implement certain methods in that class. The
MIDlet abstract class provides the basic
functionality required in all MIDlets. A MIDlet runs in a controlled
environment and therefore must implement certain methods that allow
the application manager (which installs and runs
the MIDlet) to control the behavior of the MIDlet. These methods are
known as life cycle methods, since they reflect
various states in which a MIDlet can be.
javax.microedition.midlet.MIDlet
abstract class defines three
life cycle methods that are
called during the state transitions: pauseApp(),
startApp(), and destroyApp().
These three methods were present in the example we developed in Chapter 1. The responsibilities for these three life
cycle methods are as follows.
public void startApp( )
public void pauseApp( )
PaymentMIDlet. This MIDlet creates a
List object of type EXCLUSIVE (that is, only one option
can be selected at a time), and adds three methods of payments to it.
It displays a list of options for the user to select a method of
payment.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class PaymentMIDlet extends MIDlet {
// The display for this MIDlet
private Display display;
// List to display payment methods
List method = null;
public PaymentMIDlet( ) {
method = new List("Method of Payment",
Choice.EXCLUSIVE);
}
public void startApp( ) {
display = Display.getDisplay(this);
method.append("Visa", null);
method.append("MasterCard", null);
method.append("Amex", null);
display.setCurrent(method);
}
/**
* Pause is a no-op since there are no background
* activities or record stores that need to be closed.
*/
public void pauseApp( ) {
}
/**
* Destroy must cleanup everything not handled by the
* garbage collector. In this case there is nothing to
* cleanup.
*/
public void destroyApp(boolean unconditional) {
}
}javax.microedition.lcdui.Screen class.
javax.microedition.lcdui.Canvas and
javax.microedition.lcdui.Graphics classes
implement the low-level API. However, we should point out that
MIDlets that access the low-level API are not guaranteed to be
portable, because this API provides mechanisms to access details that
are specific to a particular device.
javax.microedition.lcdui.Display
class. The Display
class is the one and only display manager that is instantiated for
each active MIDlet and provides methods to retrieve information about
the device's display capabilities.
Display class,
which includes methods for requesting that objects be displayed on
the device, and for retrieving properties of the device.
static getDisplay(
)
method.
public static Display getDisplay(MIDlet c);
startApp(
)
method of a MIDlet, as follows:
public class MyMIDlet extends MIDlet {
Display display = null;
public MyMIDlet( ) { // constructor
}
public void startApp( ) {
display = Display.getDisplay(this);
}
// other methods
}
getDisplay( ) method should be called after
the beginning of the MIDlet's startApp( )
method, as shown earlier. It should never be
called from the MIDlet's constructor, as per the MIDP
specification, as it may not be properly initialized by the
application manager at that time.
Displayable abstract class. You can pass the
GUI component you create to one of Display's
two setCurrent( )
methods:
public void setCurrent(Displayable d); public void setCurrent(Alert alert, Displayable d);
javax.microedition.lcdui package also provides a
low-level API for handling such cases.
Canvas
class. The
Canvas class provides a blank screen on which a
MIDlet can draw. For example, let's draw the string
"HelloWorld" on the screen. There's a simple way to
do this: subclass the Canvas
class, which is an abstract class that extends
Displayable, and override the paint(
)
method.
The resulting class, MyCanvas, is shown in Example 5-1.
paint( ) method uses the
drawing capabilities of the
javax.microedition.lcdui.Graphics
class.
In the paint( ) method, the drawing color is set
to red, then a rectangle is drawn in the current color. The methods
getWidth(
)
and getHeight(
)
return the width and height of the
Canvas, respectively. The next call to
setColor( )
sets the drawing color to white; then
the string "Hello World!" is drawn in the top left corner
of the screen.
import javax.microedition.lcdui.*;
public class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.setColor(255, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight( ));
g.setColor(255, 255, 255);
g.drawString("Hello World!", 0, 0, g.TOP | g.LEFT);
}
}
MyCanvas, it must be
instantiated
and displayed. Since Canvas is a subclass of
Displayable, it can be displayed the same way any
other screen using the setCurrent(
)
method. Example 5-2
shows the resulting MIDlet.
Displayable screen. We can do this by using the
Command
class, which
is part of the javax.microedition.lcdui package.
Let's take a closer look at the Command
class now.
Command class encapsulates the semantic
information of an action. Note that it only contains information
about a command, not the actual functionality that is executed when a
command is activated. Here is the constructor of the
Command class:
public Command(String label, int commandType, int priority);
Command
class. Hence, creating a Command object is
extremely simple:
Command infoCommand = new Command("Info", Command.SCREEN, 2);
Command class constructor takes three
parameters, and therefore contains the following three lightweight
pieces of information: Displayable screen. We can do this by using the
Command
class, which
is part of the javax.microedition.lcdui package.
Let's take a closer look at the Command
class now.
Command class encapsulates the semantic
information of an action. Note that it only contains information
about a command, not the actual functionality that is executed when a
command is activated. Here is the constructor of the
Command class:
public Command(String label, int commandType, int priority);
Command
class. Hence, creating a Command object is
extremely simple:
Command infoCommand = new Command("Info", Command.SCREEN, 2);
Command class constructor takes three
parameters, and therefore contains the following three lightweight
pieces of information: label, command
type, and priority.
Command class:
BACK
,
CANCEL
,
EXIT
,
HELP
,
ITEM
,
OK
,
SCREEN
, and
STOP
.
Canvas class to write applications to access
low-level input events or to issue graphics calls for drawing to the
display, you must handle low-level events. Game applications are
likely to use the Canvas class because it provides
methods to handle game actions and key events. The key events are
reported with respect to keycodes that are directly bound to concrete
keys on the device.
Canvas class, which is a subclass of
Displayable, allows the application to register a
listener for commands, but it requires applications to subclass the
listener first. Also, while screens allow the application to define a
listener and register it with an instance of the
Screen class, the Canvas class
does not allow this, because several listener interfaces need to be
created, one for each kind of event.
Canvas class:
KEY_NUM0
KEY_NUM1
KEY_NUM2
KEY_NUM3
KEY_NUM4
java.io
and
java.net
packages of the
J2SE are not suitable for handheld devices with a small memory
footprint, for the following reasons:
java.net
package provides a set of related abstractions in the form of over 20
networking classes, including Socket,
ServerSocket, and
DatagramSocket. With the CLDC, however, we need to
go a step further to save space.
open( )
methods from a single class:
javax.microedition.io.Connector
. If successful,
these methods return an object that implements one of the generic
connection interfaces. Figure 7-1 shows how these
interfaces form an inheritance
hierarchy. The
Connection
interface
(don't confuse Connection with
Connector) is the base interface.
open( )
methods from a single class:
javax.microedition.io.Connector
. If successful,
these methods return an object that implements one of the generic
connection interfaces. Figure 7-1 shows how these
interfaces form an inheritance
hierarchy. The
Connection
interface
(don't confuse Connection with
Connector) is the base interface.
Connection interface represents the most basic
connection type. It can only be opened and closed.
InputConnection
interface represents a device from which data can be read. Its
openInputStream(
)
method returns an input stream for the connection.
OutputConnection
interface represents a device to which data can be written. Likewise,
its openOutputStream(
)
method returns an output stream for the connection.
StreamConnection
interface combines the input and output connections.
ContentConnection
interface extends the StreamConnection interface.
It provides access to some of the basic metadata information provided
by HTTP connections.
StreamConnectionNotifier
interface waits for a connection to be established. It returns an
object that implements the StreamConnection
interface, on which a communication link has been established.
DatagramConnection
interface is used to represent a datagram endpoint.