KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 public class FillShapePainter implements ShapePainter {
33
34     /**
35      * The Shape to be painted.
36      */

37     protected Shape JavaDoc shape;
38
39     /**
40      * The paint attribute used to fill the shape.
41      */

42     protected Paint JavaDoc paint;
43
44     /**
45      * Constructs a new <tt>FillShapePainter</tt> that can be used to fill
46      * a <tt>Shape</tt>.
47      *
48      * @param shape Shape to be painted by this painter
49      * Should not be null.
50      */

51     public FillShapePainter(Shape JavaDoc shape) {
52         if (shape == null)
53             throw new IllegalArgumentException JavaDoc("Shape can not be null!");
54
55         this.shape = shape;
56     }
57
58     /**
59      * Sets the paint used to fill a shape.
60      *
61      * @param newPaint the paint object used to fill the shape
62      */

63     public void setPaint(Paint JavaDoc newPaint) {
64         this.paint = newPaint;
65     }
66
67     /**
68      * Paints the specified shape using the specified Graphics2D.
69      *
70      * @param g2d the Graphics2D to use
71      */

72      public void paint(Graphics2D JavaDoc g2d) {
73         if (paint != null) {
74             g2d.setPaint(paint);
75             g2d.fill(shape);
76         }
77     }
78
79     /**
80      * Returns the area painted by this shape painter.
81      */

82     public Shape JavaDoc getPaintedArea(){
83         if (paint == null)
84             return null;
85         return shape;
86     }
87
88     /**
89      * Returns the bounds of the area painted by this shape painter
90      */

91     public Rectangle2D JavaDoc getPaintedBounds2D(){
92         if ((paint == null) || (shape == null))
93             return null;
94
95         return shape.getBounds2D();
96     }
97
98     /**
99      * Returns true if pt is in the area painted by this shape painter
100      */

101     public boolean inPaintedArea(Point2D JavaDoc pt){
102         if ((paint == null) || (shape == null))
103             return false;
104
105         return shape.contains(pt);
106     }
107
108     /**
109      * Returns the area covered by this shape painter (even if not painted).
110      *
111      */

112     public Shape JavaDoc getSensitiveArea(){
113         return shape;
114     }
115
116     /**
117      * Returns the bounds of the area covered by this shape painte
118      * (even if not painted).
119      */

120     public Rectangle2D JavaDoc getSensitiveBounds2D() {
121         if (shape == null)
122             return null;
123         return shape.getBounds2D();
124     }
125
126     /**
127      * Returns true if pt is in the area painted by this shape painter
128      */

129     public boolean inSensitiveArea(Point2D JavaDoc pt){
130         if (shape == null)
131             return false;
132         return shape.contains(pt);
133     }
134
135     /**
136      * Sets the Shape this shape painter is associated with.
137      *
138      * @param shape new shape this painter should be associated with.
139      * Should not be null.
140      */

141     public void setShape(Shape JavaDoc shape){
142         if (shape == null) {
143             throw new IllegalArgumentException JavaDoc();
144         }
145         this.shape = shape;
146     }
147
148     /**
149      * Gets the Shape this shape painter is associated with.
150      *
151      * @return shape associated with this Painter.
152      */

153     public Shape JavaDoc getShape(){
154         return shape;
155     }
156 }
157
Popular Tags