KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ChartTiming3.java
24  * -----------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: ChartTiming3.java,v 1.8 2003/07/24 10:52:29 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 29-Oct-2002 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Graphics2D JavaDoc;
41 import java.awt.event.ActionEvent JavaDoc;
42 import java.awt.event.ActionListener JavaDoc;
43 import java.awt.geom.Rectangle2D JavaDoc;
44 import java.awt.image.BufferedImage JavaDoc;
45
46 import javax.swing.Timer JavaDoc;
47
48 import org.jfree.chart.ChartFactory;
49 import org.jfree.chart.JFreeChart;
50 import org.jfree.chart.plot.PlotOrientation;
51 import org.jfree.chart.plot.XYPlot;
52 import org.jfree.chart.renderer.XYDotRenderer;
53 import org.jfree.data.XYDataset;
54 import org.jfree.data.XYSeries;
55 import org.jfree.data.XYSeriesCollection;
56
57 /**
58  * Draws a scatter plot over and over for 10 seconds. Reports on how many redraws were achieved.
59  * <p>
60  * On my PC (SuSE Linux 8.2, JDK 1.4, 256mb RAM, 2.66ghz Pentium) I get 13-14 charts per second.
61  *
62  * @author David Gilbert
63  */

64 public class ChartTiming3 implements ActionListener JavaDoc {
65
66     /** A flag that indicates when time is up. */
67     private boolean finished;
68
69     /**
70      * Creates a new application.
71      */

72     public ChartTiming3() {
73     }
74
75     /**
76      * Runs the test.
77      */

78     public void run() {
79
80         this.finished = false;
81
82         // create a dataset...
83
XYSeries series = new XYSeries("Random Data", true);
84         for (int i = 0; i < 1440; i++) {
85             double x = Math.random();
86             double y = Math.random();
87             series.add(x, y);
88         }
89         XYDataset data = new XYSeriesCollection(series);
90
91         // create a scatter chart...
92
boolean withLegend = true;
93         JFreeChart chart = ChartFactory.createScatterPlot(
94             "Scatter plot timing", "X", "Y",
95             data,
96             PlotOrientation.VERTICAL,
97             withLegend,
98             false,
99             false
100         );
101
102         XYPlot plot = chart.getXYPlot();
103         plot.setRenderer(new XYDotRenderer());
104
105         BufferedImage JavaDoc image = new BufferedImage JavaDoc(400, 300, BufferedImage.TYPE_INT_RGB);
106         Graphics2D JavaDoc g2 = image.createGraphics();
107         Rectangle2D JavaDoc chartArea = new Rectangle2D.Double JavaDoc(0, 0, 400, 300);
108
109         // set up the timer...
110
Timer JavaDoc timer = new Timer JavaDoc(10000, this);
111         timer.setRepeats(false);
112         int count = 0;
113         timer.start();
114         while (!finished) {
115             chart.draw(g2, chartArea, null);
116             System.out.println("Charts drawn..." + count);
117             if (!finished) {
118                 count++;
119             }
120         }
121         System.out.println("DONE");
122
123     }
124
125     /**
126      * Receives notification of action events (in this case, from the Timer).
127      *
128      * @param event the event.
129      */

130     public void actionPerformed(ActionEvent JavaDoc event) {
131         this.finished = true;
132     }
133
134     /**
135      * Starting point for the application.
136      *
137      * @param args ignored.
138      */

139     public static void main(String JavaDoc[] args) {
140
141         ChartTiming3 app = new ChartTiming3();
142         app.run();
143
144     }
145
146 }
147
Popular Tags