|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
where g is the Graphics object passed in as the parameter of paint(). Next, the program constructs a new shape inside a GeneralPath: GeneralPath p = new GeneralPath(1); p.moveTo( theSize.width/6, theSize.height/6); p.lineTo(theSize.width*5/6, theSize.height/6); p.lineTo(theSize.width*5/6, theSize.height*5/6); p.lineTo( theSize.width/6, theSize.height*5/6); p.closePath(); Finally, the author uses the new shape: g2.draw(p); ... g2.fill(p); ... for (n=0; n < 10; n++) { at.scale(.9, .9); at.rotate(15, theSize.width/2, theSize.height/2); g2.setTransform(at); ... g2.fill(p); } where at is an AffineTransform, described later. When the object is a shape, as it is in this case, the Graphics2D class will determine where to render it by following a four-step procedure to compute the outline of the shape:
The Shapes outline will be rendered by using an implementation of the java.awt.Stroke interface. To make an ellipse, you might write draw(Ellipse2D.Float(10.0, 10.0, 150.0, 100.0)); but the Graphics2D class will implement that call as BasicStroke theStroke = new BasicStroke(); theStroke.createStrokedShape(new Ellipse2D.Float(10.0, 10.0, 150.0, 100.0); BasicStroke is a class that implements Stroke. Figure 39.17 illustrates the concept of a mitre limit, the part of BasicStroke that determines whether two lines are joined when they pass close to each other.
Ellipse2D is an abstract Shape; Ellipse2D.Float is a concrete class that accepts floating point coordinates. To transform the Shape into device space, Graphics2D calls the currently defined transform. Transforms are instances of class AffineTransform, which supports the following types of transform:
In Listing 39.18 you see the lines that set up and use an AffineTransform: AffineTransform at = new AffineTransform(); at.scale(.5, .5); at.translate(theSize.width/2, theSize.height/2); g2.setTransform(at); and later for(n = 0; n < 10; n++){ at.scale(.9, .9); at.rotate(15, theSize.width/2, theSize.height/2); g2.setTransform(at); . . . } Determining Where to Render Text If the Graphics2D represents text rather than a shape, it is rendered by using glyphsinteger codes used by Fonts to represent text graphically. If the text is in a String, the String is sent to the current Font, which is asked to compute a java.awt.font.GlyphSet based on the Fonts default layout. If the text is in a StyledString, the StyledString itself computes the GlyphSet based on its own font attributes. If the text is already a GlyphSet, then this step is skipped. After Graphics2D has a GlyphSet, it asks the current Font to convert the glyphs to shapes. It then starts the process of rendering the shapes, as described earlier in this section. Determining Where to Render an Image If the Graphics2D is an image (as indicated by a call to the drawImage() method, the class computes a bounding rectangle for the image in a local coordinate system called image space. If the programmer has specified a transform, that transform is used to convert the bounding rectangle from image space coordinates to user space coordinates. If the programmer doesnt supply a transform, an identity transform is used. The bounding rectanglenow in user coordinate spaceis transformed again, into device space. Constraining the Graphic with Clip Graphics2D inherits getClip() and setClip() methods from its base class, Graphics. You can use setClip() to limit the rendering to a specified rectangle. You specify the clipRect in user spaceGraphics2D transforms it into device space by using the current transform.
After Graphics2D has computed the rendering region (for either a graphic, text, or an image) it applies the clipRect to define the region that will actually be rendered. Determining the Colors If the Graphics2D object is an Image, the class samples the colors in the Image based on the current transform. (If you specify an Image transform, that transform is also applied during color sampling.)
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |