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