KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FunctionPlotApplet.java
3  *
4  * Created on 29. Juni 2002, 02:02
5  */

6
7 package de.progra.charting.test;
8
9
10 import de.progra.charting.*;
11 import de.progra.charting.swing.*;
12 import de.progra.charting.model.*;
13 import de.progra.charting.render.*;
14 import javax.swing.*;
15 import java.awt.*;
16 import java.awt.event.*;
17 /**
18  *
19  * @author smueller
20  */

21 public class FunctionPlotApplet extends javax.swing.JApplet JavaDoc {
22
23     JTextField function = new JTextField("x^2", 20);
24     JTextField xmin = new JTextField("-10", 10);
25     JTextField xmax = new JTextField("10", 10);
26     JButton action = new JButton("Plot");
27     ChartPanel chart;
28     DefaultChartDataModel model;
29     JLabel status = new JLabel(" ");
30     
31     /** Creates a new instance of FunctionPlotApplet */
32     public FunctionPlotApplet() {
33         initComponents();
34     }
35
36     protected void initComponents() {
37         JPanel bag = new JPanel();
38         bag.setLayout(new BorderLayout());
39         
40         JPanel panel = new JPanel();
41         panel.setLayout(new GridLayout(3, 2));
42         
43         panel.add(new JLabel("f(x) = "));
44         panel.add(function);
45         panel.add(new JLabel("Minimum x-value: "));
46         panel.add(xmin);
47         panel.add(new JLabel("Maximum x-value: "));
48         panel.add(xmax);
49         
50         JPanel buttonPanel = new JPanel();
51         buttonPanel.add(action);
52         
53         action.addActionListener(new ActionListener() {
54             public void actionPerformed(ActionEvent e) {
55                 actionPressed(e);
56             }
57         });
58   
59         bag.add(panel, BorderLayout.NORTH);
60         bag.add(buttonPanel, BorderLayout.WEST);
61         
62         this.getContentPane().add(bag, BorderLayout.NORTH);
63         
64         model = FunctionPlotter.createChartDataModelInstance(-10.0, 10.0, 2000, "x^2");
65         chart = new ChartPanel(model, "Plotting x^2", DefaultChart.LINEAR_X_LINEAR_Y);
66         chart.getCoordSystem().setPaintLabels(false);
67         chart.setLegend(null);
68         chart.addChartRenderer(new LineChartRenderer(chart.getCoordSystem(), model), 1);
69         this.getContentPane().add(chart, BorderLayout.CENTER);
70         
71         this.getContentPane().add(status, BorderLayout.SOUTH);
72     }
73     
74     public void actionPressed(ActionEvent evt) {
75         xmin.setForeground(Color.black);
76         xmax.setForeground(Color.black);
77         function.setForeground(Color.black);
78         status.setText(" ");
79         
80         double x_min = 0.0;
81         double x_max = 1.0;
82         try {
83             x_min = Double.parseDouble(xmin.getText());
84         } catch(Exception JavaDoc e) {
85             xmin.setForeground(Color.red);
86             status.setText("The minimum x-value was no valid number.");
87             return;
88         }
89         
90         try {
91             x_max = Double.parseDouble(xmax.getText());
92         } catch(Exception JavaDoc e) {
93             xmax.setForeground(Color.red);
94             status.setText("The maximum x-value was no valid number.");
95             return;
96         }
97         
98         try {
99             model = FunctionPlotter.createChartDataModelInstance(x_min, x_max, 2000, function.getText());
100         } catch(Exception JavaDoc e) {
101             function.setForeground(Color.red);
102             status.setText("The function couldn't be parsed.");
103             return;
104         }
105         
106         chart.setTitle(new Title("Plotting "+function.getText()));
107         chart.setLegend(null);
108         chart.setChartDataModel(model);
109         chart.setCoordSystem(new CoordSystem(model));
110         chart.getCoordSystem().setPaintLabels(false);
111         chart.addChartRenderer(new LineChartRenderer(chart.getCoordSystem(), model), 1);
112         chart.revalidate();
113         repaint();
114     }
115 }
116
Popular Tags