Java Reference
In-Depth Information
Tip Canvas does not automatically clear itself when you call repaint() . If you want to change what's on
the screen, rather than adding to it, you should clear the screen in the paint() method. You'll see how to do
this in the FontCanvas example later in this chapter.
An application can force the implementation to service all the repaint requests by calling
serviceRepaints() on the Canvas object. This method does not return until all pending repaint
requests have been serviced. If you are going to call serviceRepaints() , you should make sure
that you aren't trying to acquire object locks in the paint() method that won't be released until
serviceRepaints() returns. In general, you won't need to call serviceRepaints() ; you can usually
use Display 's callSerially() method instead. (See the “Multithreading and Animation” section of
this chapter for a discussion of callSerially() .)
Drawing Shapes, Text, and Images
The Graphics class contains methods for drawing shapes, text, and images on a Canvas . It also
maintains some state, like the current pen color and line style. MIDP's Graphics class is similar
to the Graphics and Graphics2D classes in J2SE but much smaller.
Coordinate Space
All drawing on a Canvas takes place in a coordinate space based on the pixels of the device. By
default, the origin of this coordinate space is located in the upper-left corner of the Canvas . X
coordinates increase in the right-hand direction, while Y coordinates increase in the downward
direction, as shown in Figure 13-1.
You can adjust the origin of this coordinate space by calling the translate() method of the
Graphics class. This sets the origin to the given coordinates in the current coordinate system.
To find out the location of the translated origin relative to the default origin, call getTranslateX()
and getTranslateY() .
Figure 13-1. Canvas coordinate axes
Drawing and Filling Shapes
Graphics contains a collection of methods that draw and fill simple shapes. These are detailed
in Table 13-1.
 
Search WWH ::




Custom Search