Java Reference
In-Depth Information
Just pass either FONT_STATIC_TEXT or FONT_INPUT_TEXT and this method returns an appro-
priate Font object that you can use for drawing text. The following code shows how to use an
appropriate font for drawing user-editable text:
public void paint(Graphics g) {
Font f = Font.getFont(Font.FONT_INPUT_TEXT);
g.setFont(f);
// Draw text ...
}
We'll cover Font in detail in Chapter 13. In brief, a Font determines the appearance of text
that is drawn on the screen.
Showing, Hiding, and Sizing
When a CustomItem is made visible, even partially visible, its showNotify() method is called by
the MIDP implementation. You can expect subsequent calls to paint() to render the item.
Similarly, hideNotify() is called when the item is no longer visible (if the user has scrolled the
item off the screen, for example).
The size of your custom item may be changed by the implementation, perhaps if the
containing Form gets laid out again in response to changing content. In this case, your item's
sizeChanged() method is called with the new width and height of the content area.
Similarly, your custom item may decide that it needs to be a different size. In this case,
your item should call the invalidate() method, which signals the implementation that it may
need to lay out the containing Form again.
Handling Events
A CustomItem can respond to keyboard and pointer events by overriding any or all of the
following methods:
protected void keyPressed(int keyCode)
protected void keyReleased(int keyCode)
protected void keyRepeated(int keyCode)
protected void pointerPressed(int x, int y)
protected void pointerReleased(int x, int y)
protected void pointerDragged(int x, int y)
These methods are called in response to the user's actions. The keyCode parameter will
most likely be one of the constants defined in the Canvas class: KEY_NUM0 through KEY_NUM9 ,
KEY_POUND , or KEY_STAR . The CustomItem class also supports a handy mechanism called game
actions , which maps device-specific keys to device-independent actions. The getGameAction()
method performs this mapping. For a full discussion of game actions, see Chapter 13.
The pointer callback methods supply the location of the pointer event as a pair of coordi-
nates relative to the custom item's content area.
Devices have varying capabilities, and some may not be able to deliver certain types of
events to CustomItem s. Many phones, for example, will not support pointer events. To find out
the capabilities of the device at runtime, custom items use the getInteractionModes() method.
This method returns some combination of the following constants (defined in CustomItem ):
Search WWH ::




Custom Search