Java Reference
In-Depth Information
Rounded Rectangles, BufferedImage s and TexturePaint Objects
Next we draw a rounded rectangle filled with a pattern created in a BufferedImage (pack-
age java.awt.image ) object. Lines 38-39 create the BufferedImage object. Class Buff-
eredImage can be used to produce images in color and grayscale. This particular
BufferedImage is 10 pixels wide and 10 pixels tall (as specified by the first two arguments
of the constructor). The third argument BufferedImage.TYPE_INT_RGB indicates that the
image is stored in color using the RGB color scheme.
To create the rounded rectangle's fill pattern, we must first draw into the Buffered-
Image . Line 42 creates a Graphics2D object (by calling BufferedImage method create-
Graphics ) that can be used to draw into the BufferedImage . Lines 43-50 use methods
setColor , fillRect and drawRect to create the pattern.
Lines 53-54 set the Paint object to a new TexturePaint (package java.awt ) object.
A TexturePaint object uses the image stored in its associated BufferedImage (the first
constructor argument) as the fill texture for a filled-in shape. The second argument spec-
ifies the Rectangle area from the BufferedImage that will be replicated through the tex-
ture. In this case, the Rectangle is the same size as the BufferedImage . However, a smaller
portion of the BufferedImage can be used.
Lines 55-56 use Graphics2D method fill to draw a filled Shape object—in this case,
a RoundRectangle2D.Double . The constructor for class RoundRectangle2D.Double
receives six arguments specifying the rectangle dimensions and the arc width and arc
height used to determine the rounding of the corners.
Arcs
Next we draw a pie-shaped arc with a thick white line. Line 59 sets the Paint object to
Color.WHITE . Line 60 sets the Stroke object to a new BasicStroke for a line 6 pixels wide.
Lines 61-62 use Graphics2D method draw to draw a Shape object—in this case, an
Arc2D.Double . The Arc2D.Double constructor's first four arguments specify the upper-
left x -coordinate, upper-left y -coordinate, width and height of the bounding rectangle for
the arc. The fifth argument specifies the start angle. The sixth argument specifies the arc
angle. The last argument specifies how the arc is closed . Constant Arc2D.PIE indicates that
the arc is closed by drawing two lines—one line from the arc's starting point to the center
of the bounding rectangle and one line from the center of the bounding rectangle to the
ending point. Class Arc2D provides two other static constants for specifying how the arc is
closed . Constant Arc2D.CHORD draws a line from the starting point to the ending point.
Constant Arc2D.OPEN specifies that the arc should not be closed .
Lines
Finally, we draw two lines using Line2D objects—one solid and one dashed. Line 65 sets
the Paint object to Color.GREEN . Line 66 uses Graphics2D method draw to draw a Shape
object—in this case, an instance of class Line2D.Double . The Line2D.Double construc-
tor's arguments specify the starting coordinates and ending coordinates of the line.
Line 69 declares a one-element float array containing the value 10. This array
describes the dashes in the dashed line. In this case, each dash will be 10 pixels long. To
create dashes of different lengths in a pattern, simply provide the length of each dash as an
element in the array. Line 70 sets the Paint object to Color.YELLOW . Lines 71-72 set the
Stroke object to a new BasicStroke . The line will be 4 pixels wide and will have rounded
ends ( BasicStroke.CAP_ROUND ). If lines join together (as in a rectangle at the corners),
 
Search WWH ::




Custom Search