Introduction To Applet
Introduction To Applet
Applets
Introduction to Applet
An applet is a small Java program which can be embedded inside a web page or can be downloaded
from the WWW. These applets are executed by web browser on user’s machine. In order to run
applets we depend upon a Java-capable browser. Although they can also be viewed using a tool
called the appletviewer, which is used to check and run applets.
The Applet class must be the super class of any applet program that is to be embedded within a web
page. To use Applet class we must have to import java.applet package.
Creating a Java Applet
Creating applets is more complex from creating a simple application, because Java applets run and
are displayed inside a Web page with other page elements. To create a simple applet, instead of only
print a message on screen, we have to create an applet to make space for our message and then use
graphics operations to paint the message to the screen.
First, we have to set up an environment so that our Java-capable browser can find our HTML files
and applets. Initially, we should keep our HTML files and applet code in the same directory. A
directory to store applet code and HTML files we should create a separate directory using following
command on command prompt –
C:\> mkdir applets
Steps to Create an Applet code
Step 1: Open a editor and edit following code into it.
//MyApplet.java
import java.applet.Applet;
public class MyApplet {
public void init() {
showStatus(“My First Applet . . “);
}
}
Step 2: Save the file with name MyApplet.java.
Step 3: Compile the file with javac.
Step 4: Create an HTML file with name MyApp.html. Insert following code into it –
<HTML>
<BODY>
<APPLET CODE=”MyApplet” WIDTH=400 HEIGHT=300>
</APPLET>
</BODY>
</HTML>
Step 5: Open a Java enabled Web Browser and open MyApp.html file into it.
However, this applet code can also be executed in appletviewer. To view applet in the appletviewer,
we have to insert <APPLET> tag into out applet code as a comment entry. This can be done in
following manner –
//MyApplet.java
import java.applet.Applet;
/*<APPLET CODE=”MyApplet” WIDTH=400 HEIGHT=300>