/*
 * Graph.java by Richard J. Davies
 * from `Introductory Java for Scientists and Engineers'
 * chapter: `The Standard Libraries'
 * section: `Simple Graphics'
 *
 * This program puts up a windows and then draws a simple graph in it. It is
 * intended as a sample of Java graphics support, but the actual graphical code
 * is not explained. 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 JSGL library supplied with the book.
 */
// Include the windowing libraries
import java.awt.*;


// Declare a button that quits when pressed
class FinishButton extends Button
{
  // Constructor taking button name.
  public FinishButton(String s)
  {
    super(s);
  }


  // Catch events referring to the button
  public boolean action(Event e, Object what)
  {
    if (e.target == this)
    {
      // Close the window and exit
      Graph.closeWindow();
      System.exit(0);
      return true;
    }
    else
      return false;
  }
}


// Declare a class capable of being drawn to
public class Graph extends Canvas
{
  // Create the window object as a static class variable.
  private static Frame window = new Frame("Drawing");

  // Declare the button to quit the program.
  private static Button finish = new FinishButton("Finish");
  
  // Accessor method for the window variable.
  public static void closeWindow()
  {
    window.dispose();
  }

// ********** 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 **********

  // main method starts up the window  
  public static void main(String[] argv)
  {

// ********** PERFORM SETUP INPUT/OUTPUT HERE **********

    // The window and button were created statically

    // Create a graph object
    Graph g = new Graph();

    // Set the size of the graph
    g.resize(GRAPHSIZE, GRAPHSIZE);

    // Add graph and button to the window
    window.add("Center", g);
    window.add("South", finish);

    // Set the window up and show it
    window.pack();
    window.setResizable(false);
    window.toFront();
    window.show();
  }
}

