package clt;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import edu.csusb.danby.applet.*;  //for DFrame, DApplet
import edu.csusb.danby.stat.*;

/**
* class CLTApplet illustrates the central limit theorem using
* 
* @author Charles Stanton
* @version Sun Jul 14 11:47:48 PDT 2002
*/
public class CLTApplet extends DApplet implements DControlPanelAction{
  //protected DFrame dFrame;
  
    private DiceModel dm;
    private DicePanel dp;  
    private HistogramPanel hp;
    private int numberRolls = 1;

  public void init(){
    Image[] dicegif = new Image[6]; //images of dice
    
    JMenuBar menubar;
    DControlPanel cp;
    int k;
    String diegifString;
    DMenu file, rolls, dice;
    String[] fileMenuItem = {"exit"};
    String[] rollsMenuItem ={ "1", "5", "20", "100"};
    String[] diceMenuItem = {"1", "2", "6", "9"};
    //load dice images
    MediaTracker tracker=new MediaTracker(this);
    for (int j=0; j< 6 ; j++) {
        diegifString="die"+(j+1)+".gif";
        java.net.URL url = this.getClass ().getResource(diegifString);
        try{            
            dicegif[j] = createImage( (java.awt.image. ImageProducer) url.getContent () );
        }
        catch (java.io.IOException ioe){
            System.err.println("couldn't find dice["+j+".gif");
        }
        tracker.addImage(dicegif[j],j);
    }
    try{tracker.waitForAll();} catch (InterruptedException e){;}
    if ( tracker.isErrorAny() ) {
         this.showStatus("error loading images");
    }   
    // construct model and panels
    dm = new DiceModel();  
    dp = new DicePanel(dm);
    dp.setDiceImages(dicegif);
    hp = new HistogramPanel(dm);
    dFrame = new DFrame("Central Limit Theorem", dp,hp); 
    
    // add two buttons
    String[]  buttonLabels ={"play", "reset"};
    cp = new DControlPanel(dFrame, this, buttonLabels);
    
    // construct the menubar and add to the frame
    menubar = new JMenuBar();
    dFrame.setJMenuBar(menubar);

    // construct "file" menu with "exit"
    file = new DMenu(this, "file", fileMenuItem);
    menubar.add(file);
    
    // rolls is a radio button menu
    rolls = new DMenu(this, "rolls", rollsMenuItem, true);
    rolls.setSelectedItem(0);//set 1 roll selected
    menubar.add(rolls);
    
    
    // dice is also a radio button menu
    dice = new DMenu(this, "dice", diceMenuItem, true);
    //originally a single die is rolled
    dice.setSelectedItem(0);
    dm.setNDice(1);
    menubar.add(dice);

    
    
    
    dFrame.pack();
    dFrame.validate();
    setVisible(true);
  }


/**
* override DApplet.controlPanelButtonAction() to provide
* correct behavior for buttons
*/
    public void doControlPanelButtonAction( ActionEvent e){
        if (e.getActionCommand().equals("play")){
            dm.rollem(numberRolls);
            dp.updatePanel();
            hp.update(dm);
            }
        else if (e.getActionCommand().equals("reset")){
            dm.reset();
            dp.updatePanel();
            hp.update(dm);	
            }
    }

/**
* override DApplet.menuAction() to provide menu actions
*/
    public void menuAction( String menuLabel, String itemLabel){
        if (menuLabel.equals("file")){
            if (itemLabel.equals("exit")){
                dFrame.dispose();
                }
            }
        else if (menuLabel.equals("rolls")){
            numberRolls = Integer.parseInt(itemLabel);
            }
        else if (menuLabel.equals("dice")){
            dm.setNDice( Integer.parseInt(itemLabel));
            }
    }
    
      /**
    * provides applet info
    */
     public String getAppletInfo() {
        return "A demonstration of the Central Limit Theorem.\nAuthor: Charles S. Stanton";
    }
}
