home

shop

quotes

optical illusions

tutor

about

links

links

     
MathImage    shop_logo

    


/*
NormalDistribution.java
Any portion of this code may be used without permission from BrainyPage.
www.BrainyPage.com | Created by Raul Aguilar | Last Modified 200205032153

Enjoy!
*/

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import java.awt.geom.*;
import java.math.*;

import java.applet.*;
import com.borland.jbcl.layout.*;

import java.util.*;


public class NormalDistribution extends JApplet{

  private aJFrame f;
  public static final int WIDTH = 400;
  public static final int HEIGHT = 400;

  public void init() {
    // Set the arguments.
    String[] args = { "65" };
    f = new aJFrame(args, getContentPane());
  }

  public void paint(Graphics g) {
    super.paint(g);
    f.redraw();
  }

  public static void main(String[] args) {
    aJFrame frame = new aJFrame(args);
    frame.setTitle("NormalDistribution");
    frame.setSize(WIDTH,HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
  }
}

class aJFrame extends JFrame {

  public aJFrame(String[] args) {
    buildPanels(args, getContentPane());
  }

  public aJFrame(String[] args, Container cp) {
    buildPanels(args, cp);
  }

  public void buildPanels(String[] args, Container cp) {

    panelSimulate = new Simulate(args);
    Control panelControl = new Control(args, panelSimulate);

    JPanel p = new JPanel();
    p.setLayout(new XYLayout());
    p.add(panelSimulate, new XYConstraints(5, 5, 380, 335));
    p.add(panelControl, new XYConstraints(5, 345, 380, 90));
    cp.add(p,BorderLayout.CENTER);
  }

  public void redraw() {
    panelSimulate.repaint();
  }

  Simulate panelSimulate;
}

class Simulate extends JPanel {

  private double sigma;
  private String[] args;

  public Simulate(String[] args) {
    setParameters(args);

    //Initialize drawing colors
    setBackground(Color.white);
    setForeground(Color.black);
  }

  public void setParameters(String controlValues) {
    StringTokenizer st = new StringTokenizer(controlValues);
    ArrayList al = new ArrayList();
    while (st.hasMoreTokens()) {
      al.add((String) st.nextToken());
    }
    String[] s = (String[]) al.toArray(new String[0]);
    setParameters(s);
  }

  public void setParameters(String[] args) {
    sigma = Double.parseDouble(args[0]);

    System.out.println("parameter values:");
    System.out.println();
    System.out.println("sigma = " + sigma);
  }

  public void paint(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    // Find the width and height of the JPanel.
    Dimension d = getSize();
    int w = d.width;
    int h = d.height;

    // Calculate the half height.
    double w2 = w/2.0;
    double h2 = h/2.0;

    // set the line thickness
    int t = 2;

    //double sigma;
    //sigma = w / 6.0;

    // render the x-axis line
    /*
    t = 2;
    g2.setPaint(new Color(255, 0, 255));
    Rectangle2D xaxis = new Rectangle2D.Double();
    xaxis.setFrameFromDiagonal(0, cy - t, w, cy + t);
    g2.draw(xaxis);
    g2.fill(xaxis);
    */

    // Set the graph center.
    double cx, cy;
    cx = 0;
    cy = h - h / 5.0;

    // Use physical xy points.
    double px;
    double py;
    double nx;
    double ny;

    double positiveUnits = 10;
    double unitsPerGridLine = 1;
    double gridLines = positiveUnits/unitsPerGridLine;

    for (int i=0; i < 2*gridLines ; ++i) {

      px = w2 + (i * unitsPerGridLine) * (w2/positiveUnits);
      py = cy - (i * unitsPerGridLine) * (h2/positiveUnits);

      // Find the minus x values for grid lines
      // on the negative side of the graph.
      nx = w2 - (i * unitsPerGridLine) * (w2/positiveUnits);
      ny = cy + (i * unitsPerGridLine) * (h2/positiveUnits);

      if (i == 0) {
        // Draw the xy axis.
        g2.setPaint(new Color(255, 0, 255));
      }
      else {
        g2.setPaint(new Color(0, 255, 255));
        g2.draw(new Line2D.Double( nx, 0, nx, h));
        g2.draw(new Line2D.Double( 0, ny, w, ny));
      }

      g2.draw(new Line2D.Double( px, 0, px, h));
      g2.draw(new Line2D.Double( 0, py, w, py));
    }

    // Set the maximum function height the desired number of pixels.
    double maxFunctionHeight;
    maxFunctionHeight = 3.0 * h / 5.0;

    double mu;
    mu = w / 2.0;

    double fy0;
    fy0 = 1 / Math.sqrt(2.0 * Math.PI * sigma * sigma);

    // render the normal distribution curve
    t = 2;

    int x_prev = 0;
    int y_prev = 0;

    // Set the pen width.
    g2.setStroke(new BasicStroke(4));

    // Iterate through all x axis values.
    for (int ix = 0; ix < w; ix+=1) {

      double z;
      z = (ix - mu) / sigma;

      double fy;
      fy =
        Math.exp(-1.0 * z * z / 2.0)
        /
        Math.sqrt(2.0 * Math.PI * sigma * sigma);

      int x, y;
      x = (int) (cx + ix);
      y = (int) (cy - (maxFunctionHeight * fy / fy0));

      g2.setPaint(new Color(0, 0, 255));

      if (0 < ix) {
        // Render the normal distribution line.
        Point2D startP = new Point2D.Double(x_prev, y_prev);
        Point2D endP = new Point2D.Double(x, y);
        g2.draw(new Line2D.Double(startP, endP));
      }
      x_prev = x;
      y_prev = y;

      // Ellipse point rendering code.
      //Ellipse2D circle = new Ellipse2D.Double();
      //circle.setFrameFromCenter(x, y, x + t, y + t);
      //g2.draw(circle);
      //g2.fill(circle);

      // Verticle line point rendering code.
      //Rectangle2D rect = new Rectangle2D.Double();
      //rect.setFrameFromCenter(x, y - t, x, y + t);
      //g2.draw(rect);
      //g2.fill(rect);

    }
  }
}

class Control extends JPanel {

  private String[] args;
  private JTextField tf;
  private Simulate s;

  public Control(String[] args, Simulate sp) {

    this.args = args;
    this.setLayout(new XYLayout());

    s = sp;

    JButton b = new JButton("Go");
    ActionListener bl = new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        s.setParameters(tf.getText().trim());
        s.repaint();
      }
    };
    b.addActionListener(bl);
    add(b, new XYConstraints(0, 3, 50, 20));

    tf = new JTextField("65",5);
    add(tf, new XYConstraints(60, 3, 50, 20));
  }

}