Search   |  About Us  |  Contact Us   |  Portfolio 

Leading your developers forward







Home

Programming

Web 
Development

E-Commerce

Web Services

Consulting

Mentoring

Training

Books &
Writing

 

 

 

Dieroll Java Applet Sample

This page is similar to our Dieroll ActiveX Control Sample page except the dice are Java applets instead of ActiveX controls. Because it is a Java applet it does not need the NCompass Labs' ScriptActive plug-in to run under Netscape like the ActiveX page. Nor do you need to have or download the DLL's. This saves a lot of time on the download:

You must have a browser capable of running Java applets and JavaScript such as Netscape 3.0 or higher and Microsoft Internet Explorer 3.0 or higher. Make sure Java support is enabled:

  • In Netscape 3.0, choose Options, Network Preferences, then choose the Languages tab and make sure both Java and JavaScript are selected.

  • In Explorer, choose View Options, then choose the Security tab. At the bottom of the page make sure that both Run ActiveX Scripts and Enable Java Programs are selected.

    # Rolls

    Total

    Average

    2 3 4 5 6 7 8 9 10 11 12

     

There are four main differences between the Java applet and the ActiveX control.

  1. Java applets cannot fire events, so when a die is clicked it rolls itself but it cannot notify the Javascript to roll the other die and update the fields. The Roll button gets around this. In Netscape, ActiveX controls wrapped in the plugin could not fire events either. We worked around that using the HTML Layout control. That would not help this situation, because it is the applets themselves that cannot fire the events.

  2. Properties of the ActiveX control could be accessed directly (for example, x=Dieroll1.Number) and could be changed directly as well. The control was notified when properties were changed. For the applet, this is not possible, but you can access Get and Set functions, for example x=Dieroll1.GetNumber() or Dieroll1.SetDots(true).

  3. The image in the ActiveX control had to be a bitmap, unless you were prepared to rewrite the code that dealt with the image and then rebuild the control. The image for this applet can be a GIF, a JPEG, or any other image format that Java's Image class can handle. It can't be a bitmap.

  4. The scaling is calculated differently. The two versions of the dieroll look about the same, but the ActiveX has a size of 100x100, and the applet is sized at 40x40.

Here is the source for the Dieroll applet. The JavaScript for this page is almost identical to that for the similar ActiveX page, so you can read the explanation of the script on that page to understand this one. Use View Source to examine the JavaScript yourself. There are five applet properties that can be set with a PARAM tag:

  • Image -- a URL

  • BackColor -- as hex #RRGGBB or a decimal number (for example, #0000FF is equivalent to 255: both mean bright blue.)

  • ForeColor -- as hex #RRGGBB or a decimal number

  • Dots -- true or false

  • Seed -- a long integer (64 bits)

Seed is a new parameter. We seed the random number with the time, but noticed in this Java version that when we had two dice on the same page, they always started with the same value, and often would stay in sync, rolling the same number as each other over and over again. We assume that the granularity of the time is coarser in Java than in C++. As a fix, we added this Seed parameter: after the random number generator has been seeded with the time (this is done automatically in Java) we multiply the first random number by the seed and use the result as the seed. As long as each die has a different seed, this will ensure that the two of them use a different seed from each other and still use a different seed each time the page is loaded.

m_RandomNumbers.setSeed(m_RandomNumbers.nextLong() * 
Long.parseLong(param));

Each parameter (except Seed) has a Get and Set function, for example GetDots() and SetDots(), and in addition there is a GetNumber() function.

If all this has you thinking about Java applets vs ActiveX controls, us too. We've summarized the issues on another page.


Copyright Gregory Consulting Limited 1996-2005. All rights reserved.