CSC 241- Week 3 (February 5th 1996)


Applets

An applet is a small embeddable Java program. In our course, we will set up HTML files that will run our applets. What you will write are subclasses of the Applet class. The Applet class is a subclass of Panel which in turn is a subclass of Container, which in turn is a subclass of Component, which in turn is a subclass of the Object class. Component implements an interface named ImageObserver. We really have not discussed interfaces and this not the time for it anyway, but suffice it to say that they are similar to abstract classes (i.e. classes that don't have an implementation). Why do I bother telling you about the class hierarchy leading to the Applet class? I guess it is rooted in my desire to know how things work. For the Applets that you will need to write for this course you will have examples to guide you as to what methods you need to write an so forth, however, if you decide to find out more about any of those methods, you may need to look much deeper than just the Applet class. Java is rich with classes that enable you to do graphics and many other neat things. For most students, the most appealing part of Java is its applets and the fact that it can be transported across the internet and run on any platform. We will begin by looking at Hello World example. I wrote a couple of simpler Applets first, if you like to check them out too: First Hello World and Second Hello World.

A Hello World Applet

This is a detailed discussion of the Third Hello World Applet.

public class hello3 extends Applet means that the class name is hello3 and it is a subclass of the Applet class.

The variable declarations at the top of this class work the same way as they do with other classes in Java--they are available in all methods of hello3.

private Font hello_font; is the declaration of hello_font which will later get constructed in the init method. This is the statement that constructs hello_font: hello_font=new Font("Helvetica",Font.BOLD,48);. Note that the arguments for the constructor are the Font name, its style, and size. In the following pair of statements, we first set the font to hello_font for our applet's Graphics object (all applets have one), then we draw the string "HELLO WORLD" using that font. g.setFont(hello_font);
g.drawString("HELLO WORLD",40,110);

private Button bye_button; is the declaration of bye_button which will also get constructed in the init method. The following sequence of statements construct the bye_button and set its front and background colors: bye_button = new Button("Bye");
bye_button.setForeground(Color.black);
bye_button.setBackground(Color.lightGray);

The statement that follows these this.add(bye_button); is designed to include the button in the drawing area associated with the applet.

The paint method is invoked once when the applet starts, but you will notice that we call it from the action method also. The parameter for this method is a Graphics object, check out the Graphics class to see the description of the methods invoked here.

The action method is the most interesting one used here. It handles events initiated by the user. In our case, it is invoked when either of our buttons are clicked. Note that we check, if the action taken by the user was the click of either hello_button or the bye_button. If the event is not one of those two, we pass it along to the supper class's action method.


An HTML file with an embedded Applet

Third Hello World HTML file is fairly simple. There is a link in it to let a user see the code for the applet, and of course, there is an applet tag that embeds the applet in the HTML file. There are typically four parameters involved in embedding an applet:
  1. codebase acts very similarly to the CLASSPATH statement in your .cshrc file, it identifies where the applet is. It gets a bit more complicated when you have developed java classes that your applet imports, but we will leave that discussion for a later time.
  2. codeshould be the name of the .class file generated when you compile the Java applet.
  3. width and height define the number of pixles needed to display the applet's graphics area.