Java Reference
In-Depth Information
Several things could cause paintComponent() to be called, including the following:
The graphical user interface containing the component is displayed for the first
time.
n
A window that was displayed on top of the component is closed.
n
The graphical user interface containing the component is resized.
n
By creating a subclass of JPanel , you can override the panel's paintComponent()
method and put all your graphical operations in this method.
As you might have noticed, a Graphics object is sent to an interface component's
paintComponent() method rather than a Graphics2D object. To create a Graphics2D
object that represents the component's drawing surface, you must use casting to convert
it, as in the following example:
public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D)comp;
// ...
}
The comp2D object in this example was produced through the use of casting.
The Graphics Coordinate System
Java2D classes use the same x,y coordinate system you have used when setting the size
of frames and other components.
Java's coordinate system uses pixels as its unit of measure. The origin coordinate 0, 0 is
in the upper-left corner of a component.
The value of x coordinates increases to the right of 0, 0, and y coordinates increase
downward.
When you set the size of a frame by calling its setSize( int , int ) method, the frame's
upper-left corner is at 0, 0, and its lower-right corner is at the two arguments sent to
setSize() .
13
For example, the following statement creates a frame 425 pixels wide by 130 pixels tall
with its lower-right corner at 425, 130:
setSize(425, 130);
CAUTION
This differs from other drawing systems in which the 0, 0 origin is
at the lower left and y values increase in an upward direction.
Search WWH ::




Custom Search