/** * Program: Graph.html * @author: Paul Garrett, garrett@math.umn.edu * @version: Sun Aug 15 19:34:16 CDT 1999 * * Purpose: * */ //import lib.*; import java.awt.*; public class Graph extends java.applet.Applet { TextField expressionField; TextField valueOfXField; Tree tree; Button graphButton; Panel p = new Panel(); boolean treeIsInitialized = false; double xLeft = -3.0; double xRight = 4.0 ; final double inc = .05; AxesAndGrid axg; public void init() { setLayout( null ); p.add(new Label("y =")); expressionField = new TextField(40); p.add(expressionField); graphButton = new Button(" graph "); p.add(graphButton); p.resize(500, 35); p.move(5, 385); add(p); axg = new AxesAndGrid(10,10,500,400,xLeft,xRight); double tot = (xRight - xLeft)*(4.0/5.0); axg.setVerticalRange(-tot/3,2*tot/3); } public void paint(Graphics g) { axg.draw(g); if (treeIsInitialized == true) { g.setColor(Color.red); double y1, y2; for (double x=xLeft; x<=xRight; x+= inc) { try { y1 = tree.eval( x ); y2 = tree.eval( x+inc ); if ( Math.abs(y1) > 100 || Math.abs(y2) > 100 ) { throw new ArithmeticException(); } axg.drawLine(g, x, y1, x+inc, y2); } catch( Exception e ) { // just do nothing about it... } } } } public boolean action(Event e, Object ob) { if (e.target == graphButton) { String str = expressionField.getText(); XVector xv = MathParser.mathTokenize(str); try { tree = MathParser.branchByParens( xv ); MathParser.branchByPrecedence( tree ); MathParser.finalizeLeaves( tree ); treeIsInitialized = true; repaint(); return true; } catch (Exception ex) { // do nothing } } return false; } } /************************************************** * * The End * ***************************************************/