|
||||||||||||||||||
IDP provides a lot of efficiency and flexibility when building user interfaces. The efficiency part can be seen in what MIDP calls the high-level user interface controls, which is what I discussed last month. This month I will explore the flexibility side of MIDP user interfaces in what MIDP calls the low-level user interface APIs. The MIDP low-level user interface APIs provide much more fine-grained control and nearly limitless customization over an application's user interface. However, there are risks to be managed and tradeoffs to be made when using the low-level APIs. In this article I explore the effort involved in creating low-level user interfaces and discuss some of the implications and risks you are likely to encounter.
The low-level user interface is essentially composed of the Canvas class and a handful of support classes such as Graphics, Font, and Image. The supporting classes are used within an instance of Canvas to create different visual effects. Canvas is an abstract class. In order to create a user interface using the low-level APIs, an application must create a subclass of Canvas and implement the paint() method. The paint() method is the only method requiring implementation. An instance of Graphics, for example, is passed as a parameter to paint(), which provides access to the device drawing capabilities that allow for directly interacting with the display at the pixel level. The nice thing about working with the MIDP low-level classes is that very complex user interfaces can be created with only a few classes. However, there are caveats to using this approach. Take the following example, which draws a string on the screen.
Figure 1 shows the MIDlet running in the J2ME Wireless Toolkit where the Canvas has been set as the current display. Although the MIDlet paints the string to the screen there are a few problems that immediately begin to surface.
The first problem is that the Canvas was not cleared before writing the sentence. As a result, the string painted directly onto the existing image, which happened to be the application launch screen of the Application Management Software (AMS). In the next example, I add a line of code to clear the screen. This technique fills the entire screen with the same color pixels.
But now I have a new problem. The screen seems to have gone blank (see Figure 2). What happened is the Canvas filled the screen with pixels of the same color, but the current Canvas color happened to be set to black. Then, when the drawString() method was called, the text was drawn in black as well, having no visible effect at all. To get the desired effect, I'm going to have to manipulate the colors.
Playing with Color
With an idea of how setColor() works, I now can add code to first clear the screen to a white background and then paint black text on top of the white background (see Figure 4).
Now things are looking better. The AMS image is cleared by the call to fillRect(), turning the background to white, and the text is now displaying in black. However, there is one more thing I need to fix. The string is too long for the screen. The Graphics.drawString() method does not support line wrapping, so I'll have to handle it myself. With an example this simple, I can probably get away with splitting the sentence into two parts and calling drawstring() twice. However, I still need to figure out how far down to start drawing the next line. I could guess that 10 or 15 pixels would be sufficient; however, there are more scientific ways of figuring this out using font metrics.
|
||||||||||||||||||
|