Free web hosting with ftp - 204 Methods Chapter 6 48 49 // main

204 Methods Chapter 6 48 49 // main entry point for application 50 [STAThread] 51 static void Main() 52 { 53 Application.Run( new RollDie() ); 54 } 55 56 } // end class RollDie Fig. 6.10 Rolling dice in a Windows application (Part 2 of 2.). Method DisplayDie (lines 39 47) invokes Random method Next to simulate a roll of a die (line 41) and loads an image that corresponds to the value rolled (lines 44 46). Line 44 uses class Label s Imageproperty (introduced in Chapter 2) to display the die. Notice that we specify which image will be displayed by invoking method FromFile of class Image, which specifies the location of the file on disk that contains the image. Each click of the button displays four images that represent the four new values of the dice. Note that the user must click rollButton at least once to display the dice. Directory method GetCurrentDirectory (line 45) returns the path of the folder in which the program is executing. If you run the program from Visual Studio .NET, this will be the binDebug directory in the project s directory. The die images must be in this folder for the example to operate properly. These images are placed in the proper folders on the CD that accompanies this book. To show that class Random produces numbers with approximately equal likelihood, let us modify the program in Fig. 6.10 to keep some simple statistics. The Windows application of Fig. 6.11 provides a Roll button for rolling the dice and a TextBox that displays the frequencies for each value rolled. The program output shows the results of clicking Roll 10 times. When the user clicks the Roll button, the program invokes the rollButton_Click event handler on lines 38 73. This event handler invokes method DisplayDiefor each of the 12 dice that the program simulates (lines 43 54). Lines 56 71 then calculate the frequencies for each die and displays the results by appending the information to displayTextBox s Text property. Method displayDie(lines 76 113) simulates a die roll (line 78), loads the appropriate Imageand increments the frequency count for the rolled value.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Leave a Reply