import java.awt.*; import java.applet.*; import java.lang.*; import expr.*; public class EulerGraph extends Applet { Panel function_panel = new Panel(); Panel x_panel = new Panel(); Panel y_panel = new Panel(); private TextField Function_Text = new TextField("0.05 * (65 - y)", 25); private TextField x_min_Text = new TextField("0", 8); private TextField x_max_Text = new TextField("30", 8); private TextField y_min_Text = new TextField("0", 8); private TextField y_max_Text = new TextField("200", 8); String UserInput; Expr function; Variable x = Variable.make("t"); Variable y = Variable.make("y"); Variable Picon = Variable.make("Pi"); Variable PIcon = Variable.make("PI"); Variable picon = Variable.make("pi"); Variable Econ = Variable.make("E"); Variable econ = Variable.make("e"); Graphics sG; Graphics bG; Image bI; Graphics cG; Image cI; Color black = new Color(0, 0, 0); Color white = new Color(255, 255, 255); Color back = new Color(247, 243, 234); Color grid = new Color(128, 128, 255); Color red = new Color(255, 64, 64); Color blue = new Color(0, 0, 255); Color gray = new Color(128, 128, 128); double xmin = 0.0; double xmax = 30.0; double ymin = 0.0; double ymax = 100.0; double px, py, qx, qy, oldx, oldy, fcn; int top = 120; int width = 400; int left = 10; int wholewidth = 421; int sw = 0; public void init() { bI = createImage(wholewidth, wholewidth); bG = bI.getGraphics(); cI = createImage(wholewidth, wholewidth); cG = cI.getGraphics(); drawback(); picon.set_value(Math.PI); Picon.set_value(Math.PI); PIcon.set_value(Math.PI); econ.set_value(Math.E); Econ.set_value(Math.E); setBackground(back); setFont(new Font("Courier", Font.BOLD, 12)); function_panel.setBackground(back); function_panel.setFont(new Font("Courier", Font.BOLD, 12)); function_panel.add(new Label(" f(t, y): ")); function_panel.add(Function_Text); add(function_panel); x_panel.setBackground(back); x_panel.setFont(new Font("Courier", Font.BOLD, 12)); x_panel.add(new Label(" minimum t: ")); x_panel.add(x_min_Text); x_panel.add(new Label(" maximum t: ")); x_panel.add(x_max_Text); add(x_panel); y_panel.setBackground(back); y_panel.setFont(new Font("Courier", Font.BOLD, 12)); y_panel.add(new Label(" minimum y: ")); y_panel.add(y_min_Text); y_panel.add(new Label(" maximum y: ")); y_panel.add(y_max_Text); add(y_panel); } public void paint(Graphics g) { g.drawImage(cI, 0, top, this); sG = getGraphics(); } public void update(Graphics g) { g.drawImage(cI, 0, top, this); } public void drawback() { bG.setColor(gray); bG.fillRect(0, 0, wholewidth, wholewidth); bG.setColor(back); bG.fillRect(left, left, width + 1, width + 1); bG.setColor(grid); for (int i = 0; i < width + 1; i = i + width/10) { bG.drawLine(left, left + i, left + width, left + i); bG.drawLine(left + i, left, left + i, left + width); } cG.drawImage(bI, 0, 0, this); } public int xscale(double x) { int ix; ix = (int) Math.round(left + width * (x - xmin) / (xmax - xmin)); return ix; } public int yscale(double y) { int iy; iy = (int) Math.round(left + width - width * (y - ymin)/(ymax - ymin)); return iy; } public double tangent(double x) { double val; val = py + fcn * (x - px); return val; } public void drawDotandLine() { if (sw == 1) { bG.setColor(red); for (int dx = -1; dx < 2; dx = dx + 1) { for (int dy = -1; dy < 2; dy = dy + 1) { bG.drawLine(xscale(oldx) + dx, yscale(oldy) + dy, xscale(px) + dx, yscale(py) + dy); } } bG.setColor(black); bG.fillOval(xscale(oldx) - 2, yscale(oldy) - 2, 5, 5); } bG.setColor(black); bG.fillOval(xscale(px) - 2, yscale(py) - 2, 5, 5); cG.drawImage(bI, 0, 0, this); cG.setColor(gray); cG.drawLine(xscale(xmin), yscale(tangent(xmin)), xscale(xmax), yscale(tangent(xmax))); oldx = px; oldy = py; sw = 1; } public boolean mouseDown(Event evt, int mx, int my) { UserInput = x_min_Text.getText().trim(); xmin = Double.valueOf(UserInput).doubleValue(); UserInput = x_max_Text.getText().trim(); xmax = Double.valueOf(UserInput).doubleValue(); UserInput = y_min_Text.getText().trim(); ymin = Double.valueOf(UserInput).doubleValue(); UserInput = y_max_Text.getText().trim(); ymax = Double.valueOf(UserInput).doubleValue(); if ((my < top + left) || (my > top + left + width) || (mx < left) || (mx > left + width)) { drawback(); sw = 0; } else { qx = xmin + (xmax - xmin) * (mx - left) / width; qy = ymax - (ymax - ymin) * (my - left - top) / width; if (sw == 1) { if (Math.abs(yscale(qy) - yscale(tangent(qx))) > 5) { return true; } qy = tangent(qx); } px = qx; py = qy; try { function = Parser.parse(Function_Text.getText()); } catch (Syntax_error e) { System.err.println ("Syntax error: " + e); } x.set_value(px); y.set_value(py); fcn = function.value(); drawDotandLine(); } sG.drawImage(cI, 0, top, this); return true; } }