|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
You can modify the transform attribute in theGraphics2Dcontext to move, rotate, scale, and shear graphics primitives when they are rendered. The transform attribute is defined by an instance ofAffineTransform. (An affine transform is a transformation such as translate, rotate, scale, or shear in which parallel lines remain parallel even after being transformed.)
Graphics2Dprovides several methods for changing the transform attribute. You can construct a newAffineTransformand change theGraphics2Dtransform attribute by callingtransform.
AffineTransformdefines the following factory methods to make it easier to construct new transforms:
getRotateInstancegetScaleInstancegetShearInstancegetTranslateInstanceAlternatively you can use one of the
Graphics2Dtransformation methods to modify the current transform. When you call one of these convenience methods, the resulting transform is concatenated with the current transform and is applied during rendering:
rotate--to specify an angle of rotation in radiansscale--to specify a scaling factor in the x and y directionsshear--to specify a shearing factor in the x and y directionstranslate--to specify a translation offset in the x and y directionsYou can also construct an
AffineTransformdirectly and concatenate it with the current transform by calling thetransformmethod.The
drawImagemethod is also overloaded to allow you to specify anAffineTransformthat is applied to the image as it is rendered. Specifying a transform when you calldrawImagedoes not affect theGraphics2Dtransform attribute.
The following program is the same asStrokeandFill, but also allows the user to choose a transformation to apply to the selected object when it is rendered.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.
Transform.javacontains the complete code for this applet.
When a transform is chosen from the Transform menu, the transform is concatenated onto the
AffineTransformat:Before displaying the shape corresponding to the menu choices, the application first retrieves the current transform from thepublic void setTrans(int transIndex) { // Sets the AffineTransform. switch ( transIndex ) { case 0 : at.setToIdentity(); at.translate(w/2, h/2); break; case 1 : at.rotate(Math.toRadians(45)); break; case 2 : at.scale(0.5, 0.5); break; case 3 : at.shear(0.5, 0.0); break; } }Graphics2Dobject:This transform will be restored to theAffineTransform saveXform = g2.getTransform();Graphics2Dafter rendering.After retrieving the current transform, another
AffineTransform,toCenterAt, is created that causes shapes to be rendered in the center of the panel. TheatAffineTransformis concatenated ontotoCenterAt:TheAffineTransform toCenterAt = new AffineTransform(); toCenterAt.concatenate(at); toCenterAt.translate(-(r.width/2), -(r.height/2));toCenterAttransform is concatenated onto theGraphics2Dtransform with thetransformmethod:After rendering is completed, the original transform is restored using theg2.transform(toCenterAt);setTransformmethod:g2.setTransform(saveXform);
Note: Never usesetTransformto concatenate a coordinate transform onto an existing transform. ThesetTransformmethod overwrites theGraphics2Dobject's current transform, which might be needed for other reasons, such as positioning Swing and lightweight components in a window. Use these steps to perform transformations:
- Use
getTransformto get the current transform.- Use
transform,translate,scale,shear, orrotateto concatenate a transform.- Perform the rendering.
- Restore the original transform using
setTransform.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.