001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2005, 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-2005, 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.2 2005/10/25 16:51:15 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     * 
050     */
051     
052    package org.jfree.chart.annotations;
053    
054    import java.awt.BasicStroke;
055    import java.awt.Color;
056    import java.awt.Graphics2D;
057    import java.awt.Paint;
058    import java.awt.Shape;
059    import java.awt.Stroke;
060    import java.awt.geom.AffineTransform;
061    import java.awt.geom.Rectangle2D;
062    import java.io.IOException;
063    import java.io.ObjectInputStream;
064    import java.io.ObjectOutputStream;
065    import java.io.Serializable;
066    
067    import org.jfree.chart.axis.ValueAxis;
068    import org.jfree.chart.plot.Plot;
069    import org.jfree.chart.plot.PlotOrientation;
070    import org.jfree.chart.plot.PlotRenderingInfo;
071    import org.jfree.chart.plot.XYPlot;
072    import org.jfree.io.SerialUtilities;
073    import org.jfree.ui.RectangleEdge;
074    import org.jfree.util.ObjectUtilities;
075    import org.jfree.util.PaintUtilities;
076    import org.jfree.util.PublicCloneable;
077    
078    /**
079     * A simple <code>Shape</code> annotation that can be placed on an 
080     * {@link XYPlot}.  The shape coordinates are specified in data space.
081     *
082     * @author Greg Steckman
083     */
084    public class XYShapeAnnotation extends AbstractXYAnnotation
085                                   implements Cloneable, PublicCloneable, 
086                                              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).
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    }