Skip to Main Content
Wireless Java
book

Wireless Java

by Qusay H. Mahmoud
January 2002
Intermediate to advanced content levelIntermediate to advanced
264 pages
8h 3m
English
O'Reilly Media, Inc.
Content preview from Wireless Java

Creating Low-Level GUI Components

In the high-level API, you have no control of what is displayed on the screen and very little freedom to “play” with the components programmatically. The implementation is responsible for selecting the best approach for the device. Some applications, however, such as games, may need more control over what is drawn on the screen. The MIDP javax.microedition.lcdui package also provides a low-level API for handling such cases.

In order to directly draw lines, text, and shapes on the screen, you must use the Canvas class. The Canvas class provides a blank screen on which a MIDlet can draw. For example, let’s draw the string “HelloWorld” on the screen. There’s a simple way to do this: subclass the Canvas class, which is an abstract class that extends Displayable, and override the paint( ) method. The resulting class, MyCanvas, is shown in Example 5-1.

The implementation of the paint( ) method uses the drawing capabilities of the javax.microedition.lcdui.Graphics class. In the paint( ) method, the drawing color is set to red, then a rectangle is drawn in the current color. The methods getWidth( ) and getHeight( ) return the width and height of the Canvas, respectively. The next call to setColor( ) sets the drawing color to white; then the string “Hello World!” is drawn in the top left corner of the screen.

Example 5-1. Subclassing Canvas

import javax.microedition.lcdui.*; public class MyCanvas extends Canvas { public void paint(Graphics g) { g.setColor(255, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Wireless J2ME™ Platform Programming

Wireless J2ME™ Platform Programming

Vartan Piroumian
Java Power Tools

Java Power Tools

John Ferguson Smart

Publisher Resources

ISBN: 0596002432Supplemental ContentCatalog PageErrata