LECTURE -61-PROGRAMMING IN JAVA -- JAVA APPLET AND APPLET LIFE CYCLE - BCA- S3.

Опубликовано: 05 Май 2026
на канале: BOSCOCAMPUSVISION
148
2

Java Applet
Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.
Applets are small Java programs that are primarily used in Internet computing. They can be transported over the Internet from one computer to another and run using the Applet Viewer or any Web browser that supports Java. An applet, like any application program, can do many things for us. It can perform arithmetic operations, display graphics, play sounds, accept user input, create animation, and play interactive games.
Local and Remote Applets
We can embed applets into Web pages in two ways. One, we can write our own applets and embed them into Web pages. Second, we can download an applet from a remote computer system and then embed it into a Web page.
An applet developed locally and stored in a local system is known as a local applet. When a Web page is trying to find a local applet, it does not need to use the Internet and therefore the local system does not require the Internet connection. It simply searches the directories in the local system and locates and loads the specified applet.
A remote applet is that which is developed by someone else and stored on a remote computer connected to the Internet. If our system is connected to the Internet, we can download the remote applet onto our system via at the Internet and run it In order to locate and load a remote applet, we must know the applet’s address on the Web. This address is known as Uniform Resource Locator (URL) and must be specified in the applet's HTML
document as the value of the CODEBASE attribute
Building Applet Code
It is essential that our applet code uses the services of two classes, namely, Applet and Graphics from the Java class library. The Applet class which is contained in the java.applet package provides life and behavior to the applet through its methods such as init( ), start( ) and paint( ). Unlike the
applications, where Java calls the main( ) method directly to initiate the execution of the program, when an applet is loaded, Java automatically calls a series of Applet class methods for starting, running, and stopping the applet code, The Applet class therefore maintains the lifecyele of an applet.
The paint( ) method of the Applet class, when it is called, actually displays the result of the applet code on the screen. The output may be text, graphics, or sound. The paint( ) method, which requires a Graphis object as an argument, is defined as follows:
public void paint (Graphics g)
This requires that the applet code imports the java.awt package that contains the Graphics class. All output operations of an applet are performed using the methods defined in the Graphics class. It is thus clear from the above discussions that an applet code will have a general format as shown below: .

import java.awt.*:
import java.applet.*:

The appletelassname is the main class for the applet. When the applet is loaded, Java creates an instance of this class, and then a series of Applet class methods are called on that instance to execute the code.
This requires that the applet code imports the java.awt package that contains the Graphics class. All output operations of an applet are performed using the methods defined in the Graphics class. It is thus clear from the above discussions that an applet code will have a general format as shown below: .

import java.awt.*:
import java.applet.*:

The appletelassname is the main class for the applet. When the applet is loaded, Java creates an instance of this class, and then a series of Applet class methods are called on that instance to execute the code.
This requires that the applet code imports the java.awt package that contains the Graphics class. All output operations of an applet are performed using the methods defined in the Graphics class. It is thus clear from the above discussions that an applet code will have a general format as shown below: .

import java.awt.*:
import java.applet.*:
public void destroy(): Finally, we have the destroy() method which is invoked in order to completely remove an applet from the memory. This method is invoked only once per applet life cycle and all the engaged resources must be freed up before this method is called.
One more method that is mostly used along with the above four is paint().
public void paint(Graphics g): This method is invoked whenever an applet needs to be redrawn or repainted in the browser, irrespective of the cause. The paint() method takes one Graphic object as a parameter that contains the graphics context in which the applet is being executed. Also, this method is invoked each time output is expected from the applet.
The appletelassname is the main class for the applet. When the applet is loaded, Java creates an instance of this class, and then a series of Applet class methods are called on that instance to execute the code.