KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9
10 /*
11 <applet code="org.nfunk.jepexamples.Fractal" width=300 height=320>
12 <param name=initialExpression value="z*z+c">
13 </applet>
14 */

15 package org.nfunk.jepexamples;
16
17 import java.applet.*;
18 import java.awt.*;
19 import java.awt.event.*;
20
21 public class Fractal extends Applet implements ActionListener {
22
23     private TextField exprField, itField;
24     private Button button, button2;
25     private FractalCanvas complexCanvas;
26
27
28     /** Initializes the applet Fractal */
29     public void init () {
30         initComponents();
31     }
32
33
34     private void initComponents () {
35         GridBagLayout gridbag = new GridBagLayout();
36         GridBagConstraints c = new GridBagConstraints();
37
38         setLayout(gridbag);
39         c.fill = GridBagConstraints.BOTH;
40         
41         // Expression field
42
String JavaDoc expr = getParameter("initialExpression");
43         if (expr!=null)
44             exprField = new TextField(expr);
45         else
46             exprField = new TextField("");
47
48
49         exprField.setBackground (java.awt.Color.white);
50         exprField.setName ("exprField");
51         exprField.setFont (new Font ("Dialog", 0, 11));
52         exprField.setForeground (Color.black);
53         exprField.addTextListener (new java.awt.event.TextListener JavaDoc () {
54             public void textValueChanged (java.awt.event.TextEvent JavaDoc evt) {
55                 exprFieldTextValueChanged (evt);
56             }
57         }
58         );
59
60         c.gridx = 0;
61         c.gridy = 0;
62         c.weightx = 1;
63         gridbag.setConstraints(exprField, c);
64         add(exprField);
65
66         // RENDER BUTTON
67
c.gridx = 1;
68         c.gridy = 0;
69         c.weightx = 0.2;
70         button = new Button("Render");
71         gridbag.setConstraints(button, c);
72         add(button);
73         button.addActionListener(this);
74
75         // Iterations field
76
itField = new TextField("20");
77         itField.addTextListener (new java.awt.event.TextListener JavaDoc () {
78             public void textValueChanged (java.awt.event.TextEvent JavaDoc evt) {
79                 itFieldTextValueChanged(evt);
80             }
81         }
82         );
83
84         c.gridx = 0;
85         c.gridy = 1;
86         c.weightx = 1;
87         gridbag.setConstraints(itField, c);
88         add(itField);
89
90
91         // CANVAS
92
c.gridx = 0;
93         c.gridy = 2;
94         c.gridwidth = 2;
95         c.weighty = 1;
96 // button2 = new Button("test");
97

98         complexCanvas = new FractalCanvas(expr, exprField);
99         gridbag.setConstraints(complexCanvas, c);
100         add(complexCanvas);
101     }
102
103
104
105     private void exprFieldTextValueChanged (java.awt.event.TextEvent JavaDoc evt) {
106         String JavaDoc newExpressionString = exprField.getText();
107         complexCanvas.setExpressionString(newExpressionString);
108         //complexCanvas.repaint();
109
}
110
111     private void itFieldTextValueChanged (java.awt.event.TextEvent JavaDoc evt) {
112         Integer JavaDoc newIterationsValue = new Integer JavaDoc(itField.getText());
113         complexCanvas.setIterations(newIterationsValue.intValue());
114         //complexCanvas.repaint();
115
}
116
117     public void actionPerformed(ActionEvent ae) {
118         String JavaDoc str = ae.getActionCommand();
119         if (str.equals("Render")) {
120             String JavaDoc newExpressionString = exprField.getText();
121             complexCanvas.setExpressionString(newExpressionString);
122             complexCanvas.repaint();
123         }
124     }
125 }
126
Popular Tags