Downloads
wygant.zip

Not long ago, Sun Microsystems introduced us to Java, a platform-independent, object-oriented programming language well-suited to Web application development. Shortly thereafter, Microsoft countered with ActiveX, a component-oriented application development solution for Windows NT 4.0 and Windows 95. Now Microsoft gives us Visual J++ (VJ++), a combination of ActiveX and Java for Windows NT 4.0 and Windows 95.

VJ++ lets you quickly build applets on NT 4.0 with the powerful and consistent IDE of Microsoft's Developer Studio--the same IDE that Visual C++ (VC++) 4.x products use. With VJ++, you can do almost everything you can do with VC++, from creating a Windows-like GUI for a Java Internet-centric application to incorporating ActiveX Controls into a Java applet. In this article, I'll explain how to create a Java applet using the VJ++ Java Applet Wizard. The resulting Java applet demonstrates the simple power of VJ++.

Java Applet Wizardry
The VJ++ Java Applet Wizard walks you through a set of option pages to create the classes and code for a Java applet and provides a sample HTML file for browsing the applet. The framework for the Wizard-created applet supports multithreading, image animation, mouse event handling, and parameters read from the HTML. After the Wizard is finished and you compile the applet, you can test it with Internet Explorer (IE) 3.0 or later, with other browsers that support Java, or with VJ++'s standalone interpreter, jview.exe. In the current version of VJ++, IE is the only browser you can use for debugging. (Before you can install VJ++ from http://www.
microsoft.com/visualj, you must install IE 3.0 from http://www.microsoft.com/ie.)

When you first run VJ++, you will see menu bars typical of those in VC++ and a project workspace window with InfoView (a powerful integrated Help system). To begin the Java Applet Wizard, select the File menu and click New. Double-click the Project Workspace option to display the New Project Workspace dialog box, as shown in Screen 1 on page 120. In the Type list, select Java Applet Wizard. Enter a name for your project in the Name text box (I used boingi, short for "boing image") and click Create. The Java Applet Wizard - Step 1 of 5 dialog box appears, as in Screen 2. Select As an applet only for how you want to run your program. (If you plan to use jview.exe to test your applet, you must choose As an applet and as an application.) Click Next, and the Java Applet Wizard - Step 2 of 5 dialog box appears, as in Screen 3. Selecting Yes, please to Would you like a sample HTML file? creates the HTML file for debugging. The initial size fields (Width, Height) let you change the size of the applet. Click Next, and the Java Applet Wizard - Step 3 of 5 dialog box appears. Our example includes animation, so keep the default option, Yes, please, for both multithreading and animation support, as shown in Screen 4. Click Next, and the Java Applet Wizard - Step 4 of 5 dialog box appears, as in Screen 5. Enter the applet parameters from Table 1 on this screen.

Click Next, and the Java Applet Wizard - Step 5 of 5 dialog box appears, as in Screen 6. This dialog box shows the information the applet will return to the browser. Click Finish, and the New Project Information dialog box displays a summary of specifications for the project. Click OK to complete the Wizard and return to the main VJ++ window.

From the Build menu, select Build boingi to build the project. To run it, select Debug from the Build menu and Go from the Debug submenu (or simply press F5). If you are asked to specify the path to a browser, I recommend you use IE 3.0 or later, but the applet will run properly with Netscape and other browsers that support Java. The applet the Wizard will create animates a series of .gif files as a spinning globe.

Applet with Attitude
The spinning globe example the Wizard creates is OK, but let's extend the example by bouncing the globe around, changing its direction and speed whenever it hits the applet's boundary. Let's jump right into the code by opening the boingi.java file (part of which is in Listing 1) that the Wizard generated. At callout A in Listing 1, add the instance data shown in Listing 2. The program uses this instance data to keep track of the previous position and to calculate the next position of the image as it moves across the applet.

Next, at the end of the public void init() method (in Listing 3), comment out (// signals a comment line) the call to resize() and save the range of the applet in m_rng, as follows:

// resize(320,240); // save the applet's range
m_rng = size();

Now move down two more methods in the boingi.java file to the private void displayImage(Graphics g) method. Modify the code to appear as in Listing 4, and you're ready to compile and test the new Java code.

The displayImage() method in Listing 4 first clears the previous image. Then the method displays a new image at the current position (m_nImgX, m_nImgY). Finally, the method calls the travelImage() method to calculate a new position to display the next image. The Wizard-generated code (commented out in Listing 4) displays the image in the center of the applet; the new code moves the image on each call to the displayImage() method.

The travelImage() method saves the previous image coordinates and calls the checkForTurnaround() method to calculate a new vector (m_incX, m_incY) when the image is about to hit the applet's boundary. The checkForTurnaround() method checks whether the image is out-of-bounds; if so, it calculates a new speed in the opposite direction.

Surf the Java
To compile the extended project, select Build boingi from the Build menu. This command processes the revised boingi.java file and generates a file called boingi.class.

The HTML file that the Wizard creates is called boingi.html. The applet tag in this HTML is in Listing 5. The line code=boingi.class specifies the name of the boingi class file that is the compiled applet. The two parameters minSpeed and maxSpeed specify to the boingi applet the minimum and maximum pixels to move.

Run the extended project by choosing Execute from the Build menu or by pressing F5. IE is the default application, but you can change the default by choosing Settings from the Build menu and selecting the Debug tab. In the Broswer field, enter the path to your favorite browser.

Where to Now?
Where do you go from here? Investigate the Test Drive topics in the VJ++ InfoView to learn more about the Java capabilities that VJ++ lets you tap. Also, visit the VJ++ site at Microsoft (http://
www.microsoft.com/visualj) for updates and more information about VJ++. These resources can help you take what you have learned and quickly develop Internet-centric NT 4.0 applets.

With the addition of VJ++ (and ActiveX) to Windows NT 4.0, you can easily argue that NT, which originally stood for New Technology, now stands for Net Technology. VJ++, the combination of Java and ActiveX on NT, will likely be to the late '90s what C++ was to the late '80s.

Visual J++
Microsoft * 206-882-8080
Web: http://www.microsoft.com
Price: $99