Visual J++ Environment Tutorial


This tutorial will differ slightly form the others because of the nature of the environment.   The VJ++ environment should be more familiar to you because of it's close correlation with the Visual C environment.  It contains extensive help files and documentation that most of the other languages did not. 

This tutorial is divided into the following sections:


 

Getting Started

Greg Badros' Java environment of choice is Microsoft Visual J++ 6.0.  A shortcut to the editor on all the NT machines is located at:

START -> PROGRAMS -> MICROSOFT STUDIO 6.0 -> MICROSOFT VISUAL J++ 6.0 -> MICROSOFT VISUAL J++ 6.0

Creating a new Project:

When the development environment starts up it will have a dialog box asking if you want to start a new project, and which type of application to make.  it should look something like this:

new_proj.jpg (41052 bytes)

For our first example we will be making the sample applet like the one that Greg went over in class.  To select an new applet project select the 'Web Pages' folder in the file tree on the left and then select the Applet icon in the left frame.   You will likely want to change the name and location from the default values before you click the 'Open' button to proceed.

The dialog box will disappear, leaving the mess of windows known as VJ++.  Now we want to view the actual Java code file (called something like MyFirstApplet.Java or Applet1.Java).  To do this find the 'Project Explorer' window, expand the file tree and double click on the aforementioned .Java file.  If the 'Project Explorer' window isn't visible got the the View menu and activate it.

Now the main panel on the screen should contain the Java code that Bill Gates was nice enough to generate for you.  This code allows the applet to read in HTML parameters, convert HTML style color codes and initialize the applet. 

 

Adding and Editing Code:

We want to Edit the code in the following manner

As a class variable (not in a method) add the follow line just below the comment that says "entry point for the applet"::

      private Font font;

We also want to change a few method variables to class variables so we can access them in the paint function.  To do this move the following three lines from the usePageParams() method to where you added the font declaration line above:

	
	String labelValue;
	String backgroundValue;
	String foregroundValue;

If you wish you can add the private descriptor in front of the three String variables, otherwise it defaults to protected.

To the init() method add the following line where it says "TODO: ...":

                     font = new Font("Helvetica",Font.BOLD, 48);

And finally make a new method in you class called paint.  This method overrides the default paint method for the applet class that we are extending.

	 public void paint (Graphics g) {
		g.setcolor(Color.blue);	g.fillOval(10,10,330,100);
		g.setColor(Color.red);		
		g.drawOval(10,10,330,100);	g.drawOval(9,9,332,102);
		g.drawOval(8,8,334,104);	g.drawOval(7,7,336,106);
		
		/* set the text color to the HTML param tag */
		g.setColor(stringToColor(foregroundValue));
		g.setFont(font);
		/* draw the string that has been assigned from the html param tag */
		g.drawString(labelValue, 40,75);
     	}

We also don't want to use the initForm() method provided to us, so simply highlight the code and delete it.  Now we are ready to build the project

 

Building your Project:

To build the code simply go to Built->Build or press Control+Shift+B.    This should result in errors because of our sloppy removal of the initForm() method.  Look in the Tasks List window and the errors will each appear on individual lines there, as well as be underlined in your code.  

Go through and remove all the errors, as well as any code referencing label1.   

Continue the build and fix process until you no longer have any syntax errors.

 

Editing the applets HTML code::

Now that the Java code is alright we want examine the HTML that will be calling the applet.  To do this double click on the HTML file in the project explorer window.   The HTML code should pop up with a box where the applet will go. 

We want to see the actual code that is behind the applet.  To do first you want to make sure that you are in the SOURCE view (as opposed to the Design or Quick view).   You can switch views using the tabs at the bottom of the HTML window.  Once in SOURCE view right click the applet box in the middle of the HTML window and select the 'Always View as Text' option.  The Applet code and parameters should appear in place of the applet box.

We want to change the dimensions of our applet as well as the string it should display.   The dimensions should be width=350 and height=120.

Here are my HTML and JAVA files for reference.

 

Debugging and Running your applet:

Microsoft gives you a handy debugger very similar to that in MSVC.  Once a project is built, you can start the debugger either from the Debug menu or by pressing the F10 key.  The F10 key steps through the code, but since this applet is not very complicated the debugger isn't very interesting.  Notice you can see the class variables in the window on the lower part of the screen.  If you wish to view more you can look at the call stack as well as other windows from the View menu.

If you haven't already select Debug->Go.  A Netscape or ie window should pop up running your very special applet.  Mine looks like this:

applet.jpg (20353 bytes)

You can experiment with changing the parameters in the HTML file and see how they affect the way the applet looks.

 


Using Help

Microsoft was nice enough to include a large and bloated maze of  help menus to help speed along your project development.  To use these select the menu item:

HELP -> Contents.

The web based help dialog should appear on your screen.  Since this is help for the entire Visual Studio collection there is a lot of information there that is not pertinent to us.  We can get only the VJ++ help by changing the active subset to "Visual J++".  Besides that the help is arranged just as any other Microsoft application.  If you know what your looking for you can use the index to look up a keyword, or otherwise wade through the massive hierarchy in the contents section.

There are several tutorials included in this help, as well as documentation and reference. 

NOTE: If you are working at home it's important to note that the help is part of MSDN and not VJ++.  This is very annoying when you just install VJ++ and have no documentation.

Enjoy.


On-line Java Class Reference

You will often need to look up information about the various classes and method.   This is readily available on line from SUN at

http://www.javasoft.com/products/jdk/1.1/docs/index.html. Most important is the documentation for the standard classes.

There is also a wealth of other Java information on the Web.  A good place to start is from the 341 Java page.

Use this material.  It will make your life much easier.

 


Back to the 341 Page