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.Paint;
023: import java.awt.Shape;
024: import java.awt.geom.Rectangle2D;
025: import java.awt.geom.Point2D;
026:
027: /**
028: * A shape painter that can be used to fill a shape.
029: *
030: * @author <a href="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
031: * @version $Id: FillShapePainter.java 475685 2006-11-16 11:16:05Z cam $
032: */
033: public class FillShapePainter implements ShapePainter {
034:
035: /**
036: * The Shape to be painted.
037: */
038: protected Shape shape;
039:
040: /**
041: * The paint attribute used to fill the shape.
042: */
043: protected Paint paint;
044:
045: /**
046: * Constructs a new <tt>FillShapePainter</tt> that can be used to fill
047: * a <tt>Shape</tt>.
048: *
049: * @param shape Shape to be painted by this painter
050: * Should not be null.
051: */
052: public FillShapePainter(Shape shape) {
053: if (shape == null)
054: throw new IllegalArgumentException("Shape can not be null!");
055:
056: this .shape = shape;
057: }
058:
059: /**
060: * Sets the paint used to fill a shape.
061: *
062: * @param newPaint the paint object used to fill the shape
063: */
064: public void setPaint(Paint newPaint) {
065: this .paint = newPaint;
066: }
067:
068: /**
069: * Gets the paint used to draw the outline of the shape.
070: */
071: public Paint getPaint() {
072: return paint;
073: }
074:
075: /**
076: * Paints the specified shape using the specified Graphics2D.
077: *
078: * @param g2d the Graphics2D to use
079: */
080: public void paint(Graphics2D g2d) {
081: if (paint != null) {
082: g2d.setPaint(paint);
083: g2d.fill(shape);
084: }
085: }
086:
087: /**
088: * Returns the area painted by this shape painter.
089: */
090: public Shape getPaintedArea() {
091: if (paint == null)
092: return null;
093: return shape;
094: }
095:
096: /**
097: * Returns the bounds of the area painted by this shape painter
098: */
099: public Rectangle2D getPaintedBounds2D() {
100: if ((paint == null) || (shape == null))
101: return null;
102:
103: return shape.getBounds2D();
104: }
105:
106: /**
107: * Returns true if pt is in the area painted by this shape painter
108: */
109: public boolean inPaintedArea(Point2D pt) {
110: if ((paint == null) || (shape == null))
111: return false;
112:
113: return shape.contains(pt);
114: }
115:
116: /**
117: * Returns the area covered by this shape painter (even if not painted).
118: *
119: */
120: public Shape getSensitiveArea() {
121: return shape;
122: }
123:
124: /**
125: * Returns the bounds of the area covered by this shape painte
126: * (even if not painted).
127: */
128: public Rectangle2D getSensitiveBounds2D() {
129: if (shape == null)
130: return null;
131: return shape.getBounds2D();
132: }
133:
134: /**
135: * Returns true if pt is in the area painted by this shape painter
136: */
137: public boolean inSensitiveArea(Point2D pt) {
138: if (shape == null)
139: return false;
140: return shape.contains(pt);
141: }
142:
143: /**
144: * Sets the Shape this shape painter is associated with.
145: *
146: * @param shape new shape this painter should be associated with.
147: * Should not be null.
148: */
149: public void setShape(Shape shape) {
150: if (shape == null) {
151: throw new IllegalArgumentException();
152: }
153: this .shape = shape;
154: }
155:
156: /**
157: * Gets the Shape this shape painter is associated with.
158: *
159: * @return shape associated with this Painter.
160: */
161: public Shape getShape() {
162: return shape;
163: }
164: }
|