import java.awt.*; import java.awt.image.*; import parserp.parser; public class applet2 extends java.applet.Applet { parser p = null; Button reset = null; Button stop = null; Button step = null; Button run = null; TextField eq = null; TextField x0 = null; TextField xn = null; TextField maxn = null; TextField guess = null; TextField prec = null; Label err = null; plot pl = null; double x[] = null; double y[] = null; Panel p3 = null; app2_thread t = null; double precision = 0; int max_iters = 0; int state = 0; public applet2() { } 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(this)); Panel p = new Panel(); p.setLayout(new GridLayout(5, 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^3 - 11*x^2 - 6*x + 5", 50)); 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)); p2.add(new Label("Display plot from x =")); p2.add(x0 = new TextField("-3", 8)); p2.add(new Label(" to x =")); p2.add(xn = new TextField("13", 8)); p2.add(new Label(" Initial guess (x0):")); p2.add(guess = new TextField("5", 8)); p.add(p2); p2 = new Panel(); p2.setLayout(new FlowLayout(FlowLayout.LEFT)); p2.add(new Label(" Stop when |x(n+1) - xn| <=")); p2.add(prec = new TextField("0.001", 10)); p2.add(new Label(" Max iterations:")); p2.add(maxn = new TextField("100", 5)); p.add(p2); p3 = new Panel(); p3.setLayout(new FlowLayout(FlowLayout.LEFT)); p3.add(reset = new Button("Reset")); p3.add(step = new Button("Step")); p3.add(run = new Button("Run")); p3.add(stop = new Button("Stop")); p3.add(new Label(" Syntax check:")); p3.add(err = new Label(" ")); p.add(p3); add(p); } public void start() { do_reset(); } 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 == reset) { do_reset(); } else if (evt.target == step) { do_step(); } else if (evt.target == stop) { state = 0; do_stop(); } else if (evt.target == run) { do_run(); } return true; } void do_reset() { do_stop(); 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) {} double xguess = 0; try {xguess = Double.valueOf(guess.getText()).doubleValue();} catch (NumberFormatException e) {} try {precision = Double.valueOf(prec.getText()).doubleValue();} catch (NumberFormatException e) {} try {max_iters = Integer.valueOf(maxn.getText()).intValue();} catch (NumberFormatException e) {} int npts = 250; 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; state = 0; y = p.eval(xx0, xxn, npts); if (y != null) pl.reset(x, y, xguess); } public void do_step() { if (!check_stop()) pl.step(); else if (state != 2) { state = 2; pl.repaint(); } } public boolean check_stop() { return pl.niters >= max_iters || Math.abs(pl.xguess - pl.xguess2) <= Math.abs(precision); } public void do_stop() { if (t != null) { //System.out.println("In do_stop()"); Thread t2 = t; //t.stop(); //try { t.join(); } //catch (InterruptedException e) {} t = null; //System.out.println("t = " + t); pl.repaint(); t2.stop(); } } void do_run() { if (!check_stop() && t == null) { t = new app2_thread(this); t.start(); state = 1; } } }