Introduction to Java. A tutorial from A-SQUARE, Inc. January 2003

© 2000-2003 A-Square, Inc. Cambridge, MA

Exercise 2, The Banner2 Applet

Compile and Run the Java Applet Banner2a

Compile and Run the Java Applet Banner2b

A Note on Security and the Java Console

This exercise introduces you to object oriented programming by showing how you can generalize code and thereby making it both easy to modify and reuse. The first example is Banner2a, functionally and visually equivalent to Banner1 from Exercise 1. See the applet execute. Click on the link Welcome to Introductory Java (2a).

Most of the detail of the Banner2a applet can be understood after viewing the commented code. The code of the Banner1 applet has actually been broken up into three classes. Banner2a.java, Logo.java and ASquareLogo.java. There are some aspects that we decided were noteworthy enough that we typed them up in a companion document: Comments to the Code of Banner2

The Banner2b applet is very similar to the Banner2a applet. The main difference comes from the fact that in Banner2a we draw the logo. in Banner2b, we import a picture, the IEEE logo which is given as a gif-file.

Compile and Run the Java Applet Banner2a

Step 1. Enter or Edit Source Code

Open the folder MyEX2a. It should contain three source files and the startup file GoBanner2a.html.You can see the source by opening Banner2a source . (Note: There are three different source files, Banner2a.java, Logo.java,and ASquareLogo.java ). This step is the simplified equivalent of typing in all the original source.

Step 2. Compile the Source Code

To compile the Java Source you have to add the CompAnyJava script to your folder MyEX2a. Find the file with the script which supports your platform in the Platform Specific Scripts folder or, the text of the script, in in the Platform Dependent Specifics document.

Copy the file of the appropriate CompAnyJava script into into the MyEX2a folder. Doubleclick the script. The compilation should proceed.

Errors, if any, are listed with line number and a brief explanation. Correct the source and repeat Step 2 until the three new compiled classes appear in the MyEX2a folder. The classes are Banner2a.class, Logo.class and ASquareLogo.class.

Step 3. Run the Banner2a applet

The first method using your favorite browser: Open GoBanner2a.html in the browser and the applet will execute.

The second method using Applet Viewer. To execute the applet, you have to add the RunAnyApplet script to your folder MyEX2a. Find the file with the script which supports your platform in the Platform Specific Scripts folder or, the text of the script, in in the Platform Dependent Specifics document. Copy and paste the file or handle the text. Doubleclick the script. The execution should proceed.

The following are the 3 steps above with a global change of 2a to 2b. Do you start to detect a pattern?

Top


Compile and Run the Java Applet Banner2b

Step 1. Enter or Edit Source Code

Open the folder MyEX2b. It should contain three source files and the startup file GoBanner2b.html.You can see the source by opening Banner2b source . (Note: There are three different source files, Banner2b.java, Logo.java,and IEEELogo.java ). This step is the simplified equivalent of typing in all the original source. The folder also should contain a gif-file, num3blu.gif.. A gif-file is a picture you can import to your applet under some circumstances. In Banner2b, rather than drawing a picture, we import a picture.

Step 2. Compile the Source Code

To compile the Java Source you have to add the CompAnyJava script to your folder MyEX2b. Find the file with the script which supports your platform in the Platform Specific Scripts folder or, the text of the script, in in the Platform Dependent Specifics document.

Copy the file of the appropriate CompAnyJava script into into the MyEX2b folder. Doubleclick the script. The compilation should proceed.

Errors, if any, are listed with line number and a brief explanation. Correct the source and repeat Step 4 until the three new compiled classes appear in the MyEX2b folder. The classes are Banner2b.class, Logo.class and IEEELogo.class.

Step 5. Run the Banner2b applet

The first method using your favorite browser: Open GoBanner2b.html in the browser and the applet will execute. Well - - - see below!

The second method using Applet Viewer. To execute the applet, you have to add the RunAnyApplet script to your folder MyEX2b. Find the file with the script which supports your platform in the Platform Specific Scripts folder or, the text of the script, in in the Platform Dependent Specifics document. Copy and paste the file or handle the text. Doubleclick the script. The execution should proceed.

Top


A Note on Security and the Java Console

In 2b, when you try to run the applet in a browser, you have a problem. The page opens, the surrounding text appears, but no animation. And, sorry to say, we have encountered a security stop to our attempt to load the IEEE image from a gif - file. It works in the AppletViewer because you are supposed to know what you are doing, developing code, and it is on your own computer anyway. But no browser will (in general) allow an applet to load any file from any file system. And we can actually load a gif-file if it in some sense is part of the applet. Here we attempted to load the resource from a component rather than from the applet.We can in the more generalized execution environment provided for a Rart universe load resources such as gif-files. Details are provided in Exercise 3d.

To actually see what is going on you should get to the Java console, a window offering status- and error messages during execution. If you execute the applet in the Applet Viewer, the console is the window that opens when you execute the script RunAnyApplet. In the various browsers you may also open the Java console. For example, in Internet Explorer version 5, check under the View menu. At the bottom you may have an entry Java Console. If you check this entry, a window will appear with a listing which may look confusing at first. Soon, however, you will learn that the most interesting thing is listed at the top. It will say in the case of the Banner2b applet:

com.ms.security.SecurityExceptionEx[Logo.getRartImage]: cannot access file num3blu.gif

This is the clue that security considerations has stopped the execution of the applet. If you use Netscape Navigator 4.5, you go under the Communicator menu to Tools where the Java Console is listed. However, AOL, which, when this is written, uses Internet Explorer built into its customized browser, does not offer any access to the Java Cosole.


Top


Back to Index Introduction to Java