Module 1: Your first applet


A simple "helloworld" applet

Follow these steps in getting a simple "helloworld" applet to work:

Troubleshooting (It didn't work):

Other ways of running applets:


Explaining the "helloworld" applet

So far, we have written, compiled and run a Java program without necessarily understanding what we are doing. That's OK for now - it's important to get going with something.

Let's take a look at the program itself:

import java.awt.*;
import java.applet.*;

public class HelloWorld extends Applet {
  
  public void paint (Graphics g)
  {
    g.drawString ("Hello World!", 50, 50);
  }
  
}
  
What do we notice?

First, let's understand reserved words and identifiers:

Next, let's go over whitespace:

Finally, let's point out a few more things:


Modifying the "helloworld" applet

Let's make a couple of changes to the helloworld applet and put the changes into a new file called HelloWorldBlah.java:

import java.awt.*;
import java.applet.*;

public class HelloWorldBlah extends Applet {
  
  public void paint (Graphics g)
  {
    g.drawString ("Hello World!", 50, 50);
    g.drawString ("Blah, blah, blah", 50, 80);
    g.drawString (".... and more blah, blah", 90, 150);
  }
  
}
Note:
  • We are writing out three strings in the program:
    • The string "Hello World!" starting at (50, 50), that we had earlier.
    • The string "Blah, blah, blah" starting at (50, 80). Thus, this is at the same X position, but deeper down.
    • The string ".... and more blah, blah" at (90, 150) which is more to the right and further down.

  • Remember to compile the program.

  • Important: the class name (HelloWorldBlah) has to be the same as the first part of the file name (HelloWorldBlah.java).

  • We need to create a new HTML file or change the reference in the earlier file:
    <HTML>
    <BODY bgcolor="#FFFFFF">
    
      <applet code="HelloWorldBlah.class" width=300 height=200> 
      </applet>
    
    </body>
    </html>
       
    (Note: we've explicitly set the background color of the page to white)

  • Load the new HTML file into a browser.

  • The resulting applet looks like:

  • Note: a link to the applet itself
Thus, we've learned how to do some programming and, more importantly, the process of getting the modified applet to execute.
In-Class Exercise: Write an applet that displays two intersecting words, one vertical and one horizontal, as in:
         J
     C L A S S
         V
         A


Comments

Comments are text that we can place in a program that don't affect the program at all. For example:

import java.awt.*;
import java.applet.*;

// This program is a modification of the HelloWorld applet

public class HelloWorldBlah extends Applet {
  
  public void paint (Graphics g)
  {
    // Our original helloworld string:
    g.drawString ("Hello World!", 50, 50);

    // A new string that's below it:
    g.drawString ("Blah, blah, blah", 50, 80);

    // A third string that's more to the left and much below:
    g.drawString (".... and more blah, blah", 90, 150);
  }
  
}
Note:
  • The comments above start with a double-forwardslash "//"

  • Such a comment causes the compiler to ignore everything from the "//" all the way up to the end of the line.

  • You can start a comment after some valid Java commands, as in:
    import java.awt.*;
    import java.applet.*;
    
    // This program is a modification of the HelloWorld applet.
    
    public class HelloWorldBlah extends Applet { 
      
      public void paint (Graphics g)
      {
        g.drawString ("Hello World!", 50, 50);       // Our original helloworld string.
    
        g.drawString ("Blah, blah, blah", 50, 80);     // A new string that's below it.
    
        // A third string that's more to the left and much below:
        g.drawString (".... and more blah, blah", 90, 150);
      }
      
    }
       

  • You can place any number of comments in a file, as in:
    import java.awt.*;
    import java.applet.*;
    
    // This program is a modification of the HelloWorld applet
    
    public class HelloWorldBlah extends Applet {
      
      public void paint (Graphics g)
      {
        // Our original helloworld string:
        g.drawString ("Hello World!", 50, 50);
    
        // A new string that's below it:
        g.drawString ("Blah, blah, blah", 50, 80);
    
        // A third string that's more to the left and much below:
        g.drawString (".... and more blah, blah", 90, 150);
    
        // Another comment.
        // Yet another comment.
        // And one more.
        // ... yada, yada, yada.
      }
      
    }
    // And for good measure, one last comment here.
      

  • Comments serve to explain programs to readers.

  • A good question to ask is: if I'm writing my programs, why do I need to explain them to me?
    • First, you most often write programs that are read by others.
    • Even if you are the only one reading your programs, you might forget why you did something. A comment helps explain.
    • Commenting is particularly important when programming in groups, which is how most software is written.
    • A program without comments is like a phonebook that has only phone-numbers (the useful part) and no names next to the phone numbers. You might remember phone-numbers initially ("Hmm, I know Joe's number is right at the bottom"), but you'll eventually forget.

  • There are other kinds of comments in Java:
    import java.awt.*;
    import java.applet.*;
    
    /** 
     *  This program is a modification of the HelloWorld applet.
     *  This is a Javadoc comment.
     */
    
    public class HelloWorldBlah extends Applet { 
                                                 
      public void paint (Graphics g)
      {
        /* Our original helloworld string.*/
        g.drawString ("Hello World!", 50, 50);     
    
        // A new string that's below it.
        g.drawString ("Blah, blah, blah", 50, 80);     
    
        /* A third string that's more to the 
           left and much below: */
        g.drawString (".... and more blah, blah", 90, 150);
      }
      
    }
        
    • There are C-style comments that begin with /* and end with */ and can span multiple lines. These are rarely used in Java programs.
    • There are javadoc comments that begin with /**, end with */ and have each line beginning with *. These are used with the javadoc standalone utility to generate documentation for large programs.
    • For the most part, we'll use the regular "//" comments.
In-Class Exercise: The following program will not compile. Why? And what kind of errors are identified by the compiler?
public class HelloWorld extends Applet {

  //  
    Comment1

  public void paint (Graphics g)
  {
    // Comment 2   // Comment 3
    g.drawString ("Hello World!", 50, 50);
  }
  
} // Comment 4


Troubleshooting: compiler errors

First, let's distinguish between three kinds of errors:

  • Compiler errors:
    • These are the result of not typing in the program correctly.
    • Example: suppose we forgot the semi-colon at the end of a statement as in:
      import java.awt.*;
      import java.applet.*;
      
      public class HelloWorld extends Applet {
        
        public void paint (Graphics g)
        {
          // Deliberately removed the semicolon below:
          g.drawString ("Hello World!", 50, 50)
        }
        
      }
           
    • This would result in compiler error, something like:
      HelloWorld.java:9: ';' expected
          g.drawString ("Hello World!", 50, 50)
                                               ^
      1 error
           
    • Compiler errors arise when a program's structure is not correct according to Java's syntactic rules.

  • Runtime errors:
    • These errors occur when a program compiles but won't run properly because of something that went wrong during execution.
    • For example, let's deliberately create one:
      import java.awt.*;
      import java.applet.*;
      
      public class HelloWorld extends Applet {
        
        public void paint (Graphics g)
        {
          g = null;
          g.drawString ("Hello World!", 50, 50);
        }
        
      }
           
    • This will compile, but will create a runtime error.
    • Unfortunately, it's hard to know when a runtime error occurs when using applets because the browser will not display anything special.
    • Runtime errors are easily seen when running regular Java programs (not applets).
    • For example, consider
      public class BadHelloWorld {
      
        public static void main (String[] argv)
        {
          argv = null;
          System.out.println ("Argument length: " + argv.length);
        }
      
      }
            
    • This produces the run time error:
      Exception in thread "main" java.lang.NullPointerException
      	at BadHelloWorld.main(BadHelloWorld.java:6)
            

  • Logical errors:
    • These errors are those found in programs that compile perfectly but don't produce the desired effect.
    • For example, the program below won't print anything because the coordinates are too big:
      import java.awt.*;
      import java.applet.*;
      
      public class HelloWorld extends Applet {
        
        public void paint (Graphics g)
        {
          g.drawString ("Hello World!", 5000, 5000);
        }
        
      }
            
    • The program compiles and executes fine but nothing is displayed because the coordinates (5000, 5000) are way off screen.
    • Logical errors usually result from sloppiness in thinking or from not knowing the effects of some actions.

It is initially difficult to tell exactly what the compiler is complaining about when you get a compiler error.

Let's get accustomed to some compiler errors when the syntax is incorrect:

In-Class Exercise: What errors are reported by the compiler for this program?

import java.awt.*;
import java.applet.*;

public class HelloWorld extends Applet // Left out this brace
  
  public void paint (Graphics g)
  {
    g.drawString ("Hello World!", 50, 50);
  }
  
}

In-Class Exercise: What errors are reported by the compiler for this program?

import java.awt.*;
import java.applet.*;

public class HelloWorld extends Applet {
  
  public void paint (Graphics g)
  {
    g.drawString (Hello World!", 50, 50); // Forgot the first double quote "
  }
  
}

In-Class Exercise: What errors are reported by the compiler for this program?

import java.awt.*;
// Removed the applet package import.

public class HelloWorld extends Applet {
  
  public void paint (Graphics g)
  {
    g.drawString ("Hello World!", 50, 50); 
  }
  
}

In-Class Exercise: What errors are reported by the compiler for this program?

import java.awt.*;
import java.applet.*;

public class HelloWorld extends Applet {
  
  public void paint (Graphics g)
  {
    g.drawString ("Hello World!", 50, 50); 
  }
  
// Removed the ending brace

In-Class Exercise: What errors are reported by the compiler for this program?

import java.awt.*;
import java.applet.*;

// Left out the "class" reserved word
public HelloWorld extends Applet {
  
  public void paint (Graphics g)
  {
    g.drawString ("Hello World!", 50, 50); 
  }
  
}

Try these programs to see what happens.

In-Class Exercise:

import java.awt.*;
import java.applet.*;

// Left out the "public" reserved word
class HelloWorld extends Applet {
  
  public void paint (Graphics g)
  {
    g.drawString ("Hello World!", 50, 50); 
  }
  
}

In-Class Exercise:

import java.awt.*;
import java.applet.*;

public class HelloWorld { // Did not extend Applet
  
  public void paint (Graphics g)
  {
    g.drawString ("Hello World!", 50, 50); 
  }
  
}

In-Class Exercise:

import java.awt.*;
import java.applet.*;

public class HelloWorld extends Applet {
  
  public void pain (Graphics g) // Misspelled "paint"
  {
    g.drawString ("Hello World!", 50, 50); 
  }
  
}