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

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

Exercise 0, The Welcome Applet

Content

The Java Applet WelcomeApplet

Modify, Compile and Run

(When I was younger, there were debates between the nerds of the time about numbering of elements in arrays. Should the first index be 0 or 1. Java has 0 numbering. So now you know! And if you really understand the implications you will avoid a large number of annoying programming errors. The discussion is introduced here because we decided to call our fist exercise, Exercise 0).

As this is written, it is not really clear who all will be reading this text. We hope that those seasoned Java programmers who know all this will skim over what they already know and appreciate that some readers may not have their extensive knowledge but may be willing to learn.

The main purpose of this exercise is to have you enter and compile source code. The bad news is that you have to do some things on the command level, that is in the way things were done before the time of simply point and click. However, as we shall see, you will be helped by command scripts, about which you can read more in How to do the Exercises. The good news is that if you master this exercise nothing will stop you from doing Java with inexpensive (actually free for the download) tools handily available to anyone with a modern computer running Windows, Linux or Mac OSX.

The Java Applet WelcomeApplet

Follow this Link for the Welcome Applet Source . You see in the source code that the class introduced in the source code is called WelcomeApplet. The source code must accordingly (See LectureNotes/General Program Structure) be stored in a file called WelcomeApplet.java. You could copy the source between ">" and ">" to a blank editor document which you would save with the filename WelcomeApplet.java in the folder named MyEX0.If you have the RART CD, you already have the file in MyEX0.

To compile the Java Source you have to add the CompAnyJava script to your folder MyEX0. You can do this in either of two ways.

If you have the folder called "Platform Specific Scripts", you should find the file with the script which supports your platform in the Platform Specific Scripts folder. You can simply copy the file from the Platform Specific Scripts folder into MyEX0 folder..

If you only have access to the course on the Internet, you can find the text of the script in the Platform Dependent Specifics document. You can copy the text of the script into a new edit document that you save into MyEX0 with the name CompAnySource and the appropriate extension.

To compile the applet in MyEX0, doubleclick the CompAySource script.. If all is well, the compiler produces a file with executable byte code named WelcomeApplet.class in the MyEX0 folder.

To start up and run the Applet, you go by way of an HTML file which is named GoWelcome.html. In this HTML file is a snippet of text, the applet tag, which looks as follows:

"<APPLET CODE="WelcomeApplet.class" WIDTH="200" HEIGHT="200" ALIGN="BOTTOM">
<PARAM NAME="greeting" VALUE="Welcome to Java!">
</APPLET>
"

We can for the time being ignore most of the entries in the Applet tag. However, the sequence <PARAM NAME="greeting" VALUE="Welcome to Java!"> is of some interest. There is a parameter with the name "Greeting" which has been given the value "Welcome to Java". It shows one way in which the HTML page hosting the applet can provide input to the applet by way of a parameter. You could change the text to, for example, "Välkommen till Java" if you anticipated a swedish speaking observer of your applet! And the applet would look different without having to be compiled again.

To see the applet doing its Internet thing, you should also be connected to the Internet and you need a browser.When you start your better browser, i.e. double click on the startup HTML document, the file called GoWelcome.html in the MyEX0 folder, the browser (and it could be Netscape Navigator 3.0 or Internet Explorer 4.0 or AOL 5.0, or any of the later releases) will actually start up its Java interpreter and execute the applet. If you press the button with the name JanAminoff, your browser may call upon your email program so you can send me an e-mail. If you also are connected to the Internet, the RART button will take you to the Rart web site (which I would be pleased if you took some time to explore).

The JDK comes with a program called the Applet Viewer (actually appletviever, all lower case) which ignores most of any HTML document but zooms in on any applet tag. When you are in a development mode, you can test your applet using the Applet viewer. The AppletViewer will show the applet, but pressing the buttons will have no effect, since the appletviewer concentrates on the internal java aspects of the applet and ignores anything but the applet tag. However, one thing you can do in the appletviewer is to change the size of the applet, ie the size in pixels given by WIDTH and HEIGHT. You do this by simply dragging the outline. You will note that the applet centers the text sideways and spaces it evenly vertically. That is to say the programmer of the welcome applet has anticipated the use of the applet in varying sizes.

To execute the applet in the Applet Viewer, you have to add the RunAnyApplet script to your folder MyEX0. You can do this in either of two ways.

If you have the folder called "Platform Specific Scripts", you should find the file with the script which supports your platform in the Platform Specific Scripts folder. You can simply copy the file from the Platform Specific Scripts folder into MyEX0 folder..

If you only have access to the course on the Internet, you can find the text of the script in the Platform Dependent Specifics document. You can copy the text of the script into a new edit document that you save into MyEX0 with the name RunAnyApplet and the appropriate extension.

Doubleclicking the RunAnyApplet script will open a command prompt window and execute the applet.

TOP


Modify, Compile and Run

1.Modify In order to become familiar with the compile and run cycle for an applet you should try to modify the source (WelcomeApplet.java) that you keep in MyEX0 folder the editor for your platform. One way to make it useful to you is to enter the URL for a web site that is relevant to you for the first button (Note that the name of the button appears in two places) and your own email address for the second button as "mailto: ". You can also change the greeting by changing the string provided as a parameter in the HTML document.

2. Compile Compile it by executing the CompileAnyJava script.

3. Run Try both Netscape and Explorer (Looks the same?) and in AOL 5.0 or better. You can also run it in the Applet Viewer by executing the RunAnyApplet script.

TOP

Back to Index Introduction to Java