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