Java Reference
In-Depth Information
Coordinate System for Graphics Objects
When drawing objects on the screen, Java uses the coordinate system shown in Display
18.10. The origin point (0, 0) is the upper-left corner of the screen area used for drawing
(usually a JFrame or JPanel ). The x -coordinate, or horizontal coordinate, is positive and
increasing to the right. The y -coordinate, or vertical coordinate, is positive and increasing
in the downward direction. The point ( x , y ) is located x pixels in from the left edge of the
screen and down y pixels from the top of the screen. All coordinates are normally positive.
Units as well as sizes of figures are in pixels. When placing a rectangle on the screen, Java
often uses a coordinate like (200, 150) to specify where the rectangle is located.
Note that, when specifying the location of a rectangle or other figure, the coordi-
nates do not indicate the center of the rectangle, but instead indicate the location of
the upper-left corner of the rectangle. In Display 18.10, the X marks the location of
the point (200, 150) and the rectangle shown is at location (200, 150).
When placing a figure other than a rectangle on the screen, Java encloses the fig-
ure in an imaginary tightly fitting rectangle, sometimes called a bounding box , and
positions the upper-left corner of the imaginary rectangle. For example, in Display
18.10, the oval displayed is located at point (200, 150).
origin
( x , y )
bounding
box
The Method paint and the Class Graphics
Almost all Swing and Swing-related components and containers have a method named
paint . The method paint draws the component or container on the screen. Up until
now, we have had no need to redefine the method paint or to even mention it. The
method paint is defined for you and is called automatically when the figure is dis-
played on the screen. However, to draw geometric figures, like circles and boxes, you
need to redefine the method paint . It is the method paint that draws the figures.
paint
Display 18.10 Screen Coordinate System
(0, 0)
Positive x-direction.
(200, 150)
Positive y-direction.
 
Search WWH ::




Custom Search