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.Stroke;
025: import java.awt.geom.Rectangle2D;
026: import java.awt.geom.Point2D;
027:
028: /**
029: * A shape painter that can be used to draw the outline of a shape.
030: *
031: * @author <a href="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
032: * @version $Id: StrokeShapePainter.java 475477 2006-11-15 22:44:28Z cam $
033: */
034: public class StrokeShapePainter implements ShapePainter {
035:
036: /**
037: * Shape painted by this painter.
038: */
039: protected Shape shape;
040:
041: /**
042: * Stroked version of the shape.
043: */
044: protected Shape strokedShape;
045:
046: /**
047: * The stroke attribute used to draw the outline of the shape.
048: */
049: protected Stroke stroke;
050:
051: /**
052: * The paint attribute used to draw the outline of the shape.
053: */
054: protected Paint paint;
055:
056: /**
057: * Constructs a new <tt>ShapePainter</tt> that can be used to draw the
058: * outline of a <tt>Shape</tt>.
059: *
060: * @param shape shape to be painted by this painter.
061: * Should not be null.
062: */
063: public StrokeShapePainter(Shape shape) {
064: if (shape == null) {
065: throw new IllegalArgumentException();
066: }
067: this .shape = shape;
068: }
069:
070: /**
071: * Sets the stroke used to draw the outline of a shape.
072: *
073: * @param newStroke the stroke object used to draw the outline of the shape
074: */
075: public void setStroke(Stroke newStroke) {
076: this .stroke = newStroke;
077: this .strokedShape = null;
078: }
079:
080: /**
081: * Gets the stroke used to draw the outline of the shape.
082: */
083: public Stroke getStroke() {
084: return stroke;
085: }
086:
087: /**
088: * Sets the paint used to fill a shape.
089: *
090: * @param newPaint the paint object used to draw the shape
091: */
092: public void setPaint(Paint newPaint) {
093: this .paint = newPaint;
094: }
095:
096: /**
097: * Gets the paint used to draw the outline of the shape.
098: */
099: public Paint getPaint() {
100: return paint;
101: }
102:
103: /**
104: * Paints the outline of the specified shape using the specified
105: * Graphics2D.
106: *
107: * @param g2d the Graphics2D to use
108: */
109: public void paint(Graphics2D g2d) {
110: if (stroke != null && paint != null) {
111: g2d.setPaint(paint);
112: g2d.setStroke(stroke);
113: g2d.draw(shape);
114: }
115: }
116:
117: /**
118: * Returns the area painted by this shape painter.
119: */
120: public Shape getPaintedArea() {
121: if ((paint == null) || (stroke == null))
122: return null;
123:
124: if (strokedShape == null)
125: strokedShape = stroke.createStrokedShape(shape);
126:
127: return strokedShape;
128: }
129:
130: /**
131: * Returns the bounds of the area painted by this shape painter
132: */
133: public Rectangle2D getPaintedBounds2D() {
134: Shape painted = getPaintedArea();
135: if (painted == null)
136: return null;
137:
138: return painted.getBounds2D();
139: }
140:
141: /**
142: * Returns the bounds of the area covered by this shape painter
143: */
144: public boolean inPaintedArea(Point2D pt) {
145: Shape painted = getPaintedArea();
146: if (painted == null)
147: return false;
148: return painted.contains(pt);
149: }
150:
151: /**
152: * Returns the area covered by this shape painter (even if not painted).
153: */
154: public Shape getSensitiveArea() {
155: if (stroke == null)
156: return null;
157:
158: if (strokedShape == null)
159: strokedShape = stroke.createStrokedShape(shape);
160:
161: return strokedShape;
162: }
163:
164: /**
165: * Returns the bounds of the area covered by this shape painter
166: * (even if not painted).
167: */
168: public Rectangle2D getSensitiveBounds2D() {
169: Shape sensitive = getSensitiveArea();
170: if (sensitive == null)
171: return null;
172:
173: return sensitive.getBounds2D();
174: }
175:
176: /**
177: * Returns the bounds of the area covered by this shape painter
178: * (even if not painted).
179: */
180: public boolean inSensitiveArea(Point2D pt) {
181: Shape sensitive = getSensitiveArea();
182: if (sensitive == null)
183: return false;
184: return sensitive.contains(pt);
185: }
186:
187: /**
188: * Sets the Shape this shape painter is associated with.
189: *
190: * @param shape new shape this painter should be associated with.
191: * Should not be null.
192: */
193: public void setShape(Shape shape) {
194: if (shape == null) {
195: throw new IllegalArgumentException();
196: }
197: this .shape = shape;
198: this .strokedShape = null;
199: }
200:
201: /**
202: * Gets the Shape this shape painter is associated with.
203: *
204: * @return shape associated with this painter.
205: */
206: public Shape getShape() {
207: return shape;
208: }
209: }
|