Java Reference
In-Depth Information
mapped to keys on the keypad. The key code values are unique for each
hardware key, unless keys are obvious synonyms for one another. These
codes are equal to the Unicode encoding for the character representing
the key. Examples of these are KEY NUM0, KEY NUM1, KEY STAR, and
KEY POUND. The problem with these key codes is that they are not
necessarily portable across devices: other keys may be present on the
keypad and may form a distinct list from those described previously. It is
therefore better, and more portable, to use game actions instead. Each key
code can be mapped to a game action using the getGameAction(int
keyCode) method. This translates the key code into constants such as
LEFT, RIGHT, FIRE, GAME AandGAMEB. Codes can be translated back
to key codes by using getKeyCode(int gameAction) . Apart from
making the application portable across devices, these game actions are
mapped in such a way as to suit gamers. For example, the LEFT, RIGHT,
UP and DOWN game actions might be mapped to the 4, 6, 2 and 8 keys
on the keypad, making game-play instantly intuitive.
A simple Canvas class might look like this:
import javax.microedition.lcdui.*;
public class SimpleCanvas extends Canvas
public void paint(Graphics g) {
// set color context to be black
g.setColor(255, 255, 255);
// draw a black filled rectangle
g.fillRect(0, 0, getWidth(), getHeight());
// set color context to be white
g.setColor(0, 0, 0);
// draw a string in the top left corner of the display
g.drawString("This is some white text", 0, 0, g.TOP
2.3.5 Putting It All Together: UIExampleMIDlet
We use our new knowledge of the high- and low-level APIs to build
a showcase of most LCDUI components: Form , Item (ImageItem ,
StringItem , ChoiceGroup , DateField and Gauge) , Ticker ,
List , TextBox ,and Canvas . We also use the Command class and
its notification interface, CommandListener , to show how you can
handle these abstract events to produce concrete behavior in your appli-
The application also shows how to build a menu using Command sand
how to create high- and low-level UI components and switch between
them, emulating what we would have in a real-world application. For
the sake of simplicity and readability, we skip some code sections which
are common knowledge among Java programmers, such as the list of
imported packages and empty method implementations. This allows us
Search WWH ::

Custom Search