KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > WindItemRenderer


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ---------------------
23  * WindItemRenderer.java
24  * ---------------------
25  * (C) Copyright 2001-2003, by Achilleus Mantzios and Contributors.
26  *
27  * Original Author: Achilleus Mantzios;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: WindItemRenderer.java,v 1.16 2003/11/03 14:21:28 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 06-Feb-2002 : Version 1, based on code contributed by Achilleus Mantzios (DG);
35  * 28-Mar-2002 : Added a property change listener mechanism so that renderers no longer need to be
36  * immutable. Changed StrictMath-->Math to retain JDK1.2 compatibility (DG);
37  * 09-Apr-2002 : Changed return type of the drawItem method to void, reflecting the change in the
38  * XYItemRenderer method (DG);
39  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  * 21-Jan-2003 : Added new constructor (DG);
41  * 25-Mar-2003 : Implemented Serializable (DG);
42  * 01-May-2003 : Modified drawItem(...) method signature (DG);
43  * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
44  * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
45  *
46  */

47
48 package org.jfree.chart.renderer;
49
50 import java.awt.Color JavaDoc;
51 import java.awt.Font JavaDoc;
52 import java.awt.Graphics2D JavaDoc;
53 import java.awt.Paint JavaDoc;
54 import java.awt.Stroke JavaDoc;
55 import java.awt.geom.Line2D JavaDoc;
56 import java.awt.geom.Rectangle2D JavaDoc;
57 import java.io.Serializable JavaDoc;
58
59 import org.jfree.chart.CrosshairInfo;
60 import org.jfree.chart.axis.ValueAxis;
61 import org.jfree.chart.labels.XYToolTipGenerator;
62 import org.jfree.chart.plot.PlotRenderingInfo;
63 import org.jfree.chart.plot.XYPlot;
64 import org.jfree.chart.urls.XYURLGenerator;
65 import org.jfree.data.WindDataset;
66 import org.jfree.data.XYDataset;
67 import org.jfree.ui.RectangleEdge;
68 import org.jfree.util.PublicCloneable;
69
70 /**
71  * A specialised renderer for displaying wind intensity/direction data.
72  *
73  * @author Achilleus Mantzios
74  */

