/*-/* Applet1.java: plots f(x) -*/*/ import java.applet.Applet; /*++-add Applet classes-++*/ import java.awt.*; /*++-Abstract Windows Toolkit-++*/ public class Applet1 extends Applet /*++- class definition -++*/ { double mx, my, bx, by; /*++- class variables for all methods -++*/ /*-//paint method. Execution begins here (no main) g: Graphics context (graph object) passed by browser-*/ public void paint(Graphics g) { double x, y; /*++- //world coordinates -++*/ int gOldX, gOldY, gNewX, gNewY; /*++-//window coordinates-++*/ g.drawString("In Applet1.class, f(x) vs x", 40, 40); /*++-//Title-++*/ System.out.println("In Applet1.class, about to draw graph"); world2win(0.0,1.0,1.0,0.0); /*++-//determine mapping params-++*/ /*-//Step through the points-*/ x = 0.; /*++-//first point-++*/ y = f(x); /*++-//f(x) = function to plot-++*/ gOldX = (int)(mx*x+bx); /*++-//convert to window coords-++*/ gOldY = (int)(my*y+by); for (x = 0.01; x <= 1.0; x = x+0.01) { y = f(x); gNewX = (int)(mx*x+bx); /*++-//convert to window coords-++*/ gNewY = (int)(my*y+by); System.out.println(" " +x+" , y= "+y+" "); g.drawLine(gOldX, gOldY, gNewX, gNewY); /*++-//line segment-++*/ gOldX = gNewX; /*++-//new point becomes old-++*/ gOldY = gNewY; } } /*-// function f(x) to plot -*/ public static double f(double x) { return x/2. + x*0.2*Math.sin(33*x); } /*-// method worl2win computes mapping parameters-*/ void world2win(double xl,double yt,double xr,double yb) { /*-//(x,y) = world (double) -> window (int)-*/ /*-//x__win = m_x * x + b_x, y_win = m_y * y + b_y-*/ double maxx=350, maxy=250, rm, lm, tm, bm; lm = 0.1 * maxX; /*++-//margins, all around-++*/ rm = 0.9 * maxX; bm = 0.9 * maxY; tm = 0.1 * maxY; mx = (lm-rm)/(xl-xr); /*++-//mapping params wrt margins-++*/ bx = (xl*rm-xr*lm) / (xl-xr); my = (tm-bm) / (yt-yb); by = (yb*tm-yt*bm) / (yb-yt); } }