KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > jcharts > demo > swing > SwingDemo


1 /***********************************************************************************************
2  * Copyright 2002 (C) Nathaniel G. Auvil. All Rights Reserved.
3  *
4  * Redistribution and use of this software and associated documentation ("Software"), with or
5  * without modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain copyright statements and notices.
8  * Redistributions must also contain a copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. The name "jCharts" or "Nathaniel G. Auvil" must not be used to endorse or promote
15  * products derived from this Software without prior written permission of Nathaniel G.
16  * Auvil. For written permission, please contact nathaniel_auvil@users.sourceforge.net
17  *
18  * 4. Products derived from this Software may not be called "jCharts" nor may "jCharts" appear
19  * in their names without prior written permission of Nathaniel G. Auvil. jCharts is a
20  * registered trademark of Nathaniel G. Auvil.
21  *
22  * 5. Due credit should be given to the jCharts Project (http://jcharts.sourceforge.net/).
23  *
24  * THIS SOFTWARE IS PROVIDED BY Nathaniel G. Auvil AND CONTRIBUTORS ``AS IS'' AND ANY
25  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * jCharts OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
33  ************************************************************************************************/

34
35
36 package org.krysalis.jcharts.demo.swing;
37
38
39 import org.krysalis.jcharts.chartData.ChartDataException;
40 import org.krysalis.jcharts.chartData.PieChartDataSet;
41 import org.krysalis.jcharts.properties.PropertyException;
42 import org.krysalis.jcharts.properties.PieChart2DProperties;
43 import org.krysalis.jcharts.properties.ChartProperties;
44 import org.krysalis.jcharts.properties.LegendProperties;
45 import org.krysalis.jcharts.nonAxisChart.PieChart2D;
46
47 import javax.swing.*;
48 import java.awt.*;
49 import java.awt.event.WindowEvent JavaDoc;
50
51
52 /*************************************************************************************
53  *
54  * @author Nathaniel Auvil
55  * @version $Id: SwingDemo.java,v 1.2 2003/08/14 00:26:21 nathaniel_auvil Exp $
56  ************************************************************************************/

57 public class SwingDemo extends JFrame
58 {
59     private JPanel panel;
60
61    private PieChart2DProperties pieChart2DProperties;
62     private LegendProperties legendProperties;
63     private ChartProperties chartProperties;
64
65
66     /*******************************************************************************
67      *
68      ********************************************************************************/

69     public SwingDemo() throws ChartDataException, PropertyException
70     {
71         initComponents();
72     }
73
74
75     /*******************************************************************************
76      *
77      ********************************************************************************/

78     private void initComponents()
79     {
80         this.setSize( 500, 500 );
81         this.panel = new JPanel( true );
82         this.panel.setSize( 500, 500 );
83         this.getContentPane().add( this.panel );
84
85         this.pieChart2DProperties = new PieChart2DProperties();
86         this.legendProperties= new LegendProperties();
87         this.chartProperties= new ChartProperties();
88
89         this.setVisible( true );
90
91         addWindowListener( new java.awt.event.WindowAdapter JavaDoc()
92         {
93             public void windowClosing( WindowEvent JavaDoc windowEvent )
94             {
95                 exitForm( windowEvent );
96             }
97         }
98         );
99     }
100
101
102     /*********************************************************************************
103      *
104      * @param graphics
105      ********************************************************************************/

106     public void paint( Graphics graphics )
107     {
108         try {
109             String JavaDoc[] labels = {"BMW", "Audi", "Lexus"};
110             String JavaDoc title = "Cars that Own";
111             Paint[] paints = {Color.blue, Color.gray, Color.red};
112             double[] data = {50d, 30d, 20d};
113         PieChartDataSet pieChartDataSet = new PieChartDataSet( title,
114                                                                                      data,
115                                                                                      labels,
116                                                                                      paints,
117                                                                                      this.pieChart2DProperties );
118
119             Dimension dimension= this.panel.getSize();
120             PieChart2D pieChart2D = new PieChart2D( pieChartDataSet,
121                                                                  this.legendProperties,
122                                                                  this.chartProperties,
123                                                                  (int) dimension.getWidth(),
124                                                                  (int) dimension.getHeight() );
125             pieChart2D.setGraphics2D( (Graphics2D) this.panel.getGraphics() );
126             pieChart2D.render();
127         }
128         catch( ChartDataException chartDataException ) {
129             chartDataException.printStackTrace();
130         }
131         catch( PropertyException propertyException ) {
132             propertyException.printStackTrace();
133         }
134     }
135
136
137     /*******************************************************************************
138      * Exit the Application
139      *
140      * @param windowEvent
141      ******************************************************************************/

142     private void exitForm( WindowEvent JavaDoc windowEvent )
143     {
144         System.exit( 0 );
145     }
146
147
148     /******************************************************************************
149      *
150      ******************************************************************************/

151     public static void main( String JavaDoc args[] ) throws ChartDataException, PropertyException
152     {
153         new SwingDemo();
154     }
155
156
157 }
158
Popular Tags