75 public class WindItemRenderer extends AbstractXYItemRenderer implements XYItemRenderer,
76                                                                         Cloneable JavaDoc,
77                                                                         PublicCloneable,
78                                                                         Serializable JavaDoc {
79
80     /**
81      * Creates a new renderer.
82      */

83     public WindItemRenderer() {
84         super();
85     }
86
87     /**
88      * Creates a new renderer.
89      *
90      * @param toolTipGenerator the tool-tip generator.
91      * @param urlGenerator the URL generator.
92      *
93      * @deprecated Use default constructor and then set generators.
94      */

95     public WindItemRenderer(XYToolTipGenerator toolTipGenerator, XYURLGenerator urlGenerator) {
96
97         super();
98         setToolTipGenerator(toolTipGenerator);
99         setURLGenerator(urlGenerator);
100
101     }
102
103     /**
104      * Draws the visual representation of a single data item.
105      *
106      * @param g2 the graphics device.
107      * @param state the renderer state.
108      * @param plotArea the area within which the plot is being drawn.
109      * @param info optional information collection.
110      * @param plot the plot (can be used to obtain standard color information etc).
111      * @param domainAxis the horizontal axis.
112      * @param rangeAxis the vertical axis.
113      * @param dataset the dataset.
114      * @param series the series index (zero-based).
115      * @param item the item index (zero-based).
116      * @param crosshairs collects information about crosshairs.
117      * @param pass the pass index.
118      */

119     public void drawItem(Graphics2D JavaDoc g2,
120                          XYItemRendererState state,
121                          Rectangle2D JavaDoc plotArea,
122                          PlotRenderingInfo info,
123                          XYPlot plot,
124                          ValueAxis domainAxis,
125                          ValueAxis rangeAxis,
126                          XYDataset dataset,
127                          int series,
128                          int item,
129                          CrosshairInfo crosshairs,
130                          int pass) {
131
132         WindDataset windData = (WindDataset) dataset;
133
134         Paint JavaDoc seriesPaint = getItemPaint(series, item);
135         Stroke JavaDoc seriesStroke = getItemStroke(series, item);
136         g2.setPaint(seriesPaint);
137         g2.setStroke(seriesStroke);
138
139         // get the data point...
140

141         Number JavaDoc x = windData.getXValue(series, item);
142         Number JavaDoc windDir = windData.getWindDirection(series, item);
143         Number JavaDoc wforce = windData.getWindForce(series, item);
144         double windForce = wforce.doubleValue();
145
146         double wdirt = Math.toRadians(windDir.doubleValue() * (-30.0) - 90.0);
147
148         double ax1, ax2, ay1, ay2, rax2, ray2;
149
150         RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
151         RectangleEdge rangeAxisLocation = plot.getRangeAxisEdge();
152         ax1 = domainAxis.translateValueToJava2D(x.doubleValue(), plotArea, domainAxisLocation);
153         ay1 = rangeAxis.translateValueToJava2D(0.0, plotArea, rangeAxisLocation);
154
155         rax2 = x.doubleValue() + (windForce * Math.cos(wdirt) * 8000000.0);
156         ray2 = windForce * Math.sin(wdirt);
157
158         ax2 = domainAxis.translateValueToJava2D(rax2, plotArea, domainAxisLocation);
159         ay2 = rangeAxis.translateValueToJava2D(ray2, plotArea, rangeAxisLocation);
160
161         int diri = windDir.intValue();
162         int forcei = wforce.intValue();
163         String JavaDoc dirforce = diri + "-" + forcei;
164         Line2D JavaDoc line = new Line2D.Double JavaDoc(ax1, ay1, ax2, ay2);
165
166         g2.draw(line);
167         g2.setPaint(Color.blue);
168         g2.setFont(new Font JavaDoc("foo", 1, 9));
169
170         g2.drawString(dirforce, (float) ax1, (float) ay1);
171
172         g2.setPaint(seriesPaint);
173         g2.setStroke(seriesStroke);
174
175         double alx2, aly2, arx2, ary2;
176         double ralx2, raly2, rarx2, rary2;
177
178         double aldir = Math.toRadians(windDir.doubleValue() * (-30.0) - 90.0 - 5.0);
179         ralx2 = wforce.doubleValue() * Math.cos(aldir) * 8000000 * 0.8 + x.doubleValue();
180         raly2 = wforce.doubleValue() * Math.sin(aldir) * 0.8;
181
182         alx2 = domainAxis.translateValueToJava2D(ralx2, plotArea, domainAxisLocation);
183         aly2 = rangeAxis.translateValueToJava2D(raly2, plotArea, rangeAxisLocation);
184
185         line = new Line2D.Double JavaDoc(alx2, aly2, ax2, ay2);
186         g2.draw(line);
187
188         double ardir = Math.toRadians(windDir.doubleValue() * (-30.0) - 90.0 + 5.0);
189         rarx2 = wforce.doubleValue() * Math.cos(ardir) * 8000000 * 0.8 + x.doubleValue();
190         rary2 = wforce.doubleValue() * Math.sin(ardir) * 0.8;
191
192         arx2 = domainAxis.translateValueToJava2D(rarx2, plotArea, domainAxisLocation);
193         ary2 = rangeAxis.translateValueToJava2D(rary2, plotArea, rangeAxisLocation);
194
195         line = new Line2D.Double JavaDoc(arx2, ary2, ax2, ay2);
196         g2.draw(line);
197
198     }
199
200     /**
201      * Returns a clone of the renderer.
202      *
203      * @return A clone.
204      *
205      * @throws CloneNotSupportedException if the renderer cannot be cloned.
206      */

207     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
208         return super.clone();
209     }
210
211 }
212
Popular Tags