Source for org.jfree.chart.annotations.XYDrawableAnnotation

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2008, 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:  * XYDrawableAnnotation.java
  29:  * -------------------------
  30:  * (C) Copyright 2003-2008, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes:
  36:  * --------
  37:  * 21-May-2003 : Version 1 (DG);
  38:  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
  39:  * 30-Sep-2004 : Added support for tool tips and URLs (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.chart.annotations;
  44: 
  45: import java.awt.Graphics2D;
  46: import java.awt.geom.Rectangle2D;
  47: import java.io.Serializable;
  48: 
  49: import org.jfree.chart.axis.ValueAxis;
  50: import org.jfree.chart.plot.Plot;
  51: import org.jfree.chart.plot.PlotOrientation;
  52: import org.jfree.chart.plot.PlotRenderingInfo;
  53: import org.jfree.chart.plot.XYPlot;
  54: import org.jfree.ui.Drawable;
  55: import org.jfree.ui.RectangleEdge;
  56: import org.jfree.util.ObjectUtilities;
  57: import org.jfree.util.PublicCloneable;
  58: 
  59: /**
  60:  * A general annotation that can be placed on an {@link XYPlot}.
  61:  */
  62: public class XYDrawableAnnotation extends AbstractXYAnnotation
  63:         implements Cloneable, PublicCloneable, Serializable {
  64: 
  65:     /** For serialization. */
  66:     private static final long serialVersionUID = -6540812859722691020L;
  67: 
  68:     /** The x-coordinate. */
  69:     private double x;
  70: 
  71:     /** The y-coordinate. */
  72:     private double y;
  73: 
  74:     /** The width. */
  75:     private double width;
  76: 
  77:     /** The height. */
  78:     private double height;
  79: 
  80:     /** The drawable object. */
  81:     private Drawable drawable;
  82: 
  83:     /**
  84:      * Creates a new annotation to be displayed within the given area.
  85:      *
  86:      * @param x  the x-coordinate for the area.
  87:      * @param y  the y-coordinate for the area.
  88:      * @param width  the width of the area.
  89:      * @param height  the height of the area.
  90:      * @param drawable  the drawable object (<code>null</code> not permitted).
  91:      */
  92:     public XYDrawableAnnotation(double x, double y, double width, double height,
  93:                                 Drawable drawable) {
  94: 
  95:         if (drawable == null) {
  96:             throw new IllegalArgumentException("Null 'drawable' argument.");
  97:         }
  98:         this.x = x;
  99:         this.y = y;
 100:         this.width = width;
 101:         this.height = height;
 102:         this.drawable = drawable;
 103: 
 104:     }
 105: 
 106:     /**
 107:      * Draws the annotation.
 108:      *
 109:      * @param g2  the graphics device.
 110:      * @param plot  the plot.
 111:      * @param dataArea  the data area.
 112:      * @param domainAxis  the domain axis.
 113:      * @param rangeAxis  the range axis.
 114:      * @param rendererIndex  the renderer index.
 115:      * @param info  if supplied, this info object will be populated with
 116:      *              entity information.
 117:      */
 118:     public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
 119:                      ValueAxis domainAxis, ValueAxis rangeAxis,
 120:                      int rendererIndex,
 121:                      PlotRenderingInfo info) {
 122: 
 123:         PlotOrientation orientation = plot.getOrientation();
 124:         RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
 125:                 plot.getDomainAxisLocation(), orientation);
 126:         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
 127:                 plot.getRangeAxisLocation(), orientation);
 128:         float j2DX = (float) domainAxis.valueToJava2D(this.x, dataArea,
 129:                 domainEdge);
 130:         float j2DY = (float) rangeAxis.valueToJava2D(this.y, dataArea,
 131:                 rangeEdge);
 132:         Rectangle2D area = new Rectangle2D.Double(j2DX - this.width / 2.0,
 133:                 j2DY - this.height / 2.0, this.width, this.height);
 134:         this.drawable.draw(g2, area);
 135:         String toolTip = getToolTipText();
 136:         String url = getURL();
 137:         if (toolTip != null || url != null) {
 138:             addEntity(info, area, rendererIndex, toolTip, url);
 139:         }
 140: 
 141:     }
 142: 
 143:     /**
 144:      * Tests this annotation for equality with an arbitrary object.
 145:      *
 146:      * @param obj  the object to test against.
 147:      *
 148:      * @return <code>true</code> or <code>false</code>.
 149:      */
 150:     public boolean equals(Object obj) {
 151: 
 152:         if (obj == this) { // simple case
 153:             return true;
 154:         }
 155:         // now try to reject equality...
 156:         if (!super.equals(obj)) {
 157:             return false;
 158:         }
 159:         if (!(obj instanceof XYDrawableAnnotation)) {
 160:             return false;
 161:         }
 162:         XYDrawableAnnotation that = (XYDrawableAnnotation) obj;
 163:         if (this.x != that.x) {
 164:             return false;
 165:         }
 166:         if (this.y != that.y) {
 167:             return false;
 168:         }
 169:         if (this.width != that.width) {
 170:             return false;
 171:         }
 172:         if (this.height != that.height) {
 173:             return false;
 174:         }
 175:         if (!ObjectUtilities.equal(this.drawable, that.drawable)) {
 176:             return false;
 177:         }
 178:         // seem to be the same...
 179:         return true;
 180: 
 181:     }
 182: 
 183:     /**
 184:      * Returns a hash code.
 185:      *
 186:      * @return A hash code.
 187:      */
 188:     public int hashCode() {
 189:         int result;
 190:         long temp;
 191:         temp = Double.doubleToLongBits(this.x);
 192:         result = (int) (temp ^ (temp >>> 32));
 193:         temp = Double.doubleToLongBits(this.y);
 194:         result = 29 * result + (int) (temp ^ (temp >>> 32));
 195:         temp = Double.doubleToLongBits(this.width);
 196:         result = 29 * result + (int) (temp ^ (temp >>> 32));
 197:         temp = Double.doubleToLongBits(this.height);
 198:         result = 29 * result + (int) (temp ^ (temp >>> 32));
 199:         return result;
 200:     }
 201: 
 202:     /**
 203:      * Returns a clone of the annotation.
 204:      *
 205:      * @return A clone.
 206:      *
 207:      * @throws CloneNotSupportedException  if the annotation can't be cloned.
 208:      */
 209:     public Object clone() throws CloneNotSupportedException {
 210:         return super.clone();
 211:     }
 212: 
 213: }