KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > parser > AWTTransformProducer


1 /*
2
3    Copyright 2000-2001,2003 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.parser;
19
20 import java.awt.geom.AffineTransform JavaDoc;
21 import java.io.Reader JavaDoc;
22
23 /**
24  * This class provides an implementation of the PathHandler that initializes
25  * an AffineTransform from the value of a 'transform' attribute.
26  *
27  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
28  * @version $Id: AWTTransformProducer.java,v 1.8 2005/03/27 08:58:35 cam Exp $
29  */

30 public class AWTTransformProducer implements TransformListHandler {
31     /**
32      * The value of the current affine transform.
33      */

34     protected AffineTransform JavaDoc affineTransform;
35
36     /**
37      * Utility method for creating an AffineTransform.
38      * @param r The reader used to read the transform specification.
39      */

40     public static AffineTransform JavaDoc createAffineTransform(Reader JavaDoc r)
41         throws ParseException {
42         TransformListParser p = new TransformListParser();
43         AWTTransformProducer th = new AWTTransformProducer();
44
45         p.setTransformListHandler(th);
46         p.parse(r);
47
48         return th.getAffineTransform();
49     }
50
51     /**
52      * Utility method for creating an AffineTransform.
53      * @param s The transform specification.
54      */

55     public static AffineTransform JavaDoc createAffineTransform(String JavaDoc s)
56         throws ParseException {
57         TransformListParser p = new TransformListParser();
58         AWTTransformProducer th = new AWTTransformProducer();
59
60         p.setTransformListHandler(th);
61         p.parse(s);
62
63         return th.getAffineTransform();
64     }
65
66     /**
67      * Returns the AffineTransform object initialized during the last parsing.
68      * @return the transform or null if this handler has not been used by
69      * a parser.
70      */

71     public AffineTransform JavaDoc getAffineTransform() {
72         return affineTransform;
73     }
74
75     /**
76      * Implements {@link TransformListHandler#startTransformList()}.
77      */

78     public void startTransformList() throws ParseException {
79         affineTransform = new AffineTransform JavaDoc();
80     }
81
82     /**
83      * Implements {@link
84      * TransformListHandler#matrix(float,float,float,float,float,float)}.
85      */

86     public void matrix(float a, float b, float c, float d, float e, float f)
87         throws ParseException {
88         affineTransform.concatenate(new AffineTransform JavaDoc(a, b, c, d, e, f));
89     }
90
91     /**
92      * Implements {@link TransformListHandler#rotate(float)}.
93      */

94     public void rotate(float theta) throws ParseException {
95         affineTransform.concatenate
96             (AffineTransform.getRotateInstance(Math.PI * theta / 180));
97     }
98
99     /**
100      * Implements {@link TransformListHandler#rotate(float,float,float)}.
101      */

102     public void rotate(float theta, float cx, float cy) throws ParseException {
103         AffineTransform JavaDoc at
104             = AffineTransform.getRotateInstance(Math.PI * theta / 180, cx, cy);
105         affineTransform.concatenate(at);
106     }
107
108     /**
109      * Implements {@link TransformListHandler#translate(float)}.
110      */

111     public void translate(float tx) throws ParseException {
112         AffineTransform JavaDoc at = AffineTransform.getTranslateInstance(tx, 0);
113         affineTransform.concatenate(at);
114     }
115
116     /**
117      * Implements {@link TransformListHandler#translate(float,float)}.
118      */

119     public void translate(float tx, float ty) throws ParseException {
120         AffineTransform JavaDoc at = AffineTransform.getTranslateInstance(tx, ty);
121         affineTransform.concatenate(at);
122     }
123
124     /**
125      * Implements {@link TransformListHandler#scale(float)}.
126      */

127     public void scale(float sx) throws ParseException {
128         affineTransform.concatenate(AffineTransform.getScaleInstance(sx, sx));
129     }
130
131     /**
132      * Implements {@link TransformListHandler#scale(float,float)}.
133      */

134     public void scale(float sx, float sy) throws ParseException {
135         affineTransform.concatenate(AffineTransform.getScaleInstance(sx, sy));
136     }
137
138     /**
139      * Implements {@link TransformListHandler#skewX(float)}.
140      */

141     public void skewX(float skx) throws ParseException {
142         affineTransform.concatenate
143             (AffineTransform.getShearInstance(Math.tan(Math.PI * skx / 180),
144                                               0));
145     }
146
147     /**
148      * Implements {@link TransformListHandler#skewY(float)}.
149      */

150     public void skewY(float sky) throws ParseException {
151         affineTransform.concatenate
152             (AffineTransform.getShearInstance(0,
153                                               Math.tan(Math.PI * sky / 180)));
154     }
155
156     /**
157      * Implements {@link TransformListHandler#endTransformList()}.
158      */

159     public void endTransformList() throws ParseException {
160     }
161 }
162
Popular Tags