Ans. 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 net scope. Applets are also java programmes but they reside on the servers. An applet cannot be executed like stand alone application. Applets can be executed by only 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 an applet embedded into it. When the web browser downloads such an HTML page, it subsequently load the executable file, which contains applet code and then execute it on the local machine. After an applet arrives on the clint, it has limited access to recourses such that it can produce an arbitrary multi -media user interface and run intricate commutations with no introduction of the risk of viruses or breaching (breaking) data integrity.
Applet architectures: Applets are different from normal java programmes.
1. Applet are GUI based (window based) programmes.
2. Applets are even driven.
When a normal java programme (which is not GUI based) needs input it. Prompts the user and then calls some input method, such as read L ine () the interaction is initialized is ; initially by the programme. 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 underlying or operating system. An applet signifies a series of interrupt service routines. An applet waits and event occurs. The awt informs the applet regarding to an event by calling an event handler that has been provided by the applet. Once this occurs, the applet should take proper action and they rapidly return control to the AWT. This is a critical point. For the most part, our applet should not enter a mode of operation in which it sustains control for an extended and then 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. 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 extended 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 compiler will provide constructor in the applet class as in that case compiler will provide the default no argument constructor. We can make use of the Int t () 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 very common to over ride the paint () method. The code in the paint () method mainly displays the output in the applet window, 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 very common to over ride the paint () method. The code in 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 an Applet. Implementing the Applet within a java compatible web browser. By means of an applet viewer like 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 codes value specifies the name of the class containing applet code. The applet tag must include at least following three width height specify the width and height of the applet windows respectively. Here the HTML file run applet. HTML with applet simple applet embedded into it.
< HTML >
< Head >
< Title > simple applet < / title >
< / Head >
< Body >
< Height > simple "Hello World" application < /hl >
< Applet code = "simple applet" width = 200 height = 200 height = 200 >
< / Applet >
< / Body >
< HTML >
Executing applet using applet viewer:
However, a more convenient method exists that we can use up testing. Simply include a comment in your java source file that contains that Applet tag. By doing so, our code is document with a proto type of the necessary HTML statements and we 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 life cycle methods: All but the most trivial applets over ride a set of methods that provides the basic mechanism by which the browser or applet viewer interfaces to the applet and controls its execution. These methods are also called life cycle methods because of the browser or applet viewer calls them automatically during different stages of applet life cycle. In all three there are five life cycle methods:
Public void Int t ()
Public void start ()
Public void stop ()
Public void destroy ()
Public void paint (graphics g)
Four of these methods: Int t () start (), stop () and destroy () are defined in the applet class. The paint (), is defined by the AWT component class, default implementations for all these methods are provided. Applets do not need to over ride those methods they do not use. However, very simple applets will not define them.
Applet initialization and termination:-
When an applet begins the following methods are called in sequence
1. Int t ()
2. Start ()
3. Paint ()
When an Applet is finished the subsequent series of methods calls takes place:
4. Stop ()
5. Destroy ()
Int t (): This is the first method to be called. This is where we should initialize variables and write code for other initializations activities. This methods is called only once during life cycle of our Applet immediately after installation.
Start () method: this is the first method to be called. The start method is called after Int t (): it is also called to restart an applet after it has been stopped. Where Int t () is called once our applets the first time an Applet is loaded, start () is called each time an applets in HTML document is displayed in 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 Int () and start () method when the applet being execution. The paint () method is also called each time our applet output must be redrawn. This situation can take place for numerous reasons. For example, the window in which the Applet is running way to 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 called when a web browser leaves the HTML document holding the applet when it moves to other page. We should use stop () to suspend threads that they dot need not 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 any costly resource and acquire it again in the start () method.
Destroy () method: The Destroy () method is called when the environment determines that our applet 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 at all times called before destroy ().
Example: a simple applet that see sets the background colour to cyan, the foreground colour to red, an displays a message that illustrates the order in which the Int t (), start, and paint () methods are called when an applet starts up.