public abstract class Graphics2D extends Graphics
Graphics2D
class extends the
Graphics
class to provide more sophisticated
control over
graphics operations.
Graphics2D
object are specified
in a device-independent coordinate system called User Space, which is
used by applications. The Graphics2D
object
behaves as if it contains
a transform
object as part of its internal rendering state
that defines how to convert coordinates from user space to
device-dependent coordinates in Device Space.
Some of the operations performed by the rendering attribute objects
occur in the device space, but all Graphics2D
methods take
user space coordinates.
Every Graphics2D
object is associated with a target that
defines where rendering takes place. A
GraphicsConfiguration
object defines the characteristics
of the rendering target, such as pixel format and resolution.
The same rendering target is used throughout the life of a
Graphics2D
object.
When creating a Graphics2D
object, the
GraphicsConfiguration
specifies the
transform for
the target of the Graphics2D
(a
Component
or Image
). This
transform maps the
user space coordinate system to screen and printer device coordinates
such that the origin maps to the upper left hand corner of the
target region of the device with increasing X coordinates extending
to the right and increasing Y coordinates extending downward.
The scaling of the
transform is set to identity for those devices
that are close to 72 dpi, such as screen devices.
The scaling of the
transform is set to approximately 72 user
space coordinates per square inch for high resolution devices, such as
printers. For image buffers, the
transform is the
identity
transform.
Graphics2D
rendering attributes.
The renderer can optimize many of these steps, either by caching the
results for future calls, by collapsing multiple virtual steps into
a single operation, or by recognizing various attributes as common
simple cases that can be eliminated by modifying other parts of the
operation.
The steps in the rendering process are:
Clip
.
The Clip
is specified by a Shape
in user
space and is controlled by the program using the various clip
manipulation methods of Graphics
.
This user clip
is transformed into device space by the
target transform
and combined with the
device clip, which is defined by the visibility of windows and
device extents. The combination of the user clip and device clip
defines the composite clip, which determines the final clipping
region. The user clip is not modified by the rendering
system to reflect the resulting composite clip.
Composite
attribute in the Graphics2D
context.
Shape
operations
draw
operation, then
the current Stroke
attribute in the
Graphics2D
context is used to construct a new
Shape
object that contains the outline of the specified
Shape
.
Shape
is transformed from user space to device space
using the
target transform
in the Graphics2D
context.
Color
in the Graphics2D
context
is
used to determine
the colors to render in device space.
String
:
String
, then the current
Font
in the Graphics2D
context is asked to
convert the Unicode characters in the String
into a set of
glyphs for presentation with whatever basic layout and shaping
algorithms the font implements.
AttributedCharacterIterator
,
its embedded font attributes
are used to implement
more sophisticated glyph layout algorithms
.
Font
is queried to obtain outlines for the
indicated glyphs. These outlines are treated as shapes in user space
relative to the position of each glyph that was determined in step 1.
Shape
operations.
Color
is
used to determine
the colors to render in device space.
Image
Operations
Image
.
This bounding box is specified in Image Space, which is the
Image
object's local coordinate system.
Image
is transformed from user
space into device space using the
target transform .
Note that the result of transforming the bounding box does not
necessarily result in a rectangular region in device space.
Image
object determines what colors to render,
sampled according to the source to destination
coordinate mapping specified by the
target transform .
Graphics2D
rendering attributes are:
Color
Component
.
Font
Font
of the Component
.
Stroke
BasicStroke
)
.
Composite
AlphaComposite.SRC_OVER
rule.
Clip
Clip
, the output is clipped to the
Component
.
Modifier | Constructor and Description |
---|---|
protected |
Graphics2D()
Constructs a new
Graphics2D object. |
Modifier and Type | Method and Description |
---|---|
abstract void |
drawString(String str,
int x,
int y)
Draws the text given by the specified string, using this
graphics context's current font and color.
|
abstract Composite |
getComposite()
Returns the current
Composite in the
Graphics2D context. |
abstract GraphicsConfiguration |
getDeviceConfiguration()
Returns the device configuration associated with this
Graphics2D . |
abstract Stroke |
getStroke()
Returns the current
Stroke in the
Graphics2D context. |
abstract void |
setComposite(Composite comp)
Sets the
Composite for the Graphics2D context. |
abstract void |
setStroke(Stroke s)
Sets the
Stroke for the Graphics2D context. |
clearRect, clipRect, copyArea, create, create, dispose, draw3DRect, drawArc, drawBytes, drawChars, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawLine, drawOval, drawPolygon, drawPolygon, drawPolyline, drawRect, drawRoundRect, drawString, fill3DRect, fillArc, fillOval, fillPolygon, fillPolygon, fillRect, fillRoundRect, finalize, getClip, getClipBounds, getClipBounds, getColor, getFont, getFontMetrics, getFontMetrics, hitClip, setClip, setClip, setColor, setFont, setPaintMode, setXORMode, toString, translate
protected Graphics2D()
Graphics2D
object. Since
Graphics2D
is an abstract class, and since it must be
customized by subclasses for different output devices,
Graphics2D
objects cannot be created directly.
Instead, Graphics2D
objects must be obtained from another
Graphics2D
object, created by a
Component
, or obtained from images such as
BufferedImage
objects.Component.getGraphics()
,
Graphics.create()
public abstract void drawString(String str, int x, int y)
drawString
in class Graphics
str
- the string to be renderedx
- the x coordinate of the location where the
String
should be renderedy
- the y coordinate of the location where the
String
should be renderedNullPointerException
- if str
is
null
Graphics.drawBytes(byte[], int, int, int, int)
,
Graphics.drawChars(char[], int, int, int, int)
public abstract GraphicsConfiguration getDeviceConfiguration()
Graphics2D
.Graphics2D
.public abstract void setComposite(Composite comp)
Composite
for the Graphics2D
context.
The Composite
is used in all drawing methods such as
drawImage
, drawString
, draw
,
and fill
. It specifies how new pixels are to be combined
with the existing pixels on the graphics device during the rendering
process.
Note: This operation is subject to
restriction
in this Profile. If the Composite
is a
custom object rather than an
instance of the AlphaComposite
class then
IllegalArgumentException
is thrown.
comp
- the Composite
object to be used for renderingIllegalArgumentException
- If comp
is not
an instance of AlphaComposite
.getComposite()
,
AlphaComposite
public abstract void setStroke(Stroke s)
Stroke
for the Graphics2D
context.s
- the Stroke
object to be used to stroke a
Shape
during the rendering process
Note: This operation is subject to
restriction
in this Profile. If the Stroke
is a
custom object rather than an
instance of the BasicStroke
class then
IllegalArgumentException
is thrown.
BasicStroke
,
getStroke()
public abstract Composite getComposite()
Composite
in the
Graphics2D
context.Graphics2D
Composite
,
which defines a compositing style.setComposite(java.awt.Composite)
public abstract Stroke getStroke()
Stroke
in the
Graphics2D
context.Graphics2D
Stroke
,
which defines the line style.setStroke(java.awt.Stroke)
Copyright © 2012 CableLabs. All Rights Reserved.