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