Applets

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

APPLETS

Definition
• It is a small application that is embedded in a
HTML page, which is accessed and
transported over the internet, automatically
installed into the client machines and run as a
part of a web page.
Uses of Applets
• Applets are used for creating dynamic and
interactive web applications.
• They can be used to provide dynamic user
interfaces and graphical effects for web pages.
• They are used to add user interfaces like
buttons, text boxes, images etc.
• It can also be used in animations, games and
text editors.
How applets gets executed?
• Create the programs ,compile it and obtain
the byte code.
• This byte code is embedded in HTML page.
• This HTML page can be viewed using web
browsers.
• Browser will execute the applets byte code
using applet engine.
How to run an applet?
here are two ways to run an applet
• By html file.
• By appletViewer tool (for testing purpose
Hierarchy of applet
object

component

container

panel

Applet
Difference between applet program and
application program
Applets Applications
1.They do not have main() method Applications have main () method
2.They run under the control of web Get executed independently
browsers
3.They executed in java browser or applet They can be executed in java interpreters
viewer.
4.They cannot run any programs in the 4.They can run other programs in the local
local computers. computers.
5.They cannot use libraries from other They can use libraries from other
languages. languages.
6.They can be embedded into HTML They can not be embedded with HTML
pages. pages.
Applet Life Cycle

Born
Idle

running
Dead
Applet Life Cycle

Applets have 4 life cycle method:

Init():Used to initialize the applet before it gets loaded.


The init( ) method is the first method to be called. This is where
you should initialize variables. This method is called only once
during the run time of your applet.

Start():The start( ) method is called after init( ).


It is also called to restart an applet after it has been stopped.
Whereas init( ) is called once—the first time an applet is loaded—
start( ) is called each time an applet’s HTML document is displayed
onscreen. So, if a user leaves a web page and comes back, the
applet resumes execution at start( ).
Stop():is called to stop any operations which are started using start method.
The stop( ) method is called when a web browser leaves the HTML
document containing the applet—when it goes to another page, for
example. When stop( ) is called, the applet is probably running. You should
use stop( ) to suspend threads that don’t need to run when the applet is not
visible. You can restart them when start( ) is called if the user returns to the
page.

Destroy():is called when the applet finishes its execution.


The destroy( ) method is called when the environment determines that your
applet needs to be removed completely from memory. At this point, you
should free up any resources the applet may be using. The stop( ) method is
always called before destroy( ).
• paint( ) : The paint() method is called each time an AWT-based
applet’s output must be redrawn.
• This situation can occur for several reasons. For example, the
window in which the applet is running may be overwritten by
another window and then uncovered. Or the applet window may
be minimized and then restored. paint( ) is also called when the
applet begins execution.
• Whatever the cause, whenever the applet must redraw its
output, paint( ) is called. The paint( ) method has one parameter
of type Graphics. This parameter will contain the graphics
context, which describes the graphics environment in which the
applet is running.
• Init(): destroy():called when the applet is
Public void init() being terminated.
{ public void destroy()
….. {……
…… ……}
}
• Start():To begin the execution
Public void start()
{
….
……
}
• Stop()
Public void stop()
{
…….
……
}
• Paint():This method is used for displaying
various user interface elements like buttons,
text, shapes, text boxes and images etc.
Public void paint(Graphics g)
{……
……
}
Should include import java.awt.Graphics;
First.java
Import java.applet.Applet;
Import java.awt.Graphics;
Public class first extends Applet
{
Public void paint(Graphics g)
{
g.drawString(“welcome”,150,150);
}
}
Myapplet.html
<html>
<body>
<applet
code=“first.class”
width=“300”
height=“300”>
</applet>
</body>
</html>
HTML tag for applets
<APPLET specifies the beginning of the HTML applet code

CODE="demoxx.class" is the actual name of the applet (usually


a 'class' file)
CODEBASE="demos/" is the location of the applet (relative as
here, or a full URL)
NAME="smily" the name you want to give to this instance of the
applet on this page
WIDTH="100" the physical width of the applet on your page
HEIGHT="50"  the physical height of the applet on your page
ALIGN="Top" where to align the applet within its page space (top,
bottom, center)
Passing parameters to an applet
• Applets can receive inputs from HTML as
parameters.
• To pass the parameters to the Applet we need to
use the param attribute of <applet> tag.
• To retrieve a parameter's value, we need to use
the getParameter()
• Applet parameters come in 2 different parts:
– Parameter name and parameter value.
– Parameters are indicated by <PARAM> tag
Example for passing parameters
Import java.applet.Applet
<HTML> <HEAD>
Import java.awt.Font
Import java.awt.Graphics <TITLE>parameter passing
Public class MyFontApplet extends Applet </TITLE>
{ <BODY>
String fontName;
Int fontsize;
<APPLET
Public void init() code=MyFontapplet.class
{ width=400 height=500>
fontName=getParameter(“font”); <PARAM NAME=font
fontsize=Integer.parseInt(getParameter(“size”));
}
VALUE=“Times Roman”>
Public void paint(Graphics g) { <PARAM NAME=size
Font f=new Font(fontName,STYLE_BOLD,font VALUE=“36”> </APPLET>
size);
</BODY> </HEAD></HTML>
g.setFont(f);
g.drawString(“skyward publishers”,50,56);
}}
Advantages and disadvantages of applets

Advantages:
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under
many platforms, including Linux, Windows, Mac Os
etc.
Disadvantages:
• Plugin is required at client browser to execute
applet.
Program 20: Program to Demonstrate Applet Life Cycle
MyApplet.html

You might also like