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

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

Exercise 1, the Banner1 Applet

This exercise introduces you to animations in a releatively simple applet. It demonstrates the main components of successful animations including double buffering and simple user/applet interaction. It also introduces some of the main elements of Java syntax. See the applet execute by clicking on the following link Welcome to Introductory Java.

Most of the detail of the Banner1 applet can be understood after viewing the commented code. However, there are some aspects that we decided were noteworthy enough that we typed them up in a companion document: Comments to the code of Banner1.java and GoBanner1.html.

Compile and Run the Java Applet Banner1

Step 0. Locate the folder MyEX1 inside the EX1 folder.

It should have in it the source WelcomeApplet.java and a startup HTML document called GoBanner1.html

Step 1. Compile the Source Banner1.java

To compile the Java Source you have to add the CompAnyJava script to your folder MyEX1. 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 MyEX1 folder. Doubleclick the script. The compilation should proceed. And the first time, the compilation should be successful and the file Banner1.class should appear in MyEX1 folder.

Step 3. Run the Banner1 applet in a web browser

To start up and run the Applet, you do not invoke the Java interpreter directly. Instead you go by way of the HTML startup file, called GoBanner1.html which is in MyEX1 folder.

Open your browser and navigate to MyEX1 directory where you open the GoBanner1.html file. Or, if your favourite browser is your default browser, doubleclick on the GoBanner1.html file (which if, for example, the Internet Explorer is your default browser, is marked by the blue "e" icon) and the applet will execute.

Step 4. Run the Banner1 applet using the appletviewer

To run the applet in the appletviewer, you have to add the RunAnyApplet script to your folder MyEX1. 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 RunAnyApplet script into into the MyEX1 folder. Doubleclick the script. The execution should proceed and you should see the applet execute.

Note the differences with execution in the browser. In the appletviewer, the applet as described in the applet tag is shown, but the rest of the HTML is ignored. Also, you can resize the applet window by dragging the edges. However, it is not always the case that the applet is programmed to adapt to a modified window size.

Modify, Compile and Run

We now have some familiarity with the compile and run cycle and we can explore various concepts by modifiying the Java source. We assume you are working in the MyEX1 folder. If you feel you wish to continue to work while keeping a successfully completed variation of the modified source or HTML startup, we suggest you duplicate the complete folder giving it the name MyEX1a, MyEX1b etc.as you progress. And you should, of course, go through the edit - compile - and run cycle with each alternative.

a: If you force the variable gc to be null you can observe the difference between doublebuffering and non double buffering. To do this insert the statement

gc = null;

immidiately below the line catch (Exception e) { gc = null; } in the init() method.

b: The variable stime stands for the time the applet suspends itself between updates. It is now calculated as 1000/40. Modify its initial value for example by changing the line

private int stime=dtime/40;

to

private int stime = 200;

This modification would have the frame rate reduced to 5 instead of 25 frames a second.

c: Modify the GoBanner1.html document using your text editor. You can study the effect of changing various parameters in the Applet tag but you should ignore the rest of the document. You do not have to recompile Banner1.java if you only change the startup document, GoBanner1.html.

Also, to observe the effect of any change in the startup document, you only need to save the modified startup document and refresh the HTLM document by, for example hitting the Refresh button on your browsers toolbar

d: By this time, when you know about compile and run, you can experiment with changing the strings s1, s2, s3 declared following the paintBanner method. You can also experiment with different fonts, colors and placement in the invocations of printString in paintBanner or by modifying the printString method itself.

e: Finally you can lookup AWT in some reference and see how you can modify the drawAsquare method so it renders a Logo that is more meaningful to you. This is an openended exercise, the result of which can be used also in EX2 and EX3

Good Luck!

Back to Index Introduction to Java