KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.nfunk.jepexamples;
11
12 import java.awt.*;
13
14 import org.nfunk.jep.JEP;
15
16 /**
17  * This class plots a graph using the JEP API.
18  */

19 public class GraphCanvas extends Canvas {
20
21     /** Scaling of the graph in x and y directions */
22     private int scaleX, scaleY;
23
24     /** Dimensions of the canvas */
25     private Dimension dimensions;
26
27     /** Buffer for the graph */
28     private Image buffer;
29
30     /** Boolean flags */
31     private boolean initializedBuffer, changedFunction, hasError;
32
33     /** Math parser */
34     private JEP myParser;
35
36     /** The expression field where the functions are entered */
37     private java.awt.TextField JavaDoc exprField;
38
39     /**
40      * Constructor
41      */

42     public GraphCanvas(String JavaDoc initialExpression,
43                         java.awt.TextField JavaDoc exprField_in) {
44         scaleX = 1;
45         scaleY = 1;
46         dimensions = getSize();
47         initializedBuffer = false;
48         changedFunction = true;
49         hasError = true;
50         exprField = exprField_in;
51         initParser(initialExpression);
52     }
53
54     /**
55      * Initializes the parser
56      */

57     private void initParser(String JavaDoc initialExpression) {
58         // Init Parser
59
myParser = new JEP();
60         
61         // Allow implicit multiplication
62
myParser.setImplicitMul(true);
63
64         // Load the standard functions
65
myParser.addStandardFunctions();
66
67         // Load the standard constants, and complex variables/functions
68
myParser.addStandardConstants();
69         myParser.addComplex();
70         
71         // Add and initialize x to 0
72
myParser.addVariable("x",0);
73
74         // Set the string to the initial value
75
setExpressionString(initialExpression);
76     }
77
78     /**
79      * Sets a new string to be used as function
80      */

81     public void setExpressionString(String JavaDoc newString) {
82         // Parse the new expression
83
myParser.parseExpression(newString);
84
85         // Find out whether there was an error in the expression
86
hasError = myParser.hasError();
87         if (hasError)
88           exprField.setForeground(Color.red);
89         else
90           exprField.setForeground(Color.black);
91
92         changedFunction = true;
93     }
94
95     /**
96      * @return The value of the function at an x value of the parameter.
97      */

98     private double getYValue(double xValue) {
99         // Save the new value in the symbol table
100
myParser.addVariable("x", xValue);
101
102         return myParser.getValue();
103     }
104
105     /**
106      * Fills the background with white.
107      */

108     private void paintWhite(Graphics g) {
109         g.setColor(Color.white);
110         g.fillRect(0,0,dimensions.width,dimensions.height);
111     }
112
113     /**
114      * Paints the axes for the graph.
115      */

116     private void paintAxes(Graphics g) {
117         g.setColor(new Color(204,204,204));
118         g.drawLine(0,dimensions.height/2,dimensions.width-1,dimensions.height/2);
119         g.drawLine(dimensions.width/2,0,dimensions.width/2,dimensions.height-1);
120     }
121
122     /**
123      * Paints the graph of the function.
124      */

125     private void paintCurve(Graphics g) {
126         boolean firstpoint=true;
127         int lastX=0, lastY=0;
128
129         g.setColor(Color.black);
130
131         for (int xAbsolute = 0; xAbsolute <= (dimensions.width-1); xAbsolute++)
132         {
133             double xRelative = (xAbsolute - dimensions.width/2)/scaleX;
134             double yRelative = getYValue(xRelative);
135             int yAbsolute = (int)(-yRelative*scaleY + dimensions.height/2);
136
137             if (yAbsolute > dimensions.height)
138                 yAbsolute = dimensions.height;
139             if (yAbsolute < -1)
140                 yAbsolute = -1;
141
142             if (firstpoint != true)
143                 g.drawLine(lastX, lastY, xAbsolute, yAbsolute);
144             else
145                 firstpoint = false;
146
147             lastX = xAbsolute;
148             lastY = yAbsolute;
149         }
150     }
151
152     /**
153      * Draws the graph to the Graphics object. If the image buffer has been
154      * initialized, and the function has not changed since the last paint,
155      * the image stored in the buffer is drawn straight to the Graphics
156      * object with drawImage().
157      * <p>
158      * If a image buffer has not yet been initialized (i.e. first time after
159      * being started) the buffer is created with createImage().
160      * <p>
161      * If the function has changed since the last paint, the graph is first
162      * drawn on the buffer image, then that image is drawn on the Graphics
163      * object.
164      */

165     public void paint(Graphics g) {
166         boolean changedDimensions = !dimensions.equals(getSize());
167         
168         // If the buffer has not been initialized, do it now
169
if (!initializedBuffer || changedDimensions)
170         {
171             dimensions = getSize();
172             buffer = createImage(dimensions.width,dimensions.height);
173             initializedBuffer = true;
174         }
175         
176         // Get the Graphics instance of the buffer
177
Graphics buffergc = buffer.getGraphics();
178
179         // Redraw the function on the buffer
180
if (changedFunction || changedDimensions)
181         {
182             paintWhite(buffergc);
183             paintAxes(buffergc);
184             if (!hasError) paintCurve(buffergc);
185             changedFunction = false;
186         }
187
188         // Copy the buffer to g
189
g.drawImage(buffer, 0, 0, null);
190     }
191 }
192
Popular Tags