KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > test > GraphFrame


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     GraphFrame.java
21     Created on 31. Mai 2001, 23:58
22  */

23
24 package de.progra.charting.test;
25
26 import java.awt.*;
27 import java.awt.event.*;
28 import javax.swing.*;
29 import javax.swing.filechooser.*;
30 import java.io.*;
31 import java.util.*;
32 import de.progra.charting.swing.*;
33 import de.progra.charting.event.*;
34 import de.progra.charting.model.*;
35 import de.progra.charting.render.*;
36 import de.progra.charting.*;
37 /**
38  *
39  * @author mueller
40  */

41 public class GraphFrame extends javax.swing.JFrame JavaDoc implements ActionListener, ChartDataModelListener {
42
43     ChartPanel panel;
44     EditableChartDataModel data;
45     javax.swing.Timer JavaDoc t = new javax.swing.Timer JavaDoc(1000, this);
46     double time = 3.0;
47     
48     /** Creates new form GraphFrame */
49     public GraphFrame() {
50         double[][] model = {{25.0, 22.0, 23.0},
51                             {13.0, 11.0, 12.0}};
52         
53         double[] columns = {0.0, 1.0, 2.0};
54         String JavaDoc[] columnString = {"1998", "1999", "2000"};
55         String JavaDoc[] rows = {"Int. Temp.", "Ext. Temp."};
56
57         String JavaDoc title = "Viewing Internal & External Temperature";
58
59         data = new EditableChartDataModel(model, columns, rows);
60         //ObjectChartDataModel odata = new ObjectChartDataModel(model, columnString, rows);
61
panel = new ChartPanel(data, title, DefaultChart.LINEAR_X_LINEAR_Y);
62         panel.addChartRenderer(new LineChartRenderer(panel.getCoordSystem(), data), 1);
63         data.addChartDataModelListener(this);
64         panel.addMouseListener(new MouseAdapter() {
65             public void mouseClicked(MouseEvent m) {
66                 if(SwingUtilities.isRightMouseButton(m)) {
67                     jPopupMenu1.setLocation(m.getX(), m.getY());
68                     jPopupMenu1.setVisible(true);
69                 } else
70                     jPopupMenu1.setVisible(false);
71                 
72             }
73         });
74         initComponents();
75         t.start();
76         setSize(640, 480);
77         this.getContentPane().add(panel, BorderLayout.CENTER);
78     }
79
80     public void actionPerformed(ActionEvent evt) {
81         data.insertValue(0, new Double JavaDoc(Math.random() * 10.0 + 20.0), new Double JavaDoc(time));
82         data.insertValue(1, new Double JavaDoc(Math.random() * 7.0 + 10.0), new Double JavaDoc(time));
83         
84         time++;
85     }
86     
87     public void chartDataChanged(ChartDataModelEvent evt) {
88         //panel.invalidate();
89
panel.revalidate();
90         repaint();
91     }
92
93     /** This method is called from within the constructor to
94      * initialize the form.
95      * WARNING: Do NOT modify this code. The content of this method is
96      * always regenerated by the Form Editor.
97      */

98     private void initComponents() {//GEN-BEGIN:initComponents
99
jPopupMenu1 = new javax.swing.JPopupMenu JavaDoc();
100         jMenuItem1 = new javax.swing.JMenuItem JavaDoc();
101
102         jMenuItem1.setText("Export Image file ...");
103         jMenuItem1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
104             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
105                 jMenuItem1ActionPerformed(evt);
106             }
107         });
108
109         jPopupMenu1.add(jMenuItem1);
110
111         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
112             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
113                 exitForm(evt);
114             }
115         });
116
117         pack();
118     }//GEN-END:initComponents
119

120     private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
121
// Add your handling code here:
122
JFileChooser f = new JFileChooser(System.getProperty("user.home"));
123         f.setFileFilter(new ImageFilter(ChartEncoder.getSupportedFormats()));
124         int ret = f.showSaveDialog(this);
125         
126         if(ret != JFileChooser.APPROVE_OPTION)
127             return;
128         
129         File selected = f.getSelectedFile();
130         String JavaDoc filename = selected.getPath();
131         String JavaDoc fileending = filename.substring(filename.lastIndexOf('.')+1);
132         System.out.println("** filename = "+filename+" fileending = "+fileending);
133         boolean equals = false;
134         String JavaDoc[] supported = ChartEncoder.getSupportedFormats();
135         
136         for( int i = 0; i < supported.length; i++) {
137             if(fileending.equalsIgnoreCase(supported[i]))
138                 equals = true;
139         }
140         
141         if( ! equals) {
142             fileending = "png";
143             filename = filename.substring(0, filename.lastIndexOf('.')) + "." +fileending;
144             System.out.println("** filename = "+filename+" fileending = "+fileending);
145             selected = new File(filename);
146         }
147         
148         try {
149             ChartEncoder.createEncodedImage(new FileOutputStream(selected), panel, fileending);
150         } catch(Exception JavaDoc e) {
151             JOptionPane.showMessageDialog(this, e.getMessage(), "Fehler beim Speichern", JOptionPane.ERROR_MESSAGE);
152             System.out.println("** Error creating the nevilletest.png file, showing the Neville Interpolation.");
153             e.printStackTrace();
154             return;
155         }
156     }//GEN-LAST:event_jMenuItem1ActionPerformed
157

158     /** Exit the Application */
159     private void exitForm(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_exitForm
160
System.exit(0);
161     }//GEN-LAST:event_exitForm
162

163     /**
164     * @param args the command line arguments
165     */

166     public static void main(String JavaDoc args[]) {
167         new GraphFrame().show();
168     }
169
170
171     // Variables declaration - do not modify//GEN-BEGIN:variables
172
private javax.swing.JPopupMenu JavaDoc jPopupMenu1;
173     private javax.swing.JMenuItem JavaDoc jMenuItem1;
174     // End of variables declaration//GEN-END:variables
175

176 }
177
178 class ImageFilter extends javax.swing.filechooser.FileFilter JavaDoc {
179
180         protected Set supported = new HashSet();
181         StringBuffer JavaDoc descr = new StringBuffer JavaDoc("Image Files");
182         public ImageFilter(String JavaDoc[] accepted) {
183             descr.append(" (");
184             for(int i = 0; i < accepted.length; i++) {
185                 supported.add(accepted[i]);
186                 descr.append("*."+accepted[i]);
187                 
188                 if(i < accepted.length - 1)
189                     descr.append(", ");
190             }
191             descr.append(" )");
192         }
193         
194         // Accept all directories and all gif, jpg, or tiff files.
195
public boolean accept(File f) {
196
197             if (f.isDirectory()) {
198                 return true;
199             }
200
201             String JavaDoc s = f.getName();
202             int i = s.lastIndexOf('.');
203
204             if (i > 0 && i < s.length() - 1) {
205                 String JavaDoc extension = s.substring(i+1).toLowerCase();
206                 if (supported.contains(extension)) {
207                         return true;
208                 } else {
209                     return false;
210                 }
211             }
212
213             return false;
214         }
215
216         // The description of this filter
217
public String JavaDoc getDescription() {
218             return descr.toString();
219         }
220     }
Popular Tags