KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > MultiShapesXYDemo


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  * MultiShapesXYDemo.java
24  * ----------------------
25  * (C) Copyright 2002, 2003, by Andreas Schneider.
26  *
27  * Original Author: Andreas Schneider;
28  * Contributor(s): -;
29  *
30  * $Id: MultiShapesXYDemo.java,v 1.6 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 13-May-2002 : Version 1, contributed by Andreas Schneider (DG);
35  * 30-May-2002 : Added title to application frame (DG);
36  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.awt.Color JavaDoc;
42 import java.awt.Image JavaDoc;
43 import java.awt.MediaTracker JavaDoc;
44 import java.awt.Paint JavaDoc;
45 import java.awt.Point JavaDoc;
46 import java.awt.Shape JavaDoc;
47 import java.awt.geom.Ellipse2D JavaDoc;
48 import java.awt.geom.Rectangle2D JavaDoc;
49 import java.net.URL JavaDoc;
50
51 import org.jfree.chart.ChartPanel;
52 import org.jfree.chart.JFreeChart;
53 import org.jfree.chart.axis.NumberAxis;
54 import org.jfree.chart.plot.Plot;
55 import org.jfree.chart.plot.XYPlot;
56 import org.jfree.chart.renderer.StandardXYItemRenderer;
57 import org.jfree.data.XYSeries;
58 import org.jfree.data.XYSeriesCollection;
59 import org.jfree.ui.ApplicationFrame;
60 import org.jfree.ui.RefineryUtilities;
61
62 /**
63  * A demonstration showing multiple shapes on an XYPlot.
64  *
65  * @author Andreas Schneider
66  */

67 public class MultiShapesXYDemo extends ApplicationFrame {
68
69     /** The number of point. */
70     private static final int NUMBER_OF_POINTS = 200;
71
72     /** The increment. */
73     private static final double INCREMENT = 0.1;
74
75     /** Color component. */
76     private static final float R1 = 0.0f;
77
78     /** Color component. */
79     private static final float G1 = 1.0f;
80
81     /** Color component. */
82     private static final float B1 = 0.0f;
83
84     /** Color component. */
85     private static final float R2 = 1.0f;
86
87     /** Color component. */
88     private static final float G2 = 0.0f;
89
90     /** Color component. */
91     private static final float B2 = 0.0f;
92
93     /** The series. */
94     private XYSeries series;
95
96     /** The ball image. */
97     private Image JavaDoc ballImage;
98
99     /** The arrow image. */
100     private Image JavaDoc arrowImage;
101
102     /**
103      * A demonstration application showing a series with different shape attributes per item.
104      *
105      * @param title the frame title.
106      */

107     public MultiShapesXYDemo(String JavaDoc title) {
108
109         super(title);
110
111         System.out.println("About to get images...");
112         URL JavaDoc url1 = getClass().getClassLoader().getResource("org/jfree/chart/demo/redball.png");
113         URL JavaDoc url2 = getClass().getClassLoader().getResource("org/jfree/chart/demo/arrow.png");
114         if (url1 != null && url2 != null) {
115             ballImage = new javax.swing.ImageIcon JavaDoc(url1).getImage();
116             arrowImage = new javax.swing.ImageIcon JavaDoc(url2).getImage();
117             MediaTracker JavaDoc tracker = new MediaTracker JavaDoc(this);
118             tracker.addImage(ballImage, 0);
119             tracker.addImage(arrowImage, 1);
120             try {
121                 tracker.waitForID(0);
122                 tracker.waitForID(1);
123             }
124             catch (InterruptedException JavaDoc e) {
125                 e.printStackTrace();
126             }
127             System.out.println("Images loaded");
128         }
129         else {
130             System.err.println("Can't find images");
131         }
132         System.out.println("Images done.");
133         this.series = new XYSeries("Some Data");
134         for (int i = 0; i < NUMBER_OF_POINTS; i++) {
135             double x = INCREMENT * i;
136             double y = Math.sin(x);
137             this.series.add(x, y);
138         }
139         XYSeriesCollection data = new XYSeriesCollection(series);
140         NumberAxis domainAxis = new NumberAxis("x");
141         NumberAxis rangeAxis = new NumberAxis("sin(x)");
142         DemoRenderer renderer = new DemoRenderer();
143         Plot plot = new XYPlot(data, domainAxis, rangeAxis, renderer);
144         JFreeChart chart = new JFreeChart(plot);
145         ChartPanel chartPanel = new ChartPanel(chart);
146         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(600, 380));
147         setContentPane(chartPanel);
148
149     }
150
151     /**
152      * A demo renderer.
153      *
154      * @author AS
155      */

