Java Reference
In-Depth Information
Note
Graphics2D
also declares
void setRenderingHints(Map<?,?>
hints)
fordiscardingallrenderinghintsandinstallingonlythoserenderinghintsin
the specified map.
Shapes
The
Shape
interface represents a vector-based geometric shape, such as a rectangle
or an ellipse. It declares
getBounds()
,
contains()
,
intersects()
, and
getPathIterator()
methods:
• The
getBounds()
methodsreturnrectanglesthatenclosetheshape'sbound-
aries; these rectangles serve as bounding boxes.
• Shapes have interiors and exteriors. The
contains()
methods tell you if a
point or a rectangle lies inside a shape.
• The
intersects()
methodstellyouifanypartofarectangleintersectsthe
shape's interior.
• The
getPathIterator()
methods return shape outlines.
Thefirstthree method categories areusefulinawiderangeoftasks, suchasgame-
based
collision detection
(are two shapes occupying the same space?) and graphics
application-based
hit testing
(was the mouse cursor over a specific shape when the
mouse button was pressed?)—perhaps the graphics application lets the user drag a se-
lected shape. The latter method helps the rendering pipeline obtain a shape outline.
Oneofthe
contains()
methodstakesa
java.awt.geom.Point2D
argument.
Instances of this class specify points in user space. (
Point2D
instances aren't shapes
because
Point2D
doesn't implement
Shape
.)
Point2D
reveals a pattern that's followed by the shape classes. This abstract class
contains a pair of nested
Double
and
Float
concrete subclasses, which override its
abstractmethods.Instantiate
Double
toincreaseaccuracyandinstantiate
Float
toin-
crease performance.
Thefollowingexampleshowsyouhowtoinstantiate
Point2D
,tospecifypointsin
user space:
Point2D pt1 = new Point2D.Double(10.0, 20.0);
Point2D pt2 = new Point2D.Float(20.0f, 30.0f);
The
java.awt.geom
package contains various geometric classes that implement
Shape
:
Arc2D
,
Area
,
CubicCurve2D
,
Ellipse2D
,
GeneralPath
,
Line2D
,
Path2D
,
QuadCurve2D
,
Rectangle2D
,
RectangularShape
, and