Applets Introduction

back

Your First Applet

Just as we did with our first java program, we will write our first "Hello, World" java applet. This is a very easy example just to get started.

BlueJ allows us to execute applets directly. Right click on the HelloWorld rectangle, and select Run as Applet.

BlueJ Applet image

You will be greeted by the above screen. Notice that you can run the applet in an appletviewer and also via HTML web page (The webpage is created for you). Choose either option to see your program execute. Your applet should look like this.


Examining the Code

Now it is time to finally understand what the code is actually doing. Just casually looking at the code, you would probably (without any programming knowledge) be able to assert that it would draw the sentance "Hello World!" somewhere. We will take a deeper look in order to understand at a surface level what exactly is going on.

Reserved Words and Identifiers

Whitespace

Whitespace refers to space characters, tabs, and new line characters in the program source code. You are required to have atleast one space between "words", but that is the minimum. For example, the following will not compile:

	import java.awt.*;
	import java.applet.*;
	
	public class HelloWorld extendsApplet{
	     public void paint(Graphics g){
	          g.drawString("Hello World", 50, 50);
	     }
	}
      
This is because the Java compiler will interpret extendsApplet as one identifier. The one exception to this rule if there is a Java symbol between two "words" there is not need to put a space. For example, there is no space between g and drawString in the above code.

You can add as much or as little whitespace as you wish to your programs. The following are two working extremes of whitespace in programs. First is an example with the minimum amount of whitespace.

	import java.awt.*;import java.applet.*;public class HelloWorld extends Applet{public void paint(Graphics g){g.drawString("HelloWorld",50,50);}}
Next, and example with a lot of whitespace.
	import java.awt.*;


	import java.applet.*;

	public class 
	HelloWorld extends 
	Applet{
	     public void 
                	 paint(Graphics g){
	                      g.drawString("HelloWorld",
	
	                      50,
	                      50);
	    }
	}
      
There are conventions in the use of whitespace. The easiest way to pick them up look at sample programs.

Final Points


Modifying the "Hello World!" Applet

Let's finally modify our boring HelloWorld applet. We will put these changes into a new file called HelloWorld2.java.

	import java.awt.*;
	import java.applet.*;
	
	public class HelloWorld2 extends Applet{
	
	     public void paint(Graphics g){
	          g.drawString("Hello World!", 50, 50);
	          g.drawString("This is the song that doesn't end,", 50, 80);
	          g.drawString("....it goes on and on my friend....", 90, 150);
	     }
	}
      
Don't forget to compile your code. It should look like this:
Picture of the HelloWorld2 applet
NOTE: My example
Exercise 2: Write an applet that displays two intersecting words, one vertical and one horizontal. Example.


Comments

Comments are text that is placed in code that does not affect the execution of the code.

	import java.awt.*;
	import java.applet.*;
	
	public class HelloWorld2 extends Applet{
	
	     public void paint(Graphics g){
	          // The original string
	          g.drawString("Hello World!", 50, 50);
		  
		  //new string right below it
	          g.drawString("This is the song that doesn't end,", 50, 80);
		  
		  //Final string below the other two
		  //and too the right
	          g.drawString("....it goes on and on my friend....", 90, 150);
	     }
	}