/*File: DiceGIF.java */ import java.awt.*; import java.applet.*; import java.net.*; import gjt.Util; /** * Main Dice Applet class * @author Charles S. Stanton * @version 11/19/96 */ public class DiceGIF extends Applet { ControlPanelGIF felt2 ; DiceCanvasGIF felt1; drawHistogram felt3; int ndice = 1; //number of dice int xbins = ndice*5+1; int xcount[] = new int[xbins]; //note that this depends on number of dice! double[] theoreticalCount=new double[xbins]; //total_nrolls*p(x), where p(x) is theoretical value int die[]= new int[ndice]; int total_nrolls=0; DiceGIF applet=this; // Probabilities are hard coded in int[][] probs = {{1,1,1,1,1,1},{1,2,3,4,5,6,5,4,3,2,1}, {1, 3, 6, 10, 15, 21, 25, 27, 27, 25, 21, 15, 10, 6, 3, 1}, {1, 4, 10, 20, 35, 56, 80, 104, 125, 140, 146, 140, 125, 104, 80, 10, 56, 35, 20, 4, 1}, {1, 5, 15, 35, 70, 126, 205, 305, 420, 540, 651, 735, 780, 780, 735, 651, 540, 420, 305, 205, 126, 70, 35, 15, 5, 1}, {1, 6, 21, 56, 126, 252, 456, 756, 1161, 1666, 2247, 2856, 3431, 3906, 4221, 4332,4221, 3906, 3431, 2856, 2247, 1666, 1161, 756, 456, 252, 126, 56, 21, 6, 1}, {1, 7, 28, 84, 210, 462, 917, 1667, 2807, 4417, 6538, 9142, 12117, 15267, 18327, 20993, 22967, 24017, 24017, 22967, 20993, 18327, 15267, 12117, 9142, 6538, 4417, 2807, 1667, 917, 462, 210, 84, 28, 7, 1}, {1, 8, 36, 120, 330, 792, 1708, 3368, 6147, 10480, 16808, 25488, 36688, 50288, 65808, 82384, 98813, 113688, 125588, 133288, 135954, 133288, 125588, 113688, 98813, 82384, 65808, 50288, 36688, 25488, 16808, 10480, 6147, 3368, 1708, 792, 330, 120, 36, 8, 1}, {1, 9, 45, 165, 495, 1287, 2994, 6354, 12465, 22825, 39303, 63999, 98979, 145899,205560, 277464, 359469, 447669, 536569, 619569, 689715, 740619, 767394, 767394, 740619, 689715, 619569, 536569, 447669, 359469, 277464, 205560, 145899, 98979, 63999, 39303, 22825, 12465, 6354, 2994, 1287, 495, 165, 45, 9, 1}}; Image[] Dicegif = new Image[6]; int imageWidth , imageHeight; Graphics offscreenGraphics; Image offscreenImage; public void init() { String diegif; int k; setLayout(new GridLayout(1,3)); felt1 = new DiceCanvasGIF(die,ndice,applet, Dicegif); //felt1.pleaseWait(); for (int j=0; j< 6 ; j++) { k=j+1; diegif = "die"+k+".gif"; System.out.println(diegif); Dicegif[j] = getImage(getCodeBase(),diegif); Util.waitForImage(this,Dicegif[j]);} felt2 = new ControlPanelGIF(applet); felt3 = new drawHistogram(xcount,xbins, theoreticalCount, applet); felt1.setBackground(new Color(0,140,0)); felt3.setBackground(new Color(0,0,140)); add(felt1); add(felt2); add(felt3); } public void paint(Graphics g){ felt1.paint(g); } /** * rollem simulates the rolling of dice using random number * generators. * @param nrolls is the number of rolls to do. */ public void rollem(int nrolls) { int sumX; for (int j=0; j< nrolls; j++){ sumX=0; for (int i=0; i< ndice; i++) { this.die[i]=(int)Math.floor(1+Math.random()*6); sumX += die[i]; } felt1.update(die, sumX); xcount[sumX-ndice]++; felt3.update(xcount); felt2.update(); } total_nrolls += nrolls; for (int i=0; i< xbins; i++){ theoreticalCount[i] = (double)total_nrolls* probs[ndice-1][i]*Math.pow(6.0,-1*ndice); } } public void clear() { ndice = Integer.valueOf(felt2.ndice_cbg.getCurrent().getLabel()).intValue(); xbins = ndice*5+1; xcount = new int[xbins]; //note that this depends on number of dice! theoreticalCount= new double[xbins]; total_nrolls=0; die = new int[ndice]; felt1.clearCanvas(ndice); felt3.clearHistogram(xcount,xbins,theoreticalCount); } }