Anyone for Java?

If you don't see a title in changing rainbow colors at the top of this page, your browser does not understand the Java language. Get rid of the old clunker and try the latest version of Netscape!

Session 1 - March 5, 1997


So Let's Start Drinking Our Java

Assuming your browser is Java-enabled, the main title of this page, with its changing colors, has been displayed by a Java program called an applet. An applet is a Java program that runs in a browser such as Netscape. We will need to learn a little HTML for this course, but only enough to post a Java applet on a page. Let's look at the HTML that is needed to run RainbowText.


Putting an Applet in a Web Page

To follow these instructions, you must have a copy of the RainbowText.class file in the same directory as the HTML file I'm about to ask you to create. Remind me to get you a copy of it. Or instead you can click on this thing to find it. Dowload the RainbowText.zip file and unzip it. You should have the RainbowText.class file after you do that. If this doesn't work, I'll give you an excuse and a disk.

Create a directory and stash the RainbowText file in it. Start a text editor like Notepad or WordPad (or even Word) and either type in, or copy and paste the following text:

<applet code=RainbowText.class width=400 height=100>
</applet>


Save as a text file called Rainbow.html. Open it in your browser and look at it. You should see:


Using Parameters for Flexibility

Actually, there is a way to use an applet on your page without obtaining a copy of the applet. You need to know the URL for the class file to do this. In the applet tag, you need to set codebase equal to the path portion of the URL. Try this as an example and you will be running CHiPR's copy of the WaveText applet in your page:

<applet codebase="http://www.chipr.sunysb.edu/MoJA/" code=WaveText.class width=600 height=200>
</applet>


What could you do if you wanted to display a string other than RainbowText? If the author of the applet programmed in some parameters, you can use them for flexibility. In order to use the parameters, you need to know their names and what kind of data you can use for their values. Try this in your html file and take a look at the results in your browser by opening the file again (Reloading probably won't work.):

<applet code=RainbowText.class width=400 height=100>
<param name=text value=CHiPR>
<param name=fontsize value=72>
</applet>

If you would like to experiment with other parameters that RainbowText has implemented, you can find a description of them on the RainbowText page.


Interactivity

RainbowText is just a text effect applet and hardly allows any interaction with the visitor to your page. Applets that are interactive are designed to respond to events such as mouse clicks, mouse movements, mouse drags, or characters typed on the keyboard. RainbowText is sensitive only to mouse entry and mouse click events. Try single-clicking on the running applet. Then try it again. Also notice what happens on the message bar at the bottom of the browser when the mouse enters the applet display. By the way, that thing you can click to get to the RainbowText page isn't even an applet - it's only an animated gif image, enclosed in an HTML anchor (link).

A Genuine Interactive Applet

Pay a visit to the Project Java home page. Visit the Waveform Interaction applet and play with it for a while. Then stop goofing off, and get back here. But while you are playing, notice that it has scrollbars, textfields, a checkbox and other components that respond to mouse drags, keystrokes, and mouse clicks.

Hello, World!

We're runnung out of time for today. Before you leave, though, let's look at the Java code for a really simple applet:

/* Say hello to the world. */ import java.applet.Applet; import java.awt.*; public class HelloWorld extends Applet { public void paint(Graphics g) { // What to put on the screen g.drawString("Hello, World!", 10, 20); } }

Now let's see what it looks like on the screen:


This is what each line does:
/* Say hello to the world. */
This is a comment for the benefit of a person reading the code itself. It is essentially ignored by the compiler that translates the program into the bytecode that the browser runs.
import java.applet.Applet;
This statement provides us with a shortcut name that we can use to refer to the java.applet.Applet class that is part of the Java environment. We can now call it by the name Applet. We need this class in order to create our applet.
import java.awt.*;
We need a class called java.awt.Graphics in order to draw text on the screen. Classes usually come in packages. java.awt.Graphics is in the java.awt package. If we use a * instead of the name of a class in our import statement, we make the shortcut name for all classes in that package available to us.
public class HelloWorld extends Applet {
Here, we name our class and declare it as public. The public modifier enables anyone using the HelloWorld.class file to access the HelloWorld class that lies within. This is necessary so that the browser can find our class. Sometimes we may declare a class as private, so the messy details are hidden from programmers who use the file. We will talk more about information hiding later. For now, let's remember that when you create code to give to someone else, you usually don't want them to have to deal with the details of the code. Instead, you specify an interface for them, such as the parameters that they can use, and you hide the messy stuff. Remember that when you buy a coffee maker, you don't want to have to learn all the technology that went into it in order for it to be built. You want to know how to put the ingredient in, how to plug it in and work the controls, and how to get the ingredients out.
public void paint(Graphics g) {
Now we begin to declare a method called paint that is part of our HelloWorld class. The method is public so the browser can use it draw our text on the screen. It does not return a value such as a number, a text string, or anything else at all, so we declare it as void. Its purpose is to do something, namely draw on the screen, rather than to return information to the program. The browser looks for the paint method for a component whenever it has to render the component on the screen. Therefore, we can write the paint method to draw what we want, and it will get done.
// What to put on the screen
This is a one-line comment. It is delimited on the left by a // and is assumed by the compiler to end at the fist line break after that.
g.drawString("Hello, World!", 10, 20);
This statement draws a text string on the screen. g was declared as a parameter on the first line of the paint method. It is a reference to the Graphics class, since it was decalred that way. The Graphics class contains a number of methods that can draw on the screen, including drawString, which, as its name suggests, draws a string on the screen. The Graphics.drawString method takes three parameters, which, in order, are:

a string
the string to be drawn
an integer
the distance from the left margin of the applet (or other component) to the place where the string will begin
another integer
the distance from the upper margin of the applet (or other component) to the point where the lower-left corner of the string will be located
}
This closing curly bracket matches the opening curly bracket for the paint method, and marks the end of the code for this method.
}
This closing curly bracket matches the opening curly bracket for the HelloWorld class, and marks the end of the code for this class.

Some Hot Links

Between now and next Wednesday you might find it interesting to visit some Java-powered sites on the Web. Here are a few that I would recommend:

Last modified April 30, 1997


[Anyone for Java? home]