Source for org.jfree.chart.annotations.XYShapeAnnotation

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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:  * XYShapeAnnotation.java
  29:  * ----------------------
  30:  * (C) Copyright 2003-2007, by Ondax, Inc. and Contributors.
  31:  *
  32:  * Original Author:  Greg Steckman (for Ondax, Inc.);
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * Changes:
  36:  * --------
  37:  * 15-Aug-2003 : Version 1, adapted from 
  38:  *               org.jfree.chart.annotations.XYLineAnnotation (GS);
  39:  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
  40:  * 20-Apr-2004 : Added new constructor and fixed bug 934258 (DG);
  41:  * 29-Sep-2004 : Added 'fillPaint' to allow for colored shapes, now extends
  42:  *               AbstractXYAnnotation to add tool tip and URL support, and 
  43:  *               implemented equals() and Cloneable (DG);
  44:  * 21-Jan-2005 : Modified constructor for consistency with other 
  45:  *               constructors (DG);
  46:  * 06-Jun-2005 : Fixed equals() method to handle GradientPaint (DG);
  47:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  48:  * 24-Oct-2006 : Calculate AffineTransform on shape's bounding rectangle 
  49:  *               rather than sample points (0, 0) and (1, 1) (DG);
  50:  * 06-Mar-2007 : Implemented hashCode() (DG);
  51:  * 
  52:  */
  53:  
  54: package org.jfree.chart.annotations;
  55: 
  56: import java.awt.BasicStroke;
  57: import java.awt.Color;
  58: import java.awt.Graphics2D;
  59: import java.awt.Paint;
  60: import java.awt.Shape;
  61: import java.awt.Stroke;
  62: import java.awt.geom.AffineTransform;
  63: import java.awt.geom.Rectangle2D;
  64: import java.io.IOException;
  65: import java.io.ObjectInputStream;
  66: import java.io.ObjectOutputStream;
  67: import java.io.Serializable;
  68: 
  69: import org.jfree.chart.HashUtilities;
  70: import org.jfree.chart.axis.ValueAxis;
  71: import org.jfree.chart.plot.Plot;
  72: import org.jfree.chart.plot.PlotOrientation;
  73: import org.jfree.chart.plot.PlotRenderingInfo;
  74: import org.jfree.chart.plot.XYPlot;
  75: import org.jfree.io.SerialUtilities;
  76: import org.jfree.ui.RectangleEdge;
  77: import org.jfree.util.ObjectUtilities;
  78: import org.jfree.util.PaintUtilities;
  79: import org.jfree.util.PublicCloneable;
  80: 
  81: /**
  82:  * A simple <code>Shape</code> annotation that can be placed on an 
  83:  * {@link XYPlot}.  The shape coordinates are specified in data space.
  84:  */
  85: public class XYShapeAnnotation extends AbstractXYAnnotation
  86:                                implements Cloneable, PublicCloneable, 
  87:                                           Serializable {
  88:     
  89:     /** For serialization. */
  90:     private static final long serialVersionUID = -8553218317600684041L;
  91:     
  92:     /** The shape. */
  93:     private transient Shape shape;
  94: 
  95:     /** The stroke used to draw the shape's outline. */
  96:     private transient Stroke stroke;
  97: 
  98:     /** The paint used to draw the shape's outline. */
  99:     private transient Paint outlinePaint;
 100:     
 101:     /** The paint used to fill the shape. */
 102:     private transient Paint fillPaint;
 103: 
 104:     /**
 105:      * Creates a new annotation (where, by default, the shape is drawn 
 106:      * with a black outline).
 107:      * 
 108:      * @param shape  the shape (coordinates in data space, <code>null</code> 
 109:      *     not permitted).
 110:      */
 111:     public XYShapeAnnotation(Shape shape) {
 112:         this(shape, new BasicStroke(1.0f), Color.black);
 113:     }
 114:     
 115:     /**
 116:      * Creates a new annotation where the shape is drawn as an outline using
 117:      * the specified <code>stroke</code> and <code>outlinePaint</code>.
 118:      *
 119:      * @param shape  the shape (<code>null</code> not permitted).
 120:      * @param stroke  the shape stroke (<code>null</code> permitted).
 121:      * @param outlinePaint  the shape color (<code>null</code> permitted).
 122:      */
 123:     public XYShapeAnnotation(Shape shape, Stroke stroke, Paint outlinePaint) {
 124:         this(shape, stroke, outlinePaint, null);
 125:     }
 126: 
 127:     /**
 128:      * Creates a new annotation.
 129:      *
 130:      * @param shape  the shape (<code>null</code> not permitted).
 131:      * @param stroke  the shape stroke (<code>null</code> permitted).
 132:      * @param outlinePaint  the shape color (<code>null</code> permitted).
 133:      * @param fillPaint  the paint used to fill the shape (<code>null</code> 
 134:      *                   permitted.
 135:      */
 136:     public XYShapeAnnotation(Shape shape, Stroke stroke, Paint outlinePaint, 
 137:                              Paint fillPaint) {
 138:         if (shape == null) {
 139:             throw new IllegalArgumentException("Null 'shape' argument.");   
 140:         }
 141:         this.shape = shape;
 142:         this.stroke = stroke;
 143:         this.outlinePaint = outlinePaint;
 144:         this.fillPaint = fillPaint;
 145:     }
 146: 
 147:     /**
 148:      * Draws the annotation.  This method is usually called by the 
 149:      * {@link XYPlot} class, you shouldn't need to call it directly.
 150:      *
 151:      * @param g2  the graphics device.
 152:      * @param plot  the plot.
 153:      * @param dataArea  the data area.
 154:      * @param domainAxis  the domain axis.
 155:      * @param rangeAxis  the range axis.
 156:      * @param rendererIndex  the renderer index.
 157:      * @param info  the plot rendering info.
 158:      */
 159:     public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
 160:                      ValueAxis domainAxis, ValueAxis rangeAxis, 
 161:                      int rendererIndex,
 162:                      PlotRenderingInfo info) {
 163: 
 164:         PlotOrientation orientation = plot.getOrientation();
 165:         RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
 166:                 plot.getDomainAxisLocation(), orientation);
 167:         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
 168:                 plot.getRangeAxisLocation(), orientation);
 169: 
 170:         // compute transform matrix elements via sample points. Assume no 
 171:         // rotation or shear.
 172:         Rectangle2D bounds = this.shape.getBounds2D();
 173:         double x0 = bounds.getMinX();
 174:         double x1 = bounds.getMaxX();
 175:         double xx0 = domainAxis.valueToJava2D(x0, dataArea, domainEdge);
 176:         double xx1 = domainAxis.valueToJava2D(x1, dataArea, domainEdge);
 177:         double m00 = (xx1 - xx0) / (x1 - x0);
 178:         double m02 = xx0 - x0 * m00;
 179:         
 180:         double y0 = bounds.getMaxY();
 181:         double y1 = bounds.getMinY();
 182:         double yy0 = rangeAxis.valueToJava2D(y0, dataArea, rangeEdge);
 183:         double yy1 = rangeAxis.valueToJava2D(y1, dataArea, rangeEdge);
 184:         double m11 = (yy1 - yy0) / (y1 - y0);
 185:         double m12 = yy0 - m11 * y0;
 186: 
 187:         //  create transform & transform shape
 188:         Shape s = null;
 189:         if (orientation == PlotOrientation.HORIZONTAL) {
 190:             AffineTransform t1 = new AffineTransform(0.0f, 1.0f, 1.0f, 0.0f, 
 191:                     0.0f, 0.0f);
 192:             AffineTransform t2 = new AffineTransform(m11, 0.0f, 0.0f, m00, 
 193:                     m12, m02);
 194:             s = t1.createTransformedShape(this.shape);
 195:             s = t2.createTransformedShape(s);
 196:         }
 197:         else if (orientation == PlotOrientation.VERTICAL) {
 198:             AffineTransform t = new AffineTransform(m00, 0, 0, m11, m02, m12);
 199:             s = t.createTransformedShape(this.shape);
 200:         }
 201: 
 202:         if (this.fillPaint != null) {
 203:             g2.setPaint(this.fillPaint);
 204:             g2.fill(s);
 205:         }
 206:         
 207:         if (this.stroke != null && this.outlinePaint != null) {
 208:             g2.setPaint(this.outlinePaint);
 209:             g2.setStroke(this.stroke);
 210:             g2.draw(s);
 211:         }
 212:         addEntity(info, s, rendererIndex, getToolTipText(), getURL());
 213:         
 214:     }
 215:         
 216:     /**
 217:      * Tests this annotation for equality with an arbitrary object.
 218:      * 
 219:      * @param obj  the object (<code>null</code> permitted).
 220:      * 
 221:      * @return A boolean.
 222:      */
 223:     public boolean equals(Object obj) {
 224:         if (obj == this) {
 225:             return true;
 226:         }
 227:         // now try to reject equality
 228:         if (!super.equals(obj)) {
 229:             return false;
 230:         }
 231:         if (!(obj instanceof XYShapeAnnotation)) {
 232:             return false;
 233:         }
 234:         XYShapeAnnotation that = (XYShapeAnnotation) obj;
 235:         if (!this.shape.equals(that.shape)) {
 236:             return false;
 237:         }
 238:         if (!ObjectUtilities.equal(this.stroke, that.stroke)) {
 239:             return false;
 240:         }
 241:         if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
 242:             return false;
 243:         }
 244:         if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) {
 245:             return false;
 246:         }
 247:         // seem to be the same
 248:         return true;
 249:     }
 250:     
 251:     /**
 252:      * Returns a hash code for this instance.
 253:      * 
 254:      * @return A hash code.
 255:      */
 256:     public int hashCode() {
 257:         int result = 193;
 258:         result = 37 * result + this.shape.hashCode();
 259:         if (this.stroke != null) {
 260:             result = 37 * result + this.stroke.hashCode();
 261:         }
 262:         result = 37 * result + HashUtilities.hashCodeForPaint(
 263:                 this.outlinePaint);
 264:         result = 37 * result + HashUtilities.hashCodeForPaint(this.fillPaint);
 265:         return result;
 266:     }
 267:     
 268:     /**
 269:      * Returns a clone.
 270:      * 
 271:      * @return A clone.
 272:      * 
 273:      * @throws CloneNotSupportedException ???.
 274:      */
 275:     public Object clone() throws CloneNotSupportedException {
 276:         return super.clone();
 277:     }
 278:     
 279:     /**
 280:      * Provides serialization support.
 281:      *
 282:      * @param stream  the output stream.
 283:      *
 284:      * @throws IOException if there is an I/O error.
 285:      */
 286:     private void writeObject(ObjectOutputStream stream) throws IOException {
 287:         stream.defaultWriteObject();
 288:         SerialUtilities.writeShape(this.shape, stream);
 289:         SerialUtilities.writeStroke(this.stroke, stream);
 290:         SerialUtilities.writePaint(this.outlinePaint, stream);
 291:         SerialUtilities.writePaint(this.fillPaint, stream);
 292:     }
 293: 
 294:     /**
 295:      * Provides serialization support.
 296:      *
 297:      * @param stream  the input stream.
 298:      *
 299:      * @throws IOException  if there is an I/O error.
 300:      * @throws ClassNotFoundException  if there is a classpath problem.
 301:      */
 302:     private void readObject(ObjectInputStream stream) 
 303:         throws IOException, ClassNotFoundException {
 304:         stream.defaultReadObject();
 305:         this.shape = SerialUtilities.readShape(stream);
 306:         this.stroke = SerialUtilities.readStroke(stream);
 307:         this.outlinePaint = SerialUtilities.readPaint(stream);
 308:         this.fillPaint = SerialUtilities.readPaint(stream);
 309:     }
 310: 
 311: }