The meat of this applet is in the for loop of the paint method.

for (int x = x0; x < xN; x++) { g.drawLine(x,(int) (yN*Math.sin(x)),x+1, (int) (yN*Math.sin(x+1))); } Here we loop across every x pixel of the applet. At each one we calculate the sine of that pixel. We also calculate the sine of the next pixel. This gives us two 2-D points and we draw a line between them. Since the sine of a real number is always between one and negative one, we scale the y value by yN. Finally we cast the y values to ints since sines are fundamentally floating point values but drawLine requires ints.

This applet runs but it's got a lot of problems. All of them can be related to two factors:

  1. Sines are floating point operations. To do a really useful graphing applet we need to be able to use floating point numbers.
  2. The coordinate system of an applet counts from (0,0) at the upper left hand corner to the right and down. The standard Cartesian coordinate system we expect graphs to use counts from (0,0) in the lower left hand corner to the right and up. The origin can be moved in both systems, for instance to the center of the applet, but we still need to transform between the y down and the y up coordinates.
There are a number of ways we can resolve this. The key to all of them, however, is to separate the data from the display. Since we are graphing more or less well behaved mathematical functions, we can assume that our data is completely described by a rectangle in Cartesian space within which we wish to plot a function. The display, on the other hand, is described by a rectangle of discrete points of fixed size and . We need to be able to calculate in the general Cartesian plane and display in the particular applet window.

We'll need a method that will convert a point in the applet window into a point in the Cartesian plane, and one that will convert it back. Here it is:

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() { // How big is the applet? Dimension d = size(); Applet = d.; Applet = d.; x0 = 0; xN = Applet-1; y0=0; yN=Applet-1; xmin = -10.0; xmax = 10.0; ymin = -1.0; ymax = 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; } }

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