Subversion Repositories wimsdev

Rev

Rev 4033 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2. Copyright 2004-2006 David P. Little
  3. license:
  4. Unless otherwise stated, the above applets were written by David Little.
  5. They may be used without permission from the author for home and/or educational (non-profit) purposes only.
  6. Any other use must be approved by the author.
  7.  
  8. Modified for wims interactive usage with permission of the author.
  9.  
  10. J.M. Evers
  11. */
  12. import java.awt.*;  
  13. import java.awt.event.*;
  14. import java.applet.*;
  15. import java.util.*;
  16. import javax.swing.*;
  17. import javax.swing.event.*;
  18. import javax.swing.border.*;
  19.  
  20.  
  21. public class Plinko extends JApplet implements Runnable, ActionListener, ChangeListener, WindowListener{
  22.     static JSpinner bins;
  23.     static JSpinner rate;
  24.     static JSpinner prob;
  25.     JLabel count;
  26.     JLabel mean;
  27.     JLabel variance;
  28.     JLabel current_bin;
  29.     JLabel current_bin_count;
  30.     JLabel current_bin_prob;
  31.     JLabel confidence;
  32.     JButton start;
  33.     JButton clear;
  34.     JButton close;
  35.     PlinkoBoard plinkoboard;
  36.     Thread thread;
  37.     boolean active = false;
  38.     int countdown = 0;
  39.     JFrame frame;
  40.     SplashPanel splash;
  41.     static AudioClip click;
  42.     static AudioClip ping;
  43. // jm.evers: declaring a few things
  44.     boolean wims_exercise = false;
  45.     int bins_num=11;
  46.     int speed=1;
  47.     int total_balls=100000;
  48.     double chance=0.5;
  49.     int frame_x=600;
  50.     int frame_y=600;
  51.     int font_size=14;
  52.     String title="Binomial Distribution";
  53.     String click_text="CLICK ANYWHERE TO BEGIN";
  54.     String start_text="START";
  55.     String stop_text="STOP";
  56.     String clear_text="CLEAR";
  57.     String close_text="CLOSE";
  58.     String label_count="Count: ";
  59.     String label_mean="Mean: ";
  60.     String label_variance="Variance: ";
  61.     String label_bin="Bin: ";
  62.     String label_bins="Bins: ";
  63.     String label_speed="Speed: ";
  64.     String label_probability="Probability: ";    
  65.     String label_bin_count="Bin Count: ";
  66.     String label_bin_probability="Bin Probability: ";
  67.     String label_confidence="Confidence Interval: ";
  68.     String some_text=" % of the balls landed in bin ";
  69.     String through=" through ";
  70.     String label_controls=" Controls ";
  71.     String label_statistics="Statistics";
  72.      
  73.     public void init(){
  74.         // jm.evers: introducing a few Applet params , used for tailoring the exercise.
  75.         String s=getParameter("wims_exercise");
  76.         if(s!=null){
  77.             if(s.equalsIgnoreCase("yes") || s.equals("1")){
  78.                 wims_exercise = true;
  79.                 s=getParameter("balls");
  80.                 if(s!=null && s.length()>0){total_balls = Integer.parseInt(s, 10);}
  81.                 s=getParameter("chance");
  82.                 if(s!=null && s.length()>0){chance = Double.parseDouble(s);}
  83.                 s=getParameter("bins");
  84.                 if(s!=null && s.length()>0){bins_num = Integer.parseInt(s, 10);}
  85.                 s=getParameter("speed");
  86.                 if(s!=null && s.length()>0){speed = Integer.parseInt(s, 10);}
  87.                 s=getParameter("xsize");
  88.                 if(s!=null && s.length()>0){frame_x = Integer.parseInt(s, 10);}
  89.                 s=getParameter("ysize");
  90.                 if(s!=null && s.length()>0){frame_y = Integer.parseInt(s, 10);}
  91.                 s=getParameter("title");
  92.                 if(s!=null && s.length()>0){title=s;}
  93.                 s=getParameter("font_size");
  94.                 if(s!=null && s.length()>0){font_size = Integer.parseInt(s, 10);}
  95.  
  96.             }
  97.         }
  98.         s=getParameter("language");
  99.         if(s!=null && s.length()>0){
  100.             if(s.equalsIgnoreCase("nl")){
  101.                 click_text="KLIK HIER OM TE BEGINNEN";
  102.                 start_text="START";
  103.                 stop_text="STOP";
  104.                 clear_text="WISSEN";
  105.                 close_text="SLUITEN";
  106.                 label_count="Aantal: ";
  107.                 label_mean="Gemiddeld: ";
  108.                 label_variance="Variantie: ";
  109.                 label_bin="Bakje: ";
  110.                 label_bins="Bakjes: ";
  111.                 label_speed="Snelheid: ";
  112.                 label_bin_count="Ballen/bakje: ";
  113.                 label_probability="Kans: ";    
  114.                 label_bin_probability="Kans per bakje: ";
  115.                 label_confidence="Betrouwbaarheids interval: ";
  116.                 some_text=" % van de ballen in bakje: ";
  117.                 through=" t/m ";
  118.                 String label_controls=" Menu ";
  119.                 String label_statistics="Statistiek";
  120.             }
  121.             if(s.equalsIgnoreCase("fr")){
  122.                 click_text="CLIQUEZ ICI";
  123.                 start_text="COMMENCER";
  124.                 stop_text="PAUSE";
  125.                 clear_text="EFFACER";
  126.                 close_text="FERMER";
  127.                 label_count="Quantit\u00E9 : ";
  128.                 label_mean="Moyenne : ";
  129.                 label_variance="Variance: ";
  130.                 label_bin="Panier : ";
  131.                 label_bins="Paniers : ";
  132.                 label_speed="Vitesse : ";
  133.                 label_bin_count="Balles dans ce panier : ";
  134.                 label_bin_probability="% de balles dans ce panier : ";
  135.                 label_probability="Probabilit\u00E9 : ";    
  136.                 label_confidence="Intervalle de confiance : ";
  137.                 some_text=" % de balles dans le panier ";
  138.                 through=" through ";
  139.                 label_controls="Menu";
  140.                 label_statistics="Statistiques";
  141.             }
  142.             if(s.equalsIgnoreCase("de")){
  143.                 click_text="Klicken Sie hier um zu starten";
  144.                 start_text="START";
  145.                 stop_text="STOP";
  146.                 clear_text="CLEAR";
  147.                 close_text="CLOSE";
  148.                 label_count="Zahl: ";
  149.                 label_mean="Mean: ";
  150.                 label_variance="Variance: ";
  151.                 label_bin="Behälter: ";
  152.                 label_bins="Behälter: ";
  153.                 label_speed="Geschwindigkeit : ";
  154.                 label_bin_count="Zahl/Behälter: ";
  155.                 label_bin_probability="Wahrscheinlichkeit in Behälter: ";
  156.                 label_probability="Wahrscheinlichkeit: ";    
  157.                 label_confidence="Wahrscheinlichkeits Intervall: ";
  158.                 some_text=" % der Kugeln fällt in Behälter ";
  159.                 through=" bis ";
  160.                 String label_controls=" Menu ";
  161.                 String label_statistics="Statistik";
  162.             }
  163.         }
  164.         setBackground(Color.white);
  165.         frame = new JFrame();
  166.         frame.setVisible(false);
  167.         frame.setSize(frame_x, frame_y);
  168.         frame.setLocation(0,0);
  169.         frame.setTitle( title );
  170.         Font font = new Font( "Helvetica", Font.BOLD, font_size );
  171.         GridBagLayout gridbaglayout = new GridBagLayout();
  172.         GridBagConstraints constraints = new GridBagConstraints();
  173.         constraints.ipadx = 5;
  174.         constraints.ipady = 5;
  175.         constraints.insets = new Insets( 1,1,1,1 );
  176.         constraints.anchor = GridBagConstraints.CENTER;
  177.         constraints.fill = GridBagConstraints.NONE;
  178.         constraints.weightx = 1.0;
  179.         JPanel controls_west = new JPanel(  );
  180.         JLabel label = new JLabel( label_bins,JLabel.LEFT );
  181.         controls_west.add( label );
  182.         bins = new JSpinner( new SpinnerNumberModel(bins_num,2,101,1) );
  183.         controls_west.add( bins );
  184.         label = new JLabel( label_probability,JLabel.LEFT );
  185.         controls_west.add( label );
  186.         prob = new JSpinner( new SpinnerNumberModel(0.50,0.00,100,0.01) );
  187.         SpinnerNumberModel model = (SpinnerNumberModel)prob.getModel();
  188.         model.setMaximum( new Double(1.00) );
  189.         controls_west.add( prob );
  190.         label = new JLabel( label_speed,JLabel.LEFT );
  191.         controls_west.add( label );
  192.         rate = new JSpinner( new SpinnerNumberModel(1,1,10,1) );
  193.         controls_west.add( rate );
  194.         JPanel controls_east = new JPanel();
  195.         controls_east.add( start = new JButton( start_text ) );
  196.         start.setDefaultCapable( true );
  197.         frame.getRootPane().setDefaultButton( start );
  198.         controls_east.add( clear = new JButton( clear_text ) );
  199.         controls_east.add( close = new JButton( close_text ) );
  200.         close.setDefaultCapable( false );      
  201.         JPanel controls = new JPanel( new BorderLayout() );
  202.         controls.add( "East", controls_east );
  203.         JPanel statistics_center = new JPanel( new GridLayout(2,3) );
  204.         statistics_center.add( count = new JLabel( label_count ) );
  205.         statistics_center.add( mean = new JLabel( label_mean ) );
  206.         statistics_center.add( variance = new JLabel( label_variance ) );
  207.         statistics_center.add( current_bin = new JLabel( label_bin ) );
  208.         statistics_center.add( current_bin_count = new JLabel(label_bin_count ) );
  209.         statistics_center.add( current_bin_prob = new JLabel( label_bin_probability ) );
  210.         JPanel statistics_south = new JPanel( new GridLayout(1,1) );
  211.         statistics_south.add( confidence = new JLabel( label_confidence ) );
  212.         JPanel statistics = new JPanel( new BorderLayout() );
  213.         if(wims_exercise == false){
  214.             // hmmm...these controls are handed over to applet params...if applet is part of an interactive exercise.
  215.             controls.add( "West", controls_west );
  216.             controls.setBorder( BorderFactory.createTitledBorder( controls.getBorder(), label_controls, TitledBorder.LEFT, TitledBorder.TOP, new Font( "Helvetica", Font.BOLD, font_size) ) );
  217.         }
  218.         statistics.add( "Center", statistics_center );
  219.         statistics.add( "South", statistics_south );
  220.         statistics.setBorder( BorderFactory.createTitledBorder( statistics.getBorder(), label_statistics, TitledBorder.LEFT, TitledBorder.TOP, new Font( "Helvetica", Font.BOLD, font_size) ) );
  221.  
  222.         JPanel south = new JPanel( new BorderLayout() );
  223.         south.add( "Center", statistics );
  224.         JLabel statusbar = new JLabel("");
  225.         statusbar.setPreferredSize( new Dimension(15,15) );
  226.         south.add( "South", statusbar );
  227.         Container contentpane = frame.getContentPane();
  228.         contentpane.setLayout(new BorderLayout(10,10));
  229.         contentpane.add( "Center", plinkoboard = new PlinkoBoard(this) );
  230.         contentpane.add( "North", controls );
  231.         contentpane.add( "South", south );
  232.         start.addActionListener( this );
  233.         clear.addActionListener( this );
  234.         close.addActionListener( this );
  235.         bins.addChangeListener( this );
  236.         rate.addChangeListener( this );
  237.         prob.addChangeListener( this );
  238.  
  239.         plinkoboard.requestFocus();
  240.         frame.addWindowListener( this );
  241.         getContentPane().add( splash = new SplashPanel( this ) );
  242.         click = getAudioClip( getCodeBase(), "sounds/click.au" );
  243.         ping = getAudioClip( getCodeBase(), "sounds/ping.au" );
  244.     }
  245.  
  246.         // jm.evers: returns a string to a javascript function ReadApplet(); [must be a string, and not an array ... IE trouble]
  247.         // the string will be send -along with the "student reply" to the questions at hand- to the WIMS server;
  248.         public String ReadApplet(){
  249.             String s=plinkoboard.ReadData();
  250.             return s;                                                                                                                      
  251.         }                                                                                                                                  
  252.  
  253.         public void showFrame(){
  254.                 frame.setVisible( true );
  255.         }
  256.  
  257.         public void stop(){
  258.                 frame.setVisible( false );
  259.                 splash.start();
  260.         }
  261.  
  262.         public void run(){             
  263.             //while ( active  || countdown > 0 ){
  264.             while ( active ){
  265.                 plinkoboard.dropBall( false );
  266.                 //if ( countdown > 0 ) countdown--;
  267.                 try {
  268.                 //if ( countdown < 1)
  269.                     if(wims_exercise){
  270.                         Thread.sleep(105 - 10*( (int) speed ));
  271.                     }
  272.                     else
  273.                     {
  274.                         Thread.sleep(105 - 10*((Integer)rate.getValue()).intValue());
  275.                     }
  276.                 } catch (InterruptedException e){}
  277.             }
  278.         }
  279.        
  280.         public void toggleStart(){
  281.             if ( active ){
  282.                 active = false;
  283.                 start.setText(start_text);
  284.             }
  285.             else
  286.             {
  287.                 active = true;
  288.                 bins.setEnabled( false );
  289.                 thread = new Thread(this);
  290.                 thread.start();
  291.                 start.setText(stop_text);
  292.             }
  293.         }
  294.  
  295.         public void actionPerformed( ActionEvent ae ){
  296.             Object obj = ae.getSource();
  297.             if (obj == start){toggleStart();plinkoboard.requestFocus();}
  298.             if( obj == clear ){
  299.                 if ( plinkoboard.FIRST_BALL == null ){plinkoboard.newHist();}
  300.                 else
  301.                 {
  302.                     plinkoboard.FIRST_BALL = null;
  303.                     //plinkoboard.BALL_COUNT = 0;
  304.                     if ( !active ){bins.setEnabled( true );}
  305.                 }
  306.                     plinkoboard.repaint();plinkoboard.requestFocus();
  307.             }
  308.             if(obj == close){stop();}
  309.         }
  310.                
  311.         public void stateChanged( ChangeEvent ce ){
  312.             Object obj = ce.getSource();
  313.             if ( obj == bins ){
  314.                 count.setText( "0" );
  315.                 plinkoboard.setup( );
  316.                 plinkoboard.newHist( );
  317.                 plinkoboard.drawBackground();
  318.             } else if ( obj == rate ){
  319.             } else if ( obj == prob ){
  320.                 plinkoboard.updatePercent();
  321.                 plinkoboard.repaint();
  322.             }
  323.         }
  324.  
  325.         public void windowActivated( WindowEvent we ){
  326.             //splash.start();
  327.         }
  328.        
  329.  
  330.         public void windowClosed( WindowEvent we ){
  331.                 //splash.start();
  332.         }
  333.  
  334.         public void windowClosing( WindowEvent we ){
  335.                 //splash.start();
  336.         }
  337.  
  338.         public void windowDeactivated( WindowEvent we ){
  339.                 //splash.start();
  340.         }
  341.  
  342.         public void windowDeiconified( WindowEvent we ){}
  343.  
  344.         public void windowIconified( WindowEvent we ){}
  345.  
  346.         public void windowOpened( WindowEvent we ){}
  347.        
  348.         public String getAppletInfo(){
  349.             return "Written by Dr. P. Little \n http://www.math.psu.edu/dlittle/java/probability/plinko/index.html" ;
  350.         }  
  351.  
  352. /*             
  353.         public void keyTyped( KeyEvent ke ){
  354.             int code = ke.getKeyCode();
  355.             char key = ke.getKeyChar();
  356.  
  357.             if ( key >= '0' && key <= '4' ){
  358.                 int x = key - '0';
  359.                 int n = 1;
  360.                 for (int i=0; i<x; i++ ){
  361.                     n *= 10;
  362.                 }
  363.                 for (int i=0; i<n; i++){
  364.                     plinkoboard.dropBall();
  365.                 }
  366.             } else if ( key == ' ' ){
  367.                         toggleStart();
  368.             } else if (key==20){ //control t - toggle erasing
  369.                         plinkoboard.toggleErase();
  370.             } else if (key==3){ //control c - clear screen
  371.                         plinkoboard.newHist();
  372.             } else if (key==24){ //control x - kill all threads and clear screen
  373.                         plinkoboard.kill();
  374.             } else if (key==16){ //control p - toggle displaying probabilities
  375.                         //plinkoboard.toggleProbabilities();
  376.             }
  377.         }
  378.  
  379.         public void keyPressed( KeyEvent ke ){
  380.         }
  381.        
  382.         public void keyReleased( KeyEvent ke ){
  383.         }*/
  384.          
  385. }
  386.  
  387.