Java Reference
In-Depth Information
g.setColor(255,255,255);
g.drawString("This is a canvas.",0,0,Graphics.TOP | Graphics.LEFT);
g.drawString("Check Options menu",0,20, Graphics.TOP |
Graphics.LEFT);
g.drawString("for more UI components",0,40,
Graphics.TOP |
Graphics.LEFT);
}
}
The options instance variable holds an array of Command swhich
serves as the main menu of the application. Each command has a label, a
positioning guide ( Command.OK ) and a priority value. In the constructor,
we loop through the array, adding all the commands to the Canvas
so they are displayed on the screen. The abstract paint(Graphics)
method is implemented so that the Canvas can be drawn upon, using
the Graphics object passed in as a parameter. In this case, we set
the context color to black, paint a screen-sized rectangle, set the color
to white and draw some strings to instruct the user how to use the
application.
Save UIExampleMIDlet in a file called UIExampleMIDlet.java
and MenuCanvas in a file called MenuCanvas.java in your project's
src/example folder, then build and package the project using the WTK.
Figure 2.5 shows the application running on the WTK emulator.
(a) (b)
Figure 2.5 UIExampleMIDlet application running on the WTK emulator: a) the Menu-
Canvas and b) the Form
Search WWH ::




Custom Search