156     class DemoRenderer extends StandardXYItemRenderer {
157
158         /**
159          * Creates a new renderer.
160          */

161         public DemoRenderer() {
162             super(StandardXYItemRenderer.SHAPES | StandardXYItemRenderer.IMAGES, null);
163         }
164
165         /**
166          * Returns true if the shape is filled, and false otherwise.
167          *
168          * @param plot the plot.
169          * @param series the series.
170          * @param item the item.
171          * @param x the x-value.
172          * @param y the y-value.
173          *
174          * @return true if the shape is filled, and false otherwise.
175          */

176         protected boolean isShapeFilled(Plot plot, int series, int item, double x, double y) {
177             return (item % 2 == 0);
178         }
179
180         /**
181          * Returns the shape scale.
182          *
183          * @param plot the plot.
184          * @param series the series.
185          * @param item the item.
186          * @param x the x-value.
187          * @param y the y-value.
188          *
189          * @return the shape scale.
190          */

191         protected double getShapeScale(Plot plot, int series, int item, double x, double y) {
192             float rat = (float) item / (float) NUMBER_OF_POINTS; // 0..1
193
return 6.0f - rat * 4.0;
194         }
195
196         /**
197          * Returns the shape paint.
198          *
199          * @param plot the plot.
200          * @param series the series.
201          * @param item the item.
202          * @param x the x-value.
203          * @param y the y-value.
204          *
205          * @return the shape paint.
206          */

207         protected Paint JavaDoc getPaint(Plot plot, int series, int item, double x, double y) {
208             float rat = (float) item / (float) NUMBER_OF_POINTS; // 0..1
209
float r = (1.0f - rat) * R1 + rat * R2;
210             float g = (1.0f - rat) * G1 + rat * G2;
211             float b = (1.0f - rat) * B1 + rat * B2;
212             Color JavaDoc c = new Color JavaDoc(r, g, b);
213             return c;
214         }
215
216         /**
217          * Returns the shape.
218          *
219          * @param plot the plot.
220          * @param series the series.
221          * @param item the item.
222          * @param x the x-value.
223          * @param y the y-value.
224          * @param scale the scale.
225          *
226          * @return the shape.
227          */

228         protected Shape JavaDoc getShape(Plot plot,
229                                  int series, int item, double x, double y, double scale) {
230             Shape JavaDoc shape;
231             switch (item % 2) {
232               case 0:
233                 shape = new Rectangle2D.Double JavaDoc(x - 0.5 * scale, y - 0.5 * scale, scale, scale);
234                 break;
235               default:
236                 shape = new Ellipse2D.Double JavaDoc(x - 0.5 * scale, y - 0.5 * scale, scale, scale);
237                 break;
238             }
239             return shape;
240         }
241
242         /**
243          * Returns the image.
244          *
245          * @param plot the plot.
246          * @param series the series.
247          * @param item the item.
248          * @param x the x-value.
249          * @param y the y-value.
250          *
251          * @return the image.
252          */

253         protected Image JavaDoc getImage(Plot plot, int series, int item, double x, double y) {
254             if (item % 10 == 0) {
255                 return ballImage;
256             }
257             else if (item == 42) {
258                 return arrowImage;
259             }
260             return null;
261         }
262
263         /**
264          * Returns the image hot spot.
265          *
266          * @param plot the plot.
267          * @param series the series.
268          * @param item the item.
269          * @param x the x-value.
270          * @param y the y-value.
271          * @param image the image.
272          *
273          * @return the image hot spot.
274          */

275         protected Point JavaDoc getImageHotspot(Plot plot,
276                                         int series, int item, double x, double y, Image JavaDoc image) {
277             if (image == arrowImage) {
278                 // the arrow has the hot spot at the bottom left
279
return new Point JavaDoc(0, image.getHeight(null));
280             }
281             return super.getImageHotspot(plot, series, item, x, y, image);
282         }
283     }
284
285     // ****************************************************************************
286
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
287
// * Please note that commercial support and documentation is available from: *
288
// * *
289
// * http://www.object-refinery.com/jfreechart/support.html *
290
// * *
291
// * This is not only a great service for developers, but is a VERY IMPORTANT *
292
// * source of funding for the JFreeChart project. Please support us so that *
293
// * we can continue developing free software. *
294
// ****************************************************************************
295

296     /**
297      * Starting point for the demonstration application.
298      *
299      * @param args ignored.
300      */

301     public static void main(String JavaDoc[] args) {
302
303         MultiShapesXYDemo demo = new MultiShapesXYDemo("XYPlot With Multiple Shapes");
304         demo.pack();
305         RefineryUtilities.centerFrameOnScreen(demo);
306         demo.setVisible(true);
307
308     }
309
310 }
311
Popular Tags