001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ----------------------
028: * XYImageAnnotation.java
029: * ----------------------
030: * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): Mike Harris;
034: *
035: * $Id: XYImageAnnotation.java,v 1.8.2.2 2006/12/01 15:57:33 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 01-Dec-2003 : Version 1 (DG);
040: * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
041: * 18-May-2004 : Fixed bug with plot orientation (DG);
042: * 29-Sep-2004 : Now extends AbstractXYAnnotation, with modified draw()
043: * method signature and updated equals() method (DG);
044: * ------------- JFREECHART 1.0.x ---------------------------------------------
045: * 01-Dec-2006 : Added anchor attribute (see patch 1584860 from
046: * Mike Harris) (DG);
047: */
048:
049: package org.jfree.chart.annotations;
050:
051: import java.awt.Graphics2D;
052: import java.awt.Image;
053: import java.awt.geom.Point2D;
054: import java.awt.geom.Rectangle2D;
055: import java.io.IOException;
056: import java.io.ObjectInputStream;
057: import java.io.ObjectOutputStream;
058: import java.io.Serializable;
059:
060: import org.jfree.chart.axis.AxisLocation;
061: import org.jfree.chart.axis.ValueAxis;
062: import org.jfree.chart.plot.Plot;
063: import org.jfree.chart.plot.PlotOrientation;
064: import org.jfree.chart.plot.PlotRenderingInfo;
065: import org.jfree.chart.plot.XYPlot;
066: import org.jfree.ui.RectangleAnchor;
067: import org.jfree.ui.RectangleEdge;
068: import org.jfree.util.ObjectUtilities;
069: import org.jfree.util.PublicCloneable;
070:
071: /**
072: * An annotation that allows an image to be placed at some location on
073: * an {@link XYPlot}.
074: *
075: * TODO: implement serialization properly (image is not serializable).
076: */
077: public class XYImageAnnotation extends AbstractXYAnnotation implements
078: Cloneable, PublicCloneable, Serializable {
079:
080: /** For serialization. */
081: private static final long serialVersionUID = -4364694501921559958L;
082:
083: /** The x-coordinate (in data space). */
084: private double x;
085:
086: /** The y-coordinate (in data space). */
087: private double y;
088:
089: /** The image. */
090: private transient Image image;
091:
092: /**
093: * The image anchor point.
094: *
095: * @since 1.0.4
096: */
097: private RectangleAnchor anchor;
098:
099: /**
100: * Creates a new annotation to be displayed at the specified (x, y)
101: * location.
102: *
103: * @param x the x-coordinate (in data space).
104: * @param y the y-coordinate (in data space).
105: * @param image the image (<code>null</code> not permitted).
106: */
107: public XYImageAnnotation(double x, double y, Image image) {
108: this (x, y, image, RectangleAnchor.CENTER);
109: }
110:
111: /**
112: * Creates a new annotation to be displayed at the specified (x, y)
113: * location.
114: *
115: * @param x the x-coordinate (in data space).
116: * @param y the y-coordinate (in data space).
117: * @param image the image (<code>null</code> not permitted).
118: * @param anchor the image anchor (<code>null</code> not permitted).
119: *
120: * @since 1.0.4
121: */
122: public XYImageAnnotation(double x, double y, Image image,
123: RectangleAnchor anchor) {
124: if (image == null) {
125: throw new IllegalArgumentException("Null 'image' argument.");
126: }
127: if (anchor == null) {
128: throw new IllegalArgumentException(
129: "Null 'anchor' argument.");
130: }
131: this .x = x;
132: this .y = y;
133: this .image = image;
134: this .anchor = anchor;
135: }
136:
137: /**
138: * Returns the x-coordinate (in data space) for the annotation.
139: *
140: * @return The x-coordinate.
141: *
142: * @since 1.0.4
143: */
144: public double getX() {
145: return this .x;
146: }
147:
148: /**
149: * Returns the y-coordinate (in data space) for the annotation.
150: *
151: * @return The y-coordinate.
152: *
153: * @since 1.0.4
154: */
155: public double getY() {
156: return this .y;
157: }
158:
159: /**
160: * Returns the image for the annotation.
161: *
162: * @return The image.
163: *
164: * @since 1.0.4
165: */
166: public Image getImage() {
167: return this .image;
168: }
169:
170: /**
171: * Returns the image anchor for the annotation.
172: *
173: * @return The image anchor.
174: *
175: * @since 1.0.4
176: */
177: public RectangleAnchor getImageAnchor() {
178: return this .anchor;
179: }
180:
181: /**
182: * Draws the annotation. This method is called by the drawing code in the
183: * {@link XYPlot} class, you don't normally need to call this method
184: * directly.
185: *
186: * @param g2 the graphics device.
187: * @param plot the plot.
188: * @param dataArea the data area.
189: * @param domainAxis the domain axis.
190: * @param rangeAxis the range axis.
191: * @param rendererIndex the renderer index.
192: * @param info if supplied, this info object will be populated with
193: * entity information.
194: */
195: public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
196: ValueAxis domainAxis, ValueAxis rangeAxis,
197: int rendererIndex, PlotRenderingInfo info) {
198:
199: PlotOrientation orientation = plot.getOrientation();
200: AxisLocation domainAxisLocation = plot.getDomainAxisLocation();
201: AxisLocation rangeAxisLocation = plot.getRangeAxisLocation();
202: RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
203: domainAxisLocation, orientation);
204: RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
205: rangeAxisLocation, orientation);
206: float j2DX = (float) domainAxis.valueToJava2D(this .x, dataArea,
207: domainEdge);
208: float j2DY = (float) rangeAxis.valueToJava2D(this .y, dataArea,
209: rangeEdge);
210: float xx = 0.0f;
211: float yy = 0.0f;
212: if (orientation == PlotOrientation.HORIZONTAL) {
213: xx = j2DY;
214: yy = j2DX;
215: } else if (orientation == PlotOrientation.VERTICAL) {
216: xx = j2DX;
217: yy = j2DY;
218: }
219: int w = this .image.getWidth(null);
220: int h = this .image.getHeight(null);
221:
222: Rectangle2D imageRect = new Rectangle2D.Double(0, 0, w, h);
223: Point2D anchorPoint = (Point2D) RectangleAnchor.coordinates(
224: imageRect, this .anchor);
225: xx = xx - (float) anchorPoint.getX();
226: yy = yy - (float) anchorPoint.getY();
227: g2.drawImage(this .image, (int) xx, (int) yy, null);
228:
229: String toolTip = getToolTipText();
230: String url = getURL();
231: if (toolTip != null || url != null) {
232: addEntity(info, new Rectangle2D.Float(xx, yy, w, h),
233: rendererIndex, toolTip, url);
234: }
235: }
236:
237: /**
238: * Tests this object for equality with an arbitrary object.
239: *
240: * @param obj the object (<code>null</code> permitted).
241: *
242: * @return A boolean.
243: */
244: public boolean equals(Object obj) {
245: if (obj == this ) {
246: return true;
247: }
248: // now try to reject equality...
249: if (!super .equals(obj)) {
250: return false;
251: }
252: if (!(obj instanceof XYImageAnnotation)) {
253: return false;
254: }
255: XYImageAnnotation that = (XYImageAnnotation) obj;
256: if (this .x != that.x) {
257: return false;
258: }
259: if (this .y != that.y) {
260: return false;
261: }
262: if (!ObjectUtilities.equal(this .image, that.image)) {
263: return false;
264: }
265: if (!this .anchor.equals(that.anchor)) {
266: return false;
267: }
268: // seems to be the same...
269: return true;
270: }
271:
272: /**
273: * Returns a hash code for this object.
274: *
275: * @return A hash code.
276: */
277: public int hashCode() {
278: return this .image.hashCode();
279: }
280:
281: /**
282: * Returns a clone of the annotation.
283: *
284: * @return A clone.
285: *
286: * @throws CloneNotSupportedException if the annotation can't be cloned.
287: */
288: public Object clone() throws CloneNotSupportedException {
289: return super .clone();
290: }
291:
292: /**
293: * Provides serialization support.
294: *
295: * @param stream the output stream.
296: *
297: * @throws IOException if there is an I/O error.
298: */
299: private void writeObject(ObjectOutputStream stream)
300: throws IOException {
301: stream.defaultWriteObject();
302: //SerialUtilities.writeImage(this.image, stream);
303: }
304:
305: /**
306: * Provides serialization support.
307: *
308: * @param stream the input stream.
309: *
310: * @throws IOException if there is an I/O error.
311: * @throws ClassNotFoundException if there is a classpath problem.
312: */
313: private void readObject(ObjectInputStream stream)
314: throws IOException, ClassNotFoundException {
315: stream.defaultReadObject();
316: //this.image = SerialUtilities.readImage(stream);
317: }
318:
319: }
|