Java Applet

Java applet

An applet is a Java program that runs in a web browser. An applet can be a fully functional Java application, since it has the entire Java API at its disposal.

There are some important differences between an applet and a separate Java application, including the following –

  • An applet is a Java class that extends the java.applet.Applet class.
  • A main () method is not invoked in an applet and an applet class does not define main ().
  • The applets are designed to be incorporated into an HTML page.
  • When a user sees an HTML page that contains an applet, the applet’s code is downloaded to the user’s machine.
  • A JVM is necessary to view an applet. JVM can be a Web browser plug-in or a separate runtime environment.
  • The JVM on the user’s machine creates an instance of the applet class and invokes several methods during the lifetime of the applet.
  • The applets have rigid security rules that are applied by the Web browser. The security of an applet is usually called the safety of the sandbox, comparing the applet to a child who is playing in a sandbox with several rules that must be followed.
  • Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.

Life Cycle of an Applet

Four methods in the Applet class provide the structure in which you build any serious applet –

  • init – This method is intended for any initialization needed for your applet. It is called after the parameter labels within the applet’s label have been processed.
  • start – This method is called automatically after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone to other pages.
  • stop – This method is called automatically when the user leaves the page where the applet is. Therefore, it can be called repeatedly in the same applet.
  • destroy – This method is called only when the browser normally shuts down. Since applets are meant to reside in an HTML page, you should not normally leave resources behind after a user exits the page containing the applet.
  • paint – Call immediately after the start () method, and also whenever the applet needs to be repainted in the browser. The paint () method is actually inherited from java.awt.

An applet “Hello, World”

Next, a simple applet called HelloWorldApplet.java –

import java.applet. *;

import java.awt. *;

public class HelloWorldApplet extends Applet {

empty public ink (Graphics g) {

g.drawString ("Hello World", 25, 50);

}

}

These import instructions bring the classes to the scope of our applet class –

  • java.applet.Applet
  • java.awt.Graphics

Without these import instructions, the Java compiler would not recognize the Applet and Graphics classes, to which the applet class refers.

The applet class

Each applet is an extension of the java.applet.Applet class. The base Applet class provides methods that a derived Applet class can call to obtain information and services from the browser context.

These include methods that do the following –

  • Obtain applet parameters
  • Obtain the network location of the HTML file that contains the applet
  • Obtain the network location of the applet’s class directory
  • Print a status message in the browser
  • Search an image
  • Search for an audio clip
  • Play an audio clip
  • Change the size of the applet

In addition, the Applet class provides an interface by which the viewer or browser obtains information about the applet and controls the execution of the applet. The viewer can,

  • Request information about the author, the version and the copyright of the applet
  • Request a description of the parameters that the applet recognizes
  • Start the applet
  • Destroy the applet
  • Start the applet execution
  • Stop the applet execution

The Applet class provides standard implementations of each of these methods. These implementations can be replaced as necessary.

The “Hello, World” applet is complete as it is. The only method substituted is the painting method.

Interested in learning Java? Enroll now:” Java training in Chennai “

Invoking an applet

An applet can be called by incorporating directives into an HTML file and by viewing the file through an applet viewer or a Java-enabled browser.

The <applet> tag is the basis for incorporating an applet into an HTML file. Next, an example that calls the applet “Hello, World” -,

<Html>

<title> The Hello, World Applet </ title>

<Hr>

<applet code = "HelloWorldApplet.class" width = "320" height = "120">

If your browser is enabled for Java, a "Hello, World"

the message would appear here.

</ applet>

<Hr>

</ html>

Note – You can check the HTML Applet Tag to understand more about the HTML call applet.

The code attribute of the <applet> tag is required. Specifies the Applet class to be executed. Width and height are also required to specify the initial size of the panel on which an applet is running. The applet directive must be closed with an </ applet> tag.

If an applet uses parameters, the values can be passed to the parameters by adding <param> tags between <applet> and </ applet>. The browser ignores the text and other tags between the tags of the applet.

Browsers not enabled for Java do not process <applet> and </ applet>. Therefore, anything that appears between the tags, not related to the applet, is visible in browsers not enabled for Java.

The viewer or browser searches for the Java code compiled in the location of the document. To specify otherwise, use the codebase attribute of the <applet> tag as shown –

<applet codebase = "https://amrood.com/applets" code = "HelloWorldApplet.class"

width = "320" height = "120">

If an applet resides in a package other than the standard, the storage package must be specified in the code attribute by the period character (.) To separate package / class components. For example –

<applet = "mypackage.subpackage.TestApplet.class"

width = "320" height = "120">

Conversion of applications for applets

It is easy to convert a graphical Java application (that is, an application that uses the AWT and that you can start with the Java programmer) in an applet that can be embedded in a web page.

The following are the specific steps to convert an application into an applet.

  • Create an HTML page with the appropriate label to load the applet code.
  • Provide a subclass of the JApplet class. Make this class public. Otherwise, the applet can not be loaded.
  • Delete the main method in the application. Do not build a frame window for the application. Your application will be displayed inside the browser.
  • Move any boot code from the constructor of the frame window to the init method of the applet. It is not necessary to explicitly build the applet object. The browser instantiates this for you and calls the init method.
  • Remove the call to setSize; for applets, the size is made with the parameters width and height in the HTML file.
  • Remove the call to setDefaultCloseOperation. An applet can not be closed; ends when the explorer leaves.
  • If the application calls setTitle, remove the call to the method. Applets can not have title bars. (You can, of course, own the web page through the HTML title tag.)
  • Do not call setVisible (true). The applet is displayed automatically.

 

To getting expert-level training for Java Training in your location – java training in Chennai | java training in Bangalore  | java training in Pune | java training in Chennai | java training in Bangalore | java training in Chennai | java interview questions and answers |  core java interview questions and answers | java training in Chennai | For getting java online training | java online training