KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > annotations > XYShapeAnnotation


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ----------------------
27  * XYShapeAnnotation.java
28  * ----------------------
29  * (C) Copyright 2003-2005, by Ondax, Inc. and Contributors.
30  *
31  * Original Author: Greg Steckman (for Ondax, Inc.);
32  * Contributor(s): David Gilbert (for Object Refinery Limited);
33  *
34  * $Id: XYShapeAnnotation.java,v 1.8 2005/05/19 15:41:53 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 15-Aug-2003 : Version 1, adapted from
39  * org.jfree.chart.annotations.XYLineAnnotation (GS);
40  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
41  * 20-Apr-2004 : Added new constructor and fixed bug 934258 (DG);
42  * 29-Sep-2004 : Added 'fillPaint' to allow for colored shapes, now extends
43  * AbstractXYAnnotation to add tool tip and URL support, and
44  * implemented equals() and Cloneable (DG);
45  * 21-Jan-2005 : Modified constructor for consistency with other
46  * constructors (DG);
47  *
48  */

49  
50 package org.jfree.chart.annotations;
51
52 import java.awt.BasicStroke JavaDoc;
53 import java.awt.Color JavaDoc;
54 import java.awt.Graphics2D JavaDoc;
55 import java.awt.Paint JavaDoc;
56 import java.awt.Shape JavaDoc;
57 import java.awt.Stroke JavaDoc;
58 import java.awt.geom.AffineTransform JavaDoc;
59 import java.awt.geom.Rectangle2D JavaDoc;
60 import java.io.IOException JavaDoc;
61 import java.io.ObjectInputStream JavaDoc;
62 import java.io.ObjectOutputStream JavaDoc;
63 import java.io.Serializable JavaDoc;
64
65 import org.jfree.chart.axis.ValueAxis;
66 import org.jfree.chart.plot.Plot;
67 import org.jfree.chart.plot.PlotOrientation;
68 import org.jfree.chart.plot.PlotRenderingInfo;
69 import org.jfree.chart.plot.XYPlot;
70 import org.jfree.io.SerialUtilities;
71 import org.jfree.ui.RectangleEdge;
72 import org.jfree.util.ObjectUtilities;
73 import org.jfree.util.PublicCloneable;
74
75 /**
76  * A simple <code>Shape</code> annotation that can be placed on an
77  * {@link XYPlot}. The shape coordinates are specified in data space.
78  *
79  * @author Greg Steckman
80  */

