Java Reference
In-Depth Information
curves, shapes, filled shapes, as well as images, and gives you a great deal of control over the drawing pro-
cess.
The Graphics2D class is derived from the java.awt.Graphics class and references to graphics contexts
are usually passed around as type Graphics , so you need to be aware of it. This is because the Component
class defines a getGraphics() method that returns a reference to a graphics context as type Graphics and
the Swing component classes, which are subclasses of Component, typically override this method. Note
that both the Graphics and Graphics2D classes are abstract classes, so you can't create objects of either
type directly. An object representing a graphics context is entirely dependent on the component to which it
relates, so a graphics context is always obtained for use with a particular component.
The Graphics2D object for a component takes care of mapping user coordinates to device coordinates,
so it contains information about the device that is the destination for output as well as the user coordinates
for the component. The information required for converting user coordinates to device coordinates is encap-
sulated in objects of three different types that are defined in the java.awt package:
• A GraphicsEnvironment object encapsulates all the graphics devices (as GraphicsDevice ob-
jects) and fonts (as Font objects) that are available to a Java application on your computer.
• A GraphicsDevice object encapsulates information about a particular device, such as a screen or
a printer, and stores it in one or more GraphicsConfiguration objects.
• A GraphicsConfiguration object defines the characteristics of a particular device, such as a
screen or a printer. Your display screen typically has several GraphicsConfiguration objects as-
sociated with it, each corresponding to a particular combination of screen resolution and number
of displayable colors.
The graphics context also maintains other information necessary for drawing operations, such as the
drawing color, the line style, and the specification of the fill color and pattern for filled shapes. You see how
to work with these attributes in examples later in this chapter.
Because a graphics context defines the drawing context for a specific component, you must have a ref-
erence to the graphics context object for a component before you can draw on the component. For the most
part, you draw on a component by implementing the paint() method that is inherited from JComponent .
This method is called whenever the component needs to be reconstructed. A reference to an object repres-
enting the graphics context for the component is passed as an argument to the paint() method, and you
use this object to do the drawing. The graphics context includes all the methods that you use to draw on a
component, and I'll introduce you to many of these in this chapter.
The paint() method is not the only way of drawing on a component. You can obtain a graphics context
for a component at any time just by calling its getGraphics() method. You can then use methods for the
Graphics object to specify the drawing operations.
There are occasions when you want to get a component redrawn while avoiding a direct call of the
paint() method. In such cases you should call repaint() for the component, versions of which are inher-
ited in a Swing component class from the Component and JComponent classes. This situation arises when
you make a succession of changes to what is drawn on a component, but want to defer redrawing the com-
ponent until all the changes have been made. Five versions of the repaint() method are available; here are
four of them:
repaint() causes the entire component to be repainted by calling its paint() method after all
the currently outstanding events have been processed.
repaint(long msec) requests that a call to paint() should occur within msec milliseconds.
Search WWH ::




Custom Search