KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > g2d > TransformStackElement


1 /*
2
3    Copyright 2001-2002 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.g2d;
19
20 import java.awt.geom.AffineTransform JavaDoc;
21
22 /**
23  * Contains a description of an elementary transform stack element,
24  * such as a rotate or translate. A transform stack element has a
25  * type and a value, which is an array of double values.<br>
26  *
27  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
28  * @author <a HREF="mailto:paul_evenblij@compuware.com">Paul Evenblij</a>
29  * @version $Id: TransformStackElement.java,v 1.6 2004/08/18 07:13:46 vhardy Exp $
30  */

31 public abstract class TransformStackElement implements Cloneable JavaDoc{
32
33     /**
34      * Transform type
35      */

36     private TransformType type;
37
38     /**
39      * Value
40      */

41     private double transformParameters[];
42
43     /**
44      * @param type transform type
45      * @param transformParameters parameters for transform
46      */

47     protected TransformStackElement(TransformType type,
48                                     double transformParameters[]){
49         this.type = type;
50         this.transformParameters = transformParameters;
51     }
52     
53     /**
54      * @return an object which is a deep copy of this one
55      */

56     public Object JavaDoc clone() {
57         TransformStackElement newElement = null;
58
59         // start with a shallow copy to get our implementations right
60
try {
61             newElement = (TransformStackElement) super.clone();
62         } catch(java.lang.CloneNotSupportedException JavaDoc ex) {}
63
64         // now deep copy the parameter array
65
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     /*
72      * Factory methods
73      */

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 JavaDoc 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     /**
127      * Implementation should determine if the parameter list represents
128      * an identity transform, for the instance transform type.
129      */

130     abstract boolean isIdentity(double[] parameters);
131     
132     /**
133      * @return true iff this transform is the identity transform
134      */

135     public boolean isIdentity() {
136         return isIdentity(transformParameters);
137     }
138
139     /**
140      * @return array of values containing this transform element's parameters
141      */

142     public double[] getTransformParameters(){
143         return transformParameters;
144     }
145
146     /**
147      * @return this transform type
148      */

149     public TransformType getType(){
150         return type;
151     }
152
153     /*
154      * Concatenation utility. Requests this transform stack element
155      * to concatenate with the input stack element. Only elements
156      * of the same types are concatenated. For example, if this
157      * element represents a translation, it will concatenate with
158      * another translation, but not with any other kind of
159      * stack element.
160      * @param stackElement element to be concatenated with this one.
161      * @return true if the input stackElement was concatenated with
162      * this one. False otherwise.
163      */

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     /**
195      * Multiplies two 2x3 matrices of double precision values
196      */

197     private double[] matrixMultiply(double[] matrix1, double[] matrix2) {
198         double[] product = new double[6];
199         AffineTransform JavaDoc transform1 = new AffineTransform JavaDoc(matrix1);
200         transform1.concatenate(new AffineTransform JavaDoc(matrix2));
201         transform1.getMatrix(product);
202         return product;
203     }
204
205 }
206
Popular Tags