001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, 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-2008, 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
054package org.jfree.chart.annotations;
055
056import java.awt.BasicStroke;
057import java.awt.Color;
058import java.awt.Graphics2D;
059import java.awt.Paint;
060import java.awt.Shape;
061import java.awt.Stroke;
062import java.awt.geom.AffineTransform;
063import java.awt.geom.Rectangle2D;
064import java.io.IOException;
065import java.io.ObjectInputStream;
066import java.io.ObjectOutputStream;
067import java.io.Serializable;
068
069import org.jfree.chart.HashUtilities;
070import org.jfree.chart.axis.ValueAxis;
071import org.jfree.chart.plot.Plot;
072import org.jfree.chart.plot.PlotOrientation;
073import org.jfree.chart.plot.PlotRenderingInfo;
074import org.jfree.chart.plot.XYPlot;
075import org.jfree.io.SerialUtilities;
076import org.jfree.ui.RectangleEdge;
077import org.jfree.util.ObjectUtilities;
078import org.jfree.util.PaintUtilities;
079import 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 */
085public class XYShapeAnnotation extends AbstractXYAnnotation
086        implements Cloneable, PublicCloneable, Serializable {
087
088    /** For serialization. */
089    private static final long serialVersionUID = -8553218317600684041L;
090
091    /** The shape. */
092    private transient Shape shape;
093
094    /** The stroke used to draw the shape's outline. */
095    private transient Stroke stroke;
096
097    /** The paint used to draw the shape's outline. */
098    private transient Paint outlinePaint;
099
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, <code>null</code>
108     *     not permitted).
109     */
110    public XYShapeAnnotation(Shape shape) {
111        this(shape, new BasicStroke(1.0f), Color.black);
112    }
113
114    /**
115     * Creates a new annotation where the shape is drawn as an outline using
116     * the specified <code>stroke</code> and <code>outlinePaint</code>.
117     *
118     * @param shape  the shape (<code>null</code> not permitted).
119     * @param stroke  the shape stroke (<code>null</code> permitted).
120     * @param outlinePaint  the shape color (<code>null</code> permitted).
121     */
122    public XYShapeAnnotation(Shape shape, Stroke stroke, Paint outlinePaint) {
123        this(shape, stroke, outlinePaint, null);
124    }
125
126    /**
127     * Creates a new annotation.
128     *
129     * @param shape  the shape (<code>null</code> not permitted).
130     * @param stroke  the shape stroke (<code>null</code> permitted).
131     * @param outlinePaint  the shape color (<code>null</code> permitted).
132     * @param fillPaint  the paint used to fill the shape (<code>null</code>
133     *                   permitted.
134     */
135    public XYShapeAnnotation(Shape shape, Stroke stroke, Paint outlinePaint,
136                             Paint fillPaint) {
137        if (shape == null) {
138            throw new IllegalArgumentException("Null 'shape' argument.");
139        }
140        this.shape = shape;
141        this.stroke = stroke;
142        this.outlinePaint = outlinePaint;
143        this.fillPaint = fillPaint;
144    }
145
146    /**
147     * Draws the annotation.  This method is usually called by the
148     * {@link XYPlot} class, you shouldn't need to call it directly.
149     *
150     * @param g2  the graphics device.
151     * @param plot  the plot.
152     * @param dataArea  the data area.
153     * @param domainAxis  the domain axis.
154     * @param rangeAxis  the range axis.
155     * @param rendererIndex  the renderer index.
156     * @param info  the plot rendering info.
157     */
158    public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
159                     ValueAxis domainAxis, ValueAxis rangeAxis,
160                     int rendererIndex,
161                     PlotRenderingInfo info) {
162
163        PlotOrientation orientation = plot.getOrientation();
164        RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
165                plot.getDomainAxisLocation(), orientation);
166        RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
167                plot.getRangeAxisLocation(), orientation);
168
169        // compute transform matrix elements via sample points. Assume no
170        // rotation or shear.
171        Rectangle2D bounds = this.shape.getBounds2D();
172        double x0 = bounds.getMinX();
173        double x1 = bounds.getMaxX();
174        double xx0 = domainAxis.valueToJava2D(x0, dataArea, domainEdge);
175        double xx1 = domainAxis.valueToJava2D(x1, dataArea, domainEdge);
176        double m00 = (xx1 - xx0) / (x1 - x0);
177        double m02 = xx0 - x0 * m00;
178
179        double y0 = bounds.getMaxY();
180        double y1 = bounds.getMinY();
181        double yy0 = rangeAxis.valueToJava2D(y0, dataArea, rangeEdge);
182        double yy1 = rangeAxis.valueToJava2D(y1, dataArea, rangeEdge);
183        double m11 = (yy1 - yy0) / (y1 - y0);
184        double m12 = yy0 - m11 * y0;
185
186        //  create transform & transform shape
187        Shape s = null;
188        if (orientation == PlotOrientation.HORIZONTAL) {
189            AffineTransform t1 = new AffineTransform(0.0f, 1.0f, 1.0f, 0.0f,
190                    0.0f, 0.0f);
191            AffineTransform t2 = new AffineTransform(m11, 0.0f, 0.0f, m00,
192                    m12, m02);
193            s = t1.createTransformedShape(this.shape);
194            s = t2.createTransformedShape(s);
195        }
196        else if (orientation == PlotOrientation.VERTICAL) {
197            AffineTransform t = new AffineTransform(m00, 0, 0, m11, m02, m12);
198            s = t.createTransformedShape(this.shape);
199        }
200
201        if (this.fillPaint != null) {
202            g2.setPaint(this.fillPaint);
203            g2.fill(s);
204        }
205
206        if (this.stroke != null && this.outlinePaint != null) {
207            g2.setPaint(this.outlinePaint);
208            g2.setStroke(this.stroke);
209            g2.draw(s);
210        }
211        addEntity(info, s, rendererIndex, getToolTipText(), getURL());
212
213    }
214
215    /**
216     * Tests this annotation for equality with an arbitrary object.
217     *
218     * @param obj  the object (<code>null</code> permitted).
219     *
220     * @return A boolean.
221     */
222    public boolean equals(Object obj) {
223        if (obj == this) {
224            return true;
225        }
226        // now try to reject equality
227        if (!super.equals(obj)) {
228            return false;
229        }
230        if (!(obj instanceof XYShapeAnnotation)) {
231            return false;
232        }
233        XYShapeAnnotation that = (XYShapeAnnotation) obj;
234        if (!this.shape.equals(that.shape)) {
235            return false;
236        }
237        if (!ObjectUtilities.equal(this.stroke, that.stroke)) {
238            return false;
239        }
240        if (!PaintUtilities.equal(this.outlinePaint, that.outlinePaint)) {
241            return false;
242        }
243        if (!PaintUtilities.equal(this.fillPaint, that.fillPaint)) {
244            return false;
245        }
246        // seem to be the same
247        return true;
248    }
249
250    /**
251     * Returns a hash code for this instance.
252     *
253     * @return A hash code.
254     */
255    public int hashCode() {
256        int result = 193;
257        result = 37 * result + this.shape.hashCode();
258        if (this.stroke != null) {
259            result = 37 * result + this.stroke.hashCode();
260        }
261        result = 37 * result + HashUtilities.hashCodeForPaint(
262                this.outlinePaint);
263        result = 37 * result + HashUtilities.hashCodeForPaint(this.fillPaint);
264        return result;
265    }
266
267    /**
268     * Returns a clone.
269     *
270     * @return A clone.
271     *
272     * @throws CloneNotSupportedException ???.
273     */
274    public Object clone() throws CloneNotSupportedException {
275        return super.clone();
276    }
277
278    /**
279     * Provides serialization support.
280     *
281     * @param stream  the output stream.
282     *
283     * @throws IOException if there is an I/O error.
284     */
285    private void writeObject(ObjectOutputStream stream) throws IOException {
286        stream.defaultWriteObject();
287        SerialUtilities.writeShape(this.shape, stream);
288        SerialUtilities.writeStroke(this.stroke, stream);
289        SerialUtilities.writePaint(this.outlinePaint, stream);
290        SerialUtilities.writePaint(this.fillPaint, stream);
291    }
292
293    /**
294     * Provides serialization support.
295     *
296     * @param stream  the input stream.
297     *
298     * @throws IOException  if there is an I/O error.
299     * @throws ClassNotFoundException  if there is a classpath problem.
300     */
301    private void readObject(ObjectInputStream stream)
302        throws IOException, ClassNotFoundException {
303        stream.defaultReadObject();
304        this.shape = SerialUtilities.readShape(stream);
305        this.stroke = SerialUtilities.readStroke(stream);
306        this.outlinePaint = SerialUtilities.readPaint(stream);
307        this.fillPaint = SerialUtilities.readPaint(stream);
308    }
309
310}