Advertisement

Java Applets

Introduction to Java Applet

In Java an Applet is a panel which allows to interact with Java program, it provides the web interface and one can run it in the browser. One of the main reasons for the popularity of Java is this feature. Java Applets run in the virtual machine of the browser. Java Applet is to be an HTML webpage to be run on the browser, but this webpage is of a special kind of web page so that it can tell the browser that the webpage is an Applet.

SecurityOne of the feature of Java Applets is that it is secure, Java Applet runs in the sandbox, and it doesn’t have access to the client’s file system.

Browser CompatibilityMost of the browsers support Java 1.4 but they need a plugin to be installed on them.

Applet ViewerSun provides an appletviewer to view the applets without a browser. It will show the exact same page as it will be displayed on the browser.

Using Java Applets

You can simply extend your class from JApplet, it is just like other classes which you can use in your projects. in the case of Applets, your browser provides the main method, for your Applet.

The Hierarchy of Java Applet

java.lang.object

java.awt.Component

java.awt.Container

java.awt.nPael

java.apple.tApplet

javax.swing.JApplet

The Methods involved in Creating Java Applet

public void init ()

init() is initially called when the program is loaded, all the member variables are initialized in this method.

public void start ()

start() is similar to init() as it starts the Applet.

public void stop ()

stop() is used when the applet is stopped by the browser or the user due to some reason.

public void destroy ()

When we need to free some space on the disk which is occupied by the Applet, we can destroy the Applet by using destroy(). and when the Applet is no longer needed we use destroy().

public void paint (Graphics)

paint() is used to draw any graphical element on the screen, in your Applet. Another similar method repaint is used to refresh the Applet.

Java Applet Life Cycle

init() -> start() -> stop() -> destroy()

Java Applet Life Cycle

You Can’t do with Java Applets

Read data from clients hard drive.

Write Data to Clients hard drive.

Open network connections other than the connection from which the applet was initially loaded.

Link client side code and can’t use client side installations.

Read any other data from the client’s computer.

Java Applet Example

To make Java Applets understandable you need to understand this Hello World Applet.

You need to import all the awt components along with JApplet from swing in the project.

1

2

import java.awt.*;

import javax.swing.JApplet;

Create a class HelloWorldApplet and Extend HelloWorldApplet from JApplet.

1

publicclassHelloWorldApplet extendsJApplet

Implement the paint method and pass an instance of Graphics in it as parameters.

1

2

3

4

publicvoidpaint(Graphicsg)

{

g.drawString("Hello World of Java Applet",100,100);//calling drawString Method to write Hello World

}

As the Applets don’t have a main method so the complete code will be like.