Java and ActiveX Made Easy with Visual J++

Experience the simple power of VJ++, and learn how to create a Java applet using the VJ++ Dava Applet Wizard.

Dennis Martin

January 31, 1997

6 Min Read
ITPro Today logo


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

VJ++ lets you quickly build applets on NT 4.0 with the powerful andconsistent 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 withVC++, from creating a Windows-like GUI for a Java Internet-centric applicationto incorporating ActiveX Controls into a Java applet. In this article, I'llexplain how to create a Java applet using the VJ++ Java Applet Wizard. Theresulting Java applet demonstrates the simple power of VJ++.

Java Applet Wizardry
The VJ++ Java Applet Wizard walks you through a set of option pages tocreate the classes and code for a Java applet and provides a sample HTML filefor browsing the applet. The framework for the Wizard-created applet supportsmultithreading, image animation, mouse event handling, and parameters read fromthe HTML. After the Wizard is finished and you compile the applet, you can testit with Internet Explorer (IE) 3.0 or later, with other browsers that supportJava, or with VJ++'s standalone interpreter, jview.exe. In the current versionof VJ++, IE is the only browser you can use for debugging. (Before you caninstall VJ++ from http://www.
microsoft.com/visualj, you must install IE3.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 Helpsystem). To begin the Java Applet Wizard, select the File menu and click New.Double-click the Project Workspace option to display the New Project Workspacedialog box, as shown in Screen 1 on page 120. In the Type list, select JavaApplet Wizard. Enter a name for your project in the Name text box (I usedboingi, short for "boing image") and click Create. The Java AppletWizard - Step 1 of 5 dialog box appears, as in Screen 2. Select As an appletonly for how you want to run your program. (If you plan to use jview.exe totest 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 inScreen 3. Selecting Yes, please to Would you like a sample HTMLfile? 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 AppletWizard - Step 3 of 5 dialog box appears. Our example includes animation, so keepthe default option, Yes, please, for both multithreading and animationsupport, as shown in Screen 4. Click Next, and the Java Applet Wizard - Step 4of 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, asin Screen 6. This dialog box shows the information the applet will return to thebrowser. Click Finish, and the New Project Information dialog box displays asummary of specifications for the project. Click OK to complete the Wizard andreturn 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 pressF5). If you are asked to specify the path to a browser, I recommend you use IE3.0 or later, but the applet will run properly with Netscape and other browsersthat support Java. The applet the Wizard will create animates a series of .giffiles as a spinning globe.

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

Next, at the end of the public void init() method (in Listing 3), commentout (// signals a comment line) the call to resize() and save the range of theapplet 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 voiddisplayImage(Graphics g) method. Modify the code to appear as in Listing 4, andyou'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 positionto display the next image. The Wizard-generated code (commented out in Listing4) displays the image in the center of the applet; the new code moves the imageon each call to the displayImage() method.

The travelImage() method saves the previous image coordinates and calls thecheckForTurnaround() method to calculate a new vector (m_incX, m_incY) when theimage is about to hit the applet's boundary. The checkForTurnaround() methodchecks whether the image is out-of-bounds; if so, it calculates a new speed inthe 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 calledboingi.class.

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

Run the extended project by choosing Execute from the Build menu or bypressing F5. IE is the default application, but you can change the default bychoosing Settings from the Build menu and selecting the Debug tab. In theBroswer 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) forupdates and more information about VJ++. These resources can help you take whatyou 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 easilyargue that NT, which originally stood for New Technology, now stands for NetTechnology. VJ++, the combination of Java and ActiveX on NT, will likely be tothe late '90s what C++ was to the late '80s.

Visual J++

Microsoft * 206-882-8080

Web: http://www.microsoft.com

Price: $99

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like