Java Reference
In-Depth Information
Actually, in Java the coordinates do not address the pixel itself, but the space between two pixels,
where the "drawing pen" hangs to the lower right. For drawing lines, this does not make any difference,
but for rectangles and filled rectangles this results in a difference of one pixel in width and height: In
contrast to filled rectangles, rectangles become one pixel wider and higher than you might expect.
While this may be confusing at first glance, it respects the mathematical notation that lines are
infinitely thin and avoids problems when extending the coordinate system to real distance measures, as
in the J2SE class Graphics2D .
In all drawing methods, the first coordinate (x) denotes the horizontal distance from the origin and the
second coordinate (y) denotes the vertical distance. Positive coordinates mean a movement down and
to the right. Many drawing methods require additional width and height parameters. An exception is
the drawLine() method, which requires the absolute coordinates of the destination point.
The origin of the coordinate system can be changed using the translate() method. The given
coordinates are added to all subsequent drawing operations automatically. This may make sense if
addressing coordinates relative to the middle of the display is more convenient for some applications,
as shown in the section " Scaling and Fitting ," later in the chapter.
The actual size of the accessible display area can be queried using the getWidth() and
getHeight() methods, as performed in the first example that cleared the screen before drawing. The
region of the screen where drawing takes effect can be further limited to a rectangular area by the
clipRect() method. Drawing outside the clip area will have no effect.
The following example demonstrates the effects of the clipRect() method. First, a dotted line is
drawn diagonally over the display. Then a clipping region is set. Finally, the same line as before is
drawn using the SOLID style:
import javax.microedition.lcdui.*;
Search WWH ::




Custom Search