KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ChartTiming4.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: ChartTiming4.java,v 1.10 2003/11/28 10:09:20 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.JFreeChart;
49 import org.jfree.chart.axis.NumberAxis;
50 import org.jfree.chart.plot.FastScatterPlot;
51 import org.jfree.chart.plot.Plot;
52
53 /**
54  * Draws a scatter plot over and over for 10 seconds. Reports on how many redraws were achieved.
55  * <p>
56  * On my PC (SuSE Linux 8.2, JDK 1.4, 256mb RAM, 2.66ghz Pentium) I get 31 charts per second.
57  *
58  * @author David Gilbert
59  */

60 public class ChartTiming4 implements ActionListener JavaDoc {
61
62     /** A flag that indicates when time is up. */
63     private boolean finished;
64
65     /** Storage for the data. */
66     private float[][] data = new float[2][1440];
67
68     /**
69      * Creates a new application.
70      */

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

77     public void run() {
78
79         this.finished = false;
80
81         // create a dataset...
82
populateData();
83
84         // create a fast scatter chart...
85
Plot plot = new FastScatterPlot(this.data, new NumberAxis("X"), new NumberAxis("Y"));
86         JFreeChart chart = new JFreeChart(
87             "Fast Scatter Plot Timing",
88             JFreeChart.DEFAULT_TITLE_FONT,
89             plot,
90             true
91         );
92
93         BufferedImage JavaDoc image = new BufferedImage JavaDoc(400, 300, BufferedImage.TYPE_INT_RGB);
94         Graphics2D JavaDoc g2 = image.createGraphics();
95         Rectangle2D JavaDoc chartArea = new Rectangle2D.Double JavaDoc(0, 0, 400, 300);
96
97         // set up the timer...
98
Timer JavaDoc timer = new Timer JavaDoc(10000, this);
99         timer.setRepeats(false);
100         int count = 0;
101         timer.start();
102         while (!finished) {
103             chart.draw(g2, chartArea, null);
104             System.out.println("Charts drawn..." + count);
105             if (!finished) {
106                 count++;
107             }
108         }
109         System.out.println("DONE");
110
111     }
112
113     /**
114      * Receives notification of action events (in this case, from the Timer).
115      *
116      * @param event the event.
117      */

118     public void actionPerformed(ActionEvent JavaDoc event) {
119         this.finished = true;
120     }
121
122     /**
123      * Populates the data array with random values.
124      */

125     private void populateData() {
126
127         for (int i = 0; i < data[0].length; i++) {
128
129             float x = i;
130             data[0][i] = x;
131             data[1][i] = 100 + (2 * x) + (float) Math.random() * 1440;
132         }
133
134     }
135
136     /**
137      * Starting point for the application.
138      *
139      * @param args ignored.
140      */

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