Putting the Die in Its Place
The program can now handle drawing a single die frame onto a PaintPanel control, but it hasn’t decided yet where to draw it or how to select which frame to draw. The initial location of the die is selected in the Form_Load event, as shown in Listing 1-11.Listing 1.11: Choosing the Initial Die Location
oRand = New Random(Now.Ticks Mod 100)
<code deleted>
diexPos = oRand.Next(0, pnLower.Width - w)
dieyPos = oRand.Next(0, pnLower.Height - h)
DrawDie()
Here, the event uses the variable named oRand. The variables diexPos and dieyPos are initialized to some random number between 0 and the width/height of the panel less the width/height of a die frame (stored in the constants w and h, respectively). If the width/height of a die frame isn’t subtracted, the die could easily be plopped partially off the right/bottom edge of the panel, as shown in Figure 1-13.

Figure 1-13: This shows why the range for placement of the die is the width of the panel less the width of the die frame. Ditto for the height.