KickJava   Java API By Example, From Geeks To Geeks.

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


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
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ----------------------
28  * XYImageAnnotation.java
29  * ----------------------
30  * (C) Copyright 2003, 2004, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: XYImageAnnotation.java,v 1.8.2.1 2005/10/25 16:51:15 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 01-Dec-2003 : Version 1 (DG);
40  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
41  * 18-May-2004 : Fixed bug with plot orientation (DG);
42  * 29-Sep-2004 : Now extends AbstractXYAnnotation, with modified draw()
43  * method signature and updated equals() method (DG);
44  *
45  */

46
47 package org.jfree.chart.annotations;
48
49 import java.awt.Graphics2D JavaDoc;
50 import java.awt.Image JavaDoc;
51 import java.awt.geom.Rectangle2D JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.io.ObjectInputStream JavaDoc;
54 import java.io.ObjectOutputStream JavaDoc;
55 import java.io.Serializable JavaDoc;
56
57 import org.jfree.chart.axis.AxisLocation;
58 import org.jfree.chart.axis.ValueAxis;
59 import org.jfree.chart.plot.Plot;
60 import org.jfree.chart.plot.PlotOrientation;
61 import org.jfree.chart.plot.PlotRenderingInfo;
62 import org.jfree.chart.plot.XYPlot;
63 import org.jfree.ui.RectangleEdge;
64 import org.jfree.util.ObjectUtilities;
65 import org.jfree.util.PublicCloneable;
66
67 /**
68  * An annotation that allows an image to be placed at some location on
69  * an {@link XYPlot}.
70  *
71  * TODO: implement serialization properly (image is not serializable).
72  */

73 public class XYImageAnnotation extends AbstractXYAnnotation
74                                implements Cloneable JavaDoc, PublicCloneable,
75                                           Serializable JavaDoc {
76
77     /** For serialization. */
78     private static final long serialVersionUID = -4364694501921559958L;
79     
80     /** The x-coordinate (in data space). */
81     private double x;
82
83     /** The y-coordinate (in data space). */
84     private double y;
85
86     /** The image. */
87     private transient Image JavaDoc image;
88
89     /**
90      * Creates a new annotation to be displayed at the specified (x, y)
91      * location.
92      *
93      * @param x the x-coordinate (in data space).
94      * @param y the y-coordinate (in data space).
95      * @param image the image (<code>null</code> not permitted).
96      */

97     public XYImageAnnotation(double x, double y, Image JavaDoc image) {
98         if (image == null) {
99             throw new IllegalArgumentException JavaDoc("Null 'image' argument.");
100         }
101         this.x = x;
102         this.y = y;
103         this.image = image;
104     }
105
106     /**
107      * Draws the annotation. This method is called by the drawing code in the
108      * {@link XYPlot} class, you don't normally need to call this method
109      * directly.
110      *
111      * @param g2 the graphics device.
112      * @param plot the plot.
113      * @param dataArea the data area.
114      * @param domainAxis the domain axis.
115      * @param rangeAxis the range axis.
116      * @param rendererIndex the renderer index.
117      * @param info if supplied, this info object will be populated with
118      * entity information.
119      */

120     public void draw(Graphics2D JavaDoc g2, XYPlot plot, Rectangle2D JavaDoc dataArea,
121                      ValueAxis domainAxis, ValueAxis rangeAxis,
122                      int rendererIndex,
123                      PlotRenderingInfo info) {
124
125         PlotOrientation orientation = plot.getOrientation();
126         AxisLocation domainAxisLocation = plot.getDomainAxisLocation();
127         AxisLocation rangeAxisLocation = plot.getRangeAxisLocation();
128         RectangleEdge domainEdge
129             = Plot.resolveDomainAxisLocation(domainAxisLocation, orientation);
130         RectangleEdge rangeEdge
131             = Plot.resolveRangeAxisLocation(rangeAxisLocation, orientation);
132         float j2DX
133             = (float) domainAxis.valueToJava2D(this.x, dataArea, domainEdge);
134         float j2DY
135             = (float) rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge);
136         float xx = 0.0f;
137         float yy = 0.0f;
138         if (orientation == PlotOrientation.HORIZONTAL) {
139             xx = j2DY;
140             yy = j2DX;
141         }
142         else if (orientation == PlotOrientation.VERTICAL) {
143             xx = j2DX;
144             yy = j2DY;
145         }
146         int w = this.image.getWidth(null);
147         int h = this.image.getHeight(null);
148         xx = xx - w / 2.0f;
149         yy = yy - h / 2.0f;
150         g2.drawImage(this.image, (int) xx, (int) yy, null);
151         
152         String JavaDoc toolTip = getToolTipText();
153         String JavaDoc url = getURL();
154         if (toolTip != null || url != null) {
155             addEntity(
156                 info, new Rectangle2D.Float JavaDoc(xx, yy, w, h), rendererIndex,
157                 toolTip, url
158             );
159         }
160     }
161
162     /**
163      * Tests this object for equality with an arbitrary object.
164      *
165      * @param obj the object (<code>null</code> permitted).
166      *
167      * @return A boolean.
168      */

169     public boolean equals(Object JavaDoc obj) {
170         if (obj == this) {
171             return true;
172         }
173         // now try to reject equality...
174
if (!super.equals(obj)) {
175             return false;
176         }
177         if (!(obj instanceof XYImageAnnotation)) {
178             return false;
179         }
180         XYImageAnnotation that = (XYImageAnnotation) obj;
181         if (this.x != that.x) {
182             return false;
183         }
184         if (this.y != that.y) {
185             return false;
186         }
187         if (!ObjectUtilities.equal(this.image, that.image)) {
188             return false;
189         }
190         // seems to be the same...
191
return true;
192     }
193     
194     /**
195      * Returns a hash code for this object.
196      *
197      * @return A hash code.
198      */

199     public int hashCode() {
200         return this.image.hashCode();
201     }
202     
203     /**
204      * Returns a clone of the annotation.
205      *
206      * @return A clone.
207      *
208      * @throws CloneNotSupportedException if the annotation can't be cloned.
209      */

210     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
211         return super.clone();
212     }
213     
214     /**
215      * Provides serialization support.
216      *
217      * @param stream the output stream.
218      *
219      * @throws IOException if there is an I/O error.
220      */

221     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
222         stream.defaultWriteObject();
223         //SerialUtilities.writeImage(this.image, stream);
224
}
225     
226     /**
227      * Provides serialization support.
228      *
229      * @param stream the input stream.
230      *
231      * @throws IOException if there is an I/O error.
232      * @throws ClassNotFoundException if there is a classpath problem.
233      */

234     private void readObject(ObjectInputStream JavaDoc stream)
235         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
236         stream.defaultReadObject();
237         //this.image = SerialUtilities.readImage(stream);
238
}
239
240
241 }
242
Popular Tags