001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.gvt;
020:
021: import java.awt.Graphics2D;
022: import java.awt.Shape;
023: import java.awt.geom.Area;
024: import java.awt.geom.Point2D;
025: import java.awt.geom.Rectangle2D;
026:
027: /**
028: * A shape painter which consists of multiple shape painters.
029: *
030: * @author <a href="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
031: * @version $Id: CompositeShapePainter.java 479564 2006-11-27 09:56:57Z dvholten $
032: */
033: public class CompositeShapePainter implements ShapePainter {
034:
035: /**
036: * The shape associated with this painter
037: */
038: protected Shape shape;
039:
040: /**
041: * The enclosed <tt>ShapePainter</tt>s of this composite shape painter.
042: */
043: protected ShapePainter[] painters;
044:
045: /**
046: * The number of shape painter.
047: */
048: protected int count;
049:
050: /**
051: * Constructs a new empty <tt>CompositeShapePainter</tt>.
052: */
053: public CompositeShapePainter(Shape shape) {
054: if (shape == null) {
055: throw new IllegalArgumentException();
056: }
057: this .shape = shape;
058: }
059:
060: /**
061: * Adds the specified shape painter to the shape painter..
062: *
063: * @param shapePainter the shape painter to add
064: */
065: public void addShapePainter(ShapePainter shapePainter) {
066: if (shapePainter == null) {
067: return;
068: }
069: if (shape != shapePainter.getShape()) {
070: shapePainter.setShape(shape);
071: }
072: if (painters == null) {
073: painters = new ShapePainter[2];
074: }
075: if (count == painters.length) {
076: ShapePainter[] newPainters = new ShapePainter[count + count
077: / 2 + 1];
078: System.arraycopy(painters, 0, newPainters, 0, count);
079: painters = newPainters;
080: }
081: painters[count++] = shapePainter;
082: }
083:
084: /**
085: * Sets to the specified index, the specified ShapePainter.
086: *
087: * @param index the index where to set the ShapePainter
088: * @param shapePainter the ShapePainter to set
089: */
090: /* public void setShapePainter(int index, ShapePainter shapePainter) {
091: if (shapePainter == null) {
092: return;
093: }
094: if (this.shape != shapePainter.getShape()) {
095: shapePainter.setShape(shape);
096: }
097: if (painters == null || index >= painters.length) {
098: throw new IllegalArgumentException("Bad index: "+index);
099: }
100: painters[index] = shapePainter;
101: }*/
102:
103: /**
104: * Returns the shape painter at the specified index.
105: *
106: * @param index the index of the shape painter to return
107: */
108: public ShapePainter getShapePainter(int index) {
109: return painters[index];
110: }
111:
112: /**
113: * Returns the number of shape painter of this composite shape painter.
114: */
115: public int getShapePainterCount() {
116: return count;
117: }
118:
119: /**
120: * Paints the specified shape using the specified Graphics2D.
121: *
122: * @param g2d the Graphics2D to use
123: */
124: public void paint(Graphics2D g2d) {
125: if (painters != null) {
126: for (int i = 0; i < count; ++i) {
127: painters[i].paint(g2d);
128: }
129: }
130: }
131:
132: /**
133: * Returns the area painted by this shape painter.
134: */
135: public Shape getPaintedArea() {
136: if (painters == null)
137: return null;
138: Area paintedArea = new Area();
139: for (int i = 0; i < count; ++i) {
140: Shape s = painters[i].getPaintedArea();
141: if (s != null) {
142: paintedArea.add(new Area(s));
143: }
144: }
145: return paintedArea;
146: }
147:
148: /**
149: * Returns the bounds of the area painted by this shape painter
150: */
151: public Rectangle2D getPaintedBounds2D() {
152: if (painters == null)
153: return null;
154:
155: Rectangle2D bounds = null;
156: for (int i = 0; i < count; ++i) {
157: Rectangle2D pb = painters[i].getPaintedBounds2D();
158: if (pb == null)
159: continue;
160: if (bounds == null)
161: bounds = (Rectangle2D) pb.clone();
162: else
163: bounds.add(pb);
164: }
165: return bounds;
166: }
167:
168: /**
169: * Returns true if pt is in the area painted by this shape painter
170: */
171: public boolean inPaintedArea(Point2D pt) {
172: if (painters == null)
173: return false;
174: for (int i = 0; i < count; ++i) {
175: if (painters[i].inPaintedArea(pt))
176: return true;
177: }
178: return false;
179: }
180:
181: /**
182: * Returns the area covered by this shape painter (even if nothing
183: * is painted there).
184: */
185: public Shape getSensitiveArea() {
186: if (painters == null)
187: return null;
188: Area paintedArea = new Area();
189: for (int i = 0; i < count; ++i) {
190: Shape s = painters[i].getSensitiveArea();
191: if (s != null) {
192: paintedArea.add(new Area(s));
193: }
194: }
195: return paintedArea;
196: }
197:
198: /**
199: * Returns the bounds of the area painted by this shape painter
200: */
201: public Rectangle2D getSensitiveBounds2D() {
202: if (painters == null)
203: return null;
204:
205: Rectangle2D bounds = null;
206: for (int i = 0; i < count; ++i) {
207: Rectangle2D pb = painters[i].getSensitiveBounds2D();
208: if (bounds == null)
209: bounds = (Rectangle2D) pb.clone();
210: else
211: bounds.add(pb);
212: }
213: return bounds;
214: }
215:
216: /**
217: * Returns true if pt is in the area painted by this shape painter
218: */
219: public boolean inSensitiveArea(Point2D pt) {
220: if (painters == null)
221: return false;
222: for (int i = 0; i < count; ++i) {
223: if (painters[i].inSensitiveArea(pt))
224: return true;
225: }
226: return false;
227: }
228:
229: /**
230: * Sets the Shape this shape painter is associated with.
231: *
232: * @param shape new shape this painter should be associated with.
233: * Should not be null.
234: */
235: public void setShape(Shape shape) {
236: if (shape == null) {
237: throw new IllegalArgumentException();
238: }
239: if (painters != null) {
240: for (int i = 0; i < count; ++i) {
241: painters[i].setShape(shape);
242: }
243: }
244: this .shape = shape;
245: }
246:
247: /**
248: * Gets the Shape this shape painter is associated with.
249: *
250: * @return shape associated with this painter
251: */
252: public Shape getShape() {
253: return shape;
254: }
255: }
|