Creating a Java Applet

Now that you know about the Java language and the Applet tag it is time to create your first Java Applet. Of course, everybody's favorite first program is Hello World.

In your favorite editor create a file called HelloWorld.java with this as the contents of the file:

      import java.awt.Graphics;

      public class HelloWorld extends java.applet.Applet
      {
         public void init()
         {
            resize(150, 50);
         }

         public void paint(Graphics graph)
         {
            graph.drawString("Hello World", 50, 30);
         }
      }
Here's the code on a separate page.

You can see that it is very simple to create a Java Applet. As I mentioned earlier the Java language only allows single inheritance and you can see that the new class HelloWorld inherits from the class java.applet.Applet. In this program we are overriding the init function and the paint function to do all of our applet's work. As I discussed before the init function is the equivalent to C++'s constructor, it gets called to initialize a new instance of a class. The paint function actually does the work of drawing on the screen. In our simple example we are telling the program to draw the words 'Hello World' on the screen.

Compile the program by typing javac HelloWorld.java at the command line. The Java compiler will create a file called HelloWorld.class which contains the compiled Java code. If you got any errors when compiling the Applet then verify that the code is exactly as typed above and try to compile it again.

To be able to run this program using Netscape 2.0 or any other java capable browser you will need to create a web page with the Applet tag. Again bring up your favorite editor and create a file called example.html. In this file place the HTML code:

<HTML>
<TITLE> Hello World </TITLE>
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=50>
</APPLET>
</HTML>

The easiest way to view this newly created Applet is to put the files example.html and HelloWorld.class into your web directory. On galaxy create a directory called www from your home directory and copy both files to that directory. Make sure that everybody has access to these pages by running the chmod command. In your home directory type chmod 755 www. In the www directory type chmod 644 example.html HelloWorld.class. To browse them go to the url "http://www.csc.calpoly.edu/~yourUsername/example.html". Another way to view the code is to run the appletviewer program. Set your display environment variable to point to the machine from which you are physically logged in, such as python or xpython. To do this type:

setenv DISPLAY xpython:0.0

From the www directory type appletviewer example.html. If you experience any problems please contact me.

Here's how the example should look.

You have created your first applet but you need to know how to read parameters into your program. We are going to modify the HelloWorld program to read in some command line parameters. In C all that you would have to do is create a main function that looked something like this:

void main(int argc, char *argv[])

An applet gets its parameters through the getParameter function. Here is the prototype:

String getParameter(String name)
The getParameter function is a member of the class java.applet.Applet. Add this code into the init function so that the new program displays the text that gets passed to it. And add a new member variable.

Old code:

      import java.awt.Graphics;

      public class HelloWorld extends java.applet.Applet
      {
         public void init()
         {
            resize(150, 50);
         }

         public void paint(Graphics graph)
         {
            graph.drawString("Hello World", 50, 30);
         }
      }
New code:

      import java.awt.Graphics;

      public class Param extends java.applet.Applet
      {        
         String textToDisplay;

         public void init()
         {
            resize(150, 50);
            textToDisplay= getParameter("text");
            if (textToDisplay == null)
            {
               textToDisplay = "Parameters Incorrect";
            }
         }

         public void paint(Graphics graph)
         {
            graph.drawString(textToDisplay, 50, 30);
         }
      }
Again here's the code on a separate page.

This new code should be put into a new file called Param.java. Compile the code the same way as before, javac Param.java. Modify the example.html file to also call the new Java code Param.class. Old example.html file:

<HTML>
<TITLE> Hello World </TITLE>
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=50>
</APPLET>
</HTML>

New example.html file:

<HTML>
<TITLE> Hello World </TITLE>
Hello World Program
<P>
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=50>
</APPLET>
<P>
Parameter Program
<P>
<APPLET CODE="Param.class" WIDTH=150 HEIGHT=50>
<PARAM NAME="text" VALUE="Interesting">
</APPLET>
</HTML>

View the example.html page again and you should see the new message.

Here's what it should look like.

You have completed the Java tutorial and you should know enough to have fun creating your own Java Applets. This tutorial has only been a brief overview of the Java language, for more information go to the references part of this tutorial where I have link the specification for the Java language. I suggest reading the Java Language Specification and looking at API for the JDK 1.0 before anything else. On to the fun stuff.


Prev Subject Next Subject