public class AffineTransform extends Object implements Cloneable, Serializable
The transformation can be represented using matrix math on a 3x3 array. Given (x,y), the transformation (x',y') can be found by:
[ x'] [ m00 m01 m02 ] [ x ] [ m00*x + m01*y + m02 ] [ y'] = [ m10 m11 m12 ] [ y ] = [ m10*x + m11*y + m12 ] [ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]The bottom row of the matrix is constant, so a transform can be uniquely represented (as in
toString()) by
"[[m00, m01, m02], [m10, m11, m12]]".| Modifier and Type | Field and Description |
|---|---|
static int |
TYPE_FLIP
The transformation includes a flip about an axis, swapping between
right-handed and left-handed coordinate systems.
|
static int |
TYPE_GENERAL_ROTATION
The transformation includes a rotation by an arbitrary angle.
|
static int |
TYPE_GENERAL_SCALE
The transformation includes a general scale - length is scaled in either
or both the x and y directions, but by different amounts; without
affecting angles.
|
static int |
TYPE_GENERAL_TRANSFORM
The transformation is an arbitrary conversion of coordinates which
could not be decomposed into the other TYPEs.
|
static int |
TYPE_IDENTITY
The transformation is the identity (x' = x, y' = y).
|
static int |
TYPE_MASK_ROTATION
This constant checks if either variety of rotation is performed.
|
static int |
TYPE_MASK_SCALE
This constant checks if either variety of scale transform is performed.
|
static int |
TYPE_QUADRANT_ROTATION
The transformation includes a rotation of a multiple of 90 degrees (PI/2
radians).
|
static int |
TYPE_TRANSLATION
The transformation includes a translation - shifting in the x or y
direction without changing length or angles.
|
static int |
TYPE_UNIFORM_SCALE
The transformation includes a uniform scale - length is scaled in both
the x and y directions by the same amount, without affecting angles.
|
| Constructor and Description |
|---|
AffineTransform()
Construct a new identity transform:
[ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]
|
AffineTransform(AffineTransform tx)
Create a new transform which copies the given one.
|
AffineTransform(double[] d)
Construct a transform from a sequence of double entries.
|
AffineTransform(double m00,
double m10,
double m01,
double m11,
double m02,
double m12)
Construct a transform with the given matrix entries:
[ m00 m01 m02 ]
[ m10 m11 m12 ]
[ 0 0 1 ]
|
AffineTransform(float[] f)
Construct a transform from a sequence of float entries.
|
AffineTransform(float m00,
float m10,
float m01,
float m11,
float m02,
float m12)
Construct a transform with the given matrix entries:
[ m00 m01 m02 ]
[ m10 m11 m12 ]
[ 0 0 1 ]
|
| Modifier and Type | Method and Description |
|---|---|
Object |
clone()
Create a new transform of the same run-time type, with the same
transforming properties as this one.
|
void |
concatenate(AffineTransform tx)
Set this transform to the result of performing the original version of
this followed by tx.
|
AffineTransform |
createInverse()
Returns a transform, which if concatenated to this one, will result in
the identity transform.
|
Shape |
createTransformedShape(Shape src)
Return a new Shape, based on the given one, where the path of the shape
has been transformed by this transform.
|
void |
deltaTransform(double[] srcPts,
int srcOff,
double[] dstPts,
int dstOff,
int num)
Perform this transformation, less any translation, on an array of points,
in (x,y) pairs, storing the results in another (possibly same) array.
|
Point2D |
deltaTransform(Point2D src,
Point2D dst)
Perform this transformation, less any translation, on the given source
point, and store the result in the destination (creating it if
necessary).
|
boolean |
equals(Object obj)
Compares two transforms for equality.
|
double |
getDeterminant()
Return the determinant of this transform matrix.
|
void |
getMatrix(double[] d)
Return the matrix of values used in this transform.
|
static AffineTransform |
getRotateInstance(double theta)
Returns a rotation transform.
|
static AffineTransform |
getRotateInstance(double theta,
double x,
double y)
Returns a rotation transform about a point.
|
static AffineTransform |
getScaleInstance(double sx,
double sy)
Returns a scaling transform:
[ sx 0 0 ]
[ 0 sy 0 ]
[ 0 0 1 ]
|
double |
getScaleX()
Returns the X coordinate scaling factor of the matrix.
|
double |
getScaleY()
Returns the Y coordinate scaling factor of the matrix.
|
static AffineTransform |
getShearInstance(double shx,
double shy)
Returns a shearing transform (points are shifted in the x direction based
on a factor of their y coordinate, and in the y direction as a factor of
their x coordinate):
[ 1 shx 0 ]
[ shy 1 0 ]
[ 0 0 1 ]
|
double |
getShearX()
Returns the X coordinate shearing factor of the matrix.
|
double |
getShearY()
Returns the Y coordinate shearing factor of the matrix.
|
static AffineTransform |
getTranslateInstance(double tx,
double ty)
Returns a translation transform:
[ 1 0 tx ]
[ 0 1 ty ]
[ 0 0 1 ]
|
double |
getTranslateX()
Returns the X coordinate translation factor of the matrix.
|
double |
getTranslateY()
Returns the Y coordinate translation factor of the matrix.
|
int |
getType()
Returns the type of this transform.
|
int |
hashCode()
Return the hashcode for this transformation.
|
void |
inverseTransform(double[] srcPts,
int srcOff,
double[] dstPts,
int dstOff,
int num)
Perform the inverse of this transformation on an array of points, in
(x,y) pairs, storing the results in another (possibly same) array.
|
Point2D |
inverseTransform(Point2D src,
Point2D dst)
Perform the inverse of this transformation on the given source point,
and store the result in the destination (creating it if necessary).
|
boolean |
isIdentity()
Tests if this transformation is the identity:
[ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]
|
void |
preConcatenate(AffineTransform tx)
Set this transform to the result of performing tx followed by the
original version of this.
|
void |
rotate(double theta)
Concatenate a rotation onto this transform.
|
void |
rotate(double theta,
double x,
double y)
Concatenate a rotation about a point onto this transform.
|
void |
scale(double sx,
double sy)
Concatenate a scale onto this transform.
|
void |
setToIdentity()
Reset this transform to the identity (no transformation):
[ 1 0 0 ]
[ 0 1 0 ]
[ 0 0 1 ]
|
void |
setToRotation(double theta)
Set this transform to a rotation.
|
void |
setToRotation(double theta,
double x,
double y)
Set this transform to a rotation about a point.
|
void |
setToScale(double sx,
double sy)
Set this transform to a scale:
[ sx 0 0 ]
[ 0 sy 0 ]
[ 0 0 1 ]
|
void |
setToShear(double shx,
double shy)
Set this transform to a shear (points are shifted in the x direction based
on a factor of their y coordinate, and in the y direction as a factor of
their x coordinate):
[ 1 shx 0 ]
[ shy 1 0 ]
[ 0 0 1 ]
|
void |
setToTranslation(double tx,
double ty)
Set this transform to a translation:
[ 1 0 tx ]
[ 0 1 ty ]
[ 0 0 1 ]
|
void |
setTransform(AffineTransform tx)
Set this transform to a copy of the given one.
|
void |
setTransform(double m00,
double m10,
double m01,
double m11,
double m02,
double m12)
Set this transform to the given values:
[ m00 m01 m02 ]
[ m10 m11 m12 ]
[ 0 0 1 ]
|
void |
shear(double shx,
double shy)
Concatenate a shearing onto this transform.
|
String |
toString()
Returns a string representation of the transform, in the format:
"AffineTransform[[" + m00 + ", " + m01 + ", " + m02 + "], ["
+ m10 + ", " + m11 + ", " + m12 + "]]". |
void |
transform(double[] srcPts,
int srcOff,
double[] dstPts,
int dstOff,
int num)
Perform this transformation on an array of points, in (x,y) pairs,
storing the results in another (possibly same) array.
|
void |
transform(double[] srcPts,
int srcOff,
float[] dstPts,
int dstOff,
int num)
Perform this transformation on an array of points, in (x,y) pairs,
storing the results in another array.
|
void |
transform(float[] srcPts,
int srcOff,
double[] dstPts,
int dstOff,
int num)
Perform this transformation on an array of points, in (x,y) pairs,
storing the results in another array.
|
void |
transform(float[] srcPts,
int srcOff,
float[] dstPts,
int dstOff,
int num)
Perform this transformation on an array of points, in (x,y) pairs,
storing the results in another (possibly same) array.
|
void |
transform(Point2D[] src,
int srcOff,
Point2D[] dst,
int dstOff,
int num)
Perform this transformation on an array of points, storing the results
in another (possibly same) array.
|
Point2D |
transform(Point2D src,
Point2D dst)
Perform this transformation on the given source point, and store the
result in the destination (creating it if necessary).
|
void |
translate(double tx,
double ty)
Concatenate a translation onto this transform.
|
public static final int TYPE_IDENTITY
public static final int TYPE_TRANSLATION
public static final int TYPE_UNIFORM_SCALE
public static final int TYPE_GENERAL_SCALE
public static final int TYPE_MASK_SCALE
public static final int TYPE_FLIP
public static final int TYPE_QUADRANT_ROTATION
public static final int TYPE_GENERAL_ROTATION
public static final int TYPE_MASK_ROTATION
public static final int TYPE_GENERAL_TRANSFORM
public AffineTransform()
[ 1 0 0 ] [ 0 1 0 ] [ 0 0 1 ]
public AffineTransform(AffineTransform tx)
tx - the transform to copyNullPointerException - if tx is nullpublic AffineTransform(float m00, float m10, float m01, float m11, float m02, float m12)
[ m00 m01 m02 ] [ m10 m11 m12 ] [ 0 0 1 ]
m00 - the x scaling componentm10 - the y shearing componentm01 - the x shearing componentm11 - the y scaling componentm02 - the x translation componentm12 - the y translation componentpublic AffineTransform(float[] f)
[ f[0] f[2] (f[4]) ] [ f[1] f[3] (f[5]) ] [ 0 0 1 ]
f - the matrix to copy from, with at least 4 (6) entriesNullPointerException - if f is nullArrayIndexOutOfBoundsException - if f is too smallpublic AffineTransform(double m00, double m10, double m01, double m11, double m02, double m12)
[ m00 m01 m02 ] [ m10 m11 m12 ] [ 0 0 1 ]
m00 - the x scaling componentm10 - the y shearing componentm01 - the x shearing componentm11 - the y scaling componentm02 - the x translation componentm12 - the y translation componentpublic AffineTransform(double[] d)
[ d[0] d[2] (d[4]) ] [ d[1] d[3] (d[5]) ] [ 0 0 1 ]
d - the matrix to copy from, with at least 4 (6) entriesNullPointerException - if d is nullArrayIndexOutOfBoundsException - if d is too smallpublic static AffineTransform getTranslateInstance(double tx, double ty)
[ 1 0 tx ] [ 0 1 ty ] [ 0 0 1 ]
tx - the x translation distancety - the y translation distancepublic static AffineTransform getRotateInstance(double theta)
[ cos(theta) -sin(theta) 0 ] [ sin(theta) cos(theta) 0 ] [ 0 0 1 ]
theta - the rotation anglepublic static AffineTransform getRotateInstance(double theta, double x, double y)
AffineTransform tx = new AffineTransform(); tx.setToTranslation(x, y); tx.rotate(theta); tx.translate(-x, -y);
The resulting matrix is:
[ cos(theta) -sin(theta) x-x*cos+y*sin ] [ sin(theta) cos(theta) y-x*sin-y*cos ] [ 0 0 1 ]
theta - the rotation anglex - the x coordinate of the pivot pointy - the y coordinate of the pivot pointpublic static AffineTransform getScaleInstance(double sx, double sy)
[ sx 0 0 ] [ 0 sy 0 ] [ 0 0 1 ]
sx - the x scaling factorsy - the y scaling factorpublic static AffineTransform getShearInstance(double shx, double shy)
[ 1 shx 0 ] [ shy 1 0 ] [ 0 0 1 ]
shx - the x shearing factorshy - the y shearing factorpublic int getType()
TYPE_IDENTITY,
TYPE_TRANSLATION,
TYPE_UNIFORM_SCALE,
TYPE_GENERAL_SCALE,
TYPE_QUADRANT_ROTATION,
TYPE_GENERAL_ROTATION,
TYPE_GENERAL_TRANSFORMpublic double getDeterminant()
If this is a uniform scale transformation, the determinant also represents the squared value of the scale. Otherwise, it carries little additional meaning. The determinant is calculated as:
| m00 m01 m02 | | m10 m11 m12 | = m00 * m11 - m01 * m10 | 0 0 1 |
createInverse()public void getMatrix(double[] d)
[ d[0] d[2] (d[4]) ] [ d[1] d[3] (d[5]) ] [ 0 0 1 ]
d - the matrix to store the results into; with 4 (6) entriesNullPointerException - if d is nullArrayIndexOutOfBoundsException - if d is too smallpublic double getScaleX()
getMatrix(double[])public double getScaleY()
getMatrix(double[])public double getShearX()
getMatrix(double[])public double getShearY()
getMatrix(double[])public double getTranslateX()
getMatrix(double[])public double getTranslateY()
getMatrix(double[])public void translate(double tx, double ty)
concatenate(AffineTransform.getTranslateInstance(tx, ty)).tx - the x translation distancety - the y translation distancegetTranslateInstance(double, double),
concatenate(AffineTransform)public void rotate(double theta)
concatenate(AffineTransform.getRotateInstance(theta)).theta - the rotation anglegetRotateInstance(double),
concatenate(AffineTransform)public void rotate(double theta, double x, double y)
concatenate(AffineTransform.getRotateInstance(theta, x, y)).theta - the rotation anglex - the x coordinate of the pivot pointy - the y coordinate of the pivot pointgetRotateInstance(double, double, double),
concatenate(AffineTransform)public void scale(double sx, double sy)
concatenate(AffineTransform.getScaleInstance(sx, sy)).sx - the x scaling factorsy - the y scaling factorgetScaleInstance(double, double),
concatenate(AffineTransform)public void shear(double shx, double shy)
concatenate(AffineTransform.getShearInstance(sx, sy)).shx - the x shearing factorshy - the y shearing factorgetShearInstance(double, double),
concatenate(AffineTransform)public void setToIdentity()
[ 1 0 0 ] [ 0 1 0 ] [ 0 0 1 ]
public void setToTranslation(double tx, double ty)
[ 1 0 tx ] [ 0 1 ty ] [ 0 0 1 ]
tx - the x translation distancety - the y translation distancepublic void setToRotation(double theta)
[ cos(theta) -sin(theta) 0 ] [ sin(theta) cos(theta) 0 ] [ 0 0 1 ]
theta - the rotation anglepublic void setToRotation(double theta, double x, double y)
tx.setToTranslation(x, y); tx.rotate(theta); tx.translate(-x, -y);
The resulting matrix is:
[ cos(theta) -sin(theta) x-x*cos+y*sin ] [ sin(theta) cos(theta) y-x*sin-y*cos ] [ 0 0 1 ]
theta - the rotation anglex - the x coordinate of the pivot pointy - the y coordinate of the pivot pointpublic void setToScale(double sx, double sy)
[ sx 0 0 ] [ 0 sy 0 ] [ 0 0 1 ]
sx - the x scaling factorsy - the y scaling factorpublic void setToShear(double shx, double shy)
[ 1 shx 0 ] [ shy 1 0 ] [ 0 0 1 ]
shx - the x shearing factorshy - the y shearing factorpublic void setTransform(AffineTransform tx)
tx - the transform to copyNullPointerException - if tx is nullpublic void setTransform(double m00, double m10, double m01, double m11, double m02, double m12)
[ m00 m01 m02 ] [ m10 m11 m12 ] [ 0 0 1 ]
m00 - the x scaling componentm10 - the y shearing componentm01 - the x shearing componentm11 - the y scaling componentm02 - the x translation componentm12 - the y translation componentpublic void concatenate(AffineTransform tx)
[ this ] = [ this ] x [ tx ]
tx - the transform to concatenateNullPointerException - if tx is nullpreConcatenate(AffineTransform)public void preConcatenate(AffineTransform tx)
[ this ] = [ tx ] x [ this ]
tx - the transform to concatenateNullPointerException - if tx is nullconcatenate(AffineTransform)public AffineTransform createInverse() throws NoninvertibleTransformException
Let A be the matrix for which we want to find the inverse:
A = [ m00 m01 m02 ]
[ m10 m11 m12 ]
[ 0 0 1 ]
1
inverse (A) = --- x adjoint(A)
det
= 1 [ m11 -m01 m01*m12-m02*m11 ]
--- x [ -m10 m00 -m00*m12+m10*m02 ]
det [ 0 0 m00*m11-m10*m01 ]
= [ m11/det -m01/det m01*m12-m02*m11/det ]
[ -m10/det m00/det -m00*m12+m10*m02/det ]
[ 0 0 1 ]
NoninvertibleTransformException - if inversion is not possiblegetDeterminant()public Point2D transform(Point2D src, Point2D dst)
src - the source pointdst - the destination, or nullNullPointerException - if src is nullpublic void transform(Point2D[] src, int srcOff, Point2D[] dst, int dstOff, int num)
src - the array of source pointssrcOff - the starting offset into srcdst - the array of destination points (may have null entries)dstOff - the starting offset into dstnum - the number of points to transformNullPointerException - if src or dst is null, or src has null
entriesArrayIndexOutOfBoundsException - if array bounds are exceededArrayStoreException - if new points are incompatible with dstpublic void transform(float[] srcPts, int srcOff, float[] dstPts, int dstOff, int num)
srcPts - the array of source pointssrcOff - the starting offset into srcdstPts - the array of destination pointsdstOff - the starting offset into dstnum - the number of points to transformNullPointerException - if src or dst is nullArrayIndexOutOfBoundsException - if array bounds are exceededpublic void transform(double[] srcPts, int srcOff, double[] dstPts, int dstOff, int num)
srcPts - the array of source pointssrcOff - the starting offset into srcdstPts - the array of destination pointsdstOff - the starting offset into dstnum - the number of points to transformNullPointerException - if src or dst is nullArrayIndexOutOfBoundsException - if array bounds are exceededpublic void transform(float[] srcPts, int srcOff, double[] dstPts, int dstOff, int num)
srcPts - the array of source pointssrcOff - the starting offset into srcdstPts - the array of destination pointsdstOff - the starting offset into dstnum - the number of points to transformNullPointerException - if src or dst is nullArrayIndexOutOfBoundsException - if array bounds are exceededpublic void transform(double[] srcPts, int srcOff, float[] dstPts, int dstOff, int num)
srcPts - the array of source pointssrcOff - the starting offset into srcdstPts - the array of destination pointsdstOff - the starting offset into dstnum - the number of points to transformNullPointerException - if src or dst is nullArrayIndexOutOfBoundsException - if array bounds are exceededpublic Point2D inverseTransform(Point2D src, Point2D dst) throws NoninvertibleTransformException
src - the source pointdst - the destination, or nullNullPointerException - if src is nullNoninvertibleTransformException - if the inverse does not existgetDeterminant()public void inverseTransform(double[] srcPts, int srcOff, double[] dstPts, int dstOff, int num) throws NoninvertibleTransformException
srcPts - the array of source pointssrcOff - the starting offset into srcdstPts - the array of destination pointsdstOff - the starting offset into dstnum - the number of points to transformNullPointerException - if src or dst is nullArrayIndexOutOfBoundsException - if array bounds are exceededNoninvertibleTransformException - if the inverse does not existgetDeterminant()public Point2D deltaTransform(Point2D src, Point2D dst)
[ x' ] = [ m00 m01 ] [ x ] = [ m00 * x + m01 * y ] [ y' ] [ m10 m11 ] [ y ] = [ m10 * x + m11 * y ]
src - the source pointdst - the destination, or nullNullPointerException - if src is nullpublic void deltaTransform(double[] srcPts, int srcOff, double[] dstPts, int dstOff, int num)
[ x' ] = [ m00 m01 ] [ x ] = [ m00 * x + m01 * y ] [ y' ] [ m10 m11 ] [ y ] = [ m10 * x + m11 * y ]
srcPts - the array of source pointssrcOff - the starting offset into srcdstPts - the array of destination pointsdstOff - the starting offset into dstnum - the number of points to transformNullPointerException - if src or dst is nullArrayIndexOutOfBoundsException - if array bounds are exceededpublic Shape createTransformedShape(Shape src)
src - the shape source to transformnull if src is
null.GeneralPath.transform(AffineTransform)public String toString()
"AffineTransform[[" + m00 + ", " + m01 + ", " + m02 + "], ["
+ m10 + ", " + m11 + ", " + m12 + "]]".toString in class ObjectObject.getClass(),
Object.hashCode(),
Class.getName(),
Integer.toHexString(int)public boolean isIdentity()
[ 1 0 0 ] [ 0 1 0 ] [ 0 0 1 ]
public Object clone()
public int hashCode()
long l = Double.doubleToLongBits(getScaleX()); l = l * 31 + Double.doubleToLongBits(getShearX()); l = l * 31 + Double.doubleToLongBits(getTranslateX()); l = l * 31 + Double.doubleToLongBits(getShearY()); l = l * 31 + Double.doubleToLongBits(getScaleY()); l = l * 31 + Double.doubleToLongBits(getTranslateY()); return (int) ((l >> 32) ^ l);
hashCode in class ObjectObject.equals(Object),
System.identityHashCode(Object)public boolean equals(Object obj)
equals in class Objectobj - the transform to compareObject.hashCode()