SSBN Degree College, ATP M Vishnuvardhan
Java Applets
-M Vishnuvardhan,
Dept. of Computer Science,
SSBN Degree College, ATP
SSBN Degree College, ATP M Vishnuvardhan
» Java programs are divided into two main
categories, applets and applications
» An application is an ordinary Java program
» An applet is a kind of Java program that can
be run across the Internet
Applets: Introduction
SSBN Degree College, ATP M Vishnuvardhan
» Applets are mainly used convert a static web
page to dynamic page
» Web is created with the help of Hypertext
markup language (HTML).
» Web site is collection of webpages of a
particular organization.
» A browser is a software which is used to browse
the internet ie., used to view web pages
Applets: Introduction
SSBN Degree College, ATP M Vishnuvardhan
» Applets do not use main() method
» Applets needs html code to run
» Applets are run by Java enabled
web browser.
» Applets are by default GUI Apps.
Applets vs. Applications
SSBN Degree College, ATP M Vishnuvardhan
» To deal with applets in java a special class is
defined in java.applet package called as
Applet.
» Applet class contains five special methods
init() :- used for initialization of applet
start() :- used to start an applet
paint() :- used to paint the contents of the
applet on the window
stop() :- used to stop an applet
destroy() :- used to destroy an applet
»Applet methods are automatically called one
after another once they are loaded into web
browser
Applet Class
SSBN Degree College, ATP M Vishnuvardhan
In order to create an applet the following steps
are to be followed
»Create a class by extending applet class
»Override any one of the applet class method
»Compile the program to obtain the class file
»Create a html document and embed the applet
»Run the applet along with the html page in
java enabled web browser
Creation of Applets
SSBN Degree College, ATP M Vishnuvardhan
// Sample Applet program
import java.awt.*;
import java.applet.*;
public class DemoApp extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Java applets”, 50,50);
}
}
Note:- After compiling the program embed the applet in a
html file and open in java enabled web browser.
Creation of Applets
SSBN Degree College, ATP M Vishnuvardhan
Html is a language used to design a web page
Basic structure of html file is
<html>
<! ------Comment Section ---- >
<head> <title> title of the page </title>
</head>
<body>
Body of the web page
</body>
</html>
HTML
SSBN Degree College, ATP M Vishnuvardhan
Commonly used html tags
<b> … </b>
<i> … </i>
<u> … </u>
<h1> .. </h1> to <h7> … </h7>
<applet> …. </applet>
HTML
SSBN Degree College, ATP M Vishnuvardhan
An Applet is embedded in a html file using
<applet> tag. Applet tag contains three main
attributes
Code :- specifies the class name of applet
Height :- specifies the height of the applet
Width :- specifies the width of the applet
Alignment :- specifies the alignment of applet
Hspace :- specifies the horizontal and
Vspace :- vertical space around the applet
EG:-
<applet code=“Demo” width=“300” height=“300”>
</applet>
Embedding an applet in html file
SSBN Degree College, ATP M Vishnuvardhan
Applet life cycle contains four necessary states
1. Born or Initialization state
2. Running State
3. Idle state
4. Dead state
In this four states applet class five methods are
executed on after another.
Applet Life Cycle
SSBN Degree College, ATP M Vishnuvardhan
Applet Life Cycle
Born
Running Idle
Dead
start()
stop()
destroy()
start()
paint()
Loading of
applet
Exit of
browser
SSBN Degree College, ATP M Vishnuvardhan
It is possible to pass data from a html page to
applet program. This can be done with the
help of <PARAM> tag in html
<APPLET .. . .. . .>
<PARAM name=“ ..” value=”…”/>
</APPLET>
Here the name corresponds the name of the
parameter and value associates the value of
the parameter
Passing parameters to Applet
SSBN Degree College, ATP M Vishnuvardhan
The parameters from the <PARAM> tag can be
read in the applet program with the help of
following method
String getParameter(String name):
returns the value associated with the
specified parameter.
Passing parameters to Applet
SSBN Degree College, ATP M Vishnuvardhan
Questions

