APPLET: an applet is an application designed to travel over the internet and to be executed on the client machine by a java compatible web browser like internet Explorer or Netscape. Applets are also java programs but they reside on the servers. An applet cannot be executed like standalone application. Applet can be executed only by embedding it into an HTML page like an image or sound file. To run an applet we need to access an HTML page which has applet embedded into it. When the web browser downloads such an HTML page it subsequently loads the executable file which contains an applet code and then executes it on the local machine. 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 intricate calculations with no introduction of the risk of viruses or breaching data integrity.
Applet Architectures: applets are different from normal java programs.
. Applets are GUI based (window based) programs.
. Applets are event driven.
When a normal java program (which is not GUI based) needs input it prompts the user and then calls some input method such us read line. The interaction is initiated by the program. This is not the way in which GUI based programs behave. The user initiates interaction with the program rather than program initiating the action. For example in a world processing software user mmsoftware user initiates action by clicking on different buttons. Which generates an event and some piece of code is executed as a result and accordingly some action takes place. Applets use awt package (Abstract Window Toolkit) for providing GUI and for event handling. The awt is called so because it totally depends on the functionality of the under lying or operating system. An applet looks like a set of interrupt service routines. An applet waits and event occurs. The awt notifies the applet about an event by calling an event handler that has been provided by the applet. Ones this happens the applet must take appropriate action and then quickly return control to the AWt. This is a critical point. For the most part over applet should not enter a mode of operation in which it maintains control for an extended period. Instead it must perform specific response to events and the return control to the AWT run time system. In those situations in which our applet needs to perform a repetitive task on its own (for example displaying a scrolling message across its window) we must start an additional thread of execution.
Writing an applet:
All applets are classes of applet class. The applet class is contained in the package java applet . the hierarchy of the applet class is as follows:
Java. Awt .panel
Java. applet. applet
The applet class provides all necessary support for applet execution such as starting and stopping. It also provides methods that load and display images. It also provides necessary support for all the window based activities. Our sub class extending the applet class must always be declared as public as it is instantiated and executed by the web browser. The browser makes use of no argument constructor when instantiating the applet so it is must to provide no argument public constructor with the public visibility. It is recommended that we do not provide constructor in the applet class as in that case complier will provide the default no argument constructor. We can make use of init () method for initialization. While writing applet code we normally over ride some of the methods of the applet class which are invoked automatically during applet execution. It is every common to over ride the paint () method mainly displays the output in the applet window.
Executing an applet: applets are not executed by the console based java run time interpreter. There are two ways in which we can run applets.
1 executing the applet with in a java compatible web browser.
2 using an applet viewer such as the standard SDK tool applet viewer. An applet viewer executes our applet in a window. This is fastest and easiest way to test our applet. Executing applet in a web browser: we need to write a short HTML text file that contains the appropriate APPLET tag. The applet tag must include at least following three attributes:
The attribute code value specifies the name of the class containing applets code. The attribute width and height specify the width and height of the applets window respectively.
Here the HTML file Run App.Html with applet simple applet embedded into it.
simple applet <\ title>
Executing applet using applet viewer: however a more convenient methods exists that we can use up testing. Simply include a comment in your java source file that consist that applet tag. By doing so our code is document with a proto type of the necessary HTML statements and we can test your compiled output merely by starting the applet viewer with our java source code file. If we use this method, then after compiling we can execute the applet as follows:
Applet viewer simple applet java
Applet life cycle methods: All but the most trival applets over ride a set of methods that provides the basic mechanism by which the browser or applet viewer communicates to the applet and controls its execution. These methods are also called life cycle methods because the applets or browser or applet viewer calls them automatically during different stages of applet life cycle. In all there are five life cycle methods.
Public void init ()
Public void start ()
Public void stop ()
Public void destroy ()
Public void paint () (graphics g)
Four of these methods init (), start (), stop (), destroy () are defined as in the applet class. The paint (), is defined by the awt component class default implementations for all these methods they do not use. However, very simple applets will not need to define them.
Applet initialization and termination:
When an applet begins, the following methods are called in sequence
1. Init ()
2. Start ()
3. Paint ()
When an Applet is terminated the following sequence of methods are called and takes place :
4. Stop ()
5. Destroy ()
Init () method: this is the first method to be called. This is when we should initialize variables and write code for other initialization activities. These methods are called only one during the life cycle of our applet immediately after installation.
Start () method: the start () method is called after init (): it is also called restart an applet after it has been stopped. Where init () is called once the first time an Applet is loaded start () is known each time an applet HTML document is displayed on screen. So if a user leaves a web page and comes back, the applet resumes execution at start ():
Paint () method: the paint () is called after the init () and start () method when the applet begins execution. The paint () method is also called each time our applet output must be redrawn. This condition can takes place for various reasons. For example, the window in which applet is running may be over written by another window and then uncovered, or the applet window may be minimized and then restored.
Stop () method: the stop () method is said when a web browser leave the HTML document containing the applet when it goes to another page. We should use stop () to suspend threads that do not needs to run when the applet is not visible. We can restart them when start () is called if the user returns to the page. We can also free from any costly resource and acquire it again in the start () method.
Destroy () method: the destroy () method is called when the environment determines that our applets needs to be removed completely from memory. At this point, we should free up any resource the applet may be using. The stop () method is always called before destroy ().
Example: a simple example of destroy () applet that sets the background colour of the cyan the foreground colour to red, an displays a message that illustrates the order in which the init (), and paint () methods are called when an applet starts up.