📄 affinetransform.java
字号:
/* * Copyright (C) 2005, 2006 data2c GmbH (www.data2c.com) * * Author: Wolfgang S. Kechel - wolfgang.kechel@data2c.com * * J2ME version of java.awt.geom.AffineTransform. *///#ifndef j2sepackage org.awt.geom;//#ifdef notdefimport java.awt.Shape;//#endif/** * The <code>AffineTransform</code> class represents a 2D affine transform * that performs a linear mapping from 2D coordinates to other 2D * coordinates that preserves the "straightness" and * "parallelness" of lines. Affine transformations can be constructed * using sequences of translations, scales, flips, rotations, and shears. * <p> * Such a coordinate transformation can be represented by a 3 row by * 3 column matrix with an implied last row of [ 0 0 1 ]. This matrix * transforms source coordinates <code>(x, y)</code> into * destination coordinates <code>(x', y')</code> by considering * them to be a column vector and multiplying the coordinate vector * by the matrix according to the following process: * <pre> * [ x'] [ m00 m01 m02 ] [ x ] [ m00x + m01y + m02 ] * [ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ] * [ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ] * </pre> */public class AffineTransform /*implements Cloneable, java.io.Serializable*/ { /* * This constant is only useful for the cached type field. * It indicates that the type has been decached and must be recalculated. */ private static final int TYPE_UNKNOWN = -1; /** * This constant indicates that the transform defined by this object * is an identity transform. * An identity transform is one in which the output coordinates are * always the same as the input coordinates. * If this transform is anything other than the identity transform, * the type will either be the constant GENERAL_TRANSFORM or a * combination of the appropriate flag bits for the various coordinate * conversions that this transform performs. * @see #TYPE_TRANSLATION * @see #TYPE_UNIFORM_SCALE * @see #TYPE_GENERAL_SCALE * @see #TYPE_FLIP * @see #TYPE_QUADRANT_ROTATION * @see #TYPE_GENERAL_ROTATION * @see #TYPE_GENERAL_TRANSFORM * @see #getType */ public static final int TYPE_IDENTITY = 0; /** * This flag bit indicates that the transform defined by this object * performs a translation in addition to the conversions indicated * by other flag bits. * A translation moves the coordinates by a constant amount in x * and y without changing the length or angle of vectors. * @see #TYPE_IDENTITY * @see #TYPE_UNIFORM_SCALE * @see #TYPE_GENERAL_SCALE * @see #TYPE_FLIP * @see #TYPE_QUADRANT_ROTATION * @see #TYPE_GENERAL_ROTATION * @see #TYPE_GENERAL_TRANSFORM * @see #getType */ public static final int TYPE_TRANSLATION = 1; /** * This flag bit indicates that the transform defined by this object * performs a uniform scale in addition to the conversions indicated * by other flag bits. * A uniform scale multiplies the length of vectors by the same amount * in both the x and y directions without changing the angle between * vectors. * This flag bit is mutually exclusive with the TYPE_GENERAL_SCALE flag. * @see #TYPE_IDENTITY * @see #TYPE_TRANSLATION * @see #TYPE_GENERAL_SCALE * @see #TYPE_FLIP * @see #TYPE_QUADRANT_ROTATION * @see #TYPE_GENERAL_ROTATION * @see #TYPE_GENERAL_TRANSFORM * @see #getType */ public static final int TYPE_UNIFORM_SCALE = 2; /** * This flag bit indicates that the transform defined by this object * performs a general scale in addition to the conversions indicated * by other flag bits. * A general scale multiplies the length of vectors by different * amounts in the x and y directions without changing the angle * between perpendicular vectors. * This flag bit is mutually exclusive with the TYPE_UNIFORM_SCALE flag. * @see #TYPE_IDENTITY * @see #TYPE_TRANSLATION * @see #TYPE_UNIFORM_SCALE * @see #TYPE_FLIP * @see #TYPE_QUADRANT_ROTATION * @see #TYPE_GENERAL_ROTATION * @see #TYPE_GENERAL_TRANSFORM * @see #getType */ public static final int TYPE_GENERAL_SCALE = 4; /** * This constant is a bit mask for any of the scale flag bits. * @see #TYPE_UNIFORM_SCALE * @see #TYPE_GENERAL_SCALE */ public static final int TYPE_MASK_SCALE = (TYPE_UNIFORM_SCALE | TYPE_GENERAL_SCALE); /** * This flag bit indicates that the transform defined by this object * performs a mirror image flip about some axis which changes the * normally right handed coordinate system into a left handed * system in addition to the conversions indicated by other flag bits. * A right handed coordinate system is one where the positive X * axis rotates counterclockwise to overlay the positive Y axis * similar to the direction that the fingers on your right hand * curl when you stare end on at your thumb. * A left handed coordinate system is one where the positive X * axis rotates clockwise to overlay the positive Y axis similar * to the direction that the fingers on your left hand curl. * There is no mathematical way to determine the angle of the * original flipping or mirroring transformation since all angles * of flip are identical given an appropriate adjusting rotation. * @see #TYPE_IDENTITY * @see #TYPE_TRANSLATION * @see #TYPE_UNIFORM_SCALE * @see #TYPE_GENERAL_SCALE * @see #TYPE_QUADRANT_ROTATION * @see #TYPE_GENERAL_ROTATION * @see #TYPE_GENERAL_TRANSFORM * @see #getType */ public static final int TYPE_FLIP = 64; /* NOTE: TYPE_FLIP was added after GENERAL_TRANSFORM was in public * circulation and the flag bits could no longer be conveniently * renumbered without introducing binary incompatibility in outside * code. */ /** * This flag bit indicates that the transform defined by this object * performs a quadrant rotation by some multiple of 90 degrees in * addition to the conversions indicated by other flag bits. * A rotation changes the angles of vectors by the same amount * regardless of the original direction of the vector and without * changing the length of the vector. * This flag bit is mutually exclusive with the TYPE_GENERAL_ROTATION flag. * @see #TYPE_IDENTITY * @see #TYPE_TRANSLATION * @see #TYPE_UNIFORM_SCALE * @see #TYPE_GENERAL_SCALE * @see #TYPE_FLIP * @see #TYPE_GENERAL_ROTATION * @see #TYPE_GENERAL_TRANSFORM * @see #getType */ public static final int TYPE_QUADRANT_ROTATION = 8; /** * This flag bit indicates that the transform defined by this object * performs a rotation by an arbitrary angle in addition to the * conversions indicated by other flag bits. * A rotation changes the angles of vectors by the same amount * regardless of the original direction of the vector and without * changing the length of the vector. * This flag bit is mutually exclusive with the * TYPE_QUADRANT_ROTATION flag. * @see #TYPE_IDENTITY * @see #TYPE_TRANSLATION * @see #TYPE_UNIFORM_SCALE * @see #TYPE_GENERAL_SCALE * @see #TYPE_FLIP * @see #TYPE_QUADRANT_ROTATION * @see #TYPE_GENERAL_TRANSFORM * @see #getType */ public static final int TYPE_GENERAL_ROTATION = 16; /** * This constant is a bit mask for any of the rotation flag bits. * @see #TYPE_QUADRANT_ROTATION * @see #TYPE_GENERAL_ROTATION */ public static final int TYPE_MASK_ROTATION = (TYPE_QUADRANT_ROTATION | TYPE_GENERAL_ROTATION); /** * This constant indicates that the transform defined by this object * performs an arbitrary conversion of the input coordinates. * If this transform can be classified by any of the above constants, * the type will either be the constant TYPE_IDENTITY or a * combination of the appropriate flag bits for the various coordinate * conversions that this transform performs. * @see #TYPE_IDENTITY * @see #TYPE_TRANSLATION * @see #TYPE_UNIFORM_SCALE * @see #TYPE_GENERAL_SCALE * @see #TYPE_FLIP * @see #TYPE_QUADRANT_ROTATION * @see #TYPE_GENERAL_ROTATION * @see #getType */ public static final int TYPE_GENERAL_TRANSFORM = 32; /** * This constant is used for the internal state variable to indicate * that no calculations need to be performed and that the source * coordinates only need to be copied to their destinations to * complete the transformation equation of this transform. * @see #APPLY_TRANSLATE * @see #APPLY_SCALE * @see #APPLY_SHEAR * @see #state */ static final int APPLY_IDENTITY = 0; /** * This constant is used for the internal state variable to indicate * that the translation components of the matrix (m02 and m12) need * to be added to complete the transformation equation of this transform. * @see #APPLY_IDENTITY * @see #APPLY_SCALE * @see #APPLY_SHEAR * @see #state */ static final int APPLY_TRANSLATE = 1; /** * This constant is used for the internal state variable to indicate * that the scaling components of the matrix (m00 and m11) need * to be factored in to complete the transformation equation of * this transform. If the APPLY_SHEAR bit is also set then it * indicates that the scaling components are not both 0.0. If the * APPLY_SHEAR bit is not also set then it indicates that the * scaling components are not both 1.0. If neither the APPLY_SHEAR * nor the APPLY_SCALE bits are set then the scaling components * are both 1.0, which means that the x and y components contribute * to the transformed coordinate, but they are not multiplied by * any scaling factor. * @see #APPLY_IDENTITY * @see #APPLY_TRANSLATE * @see #APPLY_SHEAR * @see #state */ static final int APPLY_SCALE = 2; /** * This constant is used for the internal state variable to indicate * that the shearing components of the matrix (m01 and m10) need * to be factored in to complete the transformation equation of this * transform. The presence of this bit in the state variable changes * the interpretation of the APPLY_SCALE bit as indicated in its * documentation. * @see #APPLY_IDENTITY * @see #APPLY_TRANSLATE * @see #APPLY_SCALE * @see #state */ static final int APPLY_SHEAR = 4; /* * For methods which combine together the state of two separate * transforms and dispatch based upon the combination, these constants * specify how far to shift one of the states so that the two states * are mutually non-interfering and provide constants for testing the * bits of the shifted (HI) state. The methods in this class use * the convention that the state of "this" transform is unshifted and * the state of the "other" or "argument" transform is shifted (HI). */ private static final int HI_SHIFT = 3; private static final int HI_IDENTITY = APPLY_IDENTITY << HI_SHIFT; private static final int HI_TRANSLATE = APPLY_TRANSLATE << HI_SHIFT; private static final int HI_SCALE = APPLY_SCALE << HI_SHIFT; private static final int HI_SHEAR = APPLY_SHEAR << HI_SHIFT; /** * The X coordinate scaling element of the 3x3 * affine transformation matrix. * * @serial */ double m00; /** * The Y coordinate shearing element of the 3x3 * affine transformation matrix. * * @serial */ double m10; /** * The X coordinate shearing element of the 3x3 * affine transformation matrix. * * @serial */ double m01; /** * The Y coordinate scaling element of the 3x3 * affine transformation matrix. * * @serial */ double m11; /** * The X coordinate of the translation element of the * 3x3 affine transformation matrix. * * @serial */ double m02; /** * The Y coordinate of the translation element of the * 3x3 affine transformation matrix. * * @serial */ double m12; /** * This field keeps track of which components of the matrix need to * be applied when performing a transformation. * @see #APPLY_IDENTITY * @see #APPLY_TRANSLATE * @see #APPLY_SCALE * @see #APPLY_SHEAR */ transient int state; /** * This field caches the current transformation type of the matrix. * @see #TYPE_IDENTITY * @see #TYPE_TRANSLATION * @see #TYPE_UNIFORM_SCALE * @see #TYPE_GENERAL_SCALE * @see #TYPE_FLIP * @see #TYPE_QUADRANT_ROTATION * @see #TYPE_GENERAL_ROTATION * @see #TYPE_GENERAL_TRANSFORM * @see #TYPE_UNKNOWN * @see #getType */ private transient int type; private AffineTransform(double m00, double m10, double m01, double m11, double m02, double m12, int state) { this.m00 = m00; this.m10 = m10; this.m01 = m01; this.m11 = m11; this.m02 = m02; this.m12 = m12; this.state = state; this.type = TYPE_UNKNOWN; } /** * Constructs a new <code>AffineTransform</code> representing the * Identity transformation. */ public AffineTransform() { m00 = m11 = 1.0; // m01 = m10 = m02 = m12 = 0.0; /* Not needed. */ // state = APPLY_IDENTITY; /* Not needed. */ // type = TYPE_IDENTITY; /* Not needed. */ } /** * Constructs a new <code>AffineTransform</code> that is a copy of * the specified <code>AffineTransform</code> object. * @param Tx the <code>AffineTransform</code> object to copy */ public AffineTransform(AffineTransform Tx) { this.m00 = Tx.m00; this.m10 = Tx.m10; this.m01 = Tx.m01; this.m11 = Tx.m11; this.m02 = Tx.m02; this.m12 = Tx.m12; this.state = Tx.state; this.type = Tx.type; } /** * Constructs a new <code>AffineTransform</code> from 6 floating point * values representing the 6 specifiable entries of the 3x3 * transformation matrix. * @param m00, m01, m02, m10, m11, m12 the * 6 floating point values that compose the 3x3 transformation matrix */ public AffineTransform(float m00, float m10, float m01, float m11, float m02, float m12) { this.m00 = m00; this.m10 = m10; this.m01 = m01;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -