Beruflich Dokumente
Kultur Dokumente
Download Ebooks
Download JDK
Search Java Tutorials
Hide TOC
Example: Transform
The following program is the same as StrokeandFill, but also allows the user to choose a transformation to apply to the selected object when it is
rendered.
Note: If you don't see the applet running, you need to install at least the Java SE Development Kit (JDK) 7 release.
Transform.java contains the complete code for this applet.
When a transform is chosen from the Transform menu, the transform is concatenated onto the AffineTransform at:
public 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
}
}
Before displaying the shape corresponding to the menu choices, the application first retrieves the current transform from the Graphics2D object:
AffineTransform saveXform = g2.getTransform()
The toCenterAt transform is concatenated onto the Graphics2D transform with the transform method:
g2.transform(toCenterAt)
After rendering is completed, the original transform is restored using the setTransform method:
g2.setTransform(saveXform)
Note: Never use the setTransform method to concatenate a coordinate transform onto an existing transform. The setTransform
method overwrites the Graphics2D object'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:
1. Use the getTransform method to get the current transform.
2. Use transform, translate, scale, shear, or rotate to concatenate a transform.
3. Perform the rendering.
4. Restore the original transform using the setTransform method.
Your use of this page and all the material on pages under "The Java Tutorials" banner is subject to these legal notices.
Copyright 1995, 2015 Oracle and/or its affiliates. All rights reserved.
Problems with the examples? Try Compiling and Running the Examples: FAQs.
Complaints? Compliments? Suggestions? Give us your feedback.