KickJava   Java API By Example, From Geeks To Geeks.

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


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.Graphics2D JavaDoc;
21 import java.awt.Shape JavaDoc;
22 import java.awt.geom.Area JavaDoc;
23 import java.awt.geom.Point2D JavaDoc;
24 import java.awt.geom.Rectangle2D JavaDoc;
25
26 /**
27  * A shape painter which consists of multiple shape painters.
28  *
29  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
30  * @version $Id: CompositeShapePainter.java,v 1.20 2004/08/18 07:14:25 vhardy Exp $
31  */

32 public class CompositeShapePainter implements ShapePainter {
33
34     /**
35      * The shape associated with this painter
36      */

37     protected Shape JavaDoc shape;
38
39     /**
40      * The enclosed <tt>ShapePainter</tt>s of this composite shape painter.
41      */

42     protected ShapePainter [] painters;
43
44     /**
45      * The number of shape painter.
46      */

47     protected int count;
48
49     /**
50      * Constructs a new empty <tt>CompositeShapePainter</tt>.
51      */

52     public CompositeShapePainter(Shape JavaDoc shape) {
53         if (shape == null) {
54             throw new IllegalArgumentException JavaDoc();
55         }
56         this.shape = shape;
57     }
58
59     /**
60      * Adds the specified shape painter to the shape painter..
61      *
62      * @param shapePainter the shape painter to add
63      */

64     public void addShapePainter(ShapePainter shapePainter) {
65         if (shapePainter == null) {
66             return;
67         }
68         if (this.shape != shapePainter.getShape()) {
69             shapePainter.setShape(shape);
70         }
71         if (painters == null) {
72             painters = new ShapePainter[2];
73         }
74         if (count == painters.length) {
75             ShapePainter [] newPainters = new ShapePainter[(count*3)/2 + 1];
76             System.arraycopy(painters, 0, newPainters, 0, count);
77             painters = newPainters;
78         }
79         painters[count++] = shapePainter;
80     }
81
82     /**
83      * Sets to the specified index, the specified ShapePainter.
84      *
85      * @param index the index where to set the ShapePainter
86      * @param shapePainter the ShapePainter to set
87      */

88     /* public void setShapePainter(int index, ShapePainter shapePainter) {
89         if (shapePainter == null) {
90             return;
91         }
92         if (this.shape != shapePainter.getShape()) {
93             shapePainter.setShape(shape);
94         }
95         if (painters == null || index >= painters.length) {
96             throw new IllegalArgumentException("Bad index: "+index);
97         }
98         painters[index] = shapePainter;
99         }*/

100
101     /**
102      * Returns the shape painter at the specified index.
103      *
104      * @param index the index of the shape painter to return
105      */

106     public ShapePainter getShapePainter(int index) {
107         return painters[index];
108     }
109
110     /**
111      * Returns the number of shape painter of this composite shape painter.
112      */

113     public int getShapePainterCount() {
114         return count;
115     }
116
117     /**
118      * Paints the specified shape using the specified Graphics2D.
119      *
120      * @param g2d the Graphics2D to use
121      */

122     public void paint(Graphics2D JavaDoc g2d) {
123         if (painters != null) {
124             for (int i=0; i < count; ++i) {
125                 painters[i].paint(g2d);
126             }
127         }
128     }
129
130     /**
131      * Returns the area painted by this shape painter.
132      */

133     public Shape JavaDoc getPaintedArea(){
134         if (painters == null)
135             return null;
136         Area JavaDoc paintedArea = new Area JavaDoc();
137         for (int i=0; i < count; ++i) {
138             Shape JavaDoc s = painters[i].getPaintedArea();
139             if (s != null) {
140                 paintedArea.add(new Area JavaDoc(s));
141             }
142         }
143         return paintedArea;
144     }
145
146     /**
147      * Returns the bounds of the area painted by this shape painter
148      */

149     public Rectangle2D JavaDoc getPaintedBounds2D(){
150         if (painters == null)
151             return null;
152
153         Rectangle2D JavaDoc bounds = null;
154         for (int i=0; i < count; ++i) {
155             Rectangle2D JavaDoc pb = painters[i].getPaintedBounds2D();
156             if (pb == null) continue;
157             if (bounds == null) bounds = (Rectangle2D JavaDoc)pb.clone();
158             else bounds.add(pb);
159         }
160         return bounds;
161     }
162
163     /**
164      * Returns true if pt is in the area painted by this shape painter
165      */

166     public boolean inPaintedArea(Point2D JavaDoc pt){
167         if (painters == null)
168             return false;
169         for (int i=0; i < count; ++i) {
170             if (painters[i].inPaintedArea(pt))
171                 return true;
172         }
173         return false;
174     }
175
176     /**
177      * Returns the area covered by this shape painter (even if nothing
178      * is painted there).
179      */

180     public Shape JavaDoc getSensitiveArea() {
181         if (painters == null)
182             return null;
183         Area JavaDoc paintedArea = new Area JavaDoc();
184         for (int i=0; i < count; ++i) {
185             Shape JavaDoc s = painters[i].getSensitiveArea();
186             if (s != null) {
187                 paintedArea.add(new Area JavaDoc(s));
188             }
189         }
190         return paintedArea;
191     }
192
193     /**
194      * Returns the bounds of the area painted by this shape painter
195      */

196     public Rectangle2D JavaDoc getSensitiveBounds2D() {
197         if (painters == null)
198             return null;
199
200         Rectangle2D JavaDoc bounds = null;
201         for (int i=0; i < count; ++i) {
202             Rectangle2D JavaDoc pb = painters[i].getSensitiveBounds2D();
203             if (bounds == null) bounds = (Rectangle2D JavaDoc)pb.clone();
204             else bounds.add(pb);
205         }
206         return bounds;
207     }
208
209     /**
210      * Returns true if pt is in the area painted by this shape painter
211      */

212     public boolean inSensitiveArea(Point2D JavaDoc pt){
213         if (painters == null)
214             return false;
215         for (int i=0; i < count; ++i) {
216             if (painters[i].inSensitiveArea(pt))
217                 return true;
218         }
219         return false;
220     }
221
222     /**
223      * Sets the Shape this shape painter is associated with.
224      *
225      * @param shape new shape this painter should be associated with.
226      * Should not be null.
227      */

228     public void setShape(Shape JavaDoc shape){
229         if (shape == null) {
230             throw new IllegalArgumentException JavaDoc();
231         }
232         if (painters != null) {
233             for (int i=0; i < count; ++i) {
234                 painters[i].setShape(shape);
235             }
236         }
237         this.shape = shape;
238     }
239
240     /**
241      * Gets the Shape this shape painter is associated with.
242      *
243      * @return shape associated with this painter
244      */

245     public Shape JavaDoc getShape(){
246         return shape;
247     }
248 }
249
Popular Tags