KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ChartTiming1.java
24  * -----------------
25  * (C) Copyright 2001-2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: ChartTiming1.java,v 1.6 2003/11/28 10:09:20 mungady Exp $
31  *
32  * Changes (from 24-Apr-2002)
33  * --------------------------
34  * 24-Apr-2002 : Added standard header (DG);
35  * 29-Oct-2002 : Modified to use javax.swing.Timer (DG);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.awt.Graphics2D JavaDoc;
42 import java.awt.event.ActionEvent JavaDoc;
43 import java.awt.event.ActionListener JavaDoc;
44 import java.awt.geom.Rectangle2D JavaDoc;
45 import java.awt.image.BufferedImage JavaDoc;
46
47 import javax.swing.Timer JavaDoc;
48
49 import org.jfree.chart.ChartFactory;
50 import org.jfree.chart.JFreeChart;
51 import org.jfree.data.DefaultPieDataset;
52
53 /**
54  * Draws a pie chart 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 90-95 charts per second.
57  *
58  * @author David Gilbert
59  */

60 public class ChartTiming1 implements ActionListener JavaDoc {
61
62     /** A flag that indicates when time is up. */
63     private boolean finished;
64
65     /**
66      * Creates a new application.
67      */

68     public ChartTiming1() {
69     }
70
71     /**
72      * Runs the timing.
73      */

74     public void run() {
75         this.finished = false;
76
77         // create a dataset...
78
DefaultPieDataset data = new DefaultPieDataset();
79         data.setValue("One", new Double JavaDoc(10.3));
80         data.setValue("Two", new Double JavaDoc(8.5));
81         data.setValue("Three", new Double JavaDoc(3.9));
82         data.setValue("Four", new Double JavaDoc(3.9));
83         data.setValue("Five", new Double JavaDoc(3.9));
84         data.setValue("Six", new Double JavaDoc(3.9));
85
86         // create a pie chart...
87
boolean withLegend = true;
88         JFreeChart chart = ChartFactory.createPieChart(
89             "Testing",
90             data,
91             withLegend,
92             true,
93             false
94         );
95
96         BufferedImage JavaDoc image = new BufferedImage JavaDoc(400, 300, BufferedImage.TYPE_INT_RGB);
97         Graphics2D JavaDoc g2 = image.createGraphics();
98         Rectangle2D JavaDoc chartArea = new Rectangle2D.Double JavaDoc(0, 0, 400, 300);
99
100         // set up the timer...
101
Timer JavaDoc timer = new Timer JavaDoc(10000, this);
102         timer.setRepeats(false);
103         int count = 0;
104         timer.start();
105         while (!finished) {
106             chart.draw(g2, chartArea, null);
107             System.out.println("Charts drawn..." + count);
108             if (!finished) {
109                 count++;
110             }
111         }
112         System.out.println("DONE");
113
114     }
115
116     /**
117      * Receives notification of action events (in this case, from the Timer).
118      *
119      * @param event the event.
120      */

121     public void actionPerformed(ActionEvent JavaDoc event) {
122         this.finished = true;
123     }
124
125     /**
126      * Starting point for the application.
127      *
128      * @param args ignored.
129      */

130     public static void main(String JavaDoc[] args) {
131
132         ChartTiming1 app = new ChartTiming1();
133         app.run();
134
135     }
136
137 }
138
Popular Tags