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