KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > PatternPaint


1 /*
2
3    Copyright 2000-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.gvt;
19
20 import java.awt.Paint JavaDoc;
21 import java.awt.PaintContext JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.RenderingHints JavaDoc;
24 import java.awt.geom.AffineTransform JavaDoc;
25 import java.awt.geom.Rectangle2D JavaDoc;
26 import java.awt.image.ColorModel JavaDoc;
27 import java.awt.image.Raster JavaDoc;
28
29 import org.apache.batik.ext.awt.image.PadMode;
30 import org.apache.batik.ext.awt.image.renderable.Filter;
31 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit;
32
33 /**
34  * The PatternPaint class provides a way to fill a Shape with a a pattern
35  * defined as a GVT Tree.
36  *
37  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
38  * @version $Id: PatternPaint.java,v 1.14 2004/08/18 07:14:27 vhardy Exp $
39  */

40 public class PatternPaint implements Paint JavaDoc {
41
42     /**
43      * The <tt>GraphicsNode</tt> that this <tt>Paint</tt> uses to
44      * produce the pixel pattern
45      */

46     private GraphicsNode node;
47
48     /**
49      * The region to which this paint is constrained
50      */

51     private Rectangle2D JavaDoc patternRegion;
52
53     /**
54      * Additional pattern transform, added on top of the
55      * user space to device space transform (i.e., before
56      * the tiling space
57      */

58     private AffineTransform JavaDoc patternTransform;
59
60     /*
61      * The basic tile to fill the region with.
62      * we replicate this out in the Context.
63      */

64     private Filter tile;
65
66     /**
67      * Controls whether or not the pattern overflows
68      * the pattern tile
69      */

70     private boolean overflow;
71
72     private PatternPaintContext lastContext;
73
74     /**
75      * Constructs a new <tt>PatternPaint</tt>.
76      *
77      * @param node Used to generate the paint pixel pattern
78      * @param patternRegion Region to which this paint is constrained
79      * @param overflow controls whether or not the node can overflow
80      * the patternRegion.
81      * @param patternTransform additional transform added on
82      * top of the user space to device space transform.
83      */

84     public PatternPaint(GraphicsNode node,
85                         Rectangle2D JavaDoc patternRegion,
86                         boolean overflow,
87                         AffineTransform JavaDoc patternTransform){
88
89         if (node == null) {
90             throw new IllegalArgumentException JavaDoc();
91         }
92
93         if (patternRegion == null) {
94             throw new IllegalArgumentException JavaDoc();
95         }
96
97         this.node = node;
98         this.patternRegion = patternRegion;
99         this.overflow = overflow;
100         this.patternTransform = patternTransform;
101         
102         // Wrap the input node so that the primitivePaint
103
// in GraphicsNodeRable takes the filter, clip....
104
// into account.
105
CompositeGraphicsNode comp = new CompositeGraphicsNode();
106         comp.getChildren().add(node);
107         Filter gnr = comp.getGraphicsNodeRable(true);
108
109         Rectangle2D JavaDoc padBounds = (Rectangle2D JavaDoc)patternRegion.clone();
110
111         // When there is overflow, make sure we take the full node bounds into
112
// account.
113
if (overflow) {
114             Rectangle2D JavaDoc nodeBounds = comp.getBounds();
115             // System.out.println("Comp Bounds : " + nodeBounds);
116
// System.out.println("Node Bounds : " + node.getBounds(gnrc));
117
padBounds.add(nodeBounds);
118         }
119
120         // System.out.println("Pattern region : " + patternRegion);
121
// System.out.println("Node txf : " + node.getTransform());
122
tile = new PadRable8Bit(gnr, padBounds, PadMode.ZERO_PAD);
123     }
124
125     /**
126      * Returns the graphics node that define the pattern.
127      */

128     public GraphicsNode getGraphicsNode(){
129         return node;
130     }
131
132     /**
133      * Returns the pattern region.
134      */

135     public Rectangle2D JavaDoc getPatternRect(){
136         return (Rectangle2D JavaDoc)patternRegion.clone();
137     }
138
139     /**
140      * Returns the additional transform of the pattern paint.
141      */

142     public AffineTransform JavaDoc getPatternTransform(){
143         return patternTransform;
144     }
145
146     /**
147      * Creates and returns a context used to generate the pattern.
148      */

149     public PaintContext JavaDoc createContext(ColorModel JavaDoc cm,
150                                       Rectangle JavaDoc deviceBounds,
151                                       Rectangle2D JavaDoc userBounds,
152                                       AffineTransform JavaDoc xform,
153                                       RenderingHints JavaDoc hints) {
154         // Concatenate the patternTransform to xform
155
if (patternTransform != null) {
156             xform = new AffineTransform JavaDoc(xform);
157             xform.concatenate(patternTransform);
158         }
159
160         if ((lastContext!= null) &&
161             lastContext.getColorModel().equals(cm)) {
162             
163             double p[] = new double[6];
164             double q[] = new double[6];
165             xform.getMatrix(p);
166             lastContext.getUsr2Dev().getMatrix(q);
167             if ((p[0] == q[0]) && (p[1] == q[1]) &&
168                 (p[2] == q[2]) && (p[3] == q[3])) {
169                 if ((p[4] == q[4]) && (p[5] == q[5]))
170                     return lastContext;
171                 else
172                     return new PatternPaintContextWrapper
173                         (lastContext,
174                          (int)(q[4]-p[4]+0.5),
175                          (int)(q[5]-p[5]+0.5));
176             }
177         }
178         // System.out.println("CreateContext Called: " + this);
179
// System.out.println("CM : " + cm);
180
// System.out.println("xForm : " + xform);
181

182         lastContext = new PatternPaintContext(cm, xform,
183                                        hints, tile,
184                                        patternRegion,
185                                        overflow);
186         return lastContext;
187     }
188
189     /**
190      * Returns the transparency mode for this pattern paint.
191      */

192     public int getTransparency(){
193         return TRANSLUCENT;
194     }
195
196     static class PatternPaintContextWrapper implements PaintContext JavaDoc {
197         PatternPaintContext ppc;
198         int xShift, yShift;
199         PatternPaintContextWrapper(PatternPaintContext ppc,
200                             int xShift, int yShift) {
201             this.ppc = ppc;
202             this.xShift = xShift;
203             this.yShift = yShift;
204         }
205
206         public void dispose(){ }
207
208         public ColorModel JavaDoc getColorModel(){
209             return ppc.getColorModel();
210         }
211         public Raster JavaDoc getRaster(int x, int y, int width, int height){
212             return ppc.getRaster(x+xShift, y+yShift, width, height);
213         }
214     }
215 }
216
Popular Tags