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.

Subscribe to my newsletter and never miss my upcoming articles

Table of Contents

  1. What Are Applets?
  2. The Applet Class
  3. Applet Tag
  4. Types of Applet
  5. Applet Lifecycle
  6. Difference Between Application and Applet
  7. Skeleton of an Applet
  8. 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 java.applet package.
  • All applets are subclasses either directly or indirectly of Applet.
  • Execution of an applet does not begin at main( ).
  • 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 : java.applet.Appletjava.awt.Paneljava.awt.Containerjava.awt.Componentjava.lang.Object

Applet Tag

  • 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

  1. Local Applet
  2. Remote Applet

Applet Lifecycle

  1. 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.
  2. 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 start().
  3. paint() : The paint() method is called each time your applet’s output must be redrawn. The paint() method is inherited from java.awt.Component class. 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.
  4. 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.
  5. 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 destroy() .

Difference Between Application and Applet

Screenshot (381).png

Skeleton of an Applet


Methods of Applet Class

Screenshot (379).png