Java Reference
In-Depth Information
18.3
The Graphics Class
Drawing is my life!
THE GRAPHICS CLASS
In this section, we show you how to produce drawing for your GUIs using the
Graphics class.
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 such as (200, 150) to
specify where the rectangle is located.
Note that, when specifying the location of a rectangle or other figure, the coordinates
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
figure 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
Display 18.10
Screen Coordinate System
(0, 0)
Positive x-direction.
(200, 150)
Positive y-direction.
 
 
 
Search WWH ::




Custom Search