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     * XYImageAnnotation.java
029     * ----------------------
030     * (C) Copyright 2003, 2004, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: XYImageAnnotation.java,v 1.8.2.1 2005/10/25 16:51:15 mungady Exp $
036     *
037     * Changes:
038     * --------
039     * 01-Dec-2003 : Version 1 (DG);
040     * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
041     * 18-May-2004 : Fixed bug with plot orientation (DG);
042     * 29-Sep-2004 : Now extends AbstractXYAnnotation, with modified draw() 
043     *               method signature and updated equals() method (DG);
044     *
045     */
046    
047    package org.jfree.chart.annotations;
048    
049    import java.awt.Graphics2D;
050    import java.awt.Image;
051    import java.awt.geom.Rectangle2D;
052    import java.io.IOException;
053    import java.io.ObjectInputStream;
054    import java.io.ObjectOutputStream;
055    import java.io.Serializable;
056    
057    import org.jfree.chart.axis.AxisLocation;
058    import org.jfree.chart.axis.ValueAxis;
059    import org.jfree.chart.plot.Plot;
060    import org.jfree.chart.plot.PlotOrientation;
061    import org.jfree.chart.plot.PlotRenderingInfo;
062    import org.jfree.chart.plot.XYPlot;
063    import org.jfree.ui.RectangleEdge;
064    import org.jfree.util.ObjectUtilities;
065    import org.jfree.util.PublicCloneable;
066    
067    /**
068     * An annotation that allows an image to be placed at some location on 
069     * an {@link XYPlot}.
070     * 
071     * TODO:  implement serialization properly (image is not serializable).
072     */
073    public class XYImageAnnotation extends AbstractXYAnnotation
074                                   implements Cloneable, PublicCloneable, 
075                                              Serializable {
076    
077        /** For serialization. */
078        private static final long serialVersionUID = -4364694501921559958L;
079        
080        /** The x-coordinate (in data space). */
081        private double x;
082    
083        /** The y-coordinate (in data space). */
084        private double y;
085    
086        /** The image. */
087        private transient Image image;
088    
089        /**
090         * Creates a new annotation to be displayed at the specified (x, y) 
091         * location.
092         *
093         * @param x  the x-coordinate (in data space).
094         * @param y  the y-coordinate (in data space).
095         * @param image  the image (<code>null</code> not permitted).
096         */
097        public XYImageAnnotation(double x, double y, Image image) {
098            if (image == null) {
099                throw new IllegalArgumentException("Null 'image' argument.");      
100            }
101            this.x = x;
102            this.y = y;
103            this.image = image;
104        }
105    
106        /**
107         * Draws the annotation.  This method is called by the drawing code in the 
108         * {@link XYPlot} class, you don't normally need to call this method 
109         * directly.
110         *
111         * @param g2  the graphics device.
112         * @param plot  the plot.
113         * @param dataArea  the data area.
114         * @param domainAxis  the domain axis.
115         * @param rangeAxis  the range axis.
116         * @param rendererIndex  the renderer index.
117         * @param info  if supplied, this info object will be populated with
118         *              entity information.
119         */
120        public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
121                         ValueAxis domainAxis, ValueAxis rangeAxis, 
122                         int rendererIndex,
123                         PlotRenderingInfo info) {
124    
125            PlotOrientation orientation = plot.getOrientation();
126            AxisLocation domainAxisLocation = plot.getDomainAxisLocation();
127            AxisLocation rangeAxisLocation = plot.getRangeAxisLocation();
128            RectangleEdge domainEdge 
129                = Plot.resolveDomainAxisLocation(domainAxisLocation, orientation);
130            RectangleEdge rangeEdge 
131                = Plot.resolveRangeAxisLocation(rangeAxisLocation, orientation);
132            float j2DX 
133                = (float) domainAxis.valueToJava2D(this.x, dataArea, domainEdge);
134            float j2DY 
135                = (float) rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge);
136            float xx = 0.0f;
137            float yy = 0.0f;
138            if (orientation == PlotOrientation.HORIZONTAL) {
139                xx = j2DY;
140                yy = j2DX;
141            }
142            else if (orientation == PlotOrientation.VERTICAL) {
143                xx = j2DX;
144                yy = j2DY;
145            }
146            int w = this.image.getWidth(null);
147            int h = this.image.getHeight(null);
148            xx = xx - w / 2.0f;
149            yy = yy - h / 2.0f;
150            g2.drawImage(this.image, (int) xx, (int) yy, null);
151            
152            String toolTip = getToolTipText();
153            String url = getURL();
154            if (toolTip != null || url != null) {
155                addEntity(
156                    info, new Rectangle2D.Float(xx, yy, w, h), rendererIndex, 
157                    toolTip, url
158                );
159            }
160        }
161    
162        /**
163         * Tests this object for equality with an arbitrary object.
164         * 
165         * @param obj  the object (<code>null</code> permitted).
166         * 
167         * @return A boolean.
168         */
169        public boolean equals(Object obj) {
170            if (obj == this) {
171                return true;
172            }
173            // now try to reject equality...
174            if (!super.equals(obj)) {
175                return false;
176            }
177            if (!(obj instanceof XYImageAnnotation)) {
178                return false;
179            }
180            XYImageAnnotation that = (XYImageAnnotation) obj;
181            if (this.x != that.x) {
182                return false;
183            }
184            if (this.y != that.y) {
185                return false;
186            }
187            if (!ObjectUtilities.equal(this.image, that.image)) {
188                return false;
189            }
190            // seems to be the same...
191            return true;
192        }
193        
194        /**
195         * Returns a hash code for this object.
196         * 
197         * @return A hash code.
198         */
199        public int hashCode() {
200            return this.image.hashCode();
201        }
202        
203        /**
204         * Returns a clone of the annotation.
205         * 
206         * @return A clone.
207         * 
208         * @throws CloneNotSupportedException  if the annotation can't be cloned.
209         */
210        public Object clone() throws CloneNotSupportedException {
211            return super.clone();
212        }
213        
214        /**
215         * Provides serialization support.
216         *
217         * @param stream  the output stream.
218         *
219         * @throws IOException  if there is an I/O error.
220         */
221        private void writeObject(ObjectOutputStream stream) throws IOException {
222            stream.defaultWriteObject();
223            //SerialUtilities.writeImage(this.image, stream);
224        }
225        
226        /**
227         * Provides serialization support.
228         *
229         * @param stream  the input stream.
230         *
231         * @throws IOException  if there is an I/O error.
232         * @throws ClassNotFoundException  if there is a classpath problem.
233         */
234        private void readObject(ObjectInputStream stream) 
235            throws IOException, ClassNotFoundException {
236            stream.defaultReadObject();
237            //this.image = SerialUtilities.readImage(stream);
238        }
239    
240    
241    }