Activity #1: Applets and Basics

by Steve Hirst
Version 1.1 -- August 22, 1997


This first activity involves creating a Java Applet, getting introduced to the basic syntax of Java. This activity is taken from chapter 2 of Exploring Java by Niemeyer and Peck with new event model stuff taken from Java in a Nutshell, the Second Edition . Another place to look for activities is Sun's Java tutorial Finaly, as Java constructs are discussed I will link to the Java language specification (even though it is a little out of date. I'll keep Exploring Java in my office and will try to keep Java in a Nutshell around except when I forget and leave it at home.

Procedural note

I will try to move quickly through the introductory stuff and I'm assuming your familiar with C++ and basic OO stuff. If it moves too fast just take your time; let me know when you've completed this activity and I'll send you the next. If you need help with OO or C++ questions send email or come in and ask. We have quite a range of experience in the group; so everyone should go at your own pace; some of you will be knocking this off before I can get the next one done, be patient I'll get there and try to throw in interesting features all the way along. Let me know what works and doesn't and I'll change pace and/or content. This won't be a textbook and I won't spend a lot of time worrying about English but when I'm confusing or leave stuff out tell me.

What is an applet?

Java programs can either run as stand alone applications or as Applets. An application is started by calling its main method or function. The HelloWorld program is an example of this:

// The Hello World for Java
// Steve H. 19-Aug-97

class HelloWorld {
   static public void main(String args[])
  {
     System.out.println("Hello World.");
  }
}

Applets run inside of browser (Netscape, IE, or for this activities the AppletViewer). The browser creates an instance of the Applet and calls its init method.

But, in order for this work, the applet must be referenced from a Web page which just means a document specified in HTML .

What is HTML?

Funny you should ask. It is the Hypertext Markup Language . There are lots of books about it; but bascially an html document is text file with special tags delimited by angle brackets < >. In general, tags are balanced with an opening tag of the form: <tag-name> and a closing tag of the form: </tag-name>. If you want to learn more about HTML there are loads'o books and web pages; or, you can use the View Source command in your web browser to look at interesting pages on the web. There are also a couple of cheat sheets on the web like The Barebones Guide to HTML to get you through the essentials. To work this activity you will need to create an html file named hello.html containing the following:
<HTML>
<HEAD>
<TITLE> Hello Web </TITLE>
</HEAD>
<BODY>
<APPLET code=HelloWeb width=300 height=100>
<PARAM name="message" value="Welcome to Mentor for the rest of us." >
</APPLET>
</BODY>
</HTML>

And now, the Applet:

All applets are public classes derived from the base class Applet . Unlike C++ Java declarations and definitions are combined. There are no .h files. The syntax for a class requires first the access specification public then the name, such as: HelloWeb and the super class extends java.applet.Applet . The body of the declaration is enclosed in curly braces and includes all the member declarations for the class. For, example:
public class HelloWeb extends java.applet.Applet
{
}
is a complete declaration of an applet. Put it in a file HelloWeb.java , compile it and it will work with the HTML page above. I won't do much however.

To create an applet with interesting behavior we have to override the methods of the base class. Applet is actually derived from java.awt.Panel which is derived from java.awt.Container . Contain defines a method paint which is called by the browser to paint the area covered by the applet. The following declaration overrides the paint function to draw text on the screen:

// The Hello World for Java
// Steve H. 19-Aug-97

public class HelloWeb extends java.applet.Applet {
     public void paint( java.awt.Graphics gc ) {
          gc.drawString("Hello Web.", 125,60 );
     }
}

Assignment (finally)

  1. Create hello.html with the html code above.
  2. Create HelloWeb.java with the sample java code above.
  3. Compile the java code (javac HelloWeb.java)
  4. Test the code by typing: appletviewer hello.html
  5. Look up the java.awt.Graphics on the Sun API documentation and find the drawString method.

Member variables and the init method

Just as member methods are declared in the body of a member function, member variables can be declared. So, if you add a declaration: private String message; to the body of HelloWeb.java then the variable message is available to all the object methods of the class HelloWeb. We will get to names, visibility and object versus class members in the next actitity. Unlike C or C++, Java is not a define before using language so the order doesn't really matter.

Assignment (continued)

  1. add three member variables message of type String, and xPosition and yPosition of type int.
  2. look up the init method in the API document for Applet.
  3. Override the init method to set the value of message and xPosition and yPosition variables.
  4. Use those values in the paint method instead of the literals in the example above.
  5. compile and test your class
  6. look up the getParameter method in Applet. Call that method in your init method to get the "message" parmeter in the html file. See that was there for a reason. Assign the result to the message variable.
  7. compile and test your code.

Looking ahead

Next time, we will look at Java name space issues and mouse events. We'll also use some more Graphics calls. If you want and and have time, start messing with more Graphics calls. For example, change the text color to something other than black and draw lines, rectangles, circles, etc in the window.