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: * $Id: XYShapeAnnotation.java,v 1.8.2.4 2007/03/06 16:12:18 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 15-Aug-2003 : Version 1, adapted from
040: * org.jfree.chart.annotations.XYLineAnnotation (GS);
041: * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
042: * 20-Apr-2004 : Added new constructor and fixed bug 934258 (DG);
043: * 29-Sep-2004 : Added 'fillPaint' to allow for colored shapes, now extends
044: * AbstractXYAnnotation to add tool tip and URL support, and
045: * implemented equals() and Cloneable (DG);
046: * 21-Jan-2005 : Modified constructor for consistency with other
047: * constructors (DG);
048: * 06-Jun-2005 : Fixed equals() method to handle GradientPaint (DG);
049: * ------------- JFREECHART 1.0.x ---------------------------------------------
050: * 24-Oct-2006 : Calculate AffineTransform on shape's bounding rectangle
051: * rather than sample points (0, 0) and (1, 1) (DG);
052: * 06-Mar-2007 : Implemented hashCode() (DG);
053: *
054: */
055:
056: package org.jfree.chart.annotations;
057:
058: import java.awt.BasicStroke;
059: import java.awt.Color;
060: import java.awt.Graphics2D;
061: import java.awt.Paint;
062: import java.awt.Shape;
063: import java.awt.Stroke;
064: import java.awt.geom.AffineTransform;
065: import java.awt.geom.Rectangle2D;
066: import java.io.IOException;
067: import java.io.ObjectInputStream;
068: import java.io.ObjectOutputStream;
069: import java.io.Serializable;
070:
071: import org.jfree.chart.HashUtilities;
072: import org.jfree.chart.axis.ValueAxis;
073: import org.jfree.chart.plot.Plot;
074: import org.jfree.chart.plot.PlotOrientation;
075: import org.jfree.chart.plot.PlotRenderingInfo;
076: import org.jfree.chart.plot.XYPlot;
077: import org.jfree.io.SerialUtilities;
078: import org.jfree.ui.RectangleEdge;
079: import org.jfree.util.ObjectUtilities;
080: import org.jfree.util.PaintUtilities;
081: import org.jfree.util.PublicCloneable;
082:
083: /**
084: * A simple <code>Shape</code> annotation that can be placed on an
085: * {@link XYPlot}. The shape coordinates are specified in data space.
086: */
087: public class XYShapeAnnotation extends AbstractXYAnnotation implements
088: Cloneable, PublicCloneable, Serializable {
089:
090: /** For serialization. */
091: private static final long serialVersionUID = -8553218317600684041L;
092:
093: /** The shape. */
094: private transient Shape shape;
095:
096: /** The stroke used to draw the shape's outline. */
097: private transient Stroke stroke;
098:
099: /** The paint used to draw the shape's outline. */
100: private transient Paint outlinePaint;
101:
102: /** The paint used to fill the shape. */
103: private transient Paint fillPaint;
104:
105: /**
106: * Creates a new annotation (where, by default, the shape is drawn
107: * with a black outline).
108: *
109: * @param shape the shape (coordinates in data space, <code>null</code>
110: * not permitted).
111: */
112: public XYShapeAnnotation(Shape shape) {
113: this (shape, new BasicStroke(1.0f), Color.black);
114: }
115:
116: /**
117: * Creates a new annotation where the shape is drawn as an outline using
118: * the specified <code>stroke</code> and <code>outlinePaint</code>.
119: *
120: * @param shape the shape (<code>null</code> not permitted).
121: * @param stroke the shape stroke (<code>null</code> permitted).
122: * @param outlinePaint the shape color (<code>null</code> permitted).
123: */
124: public XYShapeAnnotation(Shape shape, Stroke stroke,
125: 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,
139: Paint outlinePaint, 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, PlotRenderingInfo info) {
164:
165: PlotOrientation orientation = plot.getOrientation();
166: RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot
167: .getDomainAxisLocation(), orientation);
168: RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot
169: .getRangeAxisLocation(), orientation);
170:
171: // compute transform matrix elements via sample points. Assume no
172: // rotation or shear.
173: Rectangle2D bounds = this .shape.getBounds2D();
174: double x0 = bounds.getMinX();
175: double x1 = bounds.getMaxX();
176: double xx0 = domainAxis.valueToJava2D(x0, dataArea, domainEdge);
177: double xx1 = domainAxis.valueToJava2D(x1, dataArea, domainEdge);
178: double m00 = (xx1 - xx0) / (x1 - x0);
179: double m02 = xx0 - x0 * m00;
180:
181: double y0 = bounds.getMaxY();
182: double y1 = bounds.getMinY();
183: double yy0 = rangeAxis.valueToJava2D(y0, dataArea, rangeEdge);
184: double yy1 = rangeAxis.valueToJava2D(y1, dataArea, rangeEdge);
185: double m11 = (yy1 - yy0) / (y1 - y0);
186: double m12 = yy0 - m11 * y0;
187:
188: // create transform & transform shape
189: Shape s = null;
190: if (orientation == PlotOrientation.HORIZONTAL) {
191: AffineTransform t1 = new AffineTransform(0.0f, 1.0f, 1.0f,
192: 0.0f, 0.0f, 0.0f);
193: AffineTransform t2 = new AffineTransform(m11, 0.0f, 0.0f,
194: m00, m12, m02);
195: s = t1.createTransformedShape(this .shape);
196: s = t2.createTransformedShape(s);
197: } else if (orientation == PlotOrientation.VERTICAL) {
198: AffineTransform t = new AffineTransform(m00, 0, 0, m11,
199: m02, m12);
200: s = t.createTransformedShape(this .shape);
201: }
202:
203: if (this .fillPaint != null) {
204: g2.setPaint(this .fillPaint);
205: g2.fill(s);
206: }
207:
208: if (this .stroke != null && this .outlinePaint != null) {
209: g2.setPaint(this .outlinePaint);
210: g2.setStroke(this .stroke);
211: g2.draw(s);
212: }
213: addEntity(info, s, rendererIndex, getToolTipText(), getURL());
214:
215: }
216:
217: /**
218: * Tests this annotation for equality with an arbitrary object.
219: *
220: * @param obj the object (<code>null</code> permitted).
221: *
222: * @return A boolean.
223: */
224: public boolean equals(Object obj) {
225: if (obj == this ) {
226: return true;
227: }
228: // now try to reject equality
229: if (!super .equals(obj)) {
230: return false;
231: }
232: if (!(obj instanceof XYShapeAnnotation)) {
233: return false;
234: }
235: XYShapeAnnotation that = (XYShapeAnnotation) obj;
236: if (!this .shape.equals(that.shape)) {
237: return false;
238: }
239: if (!ObjectUtilities.equal(this .stroke, that.stroke)) {
240: return false;
241: }
242: if (!PaintUtilities.equal(this .outlinePaint, that.outlinePaint)) {
243: return false;
244: }
245: if (!PaintUtilities.equal(this .fillPaint, that.fillPaint)) {
246: return false;
247: }
248: // seem to be the same
249: return true;
250: }
251:
252: /**
253: * Returns a hash code for this instance.
254: *
255: * @return A hash code.
256: */
257: public int hashCode() {
258: int result = 193;
259: result = 37 * result + this .shape.hashCode();
260: if (this .stroke != null) {
261: result = 37 * result + this .stroke.hashCode();
262: }
263: result = 37 * result
264: + HashUtilities.hashCodeForPaint(this .outlinePaint);
265: result = 37 * result
266: + 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)
289: throws IOException {
290: stream.defaultWriteObject();
291: SerialUtilities.writeShape(this .shape, stream);
292: SerialUtilities.writeStroke(this .stroke, stream);
293: SerialUtilities.writePaint(this .outlinePaint, stream);
294: SerialUtilities.writePaint(this .fillPaint, stream);
295: }
296:
297: /**
298: * Provides serialization support.
299: *
300: * @param stream the input stream.
301: *
302: * @throws IOException if there is an I/O error.
303: * @throws ClassNotFoundException if there is a classpath problem.
304: */
305: private void readObject(ObjectInputStream stream)
306: throws IOException, ClassNotFoundException {
307: stream.defaultReadObject();
308: this.shape = SerialUtilities.readShape(stream);
309: this.stroke = SerialUtilities.readStroke(stream);
310: this.outlinePaint = SerialUtilities.readPaint(stream);
311: this.fillPaint = SerialUtilities.readPaint(stream);
312: }
313:
314: }
|