KickJava   Java API By Example, From Geeks To Geeks.

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


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  * XYDrawableAnnotation.java
28  * -------------------------
29  * (C) Copyright 2003, 2004, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: XYDrawableAnnotation.java,v 1.6 2005/05/19 15:41:53 mungady Exp $
35  *
36  * Changes:
37  * --------
38  * 21-May-2003 : Version 1 (DG);
39  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
40  * 30-Sep-2004 : Added support for tool tips and URLs (DG);
41  *
42  */

43
44 package org.jfree.chart.annotations;
45
46 import java.awt.Graphics2D JavaDoc;
47 import java.awt.geom.Rectangle2D JavaDoc;
48 import java.io.Serializable JavaDoc;
49
50 import org.jfree.chart.axis.ValueAxis;
51 import org.jfree.chart.plot.Plot;
52 import org.jfree.chart.plot.PlotOrientation;
53 import org.jfree.chart.plot.PlotRenderingInfo;
54 import org.jfree.chart.plot.XYPlot;
55 import org.jfree.ui.Drawable;
56 import org.jfree.ui.RectangleEdge;
57 import org.jfree.util.ObjectUtilities;
58 import org.jfree.util.PublicCloneable;
59
60 /**
61  * A general annotation that can be placed on
62  * an {@link org.jfree.chart.plot.XYPlot}.
63  */

64 public class XYDrawableAnnotation extends AbstractXYAnnotation
65                                   implements Cloneable JavaDoc, PublicCloneable,
66                                              Serializable JavaDoc {
67
68     /** For serialization. */
69     private static final long serialVersionUID = -6540812859722691020L;
70     
71     /** The x-coordinate. */
72     private double x;
73
74     /** The y-coordinate. */
75     private double y;
76
77     /** The width. */
78     private double width;
79
80     /** The height. */
81     private double height;
82
83     /** The drawable object. */
84     private Drawable drawable;
85
86     /**
87      * Creates a new annotation to be displayed within the given area.
88      *
89      * @param x the x-coordinate for the area.
90      * @param y the y-coordinate for the area.
91      * @param width the width of the area.
92      * @param height the height of the area.
93      * @param drawable the drawable object (<code>null</code> not permitted).
94      */

95     public XYDrawableAnnotation(double x, double y, double width, double height,
96                                 Drawable drawable) {
97
98         if (drawable == null) {
99             throw new IllegalArgumentException JavaDoc("Null 'drawable' argument.");
100         }
101         this.x = x;
102         this.y = y;
103         this.width = width;
104         this.height = height;
105         this.drawable = drawable;
106
107     }
108
109     /**
110      * Draws the annotation.
111      *
112      * @param g2 the graphics device.
113      * @param plot the plot.
114      * @param dataArea the data area.
115      * @param domainAxis the domain axis.
116      * @param rangeAxis the range axis.
117      * @param rendererIndex the renderer index.
118      * @param info if supplied, this info object will be populated with
119      * entity information.
120      */

121     public void draw(Graphics2D JavaDoc g2, XYPlot plot, Rectangle2D JavaDoc dataArea,
122                      ValueAxis domainAxis, ValueAxis rangeAxis,
123                      int rendererIndex,
124                      PlotRenderingInfo info) {
125
126         PlotOrientation orientation = plot.getOrientation();
127         RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
128             plot.getDomainAxisLocation(), orientation
129         );
130         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
131             plot.getRangeAxisLocation(), orientation
132         );
133         float j2DX = (float) domainAxis.valueToJava2D(
134             this.x, dataArea, domainEdge
135         );
136         float j2DY = (float) rangeAxis.valueToJava2D(
137             this.y, dataArea, rangeEdge
138         );
139         Rectangle2D JavaDoc area = new Rectangle2D.Double JavaDoc(
140             j2DX - this.width / 2.0, j2DY - this.height / 2.0,
141             this.width, this.height
142         );
143         this.drawable.draw(g2, area);
144         String JavaDoc toolTip = getToolTipText();
145         String JavaDoc url = getURL();
146         if (toolTip != null || url != null) {
147             addEntity(info, area, rendererIndex, toolTip, url);
148         }
149         
150     }
151
152     /**
153      * Tests this annotation for equality with an arbitrary object.
154      *
155      * @param obj the object to test against.
156      *
157      * @return <code>true</code> or <code>false</code>.
158      */

159     public boolean equals(Object JavaDoc obj) {
160         
161         if (obj == this) { // simple case
162
return true;
163         }
164         // now try to reject equality...
165
if (!super.equals(obj)) {
166             return false;
167         }
168         if (!(obj instanceof XYDrawableAnnotation)) {
169             return false;
170         }
171         XYDrawableAnnotation that = (XYDrawableAnnotation) obj;
172         if (this.x != that.x) {
173             return false;
174         }
175         if (this.y != that.y) {
176             return false;
177         }
178         if (this.width != that.width) {
179             return false;
180         }
181         if (this.height != that.height) {
182             return false;
183         }
184         if (!ObjectUtilities.equal(this.drawable, that.drawable)) {
185             return false;
186         }
187         // seem to be the same...
188
return true;
189         
190     }
191     
192     /**
193      * Returns a hash code.
194      *
195      * @return A hash code.
196      */

197     public int hashCode() {
198         int result;
199         long temp;
200         temp = Double.doubleToLongBits(this.x);
201         result = (int) (temp ^ (temp >>> 32));
202         temp = Double.doubleToLongBits(this.y);
203         result = 29 * result + (int) (temp ^ (temp >>> 32));
204         temp = Double.doubleToLongBits(this.width);
205         result = 29 * result + (int) (temp ^ (temp >>> 32));
206         temp = Double.doubleToLongBits(this.height);
207         result = 29 * result + (int) (temp ^ (temp >>> 32));
208         return result;
209     }
210     
211     /**
212      * Returns a clone of the annotation.
213      *
214      * @return A clone.
215      *
216      * @throws CloneNotSupportedException if the annotation can't be cloned.
217      */

218     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
219         return super.clone();
220     }
221
222 }
223
Popular Tags