Java Reference
In-Depth Information
Incidentally, the drawing process is often referred to as rendering , since graphical output devices
are generally raster devices and the drawing elements such as lines, rectangles, text, and so on need
to be rendered into a rasterized representation before they can be output to the device.
Having a device-independent coordinate system for drawing means that you can use essentially the
same code for outputting graphics to a variety of different devices - to your display screen, for example,
or to your printer - even though these devices themselves have quite different coordinate systems with
different resolutions. The fact that your screen might have 90 pixels per inch while your printer may
have 600 dots per inch is automatically taken care of. Java 2D will deal with converting your user
coordinates to the device coordinate system that is specific to the output device you are using.
With the default mapping from user coordinates to device coordinates, the units for user coordinates are
assumed to be 1/72 of an inch. Since for most screen devices the pixels are approximately 1/72 inch
apart, the conversion amounts to an identity transformation. If you want to use user coordinates that are
in some other units, you have to provide for this yourself. We will look into the mechanism that you
would use to do this when we discuss transformations in the next chapter.
Graphics Contexts
The user coordinate system for drawing on a component using Java 2D is encapsulated in an object of
type Graphics2D , which is usually referred to as a graphics context . It provides all the tools you need
to draw whatever you want on the surface of the component. A graphics context enables you to draw
lines, curves, shapes, filled shapes, as well as images, and gives you a great deal of control over the
drawing process.
The Graphics2D class is derived from the Graphics class that defined device contexts in earlier
versions of Java, so if you feel the need to use the old drawing methods, they are all inherited in the
Graphics2D class. We will be concentrating on the new more powerful and flexible facilities provided
by Graphics2D but, as you will see, references to graphics contexts are usually passed around as type
Graphics so you need to be aware of it. 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 encapsulated in three different kinds of object:
A GraphicsEnvironment object encapsulates all the graphics devices (as
GraphicsDevice objects) and fonts (as Font objects) that are available 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 will typically have several
GraphicsConfiguration objects associated with it, each corresponding to a particular
combination of screen resolution and number of displayable colors.
Search WWH ::




Custom Search