Back to index

wims  3.65+svn20090927
Plinko.java
Go to the documentation of this file.
00001 import java.awt.*;   
00002 import java.awt.event.*;
00003 import java.applet.*;
00004 import java.util.*;
00005 import javax.swing.*;
00006 import javax.swing.event.*;
00007 import javax.swing.border.*;
00008 
00009 
00010 public class Plinko extends JApplet implements Runnable, ActionListener, ChangeListener, WindowListener{
00011     static JSpinner bins;
00012     static JSpinner rate;
00013     static JSpinner prob;
00014     JLabel count;
00015     JLabel mean;
00016     JLabel variance;
00017     JLabel current_bin;
00018     JLabel current_bin_count;
00019     JLabel current_bin_prob;
00020     JLabel confidence;
00021     JButton start;
00022     JButton clear;
00023     JButton close;
00024     PlinkoBoard plinkoboard;
00025     Thread thread;
00026     boolean active = false;
00027     int countdown = 0;
00028     JFrame frame;
00029     SplashPanel splash;
00030     static AudioClip click;
00031     static AudioClip ping;
00032 // jm.evers: declaring a few things
00033     boolean wims_exercise = false;
00034     int bins_num=11;
00035     int speed=1;
00036     int total_balls=100000;
00037     double chance=0.5;
00038     int frame_x=600;
00039     int frame_y=600;
00040     int font_size=14;
00041     String title="Binomial Distribution";
00042     String click_text="CLICK ANYWHERE TO BEGIN";
00043     String start_text="START";
00044     String stop_text="STOP";
00045     String clear_text="CLEAR";
00046     String close_text="CLOSE";
00047     String label_count="Count: ";
00048     String label_mean="Mean: ";
00049     String label_variance="Variance: ";
00050     String label_bin="Bin: ";
00051     String label_bins="Bins: ";
00052     String label_speed="Speed: ";
00053     String label_probability="Probability: ";    
00054     String label_bin_count="Bin Count: ";
00055     String label_bin_probability="Bin Probability: ";
00056     String label_confidence="Confidence Interval: ";
00057     String some_text=" % of the balls landed in bin ";
00058     String through=" through ";
00059     String label_controls=" Controls ";
00060     String label_statistics="Statistics";
00061      
00062     public void init(){
00063        // jm.evers: introducing a few Applet params , used for tailoring the exercise.
00064        String s=getParameter("wims_exercise");
00065        if(s!=null){
00066            if(s.equalsIgnoreCase("yes") || s.equals("1")){
00067               wims_exercise = true;
00068               s=getParameter("balls");
00069               if(s!=null && s.length()>0){total_balls = Integer.parseInt(s, 10);}
00070               s=getParameter("chance");
00071               if(s!=null && s.length()>0){chance = Double.parseDouble(s);}
00072               s=getParameter("bins");
00073               if(s!=null && s.length()>0){bins_num = Integer.parseInt(s, 10);}
00074               s=getParameter("speed");
00075               if(s!=null && s.length()>0){speed = Integer.parseInt(s, 10);}
00076               s=getParameter("xsize");
00077               if(s!=null && s.length()>0){frame_x = Integer.parseInt(s, 10);}
00078               s=getParameter("ysize");
00079               if(s!=null && s.length()>0){frame_y = Integer.parseInt(s, 10);}
00080               s=getParameter("title");
00081               if(s!=null && s.length()>0){title=s;}
00082               s=getParameter("font_size");
00083               if(s!=null && s.length()>0){font_size = Integer.parseInt(s, 10);}
00084 
00085            }
00086        }
00087        s=getParameter("language");
00088        if(s!=null && s.length()>0){
00089            if(s.equalsIgnoreCase("nl")){
00090               click_text="KLIK HIER OM TE BEGINNEN";
00091               start_text="START";
00092               stop_text="STOP";
00093               clear_text="WISSEN";
00094               close_text="SLUITEN";
00095               label_count="Aantal: ";
00096               label_mean="Gemiddeld: ";
00097               label_variance="Variantie: ";
00098               label_bin="Bakje: ";
00099               label_bins="Bakjes: ";
00100               label_speed="Snelheid: ";
00101               label_bin_count="Ballen/bakje: ";
00102               label_probability="Kans: ";    
00103               label_bin_probability="Kans per bakje: ";
00104               label_confidence="Betrouwbaarheids interval: ";
00105               some_text=" % van de ballen in bakje: ";
00106               through=" t/m ";
00107               String label_controls=" Menu ";
00108               String label_statistics="Statistiek";
00109            }
00110            if(s.equalsIgnoreCase("fr")){
00111               click_text="CLIQUEZ ICI";
00112               start_text="COMMENCER";
00113               stop_text="PAUSE";
00114               clear_text="EFFACER";
00115               close_text="FERMER";
00116               label_count="Quantit\u00E9 : ";
00117               label_mean="Moyenne : ";
00118               label_variance="Variance: ";
00119               label_bin="Panier : ";
00120               label_bins="Paniers : ";
00121               label_speed="Vitesse : ";
00122               label_bin_count="Balles dans ce panier : ";
00123               label_bin_probability="% de balles dans ce panier : ";
00124               label_probability="Probabilit\u00E9 : ";    
00125               label_confidence="Intervalle de confiance : ";
00126               some_text=" % de balles dans le panier ";
00127               through=" through ";
00128               label_controls="Menu";
00129               label_statistics="Statistiques";
00130            }
00131            if(s.equalsIgnoreCase("de")){
00132               click_text="Klicken Sie hier um zu starten";
00133               start_text="START";
00134               stop_text="STOP";
00135               clear_text="CLEAR";
00136               close_text="CLOSE";
00137               label_count="Zahl: ";
00138               label_mean="Mean: ";
00139               label_variance="Variance: ";
00140               label_bin="Behälter: ";
00141               label_bins="Behälter: ";
00142               label_speed="Geschwindigkeit : ";
00143               label_bin_count="Zahl/Behälter: ";
00144               label_bin_probability="Wahrscheinlichkeit in Behälter: ";
00145               label_probability="Wahrscheinlichkeit: ";    
00146               label_confidence="Wahrscheinlichkeits Intervall: ";
00147               some_text=" % der Kugeln fällt in Behälter ";
00148               through=" bis ";
00149               String label_controls=" Menu ";
00150               String label_statistics="Statistik";
00151            }
00152        }
00153        setBackground(Color.white);
00154        frame = new JFrame();
00155        frame.setVisible(false);
00156        frame.setSize(frame_x, frame_y);
00157        frame.setLocation(0,0);
00158        frame.setTitle( title );
00159        Font font = new Font( "Helvetica", Font.BOLD, font_size );
00160        GridBagLayout gridbaglayout = new GridBagLayout();
00161        GridBagConstraints constraints = new GridBagConstraints();
00162        constraints.ipadx = 5;
00163        constraints.ipady = 5;
00164        constraints.insets = new Insets( 1,1,1,1 );
00165        constraints.anchor = GridBagConstraints.CENTER;
00166        constraints.fill = GridBagConstraints.NONE;
00167        constraints.weightx = 1.0;
00168        JPanel controls_west = new JPanel(  );
00169        JLabel label = new JLabel( label_bins,JLabel.LEFT );
00170        controls_west.add( label );
00171        bins = new JSpinner( new SpinnerNumberModel(bins_num,2,101,1) );
00172        controls_west.add( bins );
00173        label = new JLabel( label_probability,JLabel.LEFT );
00174   controls_west.add( label );
00175        prob = new JSpinner( new SpinnerNumberModel(0.50,0.00,100,0.01) );
00176        SpinnerNumberModel model = (SpinnerNumberModel)prob.getModel();
00177        model.setMaximum( new Double(1.00) );
00178        controls_west.add( prob );
00179        label = new JLabel( label_speed,JLabel.LEFT );
00180        controls_west.add( label );
00181        rate = new JSpinner( new SpinnerNumberModel(1,1,10,1) );
00182        controls_west.add( rate );
00183        JPanel controls_east = new JPanel();
00184        controls_east.add( start = new JButton( start_text ) );
00185        start.setDefaultCapable( true );
00186        frame.getRootPane().setDefaultButton( start );
00187        controls_east.add( clear = new JButton( clear_text ) );
00188        controls_east.add( close = new JButton( close_text ) );
00189        close.setDefaultCapable( false );  
00190        JPanel controls = new JPanel( new BorderLayout() );
00191        controls.add( "East", controls_east );
00192        JPanel statistics_center = new JPanel( new GridLayout(2,3) );
00193        statistics_center.add( count = new JLabel( label_count ) );
00194        statistics_center.add( mean = new JLabel( label_mean ) );
00195        statistics_center.add( variance = new JLabel( label_variance ) );
00196        statistics_center.add( current_bin = new JLabel( label_bin ) );
00197        statistics_center.add( current_bin_count = new JLabel(label_bin_count ) );
00198        statistics_center.add( current_bin_prob = new JLabel( label_bin_probability ) );
00199        JPanel statistics_south = new JPanel( new GridLayout(1,1) );
00200        statistics_south.add( confidence = new JLabel( label_confidence ) );
00201        JPanel statistics = new JPanel( new BorderLayout() );
00202        if(wims_exercise == false){
00203            // hmmm...these controls are handed over to applet params...if applet is part of an interactive exercise.
00204            controls.add( "West", controls_west );
00205            controls.setBorder( BorderFactory.createTitledBorder( controls.getBorder(), label_controls, TitledBorder.LEFT, TitledBorder.TOP, new Font( "Helvetica", Font.BOLD, font_size) ) );
00206        }
00207        statistics.add( "Center", statistics_center );
00208        statistics.add( "South", statistics_south );
00209        statistics.setBorder( BorderFactory.createTitledBorder( statistics.getBorder(), label_statistics, TitledBorder.LEFT, TitledBorder.TOP, new Font( "Helvetica", Font.BOLD, font_size) ) );
00210 
00211        JPanel south = new JPanel( new BorderLayout() );
00212        south.add( "Center", statistics );
00213        JLabel statusbar = new JLabel("");
00214        statusbar.setPreferredSize( new Dimension(15,15) );
00215        south.add( "South", statusbar );
00216        Container contentpane = frame.getContentPane();
00217        contentpane.setLayout(new BorderLayout(10,10));
00218        contentpane.add( "Center", plinkoboard = new PlinkoBoard(this) ); 
00219        contentpane.add( "North", controls );
00220        contentpane.add( "South", south );
00221        start.addActionListener( this );
00222        clear.addActionListener( this );
00223        close.addActionListener( this );
00224        bins.addChangeListener( this );
00225        rate.addChangeListener( this );
00226        prob.addChangeListener( this );
00227 
00228        plinkoboard.requestFocus();
00229        frame.addWindowListener( this );
00230        getContentPane().add( splash = new SplashPanel( this ) );
00231        click = getAudioClip( getCodeBase(), "sounds/click.au" );
00232        ping = getAudioClip( getCodeBase(), "sounds/ping.au" );
00233     }
00234 
00235        // jm.evers: returns a string to a javascript function ReadApplet(); [must be a string, and not an array ... IE trouble]
00236        // the string will be send -along with the "student reply" to the questions at hand- to the WIMS server; 
00237        public String ReadApplet(){
00238            String s=plinkoboard.ReadData();
00239            return s;                                                                                                                      
00240         }                                                                                                                                  
00241 
00242        public void showFrame(){
00243               frame.setVisible( true );
00244        }
00245 
00246        public void stop(){
00247               frame.setVisible( false );
00248               splash.start();
00249        }
00250 
00251        public void run(){          
00252            //while ( active  || countdown > 0 ){
00253            while ( active ){
00254               plinkoboard.dropBall( false );
00255               //if ( countdown > 0 ) countdown--;
00256               try {
00257               //if ( countdown < 1)
00258                   Thread.sleep(105 - 10*((Integer)rate.getValue()).intValue());
00259               } catch (InterruptedException e){}
00260            }
00261        }
00262        
00263        public void toggleStart(){
00264            if ( active ){
00265               active = false;
00266               start.setText(start_text);
00267            } 
00268            else 
00269            {
00270               active = true;
00271               bins.setEnabled( false );
00272               thread = new Thread(this);
00273               thread.start();
00274               start.setText(stop_text);
00275            }
00276        }
00277 
00278        public void actionPerformed( ActionEvent ae ){
00279            Object obj = ae.getSource();
00280            if (obj == start){toggleStart();plinkoboard.requestFocus();}
00281            if( obj == clear ){
00282               if ( plinkoboard.FIRST_BALL == null ){plinkoboard.newHist();} 
00283               else 
00284               {
00285                   plinkoboard.FIRST_BALL = null;
00286                   //plinkoboard.BALL_COUNT = 0;
00287                   if ( !active ){bins.setEnabled( true );}
00288               }
00289                   plinkoboard.repaint();plinkoboard.requestFocus();
00290            }
00291            if(obj == close){stop();}
00292        }
00293               
00294        public void stateChanged( ChangeEvent ce ){
00295            Object obj = ce.getSource();
00296            if ( obj == bins ){
00297               count.setText( "0" );
00298               plinkoboard.setup( );
00299               plinkoboard.newHist( );
00300               plinkoboard.drawBackground();
00301            } else if ( obj == rate ){
00302            } else if ( obj == prob ){
00303               plinkoboard.updatePercent();
00304               plinkoboard.repaint();
00305            }
00306        }
00307 
00308        public void windowActivated( WindowEvent we ){
00309            //splash.start();
00310        }
00311        
00312 
00313        public void windowClosed( WindowEvent we ){
00314               //splash.start();
00315        }
00316 
00317        public void windowClosing( WindowEvent we ){
00318               //splash.start();
00319        }
00320 
00321        public void windowDeactivated( WindowEvent we ){
00322               //splash.start();
00323        }
00324 
00325        public void windowDeiconified( WindowEvent we ){}
00326 
00327        public void windowIconified( WindowEvent we ){}
00328 
00329        public void windowOpened( WindowEvent we ){}
00330        
00331        public String getAppletInfo(){ 
00332            return "Written by Dr. P. Little \n http://www.math.psu.edu/dlittle/java/probability/plinko/index.html" ; 
00333        }  
00334 
00335 /*            
00336        public void keyTyped( KeyEvent ke ){
00337            int code = ke.getKeyCode();
00338            char key = ke.getKeyChar();
00339 
00340            if ( key >= '0' && key <= '4' ){
00341               int x = key - '0';
00342               int n = 1;
00343               for (int i=0; i<x; i++ ){
00344                   n *= 10;
00345               }
00346               for (int i=0; i<n; i++){
00347                   plinkoboard.dropBall();
00348               }
00349            } else if ( key == ' ' ){
00350                      toggleStart();
00351            } else if (key==20){ //control t - toggle erasing
00352                      plinkoboard.toggleErase();
00353            } else if (key==3){ //control c - clear screen
00354                      plinkoboard.newHist();
00355            } else if (key==24){ //control x - kill all threads and clear screen
00356                      plinkoboard.kill();
00357            } else if (key==16){ //control p - toggle displaying probabilities
00358                      //plinkoboard.toggleProbabilities();
00359            }
00360        }
00361 
00362        public void keyPressed( KeyEvent ke ){
00363        }
00364        
00365        public void keyReleased( KeyEvent ke ){
00366        }*/
00367           
00368 }
00369