You are on page 1of 19

Lecture - 2

Date:06/11/2020
JAVA APPLET

•An applet is a Java program that runs in a Web


browser.
•An applet can be a fully functional Java
application.
•An applet does not have main method.
•All applets inherit the super class „Applet‟
•All applets must import java.applet and java.awt
packages.
JAVA APPLET

•Unlike java application applet does not start at


main().
•Applets are event driven i.e., an applet waits until
an event occurs.
•An applet is notified by AWT by calling event
handler provided by applet,. Once this is done, an
applet takes appropriate action and returns to the
AWT.
Executing an APPLET
•There are two ways to run Applet

1. Executing an Applet within java compatible


browser.-> To execute an applet using browser we must
write small HTML file which contains APPLET TAG.

<applet code=“FirstApplet” width=300 height=400></applet>

After creating file we can execute our browser and


load the file which makes applet to execute
Executing an APPLET
There are two ways to run Applet

2. Executing an Applet using „applet viewer‟


To execute an applet using applet viewer,
we may also execute the HTML file.
Suppose HTML file is App.html, then the
following command will run the applet.
C:\>appletviewer App.html
C:\>appletviewer FirstApplet.java
Differences between local and Remote
Applet
1. Local Applet – A Local applet is an applet that is
developed and stored on the computer . They can be accessed
directly from local computer . There is no need to use the
internet. A web page trying to access a local applet searches in
the local system and locates and loads the specified applet.

1. Remote Applet – Remote applet is the one that is


developed and stored on a remote computer connected to the
server. We can download the remote applets from the server
on the local computer through the internet and run it. To
download an applet we must know its address or URL On
web, and it must be specified in the HTML document as the
code attribute.
Differences applet and Java application
JAVA Application Applet
1.Stand alone programs 1. Are not Stand alone programs
2. Main method exists 2. Main method does not exists
3.Need a Java interpreter for execution 3. need a browser for execution.
4.They have no hard disk 4. they cant access hard disk
accessing restrictions
5. They doesn‟t need any security 5. They need topmost security
6. They share any software available 6. They cant share
7.Using plug-ins we can embed 7. We cant use plug-ins directly
latest additions of software we can use browser plug-ins.
Different Types of Applets
There are two types of Applets:

1. Applets that are based directly on “Applet Class”


These type of applets make use of abstract window toolkit(AWT)
in order to provide the graphical user interface. This type of applet
is available to be used to from the time java was created .
2. Applets that are based directly on “Swing Class”
These type of applets make use of the swing classes in order to
provide the graphical user interface. This is more rich and
easy to use compare to AWT.
Life Cycle of an Applet – 7Marks

Five methods in the Applet -

1. Init( )
2. Start( )
3. Stop( )
4. Destroy( )
5. Paint( )
Example :HelloWorldApplet.java

These import statements bring the classes into the scope of our
applet class − java.applet.Applet
java.awt.Graphics
Without those import statements, the Java compiler would not
recognize the classes Applet and Graphics, which the applet class
refers to.
Invoking an Applet
•An applet may be invoked by embedding directives in an
HTML file and viewing the file through an applet viewer or
Java-enabled browser.
•The <applet> tag is the basis for embedding an applet in an
HTML file. Following is an example that invokes the "Hello,
World" applet.
Process of creating an Applet is – 7M
1. Building an applet code(.java file)
2. Creating an executable applet(.class file)
3. Designing a web page using HTML page
4. Preparing </APPLET> tag
5. Incorporating </APPLET> tag into the web page.
6. Creating HTML file
7. run your applet
Process of creating an Applet is
1.Building an applet code(.java file) AppletClass.java
import java.applet.*;
import java.awt.*;
public class AppletClass extends Applet
{
public void paint (Graphics g)
{
g.drawString (“Creation of Applet", 30, 30);
}
}
Process of creating an Applet is
2.Creating an executable applet(.class file)

Using the Java compiler compile the AppletClass as


javac AppletClass.java

3. Designing a web page using HTML page


<html>
<head>
<title>creation of an applet</title>
</head>
<body>---------------</body>
</html>
Process of creating an Applet is
4. Preparing </APPLET> tag

The tag </APPLET> is used to embed


the java applet into a html file.

Example:
<applet code=“AppletClass” width=300 height=50>
</applet>
Process of creating an Applet is
5. Incorporating </APPLET> tag into the web page
There are two ways to incorporate applet tag:
(i) Creating HTML FILE with applet tag and
running it in browser.
Here the applet tag is inserted into the HTML code.
<html>
<head>
<title>creation of an applet</title>
</head>
<body>
<applet code=“AppletClass” width=300 height=50></applet>
</body>
</html>

let this file be create.html


Process of creating an Applet is
5. Incorporating </APPLET> tag into the web page
There are two ways to incorporate applet tag:
(ii) Incorporating <applet> tag within applet code and
running with applet viewer.

import java.applet.*;
import java.awt.*;
/*<applet code=“AppletClass” width=300 height=50><
/applet>*/
public class AppletClass extends Applet
{
public void paint (Graphics g)
{
g.drawString (“Creation of Applet", 30, 30);
}
}
Process of creating an Applet is
5. Incorporating </APPLET> tag into the web page

Here the applet tag is inserted into the HTML code.


<html>
<head>
<title>creation of an applet</title>
</head>
<body>
<applet code=“AppletClass” width=300 height=50>
</applet>
</body>
</html>

let this file be create.html


Process of creating an Applet is
6. Creating HTML File
Here the code created in step 5 is saved as create.html
thereby, creating a HTML File.

7. Run the Applet


Either applet can be run by running the create .html file
in the web browser or by using applet viewer.
One of the ways is shown below:
c:/>appletviewer create.html
Finally the output will be displayed. If we use java
enabled web browser , we can see entire web page
containing the applet, whereas if we use applet viewer we
can only see the applet output.

You might also like