Previous Page TOC Next Page



- 2 -
Introducing Visual J++


Today you'll learn how to Install Microsoft Visual J++ and use VJ++ to create a simple Java applet. In doing so, you will be introduced (albeit briefly) to Microsoft Developer Studio. Microsoft Developer Studio is the Integrated Development Environment (IDE) that Microsoft is standardizing on for all of its development platforms.

Installing Visual J++


The first day dealt with an overview of Java and Visual J++. From here on out we're going to get to the meat of the matter. No more simple reading, it's hands-on time! Before you can begin using Visual J++, you have to install it (what a novel concept). This section will walk you through installing Visual J++. Even if you have Visual J++ already installed, please follow along; there are some issues presented that you should be aware of.

System Requirements


Microsoft Visual J++ is specifically designed to work on Windows 95 and Windows NT 4.0 workstations. The typical requirements to run the preceding operating systems effectively are a 486DX processor with 8MB of RAM (16MB of RAM for NT). For optimal performance, we suggest you have at least a Pentium processor with 16MB of RAM (32MB of RAM for NT). Depending on the options installed, VJ++ takes between 12MB and 20MB of disk space.

Starting the Installation




If the Master Setup program doesn't start, it will be necessary for you to manually run Setup. This can be done from within Microsoft Explorer or File Manager.

Figure 2.1. VJ++ Master Setup screen.


Installation Options


Figure 2.2. VJ++ Installation Options screen.



After the previous screen, various Setup messages might appear on your screen informing you of components that need to be or are being installed. These screens are Operating System Dependent as well as Visual J++ version dependent. If any screens do appear, the best advice that we can give you is to simply "Follow your Nose."


Reinstalling Visual J++


If for some reason you need to reinstall VJ++ (for example, one of the CLASS files becomes corrupted), simply insert the CD-ROM, and follow the same instructions in the preceding list. However, you should not have to re-enter any of the information (Name, Company, Directory, and so on) that you entered the first time you ran the installation.

Uninstalling Visual J++


