Prev Up Next
Go backward to Getting Started
Go up to Top
Go forward to Variations on a Theme

Writing (and Running) Your First Applet

The first actual line of Java in the file you are looking at is:

public class AppletMethods extends AppletTemplate

This line identifies your file as the instructions that will form an applet named "AppletMethods". Immediately after this line you will notice an open brace, "{". As you program in Java you will have to get used to both open braces and close braces, "}". Just as HTML likes to nest information within pairs of matching tags (like <P> and </P>), Java uses braces to surround regions of text that form logical units.

The rest of the file is a list of "method specifications". Each of these associates actions to be performed with some event that might occur. For example, the second method looks like:


/* Specifies how to react when the mouse button is depressed */ 	
   public void mouseDown() {
         pen.drawString("Some Short Message"); 
        } 

The first line of this method is actuallly not Java code. Anything enclosed within a pair of "/*" and "*/" delimiters is interpreted as a comment to clarify the code to human readers but to be ignored by the computer. The line that looks like:

   public void mouseDown() {
is called the "method header". It tells the computer that this is the beginning of a new set of instructions and associates the instructions with a name ("mouseDown" in this case) which determines when these instructions will be followed.

The lines following the method header up to the closing brace that matches the open brace on the header line are known as the "method body".

The "mouseDown" method included in this file states that when the mouse button goes down, any computer viewing this applet should display ("draw") a simple message on the region of the screen associated with the applet. At this point, the message that would appear will actually be the words "Some Short Message". You might as well start your programming career now by:

When you released the mouse within the applet viewer, your message disappeared. This is the result of the instruction in the next method of the applet. This method is named "mouseUp" and it currently instructs the computer to clear the rectangular area in which the applet is viewed when the mouse button is released.


Prev Up Next