
/*
   This applet can use the left endpoint rule, the right endpoint
   rule, the midpoint rule, and the trapezoid rule to approximate
   the area under the graph of a function on an interval of the x-axis.
   The user can specify the function, the left endpoint of the
   interval, the right endpoint of the interval, and the number
   of subintervals for the approximation.  by entering them into
   text input boxes.  Then the user presses a button to apply
   one of the rules.
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class RiemannSumsApplet extends Applet implements ActionListener{

   TextField functionBox;       // The input box where the user enters the function.
   TextField leftEndpointBox;   // The input box where the user enters the left
                                //    endpoint of the interval on the x-axis.
   TextField rightEndpointBox;  // The input box where the user enters the right
                                //    endpoint of the interval on the x-axis.
   TextField intervalCountBox;  // The input box where the user enters the number
                                //    subintervals to use.

   Label message;   // Used to display messages to the user.


   public void init() {
         // Called by the system when the applet is created; This
         // sets up the GUI for the applet.

      functionBox = new TextField("sqrt(4-x^2)", 20);
      functionBox.setBackground(Color.white);
      leftEndpointBox = new TextField("0.0", 10);
      leftEndpointBox.setBackground(Color.white);
      rightEndpointBox = new TextField("2.0", 10);
      rightEndpointBox.setBackground(Color.white);
      intervalCountBox = new TextField("1000", 10);
      intervalCountBox.setBackground(Color.white);
      message = new Label("Enter your data and click a button", Label.CENTER);
      message.setBackground(Color.white);
      message.setForeground(Color.red);
      message.setFont(new Font("Serif",Font.BOLD,14));

      Panel panel;  // This will be used for adding several compoents
                    // to one spot in the GridLayout.

      setBackground(Color.darkGray);
      setLayout(new GridLayout(7,1,3,3));

      add(message);

      panel = new Panel();
      panel.setBackground(Color.lightGray);
      panel.add(new Label("Enter the function:  f(x) = "));
      panel.add(functionBox);
      add(panel);

      panel = new Panel();
      panel.setBackground(Color.lightGray);
      panel.add(new Label("Enter the leftEndpoint:  A = "));
      panel.add(leftEndpointBox);
      add(panel);

      panel = new Panel();
      panel.setBackground(Color.lightGray);
      panel.add(new Label("Enter the rightEndpoint:  B = "));
      panel.add(rightEndpointBox);
      add(panel);

      panel = new Panel();
      panel.setBackground(Color.lightGray);
      panel.add(new Label("Number of subintervals:  N = "));
      panel.add(intervalCountBox);
      add(panel);

      panel = new Panel();
      panel.setLayout(new GridLayout(1,2));
      Button b1 = new Button("Left Endpoint Rule");
      b1.setBackground(Color.lightGray);
      b1.addActionListener(this);
      panel.add(b1);
      Button b2 = new Button("Right Endpoint Rule");
      b2.setBackground(Color.lightGray);
      b2.addActionListener(this);
      panel.add(b2);
      add(panel);

      panel = new Panel();
      panel.setLayout(new GridLayout(1,2));
      Button b3 = new Button("Midpoint Rule");
      b3.setBackground(Color.lightGray);
      b3.addActionListener(this);
      panel.add(b3);
      Button b4 = new Button("Trapezoid Rule");
      b4.setBackground(Color.lightGray);
      b4.addActionListener(this);
      panel.add(b4);
      add(panel);

   }  // end init()


   public Insets getInsets() {
           // tells the system how much space to leave around the edges of the applet
      return new Insets(3,3,3,3);
   }


   public void actionPerformed(ActionEvent evt) {
          // Called by the system when the user clicks on a button.
          // This routine gets the users data from the input boxes,
          // applies the rule specified by the button that was clicked,
          // and displays the result.  However, if an error os found
          // in the user's data, then an error message is displayed instead.

      String cmd = evt.getActionCommand();  // Label from button that was clicked.

      if (cmd.equals("Left Endpoint Rule")) {                 
          message.setText("Left Endpoint Rule button was clicked.");
      }
      else if (cmd.equals("Right Endpoint Rule")) {
          message.setText("Right Endpoint Rule button was clicked.");
      }
      else if (cmd.equals("Midpoint Rule")) {
          message.setText("Midpoint Rule button was clicked.");
      }
      else if (cmd.equals("Trapezoid Rule")) {
          message.setText("Trapezoid Rule button was clicked.");
      }

   } // end actionPerformed()


   double leftEndpointRule(Expr f, double A, double B, int N) {
        // A subroutine that approximates the area under the graph
        // of f(x) on the interval with left endpoint A and right
        // endpoint B.  It uses a "left-endpoint rule" sum with N terms.
      double dx = (B-A)/N;
      double sum = 0;
      for (int i = 0; i < N; i++)
         sum += f.value(A+i*dx);
      return sum*dx;
   } // end leftEndpointRule


   double rightEndpointRule(Expr f, double A, double B, int N) {
        // A subroutine that approximates the area under the graph
        // of f(x) on the interval with left endpoint A and right
        // endpoint B.  It uses a "right-endpoint rule" sum with N terms.
      double dx = (B-A)/N;
      double sum = 0;
      for (int i = 1; i <= N; i++)
         sum += f.value(A+i*dx);
      return sum*dx;
   } // end rightEndpointRule


   double midpointRule(Expr f, double A, double B, int N) {
        // A subroutine that approximates the area under the graph
        // of f(x) on the interval with left endpoint A and right
        // endpoint B.  It uses a "midpoint rule" sum with N terms.
      double dx = (B-A)/N;
      double sum = 0;
      for (int i = 0; i < N; i++)
         sum += f.value(A+(i+0.5)*dx);
      return sum*dx;
   } // end midpointRule


   double trapezoidRule(Expr f, double A, double B, int N) {
        // A subroutine that approximates the area under the graph
        // of f(x) on the interval with left endpoint A and right
        // endpoint B.  It uses a "trapezoid rule" sum with N terms.
      double dx = (B-A)/N;
      double sum = 0;
      for (int i = 1; i < N; i++)
         sum += f.value(A+i*dx);
      sum += 0.5*(f.value(A)+f.value(B));
      return sum*dx;
   } // end trapezoidRule

   
} // end class RiemannSumsApplet

