Java Reference
In-Depth Information
RoundRectangle2D . RectangularShape is the abstract superclass of Arc2D ,
Ellipse2D , Rectangle2D , and RoundRectangle2D . Also, the
java.awt.Rectangle classthatIintroducedearlierinthischapterhasbeenretro-
fitted to extend Rectangle2D . Finally, GeneralPath is a legacy final class (you
cannot extend it) that extends Path2D.Float .
RoundRectangle2D describesarectanglewithroundedcornersofaspecificradi-
us.Itsnested Double and Float subclassesdeclarenoargumentconstructorsforcon-
structinganew RoundRectangle2D instancethat'sinitializedtolocation(0.0,0.0),
size (0.0,0.0),andcornerarcswithradius0.0.Theyalsodeclare constructors forspe-
cifying location, size, and corner arcs.
If you call the noargument constructors, you can subsequently call Double 's or
Float 's setRoundRect() methods to specify location, size, and rounded corner
radius. However, if all you have is a RoundRectangle2D reference (not a
RoundRectangle2D.Double or RoundRectangle2D.Float reference), you
can call RoundRectangle2D 's void setRoundRect(double x, double
y, double w, double h, double arcWidth, double arcHeight)
method. (You'll find this constructor/set pattern repeated in other shape classes.)
I'vecreateda DragRect applicationthatdemonstrates RoundRectangle2D and
Shape 's boolean contains(double x, double y) method. DragRect
shows you how to drag this round rectangle over its drawing surface, and Listing 7-9
presents its source code.
Listing 7-9 . Dragging a round rectangle
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.RoundRectangle2D;
Search WWH ::




Custom Search