Unit 1 : Java Applets
An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a web browser.
Table of Contents
- What Are Applets?
- The Applet Class
- Applet Tag
- Types of Applet
- Applet Lifecycle
- Difference Between Application and Applet
- Skeleton of an Applet
- Methods of Applet Class
What are Applets?
- An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser.
- Applet can also be downloaded on demand and run locally on the device.
- Applets are intended to be small programs.
- They are typically used to display data provided by the server, handle user input, or provide simple functions, such as a loan calculator, that execute locally, rather than on the server.
- In essence, the applet allows some functionality to be moved from the server to the client.
- The applet is a dynamic, self-executing program.
- Such a program is an active agent on the client computer, yet it is initiated by the server.
- This also proses a security threat to the system as the applet can be run automatically and might access unintended information.
- Java achieved this protection by confining an applet to the Java execution environment and not allowing it access to other parts of the computer.
- Therefore Java Applet became the most innovative aspect.
The Applet Class
- The Applet class is contained in the
- All applets are subclasses either directly or indirectly of Applet.
- Execution of an applet does not begin at
- To use an applet, the APPLET tag it is specified in an HTML file. The OBJECT tag can also be used, but Sun currently recommends the APPLET tag.
- Applet is a window-based event-driven programs.
- Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer.
- The applet will be executed by a Java-enabled web browser when it encounters the APPLET tag within the HTML file.
- Applet class inheritance :
- An applet viewer will execute each APPLET tag that it finds in a separate window or the same page if rendered in a compatible browser.
Types of Applet
- Local Applet
- Remote Applet
init() : The
init() method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet.
start() : 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,
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
paint() : The
paint() method is called each time your applet’s output must be redrawn. The
paint() method is inherited from
paint() is also called when the applet begins execution. 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.
stop() : The
stop() method is called when a web browser leaves the HTML document containing the applet or when it goes to another page. When
stop() is called, the applet is probably running. You 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.
destroy() : The
destroy() method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The
stop() method is always called before
Difference Between Application and Applet
Skeleton of an Applet
Methods of Applet Class