Java Reference
In-Depth Information
return hasFocus;
}
}
The class begins with the constructor, which picks a (somewhat arbitrary) default
width and height for the WeatherItem and initializes its conditions field—which stores
what weather indicator should be drawn—to 0 . WeatherItem exports the conditions field
as a property through the setConditions and getConditions methods, which admittedly
could use some bounds checking.
The item returns the default bounds set by the constructor as the minimum bounds,
and uses the aspect ratio set by those bounds to compute the preferred content height
and width. Note that the parent of the item may invoke the getPrefContentHeight and
getPrefContentWidth methods with a value of -1 when setting up their layout, and to that
the CustomItem should respond with a default desired size.
The paint method does just that, switching on the weather indication and drawing a
sun, clouds, rain, or snowflakes as appropriate. The implementation assumes that each
of these images are layered, either as bitmaps or drawn using the Graphics context and 2D
vector graphics. For example, drawSun draws a circle three-quarters the size of the
WeatherItem centered within the WeatherItem . Other drawings might use the Graphics
methods or Graphics.drawImage to draw from PNG files stored within the MIDlet's JAR file.
The implementation of traverse is trivial, because it only needs to track whether the
item has been traversed into (focused) or out of (unfocused). Clearly, when the
WeatherItem is focused, the rectangle of the item to draw should be its entire rectangle;
when it's defocused, the WeatherItem doesn't return a rectangle.
Wrapping Up
Although in no way compatible with Java SE, the MIDP provides a versatile collection of
high-level user-interface items and a more flexible low-level alternative. You can create
flexible, easily ported user interfaces using the Screen subclasses Alert , TextBox , List ,
and Form , along with the various visible Item classes including TextBox , ChoiceGroup ,
StringItem , ImageItem , and Gauge . Moreover, if you need to, you can extend the hierarchy
of items by creating custom items by subclassing CustomItem , providing your own event
handling and drawing behavior for a new item. For lower-level access to the event and
drawing system, you can subclass the Canvas class, which lets you receive pointer and
keystroke events directly, as well as draw on the screen using instances of the Graphics
class. Because both the Canvas and Screen classes implement the same parent,
Displayable , you can mix and match Canvas -based and Screen -based displays in the
same application.
 
Search WWH ::




Custom Search