/*
 * WebGraph.java by Richard J. Davies
 * from `Introductory Java for Scientists and Engineers'
 * chapter: `The Standard Libraries'
 * section: `The Internet'
 *
 * This program is a version of Graph.java which has been converted into an
 * applet, and so can be embedded in Web pages. Note that the `paint' method
 * behaves in exactly the same way as before. You can copy this file and alter
 * the `paint' method to perform your own simple graphics. If you want to
 * perform more complicated graphics, you should use the library supplied with
 * the book.
 */
// Include windowing and applet libraries
import java.applet.Applet;
import java.awt.*;

// Declare a class capable of being an applet
public class WebGraph extends Applet
{

// ********** COPY EVERYTHING BEFORE HERE **********


  // Steps is the number of points plotted on the graph
  public static final int STEPS = 100;
  // Graphsize is the size of the window for the graph
  public static final int GRAPHSIZE = 300;


  // f is the function which gets drawn
  public static double f(double x)
  {
    return x*x + 0.2 * Math.sin(8*x);
  }


  // The paint method is where you should write your
  // code to draw the graph.
  public void paint(Graphics g)
  {
    // Add a title and status message on the command line.
    g.drawString("A graph", 40, 40);
    System.out.println("Drawing graph...");

    // Step through the points
    for (int i=1; i<=STEPS; i++)
    {
      // Compute the previous point
      double oldX = (i-1) / ((double) STEPS);
      double oldY = f(oldX);

      // And the current point
      double newX = i / ((double) STEPS);
      double newY = f(newX);

      // Convert these values into screen coordinates
      int goX = (int) (GRAPHSIZE*oldX);
      int goY = GRAPHSIZE - (int) (GRAPHSIZE*oldY);
      int gnX = (int) (GRAPHSIZE*newX);
      int gnY = GRAPHSIZE - (int) (GRAPHSIZE*newY);

      // And draw the line segment
      g.drawLine(goX, goY, gnX, gnY);
    }
  }


// ********** COPY EVERYTHING AFTER HERE **********
}

