introduce
Note: Before you start this tutorial, you must download and install the Java SE Development Kit.
Java applets are like Java applications. They are created by following the same three steps - write, compile and run. The difference is that they run on a portion of the web page rather than on your desktop.
The main purpose of this article is to create a simple Java applet. To achieve this there are three basic steps to follow:
1. Write a simple applet in Java
2. Compile Java source code
3. Create an HTML page involving the applet
4. Open the HTML page in the browser
Writing Java source code
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
I use Notepad to create my Java source code files. Open the editor of your choice and enter code like this:
//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 the meaning of the code. This is your first applet, and it's important to see how it is created, compiled, and run.
save file
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
Save your program file as "FirstApplet.java". Make sure you use the correct file name. If you see code like this:
public class FirstApplet extends Applet {
This is an instruction to call the applet class "FirstApplet". The file name should match the name of the class and have a ".java" extension. If your file is not saved as "FirstApplet.java", the Java compiler will complain and not compile your applet.
Open a terminal window
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 see "Run Dialog". Enter "cmd" and click "OK".
The terminal window is displayed. It thinks of it as a text version of Windows Explorer; it lets you browse different directories on your computer, look at the files they contain, and run the programs you want. This can be done by typing the command in the window.
Java Compiler
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
We need a terminal window to access the Java compiler called "javac". This is a program that reads the code in the FirstApplet.java file and translates it into a language your computer can understand. This process is compilation. Just like Java applications, Java applets must be compiled.
In order to run javac from a terminal window, you need to tell the computer where it is. On my machine it's in the directory "C:/Program Files/Java/jdk1.6.0_06/bin". If you don't have such a directory, search for the file "javac" in Windows Explorer to find its location.
Once you find its location, enter the following command into a terminal window:
set path= *the directory where javac lives*
Eg,
set path=C:/Program Files/Java/jdk1.6.0_06/bin
Press Enter. The terminal window doesn't do anything flashy, it just returns you to the command prompt. However, the compiler path has now been set.
Change directory
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
Take a look where the FirstApplet.java file is saved. My files are saved in "C:/Documents and Settings/Paul/My Documents/Java/Applets".
To change the directory in the terminal window, enter the following command:
cd *directory where FirstApplet.java file is saved*
Eg,
cd C:/Documents and Settings/Paul/My Documents/Java/Applets
You can tell you are in the directory on the right by seeing the cursor on the left. We are now ready to compile the applet. Enter the command:
javacFirstApplet.java
After hitting Enter, the compiler will see the code contained in the FirstApplet.java file and try to compile it. If it can't, it will display a series of errors to help you fix the code.
If you return to the command prompt without any information prompts, your applet was compiled successfully. If not, go back and check the code you wrote. Make sure it matches the example code and resave the file. Keep doing this until javac runs without any errors.
Tip: Once the applets compile successfully, you will see a new file in the same directory. It's called "FirstApplet.class". This is the compiled version of your applet.
Create HTML file
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
It's worth noting that so far you've followed exactly the same steps as if you were creating a Java application. The applet is created and saved in a text file and has been compiled by javac compiler.
Java Applets are different from Java applications when they run. What is needed now is a web page involving the FirstApplet.class file. Remember, a class file is a compiled version of your applet; it's a file that your computer can know and execute.
Open Notepad and enter the following HTML code:
<HTML><HEAD><TITLE>My First Java Applet</HEAD><BODY>Here's my first Java Applet: <applet code="FirstApplet.class" height ="300"></BODY></HTML>
Save the file as "MyWebpage.html" in the same directory as your Java applet file.
This is the most important line in the web page:
< applet code="FirstApplet.class" height ="300">
When the web page is displayed, it tells the browser to open your Java applet and run it.
Open HTML page
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.
The last step is also the best one; we can see the Java applet starting to run. Use Windows Explorer to navigate directories stored in HTML pages. For example, my web page is saved in "C:/Documents and Settings/Paul/My Documents/Java/Applets" and another of my Java applet files.
Double-click the MyWebpage.html file. Your default browser will open and the Java applet will run.
Congratulations! You have created your first Java applet!
Quick summary
Take a moment to summarize the steps for creating a Java applet. They are the same in every applet you create:
1. Write Java code in a text file
2. Save the file
3. Compile the code
4. Fix errors
5. Reference applet in HTML
6. Run the applet by browsing the web