Java applets

  • 1.
    SSBN Degree College,ATP M Vishnuvardhan Java Applets -M Vishnuvardhan, Dept. of Computer Science, SSBN Degree College, ATP
  • 2.
    SSBN Degree College,ATP M Vishnuvardhan » Java programs are divided into two main categories, applets and applications » An application is an ordinary Java program » An applet is a kind of Java program that can be run across the Internet Applets: Introduction
  • 3.
    SSBN Degree College,ATP M Vishnuvardhan » Applets are mainly used convert a static web page to dynamic page » Web is created with the help of Hypertext markup language (HTML). » Web site is collection of webpages of a particular organization. » A browser is a software which is used to browse the internet ie., used to view web pages Applets: Introduction
  • 4.
    SSBN Degree College,ATP M Vishnuvardhan » Applets do not use main() method » Applets needs html code to run » Applets are run by Java enabled web browser. » Applets are by default GUI Apps. Applets vs. Applications
  • 5.
    SSBN Degree College,ATP M Vishnuvardhan » To deal with applets in java a special class is defined in java.applet package called as Applet. » Applet class contains five special methods init() :- used for initialization of applet start() :- used to start an applet paint() :- used to paint the contents of the applet on the window stop() :- used to stop an applet destroy() :- used to destroy an applet »Applet methods are automatically called one after another once they are loaded into web browser Applet Class
  • 6.
    SSBN Degree College,ATP M Vishnuvardhan In order to create an applet the following steps are to be followed »Create a class by extending applet class »Override any one of the applet class method »Compile the program to obtain the class file »Create a html document and embed the applet »Run the applet along with the html page in java enabled web browser Creation of Applets
  • 7.
    SSBN Degree College,ATP M Vishnuvardhan // Sample Applet program import java.awt.*; import java.applet.*; public class DemoApp extends Applet { public void paint(Graphics g) { g.drawString(“Java applets”, 50,50); } } Note:- After compiling the program embed the applet in a html file and open in java enabled web browser. Creation of Applets
  • 8.
    SSBN Degree College,ATP M Vishnuvardhan Html is a language used to design a web page Basic structure of html file is <html> <! ------Comment Section ---- > <head> <title> title of the page </title> </head> <body> Body of the web page </body> </html> HTML
  • 9.
    SSBN Degree College,ATP M Vishnuvardhan Commonly used html tags <b> … </b> <i> … </i> <u> … </u> <h1> .. </h1> to <h7> … </h7> <applet> …. </applet> HTML
  • 10.
    SSBN Degree College,ATP M Vishnuvardhan An Applet is embedded in a html file using <applet> tag. Applet tag contains three main attributes Code :- specifies the class name of applet Height :- specifies the height of the applet Width :- specifies the width of the applet Alignment :- specifies the alignment of applet Hspace :- specifies the horizontal and Vspace :- vertical space around the applet EG:- <applet code=“Demo” width=“300” height=“300”> </applet> Embedding an applet in html file
  • 11.
    SSBN Degree College,ATP M Vishnuvardhan Applet life cycle contains four necessary states 1. Born or Initialization state 2. Running State 3. Idle state 4. Dead state In this four states applet class five methods are executed on after another. Applet Life Cycle
  • 12.
    SSBN Degree College,ATP M Vishnuvardhan Applet Life Cycle Born Running Idle Dead start() stop() destroy() start() paint() Loading of applet Exit of browser
  • 13.
    SSBN Degree College,ATP M Vishnuvardhan It is possible to pass data from a html page to applet program. This can be done with the help of <PARAM> tag in html <APPLET .. . .. . .> <PARAM name=“ ..” value=”…”/> </APPLET> Here the name corresponds the name of the parameter and value associates the value of the parameter Passing parameters to Applet
  • 14.
    SSBN Degree College,ATP M Vishnuvardhan The parameters from the <PARAM> tag can be read in the applet program with the help of following method String getParameter(String name): returns the value associated with the specified parameter. Passing parameters to Applet
  • 15.
    SSBN Degree College,ATP M Vishnuvardhan Questions