Java can be used to create two types of programs: applications and applets. An application is a program that runs on a computer, under the operating system of that Computer just like the one created using C or C++.
An applet is an application that is accessed on the Internet Server designed to be transmitted over the Internet and executed by a Java-compatible Web browser. An applet is actually a tiny Java program, dynamically downloaded across the network, just like an image, sound file, or video clip. After an applet arrives on the client, it has limited access to resources, so that it can produce an arbitrary multimedia user interface and run complex computations without introducing the risk of viruses or breaching data integrity.
The point to be noted is that an applet is an intelligent program, not just an animation or media file. In other words, an applet is a program that can react to user input and dynamically change not just run the same animation or sound over and over.
A simple Applet program:-
public class SimpleApplet extends Applet
public void paint(Graphics g)
g.drawString("A Simple Applet", 20, 20);
- Applets are not executed by the console-based Java run-time interpreter. Rather, they are executed by either a Web browser or an applet viewer. Since all applets run in a window, it is necessary to include support for that window. Applets interact with the user through the Abstract Window Toolkit (AWT) classes. The AWT contains support for a window-based, graphical interface. It contains numerous classes and methods that allow you to create and manage windows. The AWT classes are contained in the java.awt package. Therefore all applets must import java.awt. It is one of Java’s largest and sophisticated packages but this applet makes very limited use of the AWT.
- Every applet that you create must be a subclass of class Applet which provides the necessary support for applets. The Applet class is contained in the java.applet package. Applet contains several methods that give detailed control over the execution of your applet. Thus, all applets must import java.applet.
- All applets must be declared as public, because it will be accessed by code that is outside the program.
- Output to an applet’s window is not performed by System.out.println( ). Rather, it is handled with various AWT methods, such as drawstring. Input is also handled differently in an applet than in an application.
- paint( ) method is defined by the AWT and must be overridden by the applet. paint( ) is called each time that the applet must redisplay or redraw its output. This situation can occur for several reasons. For example, the window in which the applet is running can be overwritten by another window and then uncovered. Or, the applet window can be minimized and then restored. paint( ) is also called when the applet begins execution. The paint( ) method has one parameter of type Graphics. This parameter contains the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required.
Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
This method outputs a string specified as its argument beginning at the specified X,Y location. It has the following general form:
void drawString(String message, int x, int y)
- Execution of an applet does not begin at main( ). An applet begins execution when the name of its class is passed to an applet viewer or to a network browser.
- After writing the source code of an applet, compile it just the same way as we compile java applications. Running an applet involves a different process.
Running An Applet
There are two ways in which you can run an applet:
- Execute the applet within a Java-compatible Web browser.
- Use an applet viewer, such as the standard SDK tool, appletviewer. An applet viewer executes your applet in a window. This is generally the fastest and easiest way to test your applet.
- To execute an applet in a Web browser, you need to write a short HTML text file that contains the appropriate APPLET tag. Here is the HTML file that executes simpleapplet:
<applet code="simpleapplet" width=200 height=60>
After you create this file, execute your browser and then load this file. This causes simpleapplet to be executed.
- To execute simpleapplet with an applet viewer, you may also execute the HTML file shown earlier. For example, if the preceding HTML file is called RunApp.html, then the following command line will run simpleapplet:
- However, a more convenient method exists that speeds up testing.
Include a comment at the head of Java source code file that contains the APPLET tag. This documents our code with a prototype of the necessary HTML statements and we can test our compiled applet merely by starting the appletviewer with your Java source code file. If you use this method, the SimpleApplet source file looks like this:
<applet code="simpleapplet" width=500 height=500>
public class simpleapplet extends Applet
public void paint(Graphics g)
g.drawString("A Simple Applet Program",50,20);
:drum: We can develop an applet in the following way:
- Create a java source file.
- Compile it.
- Execute the applet viewer, specifying the name of your applet’s source file. The applet viewer will encounter the APPLET tag within the comment and execute your applet.
As an applet is a window based program so its architecture is different from that of console based programs.
- Applets are event driven. An applet resembles a set of interrupt service routines. An applet waits until an event occurs. The AWT notifies the applet about an event by calling an event handler that has been provided by the applet. Once this happens, the applet must take appropriate action and then quickly return control to the AWT.
- The user initiates interaction with an applet. in a nonwindowed program, when the program needs input, it will prompt the user and then call some input method, such as readLine( ). This is not the way it works in an applet. Instead, the user interacts with the applet as he or she wants, when he or she wants. These interactions are sent to the applet as events to which the applet must respond. For example, when the user clicks a mouse inside the applet’s window, a mouse-clicked event is generated. If the user presses a key while the applet’s window has input focus, a keypress event is generated. Applets can contain various controls, such as push buttons and check boxes. When the user interacts with one of these controls, an event is generated.
- The applet is not in control of the thread of execution; it simply responds when the browser or viewer tells it to. For this reason, the methods you write must take the necessary action and return promptly--they are not allowed to enter time-consuming (or infinite) loops. In order to perform a time-consuming or repetitive task, such as animation, an applet must create its own thread, over which it does have complete control.
An Applet Skeleton
All applets override a set of methods that provides the basic mechanism by which the browser or applet viewer interfaces to the applet and controls its execution. Four of these methods—init( ), start( ), stop( ), and destroy( ) are defined by Applet. Another,
paint( ), is defined by the AWT Component class. Default implementations for all of these methods are provided. Applets do not need to override those methods they do not use.
Now write first.html with the following code:
/*<applet code="first" height=500 width=500>
public class first extends Applet
public void init()
msg+=" Inside Init";
public void start()
msg+=" Inside Start";
public void paint(Graphics g)
msg+=" Inside paint";
public void stop()
msg+=" Inside stop";
public void destroy()
msg+=" Inside destroy";
Now execute this html in the browser it gives the following output:
<applet code="first" height=1000 width=1000>
Applet Initialization and Termination
:daisy: When an applet begins, the AWT calls the following methods, in this sequence:
- init( )
- start( )
- paint( )
:death: When an applet is terminated, the following sequence of method calls takes place:
- stop( )
- destroy( )
The init( ) method is the first method to be called. This is where we should initialize variables. This method is called only once during the run time of your applet.
The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Whereas init( ) is called once—the first time an applet is loaded—start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).
The paint( ) method is called each time your applet’s output must be redrawn. This situation can occur for several reasons. For example, the window in which the applet is running may be overwritten by another window and then uncovered. Or the applet window may be minimized and then restored.
paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. The paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required.
The stop( ) method is called when a web browser leaves the HTML document containing the applet when it goes to another page, for example. We should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.
The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. This frees up any resources the applet may be using. The stop( ) method is always called before destroy( ).