Building Your First Java Applet

01
of 10

Introduction

A Simple Java Applet
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Note: Before you start this tutorial, you must have downloaded and installed the Java SE Development Kit.

Java applets are like Java applications, their creation follows the same three step process of write, compile and run. The difference is, instead of running on your desktop, they run as part of a web page.

The goal in this tutorial is to create a simple Java applet. This can be achieved by following these basic steps:

  1. Write a simple applet in Java
  2. Compile the Java source code
  3. Create a HTML page that references the applet
  4. Open the HTML page in a browser

02
of 10

Write the Java Source Code

Writing the Source Code
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

I’m using Notepad to create my Java source code file. Open up your chosen editor, and type in this code:

 //Reference the required Java libraries
 import java.applet.Applet; 
 import java.awt.*; 
 
 //The applet code
 public class FirstApplet extends Applet {
 
     public void paint(Graphics g) {
 
       //Draw a rectangle width=250, height=100
       g.drawRect(0,0,250,100); 
 
       //Set the color to blue
       g.setColor(Color.blue); 
 
       //Write the message to the web page
       g.drawString("Look at me, I'm a Java Applet!",10,50); 
    }
 } 

Don’t worry too much about what the code means. For your first applet, it’s more important to see how it’s created, compiled and run.

03
of 10

Save the File

Save the File
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Save your program file as “FirstApplet.java”. Make sure the filename you use is correct. If you look at the code you will see the statement:

 public class FirstApplet extends Applet { 

It’s an instruction to call the applet class “FirstApplet”. The filename must match this class name, and have an extension of “.java”. If your file is not saved as "FirstApplet.java", the Java compiler will complain and not compile your applet.

04
of 10

Open a Terminal Window

Run Dialog Box
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

To open a terminal window, press the “Windows key” and the letter “R”.

You will now see the “Run Dialog”. Type “cmd”, and press “OK”.

A terminal window will appear. Think of it as a text version of Windows Explorer; it will let you navigate to different directories on your computer, look at the files that they contain, and run any programs that you want to. This is all done by typing commands into the window.

05
of 10

The Java Compiler

Compile the Applet
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

We need the terminal window to access the Java compiler called “javac”. This is the program that will read the code in the FirstApplet.java file, and translate it into a language that your computer can understand. This process is called compiling. Just like Java applications, Java applets must be compiled too.

To run javac from the terminal window, you need to tell your computer where it is. On my machine, it’s in a directory called “C:\Program Files\Java\jdk1.6.0_06\bin”. If you don’t have this directory, then do a file search in Windows Explorer for “javac” and find out where it lives.

Once you’ve found its location, type the following command into the terminal window:

 set path= *the directory where javac lives*

E.g.,

 set path=C:\Program Files\Java\jdk1.6.0_06\bin 

Press Enter. The terminal window won’t do anything flashy, it will just return to the command prompt. However, the path to the compiler has now been set.

06
of 10

Change the Directory

Change Directory
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Navigate to where the FirstApplet.java file is saved. I’ve saved my file in the location “C:\Documents and Settings\Paul\My Documents\Java\Applets”.

To change the directory in the terminal window, type in the command:

cd *directory where FirstApplet.java file is saved*

E.g.,

cd C:\Documents and Settings\Paul\My Documents\Java\Applets 

You can tell if you’re at the right directory by looking to the left of the cursor.

07
of 10

Compile the Applet

Compile the Applet
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

We’re now ready to compile the applet. To do so, enter the command:

javac FirstApplet.java

After you hit Enter, the compiler will look at the code contained within the FirstApplet.java file, and attempt to compile it. If it can’t, it will display a series of errors to help you fix the code.

The applet has been compiled successfully if you are returned to the command prompt without any messages. If that’s not the case, go back and check the code you’ve written. Make sure it matches the example code and re-save the file. Keep doing this until you can run javac without getting any errors.

Tip: Once the applet has been successfully compiled, you will see a new file in the same directory. It will be called “FirstApplet.class”. This is the compiled version of your applet.

08
of 10

Create the HTML File

HTML File
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

It’s worth noting that so far you have followed exactly the same steps as you would if you were creating a Java application. The applet has been created and saved in a text file, and it has been compiled by the javac compiler.

Java Applets differ from Java applications when it comes to running them. What’s needed now is a web page that references the FirstApplet.class file. Remember, the class file is the compiled version of your applet; this is the file your computer can understand and execute.

Open up Notepad, and type in the following HTML code:

 <HTML>
 <HEAD>
 <TITLE>My First Java Applet</TITLE>
 </HEAD>
 <BODY>
 Here's my first Java Applet: <BR><BR>
 
 <applet code="FirstApplet.class" width="300" height ="300">
 
 </BODY>
 </HTML> 

Save the file as “MyWebpage.html” in the same directory as your Java applet files.

This is the most important line in the webpage:

 <applet code="FirstApplet.class" width="300" height ="300"> 

When the web page is displayed, it tells the browser to open up your Java applet and run it.

09
of 10

Open the HTML Page

A Simple Java Applet
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

The last step is the best one; we get to see the Java applet in action. Use Windows Explorer to navigate to the directory where the HTML page is stored. For example, my page is stored in “C:\Documents and Settings\Paul\My Documents\Java\Applets” with my other Java applet files.

Double-click on the MyWebpage.html file. Your default browser will open, and the Java applet will run.

Congratulations, you have created your first Java applet!

10
of 10

A Quick Recap

Take a moment to review the steps you took to create the Java applet. They will be the same for every applet you make:

  1. Write the Java code in a text file
  2. Save the file
  3. Compile the code
  4. Fix any errors
  5. Reference the applet in a HTML page
  6. Run the applet by viewing the web page