Java Reference
In-Depth Information
display of a component, a graphics context can represent an off-screen image as
well.
Beginning with Java version 1.2, the object reference passed in the
paint
(Graphics g)
and
paintComponent (Graphics g)
methods became a
subclass of
Graphics
called
Graphics2D
. Since it is a subclass, you can
still treat it as a
Graphics
object or, for more functionality, you can cast the
Graphics
object type to a
Graphics2D
type to use the
Graphics2D
meth-
ods. The
Graphcs2D
class provides the drawing context for the Java 2D API,
which offers a greatly expanded set of drawing capabilities. The following pack-
ages comprise Java 2D:
java.awt
,
java.awt.color
,
java.awt.font
,
java.awt.geom
,
java.awt.image
, and
java.awt.print
. Some of these
packages existed from Java 1.0 but they were expanded to provide additional
capabilities for Java 2D.
We first briefly describe the basic drawing capabilities of the plain vanilla
Graphics
class and then look at some of the features of
Graphics2D
.
Note that you can draw with both the old and new methods in the same con-
text. In Chapter 11 we discuss image handling and processing with Java 2D
tools.
6.6.2 Graphics coordinate system
The coordinate system (x
=
horizontal, y
=
vertical) for the drawing surface goes
as follows:
origin (0,0) at top-left corner
x increases towards the right, beginning at 0
maximum x
=
width
−
1
y increases towards the bottom, beginning at 0
maximum y
=
height
−
1
We see later that with the Java 2D tools, the origin can be moved or
translated
.
Forexample, it can be mathematically convenient in some cases to work with the
origin at the center of the component's drawing area rather than the top-left
corner.
We can obtain the dimensions of a component in two ways. The
getSize()
method returns an instance of the
Dimension
class, which provides direct access
to its
height
and
width
variables. As of Java 1.2 the
component
class provided
the methods
getHeight()
and
getWidth()
.
Java 2D makes a clear distinction between the user coordinate space where
the drawing methods operate and the device coordinate space of computer mon-
itors and printers. While the
Graphics
methods work only with integer pixel
Search WWH ::
Custom Search