KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jepexamples > FunctionPlotter


1 /*
2 HTML code for applet:
3 <applet code="org.nfunk.jepexamples.FunctionPlotter" width=300 height=320>
4 <param name=initialExpression value="100 sin(x/3) cos(x/70)">
5 </applet>
6 */

7
8 package org.nfunk.jepexamples;
9
10 import java.applet.*;
11 import java.awt.*;
12
13 /**
14  * This applet is a demonstration of the possible applications of the JEP
15  * mathematical expression parser.<p>
16  * The FunctionPlotter class arranges the text field and GraphCanvas classes
17  * and requests a repainting of the graph when the expression in the text
18  * field changes. All plotting (and interaction with the JEP API) is preformed
19  * in GraphCanvas class.
20  */

21 public class FunctionPlotter extends Applet {
22
23     /** The expression field */
24     private java.awt.TextField JavaDoc exprField;
25     
26     /** The canvas for plotting the graph */
27     private GraphCanvas graphCanvas;
28
29     /**
30      * Initializes the applet FunctionPlotter
31      */

32     public void init () {
33         initComponents();
34     }
35
36     /**
37      * Sets the layout of the applet window to BorderLayout, creates all
38      * the components and associates them with event listeners if neccessary.
39      */

40     private void initComponents () {
41         setLayout(new BorderLayout());
42         setBackground (java.awt.Color.white);
43
44         // get the initial expression from the parameters
45
String JavaDoc expr = getParameter("initialExpression");
46         
47         // write the expression into the text field
48
if (expr!=null)
49             exprField = new java.awt.TextField JavaDoc(expr);
50         else
51             exprField = new java.awt.TextField JavaDoc("");
52
53         // adjust various settings for the expression field
54
exprField.setBackground (java.awt.Color.white);
55         exprField.setName ("exprField");
56         exprField.setFont (new java.awt.Font JavaDoc ("Dialog", 0, 11));
57         exprField.setForeground (java.awt.Color.black);
58         exprField.addTextListener (new java.awt.event.TextListener JavaDoc () {
59             public void textValueChanged (java.awt.event.TextEvent JavaDoc evt) {
60                 exprFieldTextValueChanged (evt);
61             }
62         }
63         );
64
65         add ("North", exprField);
66         
67         // create the graph canvas and add it
68
graphCanvas = new GraphCanvas(expr, exprField);
69         add ("Center", graphCanvas);
70     }
71
72
73     /**
74      * Repaints the graphCanvas whenever the text in the expression field
75      * changes.
76      */

77     private void exprFieldTextValueChanged(java.awt.event.TextEvent JavaDoc evt) {
78         String JavaDoc newExpressionString = exprField.getText();
79         graphCanvas.setExpressionString(newExpressionString);
80         graphCanvas.repaint();
81     }
82
83 }
84
Popular Tags