001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ---------------------- 028 * XYShapeAnnotation.java 029 * ---------------------- 030 * (C) Copyright 2003-2007, by Ondax, Inc. and Contributors. 031 * 032 * Original Author: Greg Steckman (for Ondax, Inc.); 033 * Contributor(s): David Gilbert (for Object Refinery Limited); 034 * 035 * Changes: 036 * -------- 037 * 15-Aug-2003 : Version 1, adapted from 038 * org.jfree.chart.annotations.XYLineAnnotation (GS); 039 * 21-Jan-2004 : Update for renamed method in ValueAxis (DG); 040 * 20-Apr-2004 : Added new constructor and fixed bug 934258 (DG); 041 * 29-Sep-2004 : Added 'fillPaint' to allow for colored shapes, now extends 042 * AbstractXYAnnotation to add tool tip and URL support, and 043 * implemented equals() and Cloneable (DG); 044 * 21-Jan-2005 : Modified constructor for consistency with other 045 * constructors (DG); 046 * 06-Jun-2005 : Fixed equals() method to handle GradientPaint (DG); 047 * ------------- JFREECHART 1.0.x --------------------------------------------- 048 * 24-Oct-2006 : Calculate AffineTransform on shape's bounding rectangle 049 * rather than sample points (0, 0) and (1, 1) (DG); 050 * 06-Mar-2007 : Implemented hashCode() (DG); 051 * 052 */ 053 054 package org.jfree.chart.annotations; 055 056 import java.awt.BasicStroke; 057 import java.awt.Color; 058 import java.awt.Graphics2D; 059 import java.awt.Paint; 060 import java.awt.Shape; 061 import java.awt.Stroke; 062 import java.awt.geom.AffineTransform; 063 import java.awt.geom.Rectangle2D; 064 import java.io.IOException; 065 import java.io.ObjectInputStream; 066 import java.io.ObjectOutputStream; 067 import java.io.Serializable; 068 069 import org.jfree.chart.HashUtilities; 070 import org.jfree.chart.axis.ValueAxis; 071 import org.jfree.chart.plot.Plot; 072 import org.jfree.chart.plot.PlotOrientation; 073 import org.jfree.chart.plot.PlotRenderingInfo; 074 import org.jfree.chart.plot.XYPlot; 075 import org.jfree.io.SerialUtilities; 076 import org.jfree.ui.RectangleEdge; 077 import org.jfree.util.ObjectUtilities; 078 import org.jfree.util.PaintUtilities; 079 import org.jfree.util.PublicCloneable; 080 081 /** 082 * A simple <code>Shape</code> annotation that can be placed on an 083 * {@link XYPlot}. The shape coordinates are specified in data space. 084 */ 085 public class XYShapeAnnotation extends AbstractXYAnnotation 086 implements Cloneable, PublicCloneable, 087 Serializable { 088 089 /** For serialization. */ 090 private static final long serialVersionUID = -8553218317600684041L; 091 092 /** The shape. */ 093 private transient Shape shape; 094 095 /** The stroke used to draw the shape's outline. */ 096 private transient Stroke stroke; 097 098 /** The paint used to draw the shape's outline. */ 099 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 }