1 18 package org.apache.batik.ext.awt.g2d; 19 20 import java.awt.geom.AffineTransform ; 21 22 31 public abstract class TransformStackElement implements Cloneable { 32 33 36 private TransformType type; 37 38 41 private double transformParameters[]; 42 43 47 protected TransformStackElement(TransformType type, 48 double transformParameters[]){ 49 this.type = type; 50 this.transformParameters = transformParameters; 51 } 52 53 56 public Object clone() { 57 TransformStackElement newElement = null; 58 59 try { 61 newElement = (TransformStackElement) super.clone(); 62 } catch(java.lang.CloneNotSupportedException ex) {} 63 64 double transformParameters[] = new double[this.transformParameters.length]; 66 System.arraycopy(this.transformParameters, 0, transformParameters, 0, transformParameters.length); 67 newElement.transformParameters = transformParameters; 68 return newElement; 69 } 70 71 74 75 public static TransformStackElement createTranslateElement(double tx, 76 double ty){ 77 return new TransformStackElement(TransformType.TRANSLATE, 78 new double[]{ tx, ty }) { 79 boolean isIdentity(double[] parameters) { 80 return parameters[0] == 0 && parameters[1] == 0; 81 } 82 }; 83 } 84 85 public static TransformStackElement createRotateElement(double theta){ 86 return new TransformStackElement(TransformType.ROTATE, 87 new double[]{ theta }) { 88 boolean isIdentity(double[] parameters) { 89 return Math.cos(parameters[0]) == 1; 90 } 91 }; 92 } 93 94 public static TransformStackElement createScaleElement(double scaleX, 95 double scaleY){ 96 return new TransformStackElement(TransformType.SCALE, 97 new double[]{ scaleX, scaleY }) { 98 boolean isIdentity(double[] parameters) { 99 return parameters[0] == 1 && parameters[1] == 1; 100 } 101 }; 102 } 103 104 public static TransformStackElement createShearElement(double shearX, 105 double shearY){ 106 return new TransformStackElement(TransformType.SHEAR, 107 new double[]{ shearX, shearY }) { 108 boolean isIdentity(double[] parameters) { 109 return parameters[0] == 0 && parameters[1] == 0; 110 } 111 }; 112 } 113 114 public static TransformStackElement createGeneralTransformElement 115 (AffineTransform txf){ 116 double matrix[] = new double[6]; 117 txf.getMatrix(matrix); 118 return new TransformStackElement(TransformType.GENERAL, matrix) { 119 boolean isIdentity(double[] m) { 120 return (m[0] == 1 && m[2] == 0 && m[4] == 0 && 121 m[1] == 0 && m[3] == 1 && m[5] == 0); 122 } 123 }; 124 } 125 126 130 abstract boolean isIdentity(double[] parameters); 131 132 135 public boolean isIdentity() { 136 return isIdentity(transformParameters); 137 } 138 139 142 public double[] getTransformParameters(){ 143 return transformParameters; 144 } 145 146 149 public TransformType getType(){ 150 return type; 151 } 152 153 164 public boolean concatenate(TransformStackElement stackElement){ 165 boolean canConcatenate = false; 166 167 if(type.toInt() == stackElement.type.toInt()){ 168 canConcatenate = true; 169 switch(type.toInt()){ 170 case TransformType.TRANSFORM_TRANSLATE: 171 transformParameters[0] += stackElement.transformParameters[0]; 172 transformParameters[1] += stackElement.transformParameters[1]; 173 break; 174 case TransformType.TRANSFORM_ROTATE: 175 transformParameters[0] += stackElement.transformParameters[0]; 176 break; 177 case TransformType.TRANSFORM_SCALE: 178 transformParameters[0] *= stackElement.transformParameters[0]; 179 transformParameters[1] *= stackElement.transformParameters[1]; 180 break; 181 case TransformType.TRANSFORM_GENERAL: 182 transformParameters 183 = matrixMultiply(transformParameters, 184 stackElement.transformParameters); 185 break; 186 default: 187 canConcatenate = false; 188 } 189 } 190 191 return canConcatenate; 192 } 193 194 197 private double[] matrixMultiply(double[] matrix1, double[] matrix2) { 198 double[] product = new double[6]; 199 AffineTransform transform1 = new AffineTransform (matrix1); 200 transform1.concatenate(new AffineTransform (matrix2)); 201 transform1.getMatrix(product); 202 return product; 203 } 204 205 } 206 | Popular Tags |