Learning Java
Custom Search

Previous Next

Java Applet

Unlike the java application which is a standalone program, a java applet requires a web page to run. So, for this project, we will create 2 files, a .java file for the applet and an .html file to run it.

Copy the code below into Notepad and name it HelloWorld.java

The name of the class here is HelloWorld. It extends the class called Applet found in the package java.applet. This means that it has all the qualities of an Applet class with some modifications. It doesn't have a main method, but the applet HelloWorld is public or available to the outside, that is to the web page. Our HelloWorld applet includes a method called paint to write a message to the user's monitor. We need the package java.awt because we are using the class Graphics. It allows us to use the method drawString to write text in the applet area at location x=50 and y=25.

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

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

You will compile it by first opening a cmd window. At the cmd prompt, get to the directory where you saved HelloWorld.java. To compile it, enter the following: Put capitals where you see them.

    javac HelloWorld.java

This creates the file HelloWorld.class

To run your compiled applet, copy the following HTML code into Notepad and name it Hello.html and place it in the same directory with HelloWorld.java above.

In the HTML document, note the line with the before and after APPLET tags. They contain the name of the class after compilation. They also contain the width and height of the area to contain the applets output.

<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
<APPLET CODE="HelloWorld.class" WIDTH="150" HEIGHT="25">
</APPLET>
</BODY>
</HTML>

This tutorial assumes that you already have knowledge of basic HTML code. However, we are only using very simple HTML code for executing our applets. The above code only consists of the following tags:

    HTML - tags to contain the entire HTML document.
    HEAD - tags to contain the heading information. This heading, in this example, only contains a title.
    TITLE - tags to contain the title that will display in the title area of the browser window.
    BODY - tags to contain the body of the HTML document where we will put our applet run instructions.
    APPLET - tags to contain the run instructions for our applet. Here for APPLET CODE, we put the name of the compiled file, HelloWorld.class. We have to reserve the space on the web page for our applet. We're asking for 150 pixels for the width and 25 pixels for the height.

To test it, double click on Hello.html in My Computer or the File Explorer. It will open your internet browser and execute your applet.

You might get a security warning when you try to run java applets in your browser. You can change the security in your browser to allow applets. If the general internet options are not changed, the following shows a series of steps in Windows XP to allow a java applet to run for this occurrence.

Click the "Close" button to close the Information Bar.

Click "Click here for options...".

Then in the list of options, click "Allow Blocked Content...".

Click "Yes".

Finally, the run of the applet shows this.

Congratulations on your first Java Applet.

Previous Next

 

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved