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: * XYLineAnnotation.java
029: * ---------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: XYLineAnnotation.java,v 1.7.2.3 2007/03/06 16:12:18 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 02-Apr-2003 : Version 1 (DG);
040: * 19-Aug-2003 : Added equals method, implemented Cloneable, and applied
041: * serialization fixes (DG);
042: * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
043: * 14-Apr-2004 : Fixed draw() method to handle plot orientation correctly (DG);
044: * 29-Sep-2004 : Added support for tool tips and URLS, now extends
045: * AbstractXYAnnotation (DG);
046: * 04-Oct-2004 : Renamed ShapeUtils --> ShapeUtilities (DG);
047: * 08-Jun-2005 : Fixed equals() method to handle GradientPaint() (DG);
048: *
049: */
050:
051: package org.jfree.chart.annotations;
052:
053: import java.awt.BasicStroke;
054: import java.awt.Color;
055: import java.awt.Graphics2D;
056: import java.awt.Paint;
057: import java.awt.Stroke;
058: import java.awt.geom.Line2D;
059: import java.awt.geom.Rectangle2D;
060: import java.io.IOException;
061: import java.io.ObjectInputStream;
062: import java.io.ObjectOutputStream;
063: import java.io.Serializable;
064:
065: import org.jfree.chart.axis.ValueAxis;
066: import org.jfree.chart.plot.Plot;
067: import org.jfree.chart.plot.PlotOrientation;
068: import org.jfree.chart.plot.PlotRenderingInfo;
069: import org.jfree.chart.plot.XYPlot;
070: import org.jfree.io.SerialUtilities;
071: import org.jfree.ui.RectangleEdge;
072: import org.jfree.util.ObjectUtilities;
073: import org.jfree.util.PaintUtilities;
074: import org.jfree.util.PublicCloneable;
075: import org.jfree.util.ShapeUtilities;
076:
077: /**
078: * A simple line annotation that can be placed on an {@link XYPlot}.
079: */
080: public class XYLineAnnotation extends AbstractXYAnnotation implements
081: Cloneable, PublicCloneable, Serializable {
082:
083: /** For serialization. */
084: private static final long serialVersionUID = -80535465244091334L;
085:
086: /** The x-coordinate. */
087: private double x1;
088:
089: /** The y-coordinate. */
090: private double y1;
091:
092: /** The x-coordinate. */
093: private double x2;
094:
095: /** The y-coordinate. */
096: private double y2;
097:
098: /** The line stroke. */
099: private transient Stroke stroke;
100:
101: /** The line color. */
102: private transient Paint paint;
103:
104: /**
105: * Creates a new annotation that draws a line from (x1, y1) to (x2, y2)
106: * where the coordinates are measured in data space (that is, against the
107: * plot's axes).
108: *
109: * @param x1 the x-coordinate for the start of the line.
110: * @param y1 the y-coordinate for the start of the line.
111: * @param x2 the x-coordinate for the end of the line.
112: * @param y2 the y-coordinate for the end of the line.
113: */
114: public XYLineAnnotation(double x1, double y1, double x2, double y2) {
115: this (x1, y1, x2, y2, new BasicStroke(1.0f), Color.black);
116: }
117:
118: /**
119: * Creates a new annotation that draws a line from (x1, y1) to (x2, y2)
120: * where the coordinates are measured in data space (that is, against the
121: * plot's axes).
122: *
123: * @param x1 the x-coordinate for the start of the line.
124: * @param y1 the y-coordinate for the start of the line.
125: * @param x2 the x-coordinate for the end of the line.
126: * @param y2 the y-coordinate for the end of the line.
127: * @param stroke the line stroke (<code>null</code> not permitted).
128: * @param paint the line color (<code>null</code> not permitted).
129: */
130: public XYLineAnnotation(double x1, double y1, double x2, double y2,
131: Stroke stroke, Paint paint) {
132:
133: if (stroke == null) {
134: throw new IllegalArgumentException(
135: "Null 'stroke' argument.");
136: }
137: if (paint == null) {
138: throw new IllegalArgumentException("Null 'paint' argument.");
139: }
140: this .x1 = x1;
141: this .y1 = y1;
142: this .x2 = x2;
143: this .y2 = y2;
144: this .stroke = stroke;
145: this .paint = paint;
146:
147: }
148:
149: /**
150: * Draws the annotation. This method is called by the {@link XYPlot}
151: * class, you won't normally need to call it yourself.
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 if supplied, this info object will be populated with
160: * entity information.
161: */
162: public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
163: ValueAxis domainAxis, ValueAxis rangeAxis,
164: int rendererIndex, PlotRenderingInfo info) {
165:
166: PlotOrientation orientation = plot.getOrientation();
167: RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot
168: .getDomainAxisLocation(), orientation);
169: RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot
170: .getRangeAxisLocation(), orientation);
171: float j2DX1 = 0.0f;
172: float j2DX2 = 0.0f;
173: float j2DY1 = 0.0f;
174: float j2DY2 = 0.0f;
175: if (orientation == PlotOrientation.VERTICAL) {
176: j2DX1 = (float) domainAxis.valueToJava2D(this .x1, dataArea,
177: domainEdge);
178: j2DY1 = (float) rangeAxis.valueToJava2D(this .y1, dataArea,
179: rangeEdge);
180: j2DX2 = (float) domainAxis.valueToJava2D(this .x2, dataArea,
181: domainEdge);
182: j2DY2 = (float) rangeAxis.valueToJava2D(this .y2, dataArea,
183: rangeEdge);
184: } else if (orientation == PlotOrientation.HORIZONTAL) {
185: j2DY1 = (float) domainAxis.valueToJava2D(this .x1, dataArea,
186: domainEdge);
187: j2DX1 = (float) rangeAxis.valueToJava2D(this .y1, dataArea,
188: rangeEdge);
189: j2DY2 = (float) domainAxis.valueToJava2D(this .x2, dataArea,
190: domainEdge);
191: j2DX2 = (float) rangeAxis.valueToJava2D(this .y2, dataArea,
192: rangeEdge);
193: }
194: g2.setPaint(this .paint);
195: g2.setStroke(this .stroke);
196: Line2D line = new Line2D.Float(j2DX1, j2DY1, j2DX2, j2DY2);
197: g2.draw(line);
198:
199: String toolTip = getToolTipText();
200: String url = getURL();
201: if (toolTip != null || url != null) {
202: addEntity(info,
203: ShapeUtilities.createLineRegion(line, 1.0f),
204: rendererIndex, toolTip, url);
205: }
206: }
207:
208: /**
209: * Tests this object for equality with an arbitrary object.
210: *
211: * @param obj the object to test against (<code>null</code> permitted).
212: *
213: * @return <code>true</code> or <code>false</code>.
214: */
215: public boolean equals(Object obj) {
216: if (obj == this ) {
217: return true;
218: }
219: if (!super .equals(obj)) {
220: return false;
221: }
222: if (!(obj instanceof XYLineAnnotation)) {
223: return false;
224: }
225: XYLineAnnotation that = (XYLineAnnotation) obj;
226: if (this .x1 != that.x1) {
227: return false;
228: }
229: if (this .y1 != that.y1) {
230: return false;
231: }
232: if (this .x2 != that.x2) {
233: return false;
234: }
235: if (this .y2 != that.y2) {
236: return false;
237: }
238: if (!PaintUtilities.equal(this .paint, that.paint)) {
239: return false;
240: }
241: if (!ObjectUtilities.equal(this .stroke, that.stroke)) {
242: return false;
243: }
244: // seems to be the same...
245: return true;
246: }
247:
248: /**
249: * Returns a hash code.
250: *
251: * @return A hash code.
252: */
253: public int hashCode() {
254: int result;
255: long temp;
256: temp = Double.doubleToLongBits(this .x1);
257: result = (int) (temp ^ (temp >>> 32));
258: temp = Double.doubleToLongBits(this .x2);
259: result = 29 * result + (int) (temp ^ (temp >>> 32));
260: temp = Double.doubleToLongBits(this .y1);
261: result = 29 * result + (int) (temp ^ (temp >>> 32));
262: temp = Double.doubleToLongBits(this .y2);
263: result = 29 * result + (int) (temp ^ (temp >>> 32));
264: return result;
265: }
266:
267: /**
268: * Returns a clone of the annotation.
269: *
270: * @return A clone.
271: *
272: * @throws CloneNotSupportedException if the annotation can't be cloned.
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)
286: throws IOException {
287: stream.defaultWriteObject();
288: SerialUtilities.writePaint(this .paint, stream);
289: SerialUtilities.writeStroke(this .stroke, stream);
290: }
291:
292: /**
293: * Provides serialization support.
294: *
295: * @param stream the input stream.
296: *
297: * @throws IOException if there is an I/O error.
298: * @throws ClassNotFoundException if there is a classpath problem.
299: */
300: private void readObject(ObjectInputStream stream)
301: throws IOException, ClassNotFoundException {
302: stream.defaultReadObject();
303: this.paint = SerialUtilities.readPaint(stream);
304: this.stroke = SerialUtilities.readStroke(stream);
305: }
306:
307: }
|