81 public class XYShapeAnnotation extends AbstractXYAnnotation
82                                implements Cloneable JavaDoc, PublicCloneable,
83                                           Serializable JavaDoc {
84     
85     /** For serialization. */
86     private static final long serialVersionUID = -8553218317600684041L;
87     
88     /** The shape. */
89     private transient Shape JavaDoc shape;
90
91     /** The stroke used to draw the shape's outline. */
92     private transient Stroke JavaDoc stroke;
93
94     /** The paint used to draw the shape's outline. */
95     private transient Paint JavaDoc outlinePaint;
96     
97     /** The paint used to fill the shape. */
98     private transient Paint JavaDoc fillPaint;
99
100     /**
101      * Creates a new annotation (where, by default, the shape is drawn
102      * with a black outline).
103      *
104      * @param shape the shape (coordinates in data space).
105      */

106     public XYShapeAnnotation(Shape JavaDoc shape) {
107         this(shape, new BasicStroke JavaDoc(1.0f), Color.black);
108     }
109     
110     /**
111      * Creates a new annotation where the shape is drawn as an outline using
112      * the specified <code>stroke</code> and <code>outlinePaint</code>.
113      *
114      * @param shape the shape (<code>null</code> not permitted).
115      * @param stroke the shape stroke (<code>null</code> permitted).
116      * @param outlinePaint the shape color (<code>null</code> permitted).
117      */

118     public XYShapeAnnotation(Shape JavaDoc shape, Stroke JavaDoc stroke, Paint JavaDoc outlinePaint) {
119         this(shape, stroke, outlinePaint, null);
120     }
121
122     /**
123      * Creates a new annotation.
124      *
125      * @param shape the shape (<code>null</code> not permitted).
126      * @param stroke the shape stroke (<code>null</code> permitted).
127      * @param outlinePaint the shape color (<code>null</code> permitted).
128      * @param fillPaint the paint used to fill the shape (<code>null</code>
129      * permitted.
130      */

131     public XYShapeAnnotation(Shape JavaDoc shape, Stroke JavaDoc stroke, Paint JavaDoc outlinePaint,
132                              Paint JavaDoc fillPaint) {
133         if (shape == null) {
134             throw new IllegalArgumentException JavaDoc("Null 'shape' argument.");
135         }
136         this.shape = shape;
137         this.stroke = stroke;
138         this.outlinePaint = outlinePaint;
139         this.fillPaint = fillPaint;
140     }
141
142     /**
143      * Draws the annotation. This method is usually called by the
144      * {@link XYPlot} class, you shouldn't need to call it directly.
145      *
146      * @param g2 the graphics device.
147      * @param plot the plot.
148      * @param dataArea the data area.
149      * @param domainAxis the domain axis.
150      * @param rangeAxis the range axis.
151      * @param rendererIndex the renderer index.
152      * @param info the plot rendering info.
153      */

154     public void draw(Graphics2D JavaDoc g2, XYPlot plot, Rectangle2D JavaDoc dataArea,
155                      ValueAxis domainAxis, ValueAxis rangeAxis,
156                      int rendererIndex,
157                      PlotRenderingInfo info) {
158
159         PlotOrientation orientation = plot.getOrientation();
160         RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
161             plot.getDomainAxisLocation(), orientation
162         );
163         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
164             plot.getRangeAxisLocation(), orientation
165         );
166
167         //compute transform matrix elements via sample points. Assume no
168
// rotation or shear.
169
// x-axis translation
170
double m02 = domainAxis.valueToJava2D(0, dataArea, domainEdge);
171         // y-axis translation
172
double m12 = rangeAxis.valueToJava2D(0, dataArea, rangeEdge);
173         // x-axis scale
174
double m00 = domainAxis.valueToJava2D(1, dataArea, domainEdge) - m02;
175         // y-axis scale
176
double m11 = rangeAxis.valueToJava2D(1, dataArea, rangeEdge) - m12;
177
178         //create transform & transform shape
179
Shape JavaDoc s = null;
180         if (orientation == PlotOrientation.HORIZONTAL) {
181             AffineTransform JavaDoc t1 = new AffineTransform JavaDoc(
182                 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f
183             );
184             AffineTransform JavaDoc t2 = new AffineTransform JavaDoc(
185                 m11, 0.0f, 0.0f, m00, m12, m02
186             );
187             s = t1.createTransformedShape(this.shape);
188             s = t2.createTransformedShape(s);
189         }
190         else if (orientation == PlotOrientation.VERTICAL) {
191             AffineTransform JavaDoc t = new AffineTransform JavaDoc(m00, 0, 0, m11, m02, m12);
192             s = t.createTransformedShape(this.shape);
193         }
194
195         if (this.fillPaint != null) {
196             g2.setPaint(this.fillPaint);
197             g2.fill(s);
198         }
199         
200         if (this.stroke != null && this.outlinePaint != null) {
201             g2.setPaint(this.outlinePaint);
202             g2.setStroke(this.stroke);
203             g2.draw(s);
204         }
205         addEntity(info, s, rendererIndex, getToolTipText(), getURL());
206         
207     }
208         
209     /**
210      * Tests this annotation for equality with an arbitrary object.
211      *
212      * @param obj the object (<code>null</code> permitted).
213      *
214      * @return A boolean.
215      */

216     public boolean equals(Object JavaDoc obj) {
217         if (obj == this) {
218             return true;
219         }
220         // now try to reject equality
221
if (!super.equals(obj)) {
222             return false;
223         }
224         if (!(obj instanceof XYShapeAnnotation)) {
225             return false;
226         }
227         XYShapeAnnotation that = (XYShapeAnnotation) obj;
228         if (!this.shape.equals(that.shape)) {
229             return false;
230         }
231         if (!ObjectUtilities.equal(this.stroke, that.stroke)) {
232             return false;
233         }
234         if (!ObjectUtilities.equal(this.outlinePaint, that.outlinePaint)) {
235             return false;
236         }
237         if (!ObjectUtilities.equal(this.fillPaint, that.fillPaint)) {
238             return false;
239         }
240         // seem to be the same
241
return true;
242     }
243     
244     /**
245      * Returns a hash code for this instance.
246      *
247      * @return A hash code.
248      */

249     public int hashCode() {
250         // TODO: implement this properly.
251
return this.shape.hashCode();
252     }
253     
254     /**
255      * Returns a clone.
256      *
257      * @return A clone.
258      *
259      * @throws CloneNotSupportedException ???.
260      */

261     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
262         return super.clone();
263     }
264     
265     /**
266      * Provides serialization support.
267      *
268      * @param stream the output stream.
269      *
270      * @throws IOException if there is an I/O error.
271      */

272     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
273         stream.defaultWriteObject();
274         SerialUtilities.writeShape(this.shape, stream);
275         SerialUtilities.writeStroke(this.stroke, stream);
276         SerialUtilities.writePaint(this.outlinePaint, stream);
277         SerialUtilities.writePaint(this.fillPaint, stream);
278     }
279
280     /**
281      * Provides serialization support.
282      *
283      * @param stream the input stream.
284      *
285      * @throws IOException if there is an I/O error.
286      * @throws ClassNotFoundException if there is a classpath problem.
287      */

288     private void readObject(ObjectInputStream JavaDoc stream)
289         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
290         stream.defaultReadObject();
291         this.shape = SerialUtilities.readShape(stream);
292         this.stroke = SerialUtilities.readStroke(stream);
293         this.outlinePaint = SerialUtilities.readPaint(stream);
294         this.fillPaint = SerialUtilities.readPaint(stream);
295     }
296
297 }
298
Popular Tags