import java.awt.*; import java.awt.image.*; import parserp.parser; public class applet1 extends java.applet.Applet { parser p = null; Button eval = null; TextField eq = null; TextField x0 = null; TextField xn = null; TextField nx = null; Label err = null; plot pl = null; double x[] = null; double y[] = null; int nint = 0; Checkbox shade_trap, shade_simp, shade_left, shade_mid, shade_right; Panel p3 = null; public applet1() { } public void init() { p = new parser("-1 * x + (e / pi) - 1^1"); p.eval(0); p = null; setBackground(Color.white); setLayout(new FlowLayout()); add(pl = new plot()); Panel p = new Panel(); p.setLayout(new GridLayout(4, 0, 0, 0)); Panel p2 = new Panel(); p2.setLayout(new FlowLayout(FlowLayout.LEFT)); p2.add(new Label("Equation: y =")); p2.add(eq = new TextField("x * sin(x)", 40)); p2.add(eval = new Button("Evaluate")); p.add(p2); p2 = new Panel(); p2.setLayout(new FlowLayout(FlowLayout.LEFT)); p2.add(new Label("x0:")); p2.add(x0 = new TextField("0", 15)); p2.add(new Label(" xn:")); p2.add(xn = new TextField("10", 15)); p2.add(new Label(" # intervals:")); p2.add(nx = new TextField("10", 15)); p.add(p2); p3 = new Panel(); p3.setLayout(new FlowLayout(FlowLayout.LEFT)); p3.add(new Label("Syntax check:")); p3.add(err = new Label(" ")); p.add(p3); p2 = new Panel(); p2.setLayout(new FlowLayout(FlowLayout.LEFT)); CheckboxGroup cbg = new CheckboxGroup(); p2.add(new Label("Shading:")); p2.add(shade_trap = new Checkbox("Trapezoid", cbg, true)); p2.add(shade_simp = new Checkbox("Simpson", cbg, false)); p2.add(shade_left = new Checkbox("Left endpoint", cbg, false)); p2.add(shade_mid = new Checkbox("Midpoint", cbg, false)); p2.add(shade_right = new Checkbox("Right endpoint", cbg, false)); p.add(p2); add(p); } public void start() { do_eval(); } public void paint(Graphics g) { //g.drawRect(0, 0, this.size().width - 1, this.size().height - 1); } public boolean action(Event evt, Object arg) { if (evt.target == eval) { do_eval(); } else if (evt.target == shade_trap) pl.set_shade(0); else if (evt.target == shade_simp) pl.set_shade(1); else if (evt.target == shade_left) pl.set_shade(2); else if (evt.target == shade_mid) pl.set_shade(3); else if (evt.target == shade_right) pl.set_shade(4); return true; } void do_eval() { p = new parser(eq.getText()); err.setText(p.last_err()); p3.layout(); double xx0 = 0; try {xx0 = Double.valueOf(x0.getText()).doubleValue();} catch (NumberFormatException e) {} double xxn = 0; try {xxn = Double.valueOf(xn.getText()).doubleValue();} catch (NumberFormatException e) {} int nnx = 0; try {nnx = Integer.valueOf(nx.getText()).intValue();} catch (NumberFormatException e) {} if (nnx < 1) return; if (xx0 == xxn) return; nint = nnx; int ppi = 2; int npts = nint * ppi + 1; while (npts < 200) { ppi *= 2; npts = nint * ppi + 1; } x = new double[npts]; double dx = (xxn - xx0) / (npts - 1); for (int i = 0; i < npts; i ++) if (i == 0) x[i] = xx0; else if (i == npts - 1) x[i] = xxn; else x[i] = xx0 + i * dx; y = p.eval(xx0, xxn, npts); if (y != null) pl.draw(x, y, nint); } }