[Math Lair] pascal.java

Math Lair Home > Interactive content > pascal.java
/**
 * pascal.java - Draws Pascal's triangle (mod n).
 *	@version	1.00; 24 April 2000
 *      @author         James Yolkowski (yolk4870@mach1.wlu.ca)
 *
 * This program is Copyright (C) 2000, James Yolkowski.
 * This program is free and open software; you may use, modify, and
 * distribute this program and any modified variants in any way you
 * wish, provided you do not restrict others from doing the same.
 * (Therefore, any redistribution of the class file must include this
 * file.)
 */

import java.applet.*;
import java.awt.*;
import java.lang.*;

public class pascal extends Applet {

  Color colorArray[] = new Color[16];

  TextField tModulus;
  Label lModulus;
  Button computeIt;

  int modulus;
  Color colorArray[] = new Color[16];

  public void init() {
  // resize window and add fields.
    resize(320,380);
    tModulus = new TextField("2");
    lModulus = new Label("Modulus: ");
    computeIt = new Button("Compute");
    modulus = 2;
    add(lModulus);
    add(tModulus);
    add(computeIt);

    //Initialise colours.
    int i;
    for (i = 0; i < 16; i++) {
      int j;
      int c[] = new int[3];
      for (j = 0; j < 3; j++)
	c[j] = (int) ((i & (int) Math.pow(2,j))/Math.pow(2,j)) * 170;
      if (i == 6) c[1] = 85;
      if (i < 8) colorArray[i] = new Color(c[2], c[1], c[0]);
      else colorArray[i] = new Color(c[2]+85, c[1]+85, c[0]+85);
    }
  }

  public void paint(Graphics g) {
  //
    String tempstring;
    tempstring = "0";
    tempstring = tempstring.concat(tModulus.getText());
    modulus = Integer.parseInt(tempstring);

    int points[][] = new int[320][320];
    int x, y;
    for (y = 0; y < 320; y++) {
      for (x = 0; x <= y; x++) {
	if ((y==x) || (x==0)) points[x][y] = 1;
	else points[x][y] = (points[x][y-1] + points[x-1][y-1]) % modulus;
	g.setColor(colorArray[points[x][y]]);
	g.drawLine(x,y+60,x,y+60);
      }
    }
  }

  public boolean action (Event e, Object O) {
    if(e.target instanceof Button)	/* only one button currently	*/
      repaint();
    return true;
  }

}