001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: GraphFrame.java
021: Created on 31. Mai 2001, 23:58
022: */
023:
024: package de.progra.charting.test;
025:
026: import java.awt.*;
027: import java.awt.event.*;
028: import javax.swing.*;
029: import javax.swing.filechooser.*;
030: import java.io.*;
031: import java.util.*;
032: import de.progra.charting.swing.*;
033: import de.progra.charting.event.*;
034: import de.progra.charting.model.*;
035: import de.progra.charting.render.*;
036: import de.progra.charting.*;
037:
038: /**
039: *
040: * @author mueller
041: */
042: public class GraphFrame extends javax.swing.JFrame implements
043: ActionListener, ChartDataModelListener {
044:
045: ChartPanel panel;
046: EditableChartDataModel data;
047: javax.swing.Timer t = new javax.swing.Timer(1000, this );
048: double time = 3.0;
049:
050: /** Creates new form GraphFrame */
051: public GraphFrame() {
052: double[][] model = { { 25.0, 22.0, 23.0 }, { 13.0, 11.0, 12.0 } };
053:
054: double[] columns = { 0.0, 1.0, 2.0 };
055: String[] columnString = { "1998", "1999", "2000" };
056: String[] rows = { "Int. Temp.", "Ext. Temp." };
057:
058: String title = "Viewing Internal & External Temperature";
059:
060: data = new EditableChartDataModel(model, columns, rows);
061: //ObjectChartDataModel odata = new ObjectChartDataModel(model, columnString, rows);
062: panel = new ChartPanel(data, title,
063: DefaultChart.LINEAR_X_LINEAR_Y);
064: panel.addChartRenderer(new LineChartRenderer(panel
065: .getCoordSystem(), data), 1);
066: data.addChartDataModelListener(this );
067: panel.addMouseListener(new MouseAdapter() {
068: public void mouseClicked(MouseEvent m) {
069: if (SwingUtilities.isRightMouseButton(m)) {
070: jPopupMenu1.setLocation(m.getX(), m.getY());
071: jPopupMenu1.setVisible(true);
072: } else
073: jPopupMenu1.setVisible(false);
074:
075: }
076: });
077: initComponents();
078: t.start();
079: setSize(640, 480);
080: this .getContentPane().add(panel, BorderLayout.CENTER);
081: }
082:
083: public void actionPerformed(ActionEvent evt) {
084: data.insertValue(0, new Double(Math.random() * 10.0 + 20.0),
085: new Double(time));
086: data.insertValue(1, new Double(Math.random() * 7.0 + 10.0),
087: new Double(time));
088:
089: time++;
090: }
091:
092: public void chartDataChanged(ChartDataModelEvent evt) {
093: //panel.invalidate();
094: panel.revalidate();
095: repaint();
096: }
097:
098: /** This method is called from within the constructor to
099: * initialize the form.
100: * WARNING: Do NOT modify this code. The content of this method is
101: * always regenerated by the Form Editor.
102: */
103: private void initComponents() {//GEN-BEGIN:initComponents
104: jPopupMenu1 = new javax.swing.JPopupMenu();
105: jMenuItem1 = new javax.swing.JMenuItem();
106:
107: jMenuItem1.setText("Export Image file ...");
108: jMenuItem1
109: .addActionListener(new java.awt.event.ActionListener() {
110: public void actionPerformed(
111: java.awt.event.ActionEvent evt) {
112: jMenuItem1ActionPerformed(evt);
113: }
114: });
115:
116: jPopupMenu1.add(jMenuItem1);
117:
118: addWindowListener(new java.awt.event.WindowAdapter() {
119: public void windowClosing(java.awt.event.WindowEvent evt) {
120: exitForm(evt);
121: }
122: });
123:
124: pack();
125: }//GEN-END:initComponents
126:
127: private void jMenuItem1ActionPerformed(
128: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
129: // Add your handling code here:
130: JFileChooser f = new JFileChooser(System
131: .getProperty("user.home"));
132: f.setFileFilter(new ImageFilter(ChartEncoder
133: .getSupportedFormats()));
134: int ret = f.showSaveDialog(this );
135:
136: if (ret != JFileChooser.APPROVE_OPTION)
137: return;
138:
139: File selected = f.getSelectedFile();
140: String filename = selected.getPath();
141: String fileending = filename.substring(filename
142: .lastIndexOf('.') + 1);
143: System.out.println("** filename = " + filename
144: + " fileending = " + fileending);
145: boolean equals = false;
146: String[] supported = ChartEncoder.getSupportedFormats();
147:
148: for (int i = 0; i < supported.length; i++) {
149: if (fileending.equalsIgnoreCase(supported[i]))
150: equals = true;
151: }
152:
153: if (!equals) {
154: fileending = "png";
155: filename = filename.substring(0, filename.lastIndexOf('.'))
156: + "." + fileending;
157: System.out.println("** filename = " + filename
158: + " fileending = " + fileending);
159: selected = new File(filename);
160: }
161:
162: try {
163: ChartEncoder.createEncodedImage(new FileOutputStream(
164: selected), panel, fileending);
165: } catch (Exception e) {
166: JOptionPane.showMessageDialog(this , e.getMessage(),
167: "Fehler beim Speichern", JOptionPane.ERROR_MESSAGE);
168: System.out
169: .println("** Error creating the nevilletest.png file, showing the Neville Interpolation.");
170: e.printStackTrace();
171: return;
172: }
173: }//GEN-LAST:event_jMenuItem1ActionPerformed
174:
175: /** Exit the Application */
176: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
177: System.exit(0);
178: }//GEN-LAST:event_exitForm
179:
180: /**
181: * @param args the command line arguments
182: */
183: public static void main(String args[]) {
184: new GraphFrame().show();
185: }
186:
187: // Variables declaration - do not modify//GEN-BEGIN:variables
188: private javax.swing.JPopupMenu jPopupMenu1;
189: private javax.swing.JMenuItem jMenuItem1;
190: // End of variables declaration//GEN-END:variables
191:
192: }
193:
194: class ImageFilter extends javax.swing.filechooser.FileFilter {
195:
196: protected Set supported = new HashSet();
197: StringBuffer descr = new StringBuffer("Image Files");
198:
199: public ImageFilter(String[] accepted) {
200: descr.append(" (");
201: for (int i = 0; i < accepted.length; i++) {
202: supported.add(accepted[i]);
203: descr.append("*." + accepted[i]);
204:
205: if (i < accepted.length - 1)
206: descr.append(", ");
207: }
208: descr.append(" )");
209: }
210:
211: // Accept all directories and all gif, jpg, or tiff files.
212: public boolean accept(File f) {
213:
214: if (f.isDirectory()) {
215: return true;
216: }
217:
218: String s = f.getName();
219: int i = s.lastIndexOf('.');
220:
221: if (i > 0 && i < s.length() - 1) {
222: String extension = s.substring(i + 1).toLowerCase();
223: if (supported.contains(extension)) {
224: return true;
225: } else {
226: return false;
227: }
228: }
229:
230: return false;
231: }
232:
233: // The description of this filter
234: public String getDescription() {
235: return descr.toString();
236: }
237: }
|