APPLETS IN JAVA
PRESENTED BY
LT. SONIA MAHINDRU
ASSTT. PROFESSOR IN COMPUTER SCIENCE
INTRODUCTION TO APPLETS IN JAVA
An applet is a special kind of Java program that runs in a Java enabled browser. This is the first Java program that can run over the network using the browser. Applet is typically embedded inside a web page and runs in the browser.
In other words, we can say that Applets are small Java applications that can be accessed on an Internet server, transported over Internet, and can be automatically installed and run as apart of a web document.
After a user receives an applet, the applet can produce a graphical user interface. It has limited access to resources so that it can run complex computations without introducing the risk of viruses or breaching data integrity.
INTRODUCTION TO APPLETS IN JAVA
To create an applet, a class must class extends java.applet.Applet class.
An Applet class does not have any main() method. It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a separate runtime environment to run an applet application.
JVM creates an instance of the applet class and invokes init() method to initialize an Applet.
TYPES APPLETS IN JAVA
A web page can contain two types of applets:
Local applets
Local applets are developed and stored locally, and therefore do not require an Internet connection to be located because the directory is located on the local system.
Remote applets
Remote applets are stored in a remote computer and an Internet connection is needed to access them. The remote applet is designed and developed by other developers. To find and load a remote applet, you need to know the address of the network applet, i.e., the Uniform Resource Locator (URL).
ADVANTAGES AND DISADVANTAGE OF APPLETS IN JAVA
There are many advantages of applet:
Drawback
LIFECYCLE OF AN APPLET
Four methods in the Applet class gives you the framework on which you build any applet :
init − This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.
start − This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.
stop − This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.
LIFECYCLE OF AN APPLET
destroy − This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.
paint − Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.
RUNNING AN APPLET
An applet can be run two ways:
RUNNING AN APPLET
Through HTML file:
For an applet to run in a web browser, we must write a short HTML text file that contains a tag to load an applet. For this, we can use <applet> or <object> tags. The HTML <applet> tag specifies an applet. It embeds Java applets in HTML documents. It does not support HTML5.
RUNNING AN APPLET
EXAMPLE
<!DOCTYPE html>
<html>
� <head>
<title>HTML applet Tag</title>
</head>
� <body>
<applet code = "newClass.class" width = "300" height = "200"></applet>
</body>
</html>
RUNNING AN APPLET
In the HTML text file , the code attribute of the <applet> tag specifies the applet class to execute. The width and height attributes are also required. They define the initial size of the panel on which the applet is running. The applet command must be closed with the </applet> tag.
Below is the applet class embedded in the HTML file
RUNNING AN APPLET
Example of applet class embedded in the HTML file:
import java.applet.*;
import java.awt.*;
�public class newClass extends Applet {
public void paint (Graphics gh) {
gh.drawString(“hrmmv.org", 300, 150);
}
}
RUNNING AN APPLET
Using the appletviewer tool:
The appletviewer runs an applet in the window. It is usually the fastest and easiest way to test an applet. Create an applet containing the <applet> tag in the comment, and compile it. It is for testing purposes only.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
EXAMPLE
To run an applet using the appletviewer tool, write in the command prompt:
c:\>javac First.java
c:\>appletviewer First.java
RUNNING AN APPLET
THANK YOU