As with any Windows 95 compliant application , VJ++ comes with its own Uninstall application. If for some reason you need to Uninstall VJ++ (and we wholeheartedly hope you don't!), just select the Uninstall Visual J++ item from the Start menu. You can also uninstall Visual J++ by using Control Panel's Add/Remove Program. The Uninstall application will start and you can simply follow the instructions presented to you to remove VJ++. This is the cleanest way to uninstall VJ++ (or any application with an Uninstall application) because it will clean up the Registry and remove all the files associated with VJ++, not just the ones in the MSDEV directory.

Microsoft Developer Studio


This section is designed to acquaint you with using Microsoft Developer Studio. Microsoft Developer Studio is the Integrated Development Environment for Visual J++. It is also the IDE for Microsoft Visual C++ 4.0 and greater, Visual Test, and FORTRAN PowerStation. When Visual Basic 5.0 is released, Microsoft Developer Studio will be the IDE for it as well. In the future (most likely), it will be the only IDE for all Microsoft Development Products.



If you are currently using Microsoft Developer Studio with another Microsoft development product, you might want to simply browse through this section and skip ahead to the next section: Getting Started with VJ++.

If Microsoft Developer Studio is the first IDE that you have used, it is understandable that it might appear to be a bit intimidating. But do not fear: once you have a basic understanding of what all the buttons, tabs, and windows mean, you will hardly believe that any sort of development actually occurred before developers had the benefit of Microsoft Developer Studio.

Microsoft Developer Studio gives you all the tools you need to write, compile, test, debug, and refine your VJ++ programs—all in one easy-to-use interface. It includes a text editor, resource editors, an integrated debugger, an integrated compiler, and online help. Microsoft Developer Studio also has a group of default toolbars that allow you to access nearly all of the VJ++ functionality just by clicking on a button. If you are not satisfied with the functionality provided on the default toolbars, you can easily build your own toolbar using simple drag and drop techniques. If you have a subscription to Microsoft Developer Network, Microsoft Developer Studio also provides an interface to the Microsoft Developer Library.



As you go through the rest of this chapter, do not hesitate to experiment within the IDE, especially with the right mouse button: it is so loaded with functionality that it would take forever (well almost) to describe it all.


Starting Microsoft Developer Studio


If you accepted the defaults when installing Microsoft Developer Studio (and have not since changed its location), perform the following instructions. If you've installed (or moved) VisualJ++ to another location, you know much better than we do how to start VisualJ++ on your system, so please start Microsoft Developer Studio yourself.

To start Microsoft Developer Studio, select the Start menu, Programs, Microsoft Visual J++, and Microsoft Developer Studio.

The Project Workspace Window


For people new to an IDE, the Project Workspace Window will be the most welcome enhancement of all. The Project Workspace Window is the window docked to the left side of your screen. At this time, there should be a single TAB at the bottom of the window that reads InfoView. (See Figure 2.3.)

Figure 2.3. Microsoft Developer Studio main screen.

A docked window is a window that is tied to a given side of Microsoft Developer Studio. If you resize Microsoft Developer Studio, all docked windows remain docked to their side.

Navigating the Project Workspace Window


All items displayed in the Project Workspace Window are in the form of a hierarchical indexed list. If an item in the list window has any items beneath it (that is, its children) in the hierarchy, a + will be displayed to the left of its icon. Simply double-click the item, or click the +, to expand the item and see an item's children.

InfoView


When the InfoView tab is selected in the Project Workspace Window, a list of all available online materials is presented. This information includes the full VJ++ users guide, API guide, Samples, and Build Errors. You are also able to search on a given subject by clicking anywhere in the InfoView window with the right mouse button and selecting Search.

Files


Once you create a Project Workspace, selecting the Files tab will display a list of all source files that are included in your project. This will include all *.java files on which your application is dependent. You can open any of these files by double-clicking on the file or by using your right mouse button to bring up the pop-up menu and select Open.



The preceding paragraph might seem confusing because of the way Project Workspace is used. Here is some clarification. The Project Workspace Window is the window that we have been talking about that is, by default, docked to the left side of Microsoft Developer Studio. A Project Workspace, which is saved as a *.MDP file is a configuration for a given project. It specifies what the *.MAK file is, and what the current Microsoft Developer Studio screen configuration is. The .MAK file is carried over from C/C++ and specifies all files that are included in a project as well as how to compile them into their final form.


Classes


Classes are the heart of VJ++ and the Project Workspace Window displays them in the most logical of formats. (You will learn more about Classes on Day 4.) By selecting the Classes Tab, Microsoft Developer Studio will display all classes that are in your project. You can easily open the file that contains a method within one of your classes by simply double-clicking the item or by using the right mouse button to select Open.

Customizing the Workspace


One of the advantages of using an IDE is the ability to customize your workspace for the way that you work. Suppose you tend to always look to the left most side of your screen for the code that you are working on currently. At first glance, you might be annoyed that the Project Workspace Window is there. Not to worry. Microsoft has made it incredibly easy to not only move the Project Workspace Window to anywhere on the screen that you want, but they have given you the ability to customize nearly any aspect of Microsoft Developer Studio. Now, look at some of the ways you can customize your workspace.

Toolbars and Docking Windows


The two easiest and most useful things to modify in Microsoft Developer Studio are the Toolbars and the Docking window. Remember, these are just a couple of the numerous customizable parameters in Microsoft Developer Studio.

Toolbars

To modify (move, create, change) anything about a toolbar, select Tools | Customize from the Microsoft Developer Studio menu. The Customize window will appear. (See Figure 2.4.) Once this window is displayed, you can change any of the currently displayed toolbars. Try some of the following to get a feel for how customizing toolbars works.

Figure 2.4. Customize window.

Once you have played a bit, close the Customize window. Now try the following: Click and hold the mouse button down on a location in any of the toolbars. Now move your mouse into the center of the screen. The toolbar should follow and end up floating.

Docking Windows

Try the following: Resize Microsoft Developer Studio.

Assuming that the Project Workspace Window is still visible, notice how it travels with the outside edge of Microsoft Developer Studio. That informs us that the Project Workspace Window is docked. To change that docking feature, do the following: Select Tools | Options from the Microsoft Developer Studio menu. The Options Window appears. (See Figure 2.5.) Click on the Workspace tab. Experiment a bit and see what happens when you change the Docking characteristic for the Project Workspace window.

Figure 2.5. Options window.

Saving the Workspace


To save the current configuration of the workspace (toolbars, Project Workspace window, and so on), exit Microsoft Visual J++ by selecting File | Exit from the menu.

Getting Started with Microsoft Visual J++


Enough introduction to VJ++! You'll complete today by creating a VJ++ application (and an applet!), compiling them, and finally testing them. Even though you are going to build the rather simple obligatory Hello World program, it will do a good job of demonstrating the basics of working with VJ++.

Creating a New Project Workspace


Select File | New from the Microsoft Developer Studio menu. A small dialog box will then appear prompting you to select the type of file you wish to create. Select Project Workspace. The New Project Workspace Window appears. (See Figure 2.6.) At this point, do not change any of the defaults. Type in a name for your Hello World application in the Name field. Although it's not required, we suggest you use the name of the highest level class as the name for the project workspace, which for this example would be HelloWorld.

Figure 2.6. New Project Workspace window.

Creating a VJ++ Application


Open a new source file window in the Workspace. You can do this either by selecting File | New | Text File from the Microsoft Developer Studio menu or by clicking the New Source File button on the toolbar. Once the new source file window is open, type the program in Listing 2.1. Be careful to include all the parentheses, braces, and quotes.

Listing 2.1. Your first VJ++ application.




1: class HelloWorld



2: {



3:     public static void main (String args[])



4:     {



5:         System.out.println("Hello World!");



6:     }



7: }

The numbers before each line are part of the listing and not part of the program; they're there so we can refer to specific line numbers when we explain what's going on in the program. Do not include them in your own file.

This program has two main parts:



For those of you used to programming in other languages, remember that a method is almost identical to a function in other languages.

ou'll learn more about both of these parts of a Java application as the book progresses.

Once you finish typing the program, you need to save the file. Java source files are typically named the same name as the class they define, with an extension of .java. This file should therefore be called HelloWorld.java.

n most cases the name of the .java file does not particularly matter. What does matter is that the name of your class and the name of your project match exactly, both in spelling and in case. If the names do not match exactly, the results can be somewhat unpredictable. To play it safe, we recommend that you always name your .java and .class files the same (same case too), as well as the name of your project.

To save the file, select File | Save from the Microsoft Developer Studio menu or click on the Save button (looks like a floppy disk) on the button bar.

If you look in the Project Workspace window and click on the Files tab, you will notice that there are still no files defined for your HelloWorld project. This is because you have not added your recently saved source file to the project. Add that file now. Select Insert | Files into Project from the Microsoft Developer Studio menu. In the dialog that appears, select the HelloWorld list and click Add. Now, if you look in the Project Workspace Window, you will notice that there is a file listed called HelloWorld.java.

To compile your Hello World application, select Build | Build HelloWorld from the menu. If you look at the bottom of the screen, you should see the status of the build process. If there are any errors, go back to the source code (using Project Workspace Window to get to the source file) and fix the problem. Repeat this process until a message displays with no errors and warnings, like the following:




HelloWorld - 0 errors(s), 0 warning(s)

Testing Your VJ++ Application with JVIEW


Once you build your program, you'll want to see it run and also test it. To execute your program, do the following:

Select Build | Execute. The Information for Running Class dialog box displays. In the Class file name text box, type in the name of your class (HelloWorld). In the Run project under group box, select Stand-alone interpreter, and click OK. A command window will open and your application will run (depending upon how your system is configured, the window might close quite quickly or remain open until you close it yourself. You can change this behavior, using Windows 95 or NT, to suit your own preferences.)



The next time you select Build | Execute, the Execute command will have changed to read Execute yourclassname. You'll not have the option of specifying a class name or runtime environment. If you need to change any of this information, you can do so by selecting Build | Settings. In the Settings For list box, select the debug version of your program, then select the Debug tab—change the settings as need be.


Creating a VJ++ Applet


Creating applets is different from creating a simple application, because Java applets run and are displayed inside a Web page with other page elements and, as such, have special rules for how they behave. Because of these special rules for applets, in many cases (particularly the simple ones) creating an applet might be more complex than creating an application.

For example, to do a simple Hello World applet, instead of merely being able to print a message, you have to create an applet to make space for your message and then use graphics operations to paint the message to the screen.



If you were to run the Hello World application as an applet, the Hello World message would print to a special window or to a log file, depending upon how the browser has screen messages set up. It will not appear on the screen, as you might expect, unless you write your applet to put it there. Remember, applets depend upon a browser, so they need to include special instructions (code) to be able to interact with the browser.

In the next example, you create that simple HelloWorld applet, place it inside a Web page, and view the result.

Open a new source code window in Microsoft Developer Studio and enter the code from Listing 2.2.

Listing 2.2. The Hello World applet.




1: import java.awt.Graphics;



2:



3: public class HelloWorldApplet extends java.applet.Applet



4: {



5:     public void paint(Graphics g)



6:     {



7:         g.drawString("Hello world!", 5, 25);



8:     }



9:}

Save this file as HelloWorldApplet.java. Remember the case.

Are you wondering what features to note about applets? There are a couple we'd like to point out:

Now go over to the Project Workspace Window and select the File tab. Highlight the existing HelloWorld.java file and press Del to remove the file from your project. Now insert the new file into the HelloWorldApplet.java project. Compile the modified project using the same procedure as before.

Unlike VJ++ applications, applets are designed to be run from within a browser. For an applet to be run, it needs to be embedded within an HTML file. Create an HTML file for your Hello World applet now. Open a new code window in Microsoft Developer Studio and type in Listing 2.3. (Yep, same as Day 1.)

Listing 2.3. The HTML with the applet in it.




1: <HTML>



2: <HEAD>



3: <TITLE>Hello to Everyone!</TITLE>



4: </HEAD><BODY>



5: <P>My Java applet says: </P>



6: <APPLET CODE="HelloWorldApplet.class" WIDTH=150 HEIGHT=25>



7: </APPLET>



8: </BODY>



9: </HTML>

Save the code window as an HTML file with a descriptive name. (We suggest: HelloWorld.html.)

To test your new VJ++ applet, you must first confirm some settings. Select Build | Options from the menu. The Project Settings window appears (See Figure 2.7.) Select the Debug tab, making sure that the Category selected is General and that the Debug project under is set to Browser.

Figure 2.7. Project Settings window.

Set the Category to Browser and make sure that the HTML page selected is the HelloWorld.html that you created a few minutes ago. You can click OK to close this window now.

Now to test your new HelloWorld VJ++ applet! Select Build | Execute HelloWorldApplet from the Microsoft Developer Studio menu. This should start your default browser (probably Microsoft Internet Explorer or Netscape). At this point your Hello World applet should be displayed in the browser.

If your browser supports loading of HTML files from the local machine, then you could simply load your browser and load the HelloWorld.html that you created. The results should be the same.

It is important to note that you do NOT have to create your own *.HTML file in order to test your applets. By default, VJ++ will create one for you. VJ++ will create its own *.HTML when you select Execute and will name it vjxxx (where xxx is a VJ++ generated alpha-numeric sequence), for example, vj934.html. VJ++ does this each time you execute your applet, so making your own changes to the VJ++ created .html will be of no use. If you do create (or want to reuse) your own .html file, you must add it (Insert | Files into Project) to your project.

Summary


Today you were given a basic tour of Microsoft Developer Studio and of some of its features. Microsoft Developer Studio is an immensely powerful IDE that allows you, the developer, to customize your workspace however you see fit. It effectively encompasses all of the separate applications that a developer of just a few years ago would have had to use: editor, compiler, debugger, online help system and online reference materials.

You also learned how to use VJ++ and Microsoft Developer Studio to create a simple applet and a simple application. The two programs that you created today form the basis for every VJ++ program that you will write in the future.

Q&A


Q: I have been looking at Microsoft Developer Studio and I can't seem to be able to make it look the way I want. Where can I go for more help?

A: The best location to go for Microsoft Developer Studio help are the Books Online that are included with VJ++. If you did not install the Books Online, then you can do so by running the setup procedure again.

A second great reference for Microsoft Developer Studio are the Microsoft Developer Network CDs. This subscription (available from Microsoft) includes full documentation on all of their development tools and SDKs.

Q: I have previously used the SUN JDK to create some Java applications/applets and I would like to know if I can use any of their tools with VJ++.

A: To the best of our knowledge, the file JAVA.EXE that is part of Version 1.0 of the JDK works very well on VJ++ compiled applications.

Previous Page Page Top TOC Next Page