The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
You can modify the transform attribute in the 可以在Graphics2D context to move, rotate, scale, and shear graphics primitives when they are rendered.Graphics2D上下文中修改“变换”属性,以在渲染图形基本体时移动、旋转、缩放和剪切图形基本体。The transform attribute is defined by an instance of the AffineTransform class.transform属性由AffineTransform类的实例定义。An affine transform is a transformation such as translate, rotate, scale, or shear in which parallel lines remain parallel even after being transformed.仿射变换是一种变换,例如平移、旋转、缩放或剪切,其中平行线即使在变换后仍保持平行。
The Graphics2D class provides several methods for changing the transform attribute.Graphics2D类提供了几种更改变换属性的方法。You can construct a new 可以通过调用AffineTransform and change the Graphics2D transform attribute by calling transform.transform构造新的AffineTransform并更改Graphics2D变换属性。
AffineTransform defines the following factory methods to make it easier to construct new transforms:
getRotateInstancegetScaleInstancegetShearInstancegetTranslateInstanceAlternatively you can use one of the Graphics2D transformation 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 AffineTransform object directly and concatenate it with the current transform by calling the transform method.
The drawImage method is also overloaded to allow you to specify an AffineTransform that is applied to the image as it is rendered. Specifying a transform when you call drawImage does not affect the Graphics2D transform attribute.
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.
contains the complete code for this applet.Transform.java
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();
This transform will be restored to the Graphics2D after rendering.
After retrieving the current transform, another AffineTransform, toCenterAt, is created that causes shapes to be rendered in the center of the panel. The at AffineTransform is concatenated onto toCenterAt:
AffineTransform toCenterAt = new AffineTransform(); toCenterAt.concatenate(at); toCenterAt.translate(-(r.width/2), -(r.height/2));
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);
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:
getTransform method to get the current transform.transform, translate, scale, shear, or rotate to concatenate a transform.setTransform method.