Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


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:

1.  Compute a Stroke to fill based on the Shape.
2.  Transform the Shape from user space into device space.
3.  Call Shape.getPathIterator() to extract the outline of the Shape. (The outline is an instance of class PathIterator, which may contain curved segments.)
4.  If the Graphic2D object cannot handle the curved segments in the PathIterator, it calls an alternative version of Shape.getPathIterator() that accepts a flatness parameter. This alternative version only returns straight line segments.

The Shape’s 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.


NOTE:  BasicStroke’s default constructor specifies a line width of 1.0, a CAP_SQUARE style at the ends of lines, a JOIN_MITRE style where lines come together, a mitre limit of 10.0, and no dashing. (A mitre limit is the limit at which to trim the mitre join where two lines come together.)


FIGURE 39.17  Class BasicStroke supports three ways to join lines—JOIN_MITRE is the default.

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:

  GENERAL_ROTATION—Rotation through an arbitrary angle.
  GENERAL_SCALE—Scale by arbitrary factors in x and y.
  GENERAL_TRANSFORM—Arbitrary conversion of the input coordinates.
  IDENTITY—The output is identical to the input.
  QUADRANT_ROTATION—Rotation through a multiple of 90 degrees
  TRANSLATION—The graphic is moved in the x and y dimensions.
  UNIFORM_SCALE—The graphic is scaled uniformly in both dimensions.

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 glyphs—integer 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 Font’s 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 doesn’t supply a transform, an identity transform is used.

The bounding rectangle—now in user coordinate space—is 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 space—Graphics2D transforms it into device space by using the current transform.


Suppose you had a complex graphic, or an animation, in which performance was important. You could improve performance by limiting the rendering area by using setClip().

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.)


Previous Table of Contents Next


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.