Photo web hosting - Chapter 6 Methods 203 The Windows application of
Chapter 6 Methods 203 The Windows application of Fig. 6.10 simulates rolls of four dice. The program enables the user to click a button that rolls four dice at a time and displays an image of each die in the window. The next example (Fig. 6.11) uses many of this example s features to demonstrate that the numbers generated by Next occur with approximately equal likelihood. 1 // Fig. 6.10: RollDie.cs 2 // Using random number generation to simulate dice rolling. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 using System.IO; // enables reading data from files 10 11 public class RollDie : System.Windows.Forms.Form 12 { 13 private System.Windows.Forms.Button rollButton; 14 15 private System.Windows.Forms.Label dieLabel2; 16 private System.Windows.Forms.Label dieLabel1; 17 private System.Windows.Forms.Label dieLabel3; 18 private System.Windows.Forms.Label dieLabel4; 19 20 private Random randomNumber = new Random(); 21 22 // Visual Studio .NET generated code 23 24 // method called when rollButton clicked, 25 // passes labels to another method 26 protected void rollButton_Click( 27 object sender, System.EventArgs e ) 28 { 29 // pass the labels to a method that will 30 // randomly assign a face to each die 31 DisplayDie( dieLabel1 ); 32 DisplayDie( dieLabel2 ); 33 DisplayDie( dieLabel3 ); 34 DisplayDie( dieLabel4 ); 35 36 } // end rollButton_Click 37 38 // determines image to be displayed by current die 39 public void DisplayDie( Label dieLabel ) 40 { 41 int face = randomNumber.Next( 1, 7 ); 42 43 // displays image specified by filename 44 dieLabel.Image = Image.FromFile( 45 Directory.GetCurrentDirectory() + 46 “\images\die” + face + “.gif” ); 47 } Fig. 6.10 Rolling dice in a Windows application (Part 1 of 2.).
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.