KickJava   Java API By Example, From Geeks To Geeks.

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


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.Paint JavaDoc;
22 import java.awt.Shape JavaDoc;
23 import java.awt.Stroke JavaDoc;
24 import java.awt.geom.Rectangle2D JavaDoc;
25 import java.awt.geom.Point2D JavaDoc;
26
27 /**
28  * A shape painter that can be used to draw the outline of a shape.
29  *
30  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
31  * @version $Id: StrokeShapePainter.java,v 1.15 2004/08/18 07:14:27 vhardy Exp $
32  */

33 public class StrokeShapePainter implements ShapePainter {
34
35     /**
36      * Shape painted by this painter.
37      */

38     protected Shape JavaDoc shape;
39
40     /**
41      * Stroked version of the shape.
42      */

43     protected Shape JavaDoc strokedShape;
44
45     /**
46      * The stroke attribute used to draw the outline of the shape.
47      */

48     protected Stroke JavaDoc stroke;
49
50     /**
51      * The paint attribute used to draw the outline of the shape.
52      */

53     protected Paint JavaDoc paint;
54
55     /**
56      * Constructs a new <tt>ShapePainter</tt> that can be used to draw the
57      * outline of a <tt>Shape</tt>.
58      *
59      * @param shape shape to be painted by this painter.
60      * Should not be null.
61      */

62     public StrokeShapePainter(Shape JavaDoc shape) {
63         if (shape == null) {
64             throw new IllegalArgumentException JavaDoc();
65         }
66         this.shape = shape;
67     }
68
69     /**
70      * Sets the stroke used to draw the outline of a shape.
71      *
72      * @param newStroke the stroke object used to draw the outline of the shape
73      */

74     public void setStroke(Stroke JavaDoc newStroke) {
75         this.stroke = newStroke;
76         this.strokedShape = null;
77     }
78
79     /**
80      * Sets the paint used to fill a shape.
81      *
82      * @param newPaint the paint object used to draw the shape
83      */

84     public void setPaint(Paint JavaDoc newPaint) {
85         this.paint = newPaint;
86     }
87
88     /**
89      * Paints the outline of the specified shape using the specified
90      * Graphics2D.
91      *
92      * @param g2d the Graphics2D to use
93      */

94     public void paint(Graphics2D JavaDoc g2d) {
95         if (stroke != null && paint != null) {
96             g2d.setPaint(paint);
97             g2d.setStroke(stroke);
98             g2d.draw(shape);
99         }
100     }
101
102     /**
103      * Returns the area painted by this shape painter.
104      */

105     public Shape JavaDoc getPaintedArea(){
106         if ((paint == null) || (stroke == null))
107             return null;
108
109         if (strokedShape == null)
110             strokedShape = stroke.createStrokedShape(shape);
111
112         return strokedShape;
113     }
114
115     /**
116      * Returns the bounds of the area painted by this shape painter
117      */

118     public Rectangle2D JavaDoc getPaintedBounds2D() {
119         Shape JavaDoc painted = getPaintedArea();
120         if (painted == null)
121             return null;
122
123         return painted.getBounds2D();
124     }
125
126     /**
127      * Returns the bounds of the area covered by this shape painter
128      */

129     public boolean inPaintedArea(Point2D JavaDoc pt){
130         Shape JavaDoc painted = getPaintedArea();
131         if (painted == null)
132             return false;
133         return painted.contains(pt);
134     }
135         
136     /**
137      * Returns the area covered by this shape painter (even if not painted).
138      */

139     public Shape JavaDoc getSensitiveArea(){
140         if (stroke == null)
141             return null;
142
143         if (strokedShape == null)
144             strokedShape = stroke.createStrokedShape(shape);
145
146         return strokedShape;
147     }
148
149     /**
150      * Returns the bounds of the area covered by this shape painter
151      * (even if not painted).
152      */

153     public Rectangle2D JavaDoc getSensitiveBounds2D() {
154         Shape JavaDoc sensitive = getSensitiveArea();
155         if (sensitive == null)
156             return null;
157
158         return sensitive.getBounds2D();
159     }
160
161     /**
162      * Returns the bounds of the area covered by this shape painter
163      * (even if not painted).
164      */

165     public boolean inSensitiveArea(Point2D JavaDoc pt){
166         Shape JavaDoc sensitive = getSensitiveArea();
167         if (sensitive == null)
168             return false;
169         return sensitive.contains(pt);
170     }
171         
172     /**
173      * Sets the Shape this shape painter is associated with.
174      *
175      * @param shape new shape this painter should be associated with.
176      * Should not be null.
177      */

178     public void setShape(Shape JavaDoc shape){
179         if (shape == null) {
180             throw new IllegalArgumentException JavaDoc();
181         }
182         this.shape = shape;
183         this.strokedShape = null;
184     }
185
186     /**
187      * Gets the Shape this shape painter is associated with.
188      *
189      * @return shape associated with this painter.
190      */

191     public Shape JavaDoc getShape(){
192         return shape;
193     }
194 }
195
Popular Tags