Java Reference
In-Depth Information
return y + g.getFont().getHeight();
}
The
paint()
method analyzes the
keyCode
and prints the result by calling the
write()
method
defined previously, as shown in
Figure 3.18
:
public void paint (Graphics g) {
g.setGrayScale (255);
g.fillRect (0, 0, getWidth(), getHeight());
g.setGrayScale (0);
int y = 0;
y = write (g, y, "Key "+ eventType);
if (keyCode == 0) return;
y = write (g, y, "Char/Code: "+ ((keyCode < 0) ? "N/A" : ""
+(char) keyCode) + "/" + keyCode);
y = write (g, y, "Name: "+getKeyName (keyCode));
String gameAction;
switch (getGameAction (keyCode)) {
case LEFT: gameAction = "LEFT"; break;
case RIGHT: gameAction = "RIGHT"; break;
case UP: gameAction = "UP"; break;
case DOWN: gameAction = "DOWN"; break;
case FIRE: gameAction = "FIRE"; break;
case GAME_A: gameAction = "GAME_A"; break;
case GAME_B: gameAction = "GAME_B"; break;
case GAME_C: gameAction = "GAME_C"; break;
case GAME_D: gameAction = "GAME_D"; break;
default: gameAction = "N/A";
}
write (g, y, "Action: "+gameAction);
}
}
Figure 3.18. Output of the
KeyDemo
example when the "Fire" key was released.
Pointer Events
For devices supporting a pointer device such as a stylus, touch screen, or trackball, the
Canvas
class
provides three notification methods:
pointerPressed()
,
pointerDragged()
, and
pointerReleased()
. These methods work similarly to the key event methods, except that they
provide two integer parameters, denoting the x and y position of the pointer when the corresponding
event occurs. (Please note that pointer support is optional in MIDP, so the application should not rely
on the presence of a pointer. Such devices are uncommon for devices such as mobile phones.) The
following sample program demonstrates the usage of the three methods:
import javax.microedition.lcdui.*;
Search WWH ::
Custom Search