KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PieChart2DFrameDemo


1 /**
2  * Chart2D, a java library for drawing two dimensional charts.
3  * Copyright (C) 2001 Jason J. Simas
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * The author of this library may be contacted at:
19  * E-mail: jjsimas@users.sourceforge.net
20  * Street Address: J J Simas, 887 Tico Road, Ojai, CA 93023-3555 USA
21  */

22
23
24 import net.sourceforge.chart2d.*;
25
26 import java.awt.Dimension JavaDoc;
27 import java.awt.Toolkit JavaDoc;
28 import java.awt.event.WindowAdapter JavaDoc;
29 import java.awt.event.WindowEvent JavaDoc;
30 import javax.swing.JApplet JavaDoc;
31 import javax.swing.JFrame JavaDoc;
32
33
34 /**
35  * A Chart2D demo demonstrating the PieChart2D object.
36  * Container Class: JFrame<br>
37  * Program Types: Applet or Application<br>
38  * Sizing: Magnification<br>
39  */

40 public class PieChart2DFrameDemo extends JApplet JavaDoc {
41
42
43   private JFrame JavaDoc frame = null;
44   private static boolean isApplet = true;
45
46
47   /**
48    * For running as an application.
49    * Calls init() and start().
50    * @param args An unused parameter.
51    */

52   public static void main (String JavaDoc[] args) {
53
54     isApplet = false;
55     PieChart2DFrameDemo demo = new PieChart2DFrameDemo();
56     demo.init();
57     demo.start();
58   }
59
60
61   /**
62    * Configure the chart and frame, and open the frame.
63    */

64   public void init() {
65
66     //<-- Begin Chart2D configuration -->
67

68     //Configure object properties
69
Object2DProperties object2DProps = new Object2DProperties();
70     object2DProps.setObjectTitleText ("LOC per Class");
71
72     //Configure chart properties
73
Chart2DProperties chart2DProps = new Chart2DProperties();
74     chart2DProps.setChartDataLabelsPrecision (-3);
75
76     //Configure legend properties
77
LegendProperties legendProps = new LegendProperties();
78     String JavaDoc[] legendLabels =
79       {"PieChart2D", "LBChart2D", "LLChart2D", "GraphChart2D", "Chart2D", "Object2D"};
80     legendProps.setLegendLabelsTexts (legendLabels);
81
82     //Configure dataset
83
int numSets = 6, numCats = 1, numItems = 1;
84     Dataset dataset = new Dataset (numSets, numCats, numItems);
85     dataset.set (0, 0, 0, .419f);
86     dataset.set (1, 0, 0, .284f);
87     dataset.set (2, 0, 0, .284f);
88     dataset.set (3, 0, 0, .714f);
89     dataset.set (4, 0, 0, .193f);
90     dataset.set (5, 0, 0, .241f);
91
92     //Configure graph component colors
93
MultiColorsProperties multiColorsProps = new MultiColorsProperties();
94
95     //Configure pie area
96
PieChart2DProperties pieChart2DProps = new PieChart2DProperties();
97
98     //Configure chart
99
PieChart2D chart2D = new PieChart2D();
100     chart2D.setObject2DProperties (object2DProps);
101     chart2D.setChart2DProperties (chart2DProps);
102     chart2D.setLegendProperties (legendProps);
103     chart2D.setDataset (dataset);
104     chart2D.setMultiColorsProperties (multiColorsProps);
105     chart2D.setPieChart2DProperties (pieChart2DProps);
106
107     //Optional validation: Prints debug messages if invalid only.
108
if (!chart2D.validate (false)) chart2D.validate (true);
109
110     //<-- End Chart2D configuration -->
111

112     //Configure a JFrame GUI
113
frame = new JFrame JavaDoc();
114     frame.getContentPane().add (chart2D); //Add Chart2D to GUI
115
frame.setTitle ("LLChart2DFrameDemo");
116     frame.addWindowListener (
117       new WindowAdapter JavaDoc() {
118         public void windowClosing (WindowEvent JavaDoc e) {
119           destroy();
120     } } );
121
122     frame.pack();
123     Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
124     frame.setLocation (
125       (screenSize.width - frame.getSize().width) / 2,
126       (screenSize.height - frame.getSize().height) / 2);
127
128     if (!isApplet) start();
129   }
130
131
132   /**
133    * Shows the JFrame GUI.
134    */

135   public void start() {
136     frame.show();
137   }
138
139
140   /**
141    * Ends the application or applet.
142    */

143   public void destroy() {
144
145     if (frame != null) frame.dispose();
146     if (!isApplet) System.exit (0);
147   }
148 }
Popular Tags