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
088                                   implements Cloneable, PublicCloneable, 
089                                              Serializable {
090        
091        /** For serialization. */
092        private static final long serialVersionUID = -8553218317600684041L;
093        
094        /** The shape. */
095        private transient Shape shape;
096    
097        /** The stroke used to draw the shape's outline. */
098        private transient Stroke stroke;
099    
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    }