Back to index

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