Run this applet.

Isn't that a much nicer looking sine wave? There are still a number of things we can add to make this a more complete applet though. The most important would be to add some parameters so that we can define the size of the applet in HTML. The following modification of the init and paint methods looks for xmin, xmax, ymin, and ymax to be specified via parameters. However for robustness if the author of the HTML forgets to specify them we supply some reasonable values.

import java.applet.*; import java.awt.*; public class GraphApplet extends Applet { int x0, xN, y0, yN; double xmin, xmax, ymin, ymax; int Applet, Applet; public void init() { String ParamString; // How big is the applet? Dimension d = size(); Applet = d.; Applet = d.; x0 = 0; xN = Applet-1; y0=0; yN=Applet-1; ParamString = getParameter("xmin); if (ParamString != null) { xmin = Double.valueOf(ParamString).doubleValue(); } else { xmin = -1.0; } ParamString = getParameter("xmax); if (ParamString != null) { xmax = Double.valueOf(ParamString).doubleValue(); } else { xmax = 1.0; } ParamString = getParameter("ymax); if (ParamString != null) { ymax = Double.valueOf(ParamString).doubleValue(); } else { ymax = 1.0; } ParamString = getParameter("ymin); if (ParamString != null) { ymin = Double.valueOf(ParamString).doubleValue(); } else { ymin = -1.0; } } public void paint(Graphics g) { double x1,y1,x2,y2; int i, j1, j2; j1 = yvalue(0); for (i = 0; i < Applet; i++) { j2 = yvalue(i+1); g.drawLine(i, j1 ,i+1, j2); j1 = j2; } } private int yvalue(int ivalue) { // Given the xpoint we're given calculate the Cartesian equivalent double x, y; int jvalue; x = (ivalue * (xmax - xmin)/(Applet - 1)) + xmin; // Take the sine of that x y = Math.sin(x); // Scale y into window coordinates jvalue = (int) ((y - ymin)*(Applet - 1)/(ymax - ymin)); // Switch jvalue from cartesian coordinates to computer graphics coordinates jvalue = Applet - jvalue; return jvalue; } } Now we can adjust the range over which we graph without modifying our code!

So far we've only graphed sine functions. It should be obvious how to modify the code to graph cosines or many other kinds of functions. However what if we want to define the function at runtime?

Bhopal news
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

freelance web designer india ecommerce web developer | Ecommerce web design, software developer india | Web hosting India Windows hosting | India web hosting Windows hosting India | India software developer | web designer india