Java Reference
In-Depth Information
CHAPTER 5
■ ■ ■
Creating a User Interface
M any MIDP applications are built to run on many different devices without modification.
This is particularly difficult in the area of the user interface because devices have screens of all
sizes, in grayscale and in color. Furthermore, devices vary widely in their input capabilities,
from numeric keypads to alphabetic keyboards, soft keys, and even touch screens. The minimum
screen size mandated by MIDP is 96×54 pixels, with at least one bit of color depth. 1 As for input,
MIDP is fairly open ended: devices are expected to have some type of keyboard, or a touch
screen, or possibly both.
Given the wide variety of devices that are compliant with MIDP, there are two ways to
create applications that work well on all devices:
Abstraction : Specify a user interface in abstract terms, relying on the MIDP implementa-
tion to create something concrete. Instead of saying something like, “Display the word
'Next' on the screen above the soft button,” you say, “Give me a Next command some-
where in this interface.”
Discovery : The application learns about the device at runtime and tailors the user inter-
face programmatically. You might, for example, find out how big the device's screen was
in order to scale your user interface appropriately.
The MIDP APIs support both methods. Abstraction is the preferred method because it
involves less code in your application and more work by the MIDP implementation. In some
cases, like games, you need to be more specific about the user interface; these types of applica-
tions will discover the capabilities of a device and attempt to tailor their behavior appropriately.
MIDP's user interface APIs are designed so that it's easy to mix the two techniques in the same
application.
The View from the Top
MIDP contains user interface classes in the javax.microedition.lcdui and
javax.microedition.lcdui.game packages. The device's display, as seen by the MIDlet, is
represented by an instance of the Display class, accessed from a factory method, getDisplay() .
Display 's main purpose in life is to keep track of what is currently shown, which is an instance
1.
Color depth is the number of bits that determine the color of a pixel on the screen. One bit allows for
two colors (usually black and white). Four bits allows for 16 colors, which could be different levels of
gray or a palette of other colors. In general, N bits allow 2 to the power of N colors or levels of gray.
53
Search WWH ::




Custom Search