    Contents    Previous    Next

# Writing an Applet

By following the steps on this page, you can create and use an applet. Each step has links to where you can find more information about it. When you've followed all the steps, your directory (folder) structure should look something like this:

    Html--+--Hello.html (an HTML file you create)
          |
          +--classes--+--HelloWorld.java (a Java source file you create)
    	          |
    	          +--HelloWorld.class (created by the compiler)

1. Create a directory to hold your HTML pages, if you don't already have one. Put a directory named classes under it. (Here are the platform-specific details.)

   Important: Do NOT invoke hotjava from the classes directory. Due to a bug, HotJava can't reload an applet (for example, after you make changes to its code) when you invoke hotjava from the directory that contains the applet's compiled code.

2. Create a file named `HelloWorld.java` in the classes directory with the Java code shown here:

           import browser.Applet;
           import awt.Graphics;
           class HelloWorld extends Applet {
               public void init() {
       	    resize(150, 25);
               }
               public void paint(Graphics g) {
       	    g.drawString("Hello world!", 50, 25);
               }
           }

   Note: When creating the Java source files, be sure to use an editor that can save files with mixed-case names and 4-letter suffixes.

   Details of creating an Applet subclass (which is what the above code does) are discussed elsewhere in this document. To see implementations of Applet subclasses, look at the code examples.

3. Compile the file. (Here are the platform-specific details.)

   If compilation succeeds, the compiler creates a file named `HelloWorld.class`. If compilation fails, make sure you typed in and named the program exactly as shown above. See the Java compiler documentation for more information on compiling Java code, and the Java Language Specification for information on the Java language.

4. Create a file named `Hello.html` in your HTML directory containing the following text:

           <HTML>
           <HEAD>
           <TITLE> A Simple Program </TITLE>
           </HEAD>

           <BODY>
           Here is the output of my program:
           <APP CLASS="HelloWorld">
           </BODY>
           </HTML>

   The `APP` HTML tag (which you use to add applets to a page) is discussed later in this document.

5. Load the new HTML file into HotJava by entering its URL in the `Document URL` field near the top of the HotJava window. For example:

           file:/home/kwalrath/Html/Hello.html

   or, for files on Windows NT:

           file:///c:/Html/Hello.html

   Information on URLs is available in various places on the Internet, such as from the NCSA in A Beginner's Guide to URLs.

Once you've successfully completed these steps, you should see the following in the HotJava page that comes up:

  Here is the output of my program:

    Contents    Previous    Next