001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * XYDrawableAnnotation.java
029: * -------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYDrawableAnnotation.java,v 1.6.2.2 2007/03/06 16:12:18 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 21-May-2003 : Version 1 (DG);
040: * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
041: * 30-Sep-2004 : Added support for tool tips and URLs (DG);
042: *
043: */
044:
045: package org.jfree.chart.annotations;
046:
047: import java.awt.Graphics2D;
048: import java.awt.geom.Rectangle2D;
049: import java.io.Serializable;
050:
051: import org.jfree.chart.axis.ValueAxis;
052: import org.jfree.chart.plot.Plot;
053: import org.jfree.chart.plot.PlotOrientation;
054: import org.jfree.chart.plot.PlotRenderingInfo;
055: import org.jfree.chart.plot.XYPlot;
056: import org.jfree.ui.Drawable;
057: import org.jfree.ui.RectangleEdge;
058: import org.jfree.util.ObjectUtilities;
059: import org.jfree.util.PublicCloneable;
060:
061: /**
062: * A general annotation that can be placed on an {@link XYPlot}.
063: */
064: public class XYDrawableAnnotation extends AbstractXYAnnotation
065: implements Cloneable, PublicCloneable, Serializable {
066:
067: /** For serialization. */
068: private static final long serialVersionUID = -6540812859722691020L;
069:
070: /** The x-coordinate. */
071: private double x;
072:
073: /** The y-coordinate. */
074: private double y;
075:
076: /** The width. */
077: private double width;
078:
079: /** The height. */
080: private double height;
081:
082: /** The drawable object. */
083: private Drawable drawable;
084:
085: /**
086: * Creates a new annotation to be displayed within the given area.
087: *
088: * @param x the x-coordinate for the area.
089: * @param y the y-coordinate for the area.
090: * @param width the width of the area.
091: * @param height the height of the area.
092: * @param drawable the drawable object (<code>null</code> not permitted).
093: */
094: public XYDrawableAnnotation(double x, double y, double width,
095: double height, Drawable drawable) {
096:
097: if (drawable == null) {
098: throw new IllegalArgumentException(
099: "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 g2, XYPlot plot, Rectangle2D dataArea,
122: ValueAxis domainAxis, ValueAxis rangeAxis,
123: int rendererIndex, PlotRenderingInfo info) {
124:
125: PlotOrientation orientation = plot.getOrientation();
126: RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot
127: .getDomainAxisLocation(), orientation);
128: RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot
129: .getRangeAxisLocation(), orientation);
130: float j2DX = (float) domainAxis.valueToJava2D(this .x, dataArea,
131: domainEdge);
132: float j2DY = (float) rangeAxis.valueToJava2D(this .y, dataArea,
133: rangeEdge);
134: Rectangle2D area = new Rectangle2D.Double(j2DX - this .width
135: / 2.0, j2DY - this .height / 2.0, this .width,
136: this .height);
137: this .drawable.draw(g2, area);
138: String toolTip = getToolTipText();
139: String url = getURL();
140: if (toolTip != null || url != null) {
141: addEntity(info, area, rendererIndex, toolTip, url);
142: }
143:
144: }
145:
146: /**
147: * Tests this annotation for equality with an arbitrary object.
148: *
149: * @param obj the object to test against.
150: *
151: * @return <code>true</code> or <code>false</code>.
152: */
153: public boolean equals(Object obj) {
154:
155: if (obj == this ) { // simple case
156: return true;
157: }
158: // now try to reject equality...
159: if (!super .equals(obj)) {
160: return false;
161: }
162: if (!(obj instanceof XYDrawableAnnotation)) {
163: return false;
164: }
165: XYDrawableAnnotation that = (XYDrawableAnnotation) obj;
166: if (this .x != that.x) {
167: return false;
168: }
169: if (this .y != that.y) {
170: return false;
171: }
172: if (this .width != that.width) {
173: return false;
174: }
175: if (this .height != that.height) {
176: return false;
177: }
178: if (!ObjectUtilities.equal(this .drawable, that.drawable)) {
179: return false;
180: }
181: // seem to be the same...
182: return true;
183:
184: }
185:
186: /**
187: * Returns a hash code.
188: *
189: * @return A hash code.
190: */
191: public int hashCode() {
192: int result;
193: long temp;
194: temp = Double.doubleToLongBits(this .x);
195: result = (int) (temp ^ (temp >>> 32));
196: temp = Double.doubleToLongBits(this .y);
197: result = 29 * result + (int) (temp ^ (temp >>> 32));
198: temp = Double.doubleToLongBits(this .width);
199: result = 29 * result + (int) (temp ^ (temp >>> 32));
200: temp = Double.doubleToLongBits(this .height);
201: result = 29 * result + (int) (temp ^ (temp >>> 32));
202: return result;
203: }
204:
205: /**
206: * Returns a clone of the annotation.
207: *
208: * @return A clone.
209: *
210: * @throws CloneNotSupportedException if the annotation can't be cloned.
211: */
212: public Object clone() throws CloneNotSupportedException {
213: return super.clone();
214: }
215:
216: }
|