Looking to Reprint this content?
Cover | Table of Contents | Colophon
http://jcp.org/jsr/all/. The JSRs that define
the current J2ME configurations and profiles are as follows:
|
Number
|
Scope
|
|---|---|
|
JSR 30
|
J2ME Connected Limited Device Configuration (CLDC)
|
|
JSR 37
|
Mobile Information Device Profile for the J2ME Platform (MIDP)
|
|
JSR 75
|
PDA Profile for the J2ME Platform
|
|
JSR 36
|
J2ME Connected Device Configuration (CDC)
|
|
JSR 46
|
J2ME Foundation Profile
|
|
JSR 129
|
Personal Basis Profile Specification |
http://java.sun.com/products/javacard/.
http://java.sun.com/products/embeddedjava/,
defines only the maximum possible content of the platform, rather
than a minimum (as is the case with J2ME specifications).
javax.microedition.io), along with a selection of
classes from the following packages in the core J2SE
platform:
java.io
java.lang
java.util
java.lang package in a package
called javax.microedition.lang if the API in the
java.lang package can be used.
http://jcp.org/jsr/detail/37.jsp.
http://jcp.org/jsr/detail/37.jsp.
javax.microedition package hierarchy. The core
libraries themselves are almost unaffected by the MIDP specification;
the only change is the addition of the J2SE 1.3 timer facility in the
java.util package, which will be covered in Section 3.5.4. The
MIDP specification also places the following requirements on the core
libraries:
main( ) method of its MIDlet
class, and the MIDlet is not allowed to cause the termination of the
Java VM. In order to enforce this restriction, the exit(
) methods in both the System and
Runtime classes are required to throw a
SecurityException if they are invoked.
microedition.locale property to
reflect the locale in which the device is operating. The locale names
are formed in a slightly different way from those used by J2SE,
because the language and country components are separated by a hyphen
instead of an underscore character. A typical value for this property
would be en-US on a MIDP device, whereas a J2SE
developer would expect the locale name in the form
en_US. Since both MIDP and CLDC provide almost no
support for localization, however, the precise format of this
property is of little direct interest to MIDlets. Instead, it is
intended to be used when installing MIDlets from external sources, to
allow the selection of a version of the MIDlet suitable for the
device owner's locale. The property must therefore
be properly interpreted by the agent (perhaps a servlet running in a
web server) that supplies the software.
javax.microedition.midlet.MIDlet. MIDlets run in
an execution environment within the Java VM that provides a
well-defined lifecycle controlled via methods of the
MIDlet class that each MIDlet must implement. A
MIDlet can also use methods in the MIDlet class to obtain services
from its environment, and it must use only the APIs defined in the
MIDP specification if it is to be device-portable.
javax.microedition.midlet.MIDlet, which contains
methods that the MIDP platform calls to control the
MIDlet's lifecycle, as well as methods that the
MIDlet itself can use to request a change in its state. A MIDlet must
have a public default
constructor (that is, a constructor that
requires no arguments), which may be one supplied by the developer if
there is any initialization to perform or, when there are no explicit
constructors, the empty default constructor inserted by the Java
compiler. This is what a skeleton MIDlet class might look like:
public class MyMIDlet extends MIDlet {
// Optional constructor
MyMIDlet( ) {
}
protected void startApp( ) throws MIDletStateChangedException {
}
protected void pauseApp( ) {
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangedException {
}
}
startApp( )
method is called. The MIDlet class declares
this method as follows:
javax.microedition.lcdui package, includes several
classes that represent the device's screen and
provide the basic top-level windows. Developers can use these as the
basis for building form-based MIDlets or more graphically
sophisticated MIDlets, such as games.
Display
class represents a logical device screen
on which a MIDlet can display its user interface. Each MIDlet has
access to a single instance of this class; you can obtain a reference
to it by using the static getDisplay( ) method:
public static Display getDisplay(MIDlet midlet);
getDisplay(
)
when its startApp( ) method is called for the
first time and then uses the returned Display
object to display the first screen of its user interface. You can
safely call this method at any time from the start of the initial
invocation of the startApp( ) method, up to the
time when the MIDlet returns from destroyApp( ) or
notifyDestroyed( ), whichever happens first. Each
MIDlet has its own, unique and dedicated instance of
Display, so getDisplay( )
returns the same value every time it is called. A MIDlet will,
therefore, usually save a reference to its Display
object in an instance variable rather than repeatedly call
Form, List, or
TextBox classes, together with a set of
Commands that allow the user to tell the MIDlet
what actions to perform and how to navigate from screen to screen.
Let's start our examination of the high-level API by
creating a simple MIDlet with a single screen containing a
TextBox.
TextBox
is a component used to display and modify text. Since it is derived
from Screen, TextBox occupies
the entire screen of the device and therefore can accomodate
relatively large amounts of text spread over several lines. Most of
the API provided by TextBox is identical to that
of a similar component called TextField, which is
covered in detail in Section 4.2.9,
later in this chapter. In this example, we use only the features that
TextBox inherits from Screen
(and which are not available to TextField, because
it is not derived from Screen). The code for this
example is shown in Example 4-1.
package ora.ch4;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
public class TextBoxMIDlet extends MIDlet {
// Maximum size of the text in the TextBox
private static final int MAX_TEXT_SIZE = 64;
// The TextBox
protected TextBox textBox;
// The MIDlet's Display object
protected Display display;
// Flag indicating first call of startApp
protected boolean started;
protected void startApp( ) {
if (!started) {
// First time through - initialize
// Get the text to be displayed
String str = null;
try {
InputStream is = getClass( ).getResourceAsStream(
"resources/text.txt");
InputStreamReader r = new InputStreamReader(is);
char[] buffer = new char[32];
StringBuffer sb = new StringBuffer( );
int count;
while ((count = r.read(buffer, 0, buffer.length)) > -1) {
sb.append(buffer, 0, count);
}
str = sb.toString( );
} catch (IOException ex) {
str = "Failed to load text";
}
// Create the TextBox
textBox = new TextBox("TextBox Example", str,
MAX_TEXT_SIZE, TextField.ANY);
// Create a ticker and install it
Ticker ticker = new Ticker("This is a ticker...");
textBox.setTicker(ticker);
// Install the TextBox as the current screen
display = Display.getDisplay(this);
display.setCurrent(textBox);
started = true;
}
}
protected void pauseApp( ) {
}
protected void destroyApp(boolean unconditional) {
}
}javax.microedition.lcdui package, and you have
very little control over the appearance of your MIDlet.
Form and the other classes covered in the previous
chapter.
Canvas
is
the basic building block of the low-level API. Because it is derived
directly from Displayable, it inherits the ability
to have associated Commands, but it does not
provide a title or the ability to contain other components.
Canvas gives you direct access to the screen of a
MIDP device, apart from the area used to draw
Command buttons or labels, as shown in Figure 5-1. In the figure, the black area is the part of
the screen occupied by the Canvas itself.
Canvas is an abstract class. To use
Canvas, you have to subclass it and implement the
paint method to draw whatever you want to appear
on the screen. This method is called with a single argument, which is
an instance of another low-level API class called
Graphics. This class provides methods that allow
you to draw lines, rectangles, and arcs, fill areas with a solid
color, and render text onto the device's screen. The
Canvas
is
the basic building block of the low-level API. Because it is derived
directly from Displayable, it inherits the ability
to have associated Commands, but it does not
provide a title or the ability to contain other components.
Canvas gives you direct access to the screen of a
MIDP device, apart from the area used to draw
Command buttons or labels, as shown in Figure 5-1. In the figure, the black area is the part of
the screen occupied by the Canvas itself.
Canvas is an abstract class. To use
Canvas, you have to subclass it and implement the
paint method to draw whatever you want to appear
on the screen. This method is called with a single argument, which is
an instance of another low-level API class called
Graphics. This class provides methods that allow
you to draw lines, rectangles, and arcs, fill areas with a solid
color, and render text onto the device's screen. The
Canvas class also has methods -- which you can
override -- to receive notification of key presses and use of the
pointer (on those devices that have one).
Canvas and Display classes
provide methods, described in the following sections, to allow you to
query the attributes that distinguish one device from another.
Canvas needs to be drawn onto the
screen, it calls the paint(
)
method,
which the MIDlet developer is required to implement:
protected void paint(Graphics g)
Canvas becomes visible as a result of the
Display
setCurrent( ) method
being invoked
Canvas reappears after
being partly or wholly obscured by an Alert or a
system screen, such as a menu of Commands opened
from a soft button
Graphics object passed to the paint(
) method provides methods that allow graphics operations,
such as line and text rendering and color filling, to be performed on
its target. The target is either the screen itself or, in the case of
a platform that supports double buffering, an off-screen image that
will be copied to the screen when the paint( )
method returns. Implementing this method is the only way to get a
Graphics object that can access the screen; unlike
the J2SE Component class,
Canvas does not have a getGraphics(
) method that can be used to get access on demand to the
screen space that it occupies. Therefore, all screen updates must be
performed in the paint( ) method. The MIDP
specification prohibits holding a reference to the
Graphics object passed to paint(
) for use elsewhere.
Canvas changes, the
following methods are called:
protected void showNotify( ) protected void hideNotify( )
paint(
) method will not be invoked before showNotify(
)
is
called and, following return from hideNotify(
)Graphics
class
provides operations that let you do the following:
String or as
character data
Graphics object that act as
implicit parameters. When the paint( ) method is
called, the attributes of the Graphics object that
is passed to it have well-defined values that can be modified if
necessary. The attributes and their initial values are listed in
Table 5-1. A more detailed description of each
attribute and the way in which it is used are found in later
sections.
|
Attribute
|
Use
|
Initial Value
|
|---|---|---|
|
Clip
|
The clip sets the region of the
Canvas within
which graphics operations have any effect. The clip is discussed in
Section 5.7.
|
Depends on the reason
paint( ) was invoked |
|
Color
|
The color that will be used when drawing or filling shapes or
rendering text. See Section 5.3.2
for further details.
|
Black
|
Graphics class methods that let you draw
straight lines, rectangles, and arcs are very similar to those
available in J2SE. There are, of course, none of the advanced
features provided by Java 2D. Even some of the more basic features,
such as convenience methods that let you draw polygons and polylines,
are missing, although some of them can easily be simulated.
setColor( ) or
setGrayScale( ). Because there is no support for
transparency and color
blending, no account is taken of the initial state of an affected
pixel.
Graphics object
only in the paint( ) method, when everything must
be redrawn, whether it has moved or not. By contrast, it is possible
in J2SE to get a Graphics object at any time, and,
therefore, parts of the screen can be updated directly, without
having to wait for the paint( ) method to be
called.
Canvas is a straight line:
public void drawLine(int x1, int y1, int x2, int y2)
x1, y1) and
(x2, y2