We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
Applet
An applet is a Java program that runs in a Web browser.
There are some important differences between an applet and a standalone Java
application.
= An applet is a Java class that extends the java.applet.Applet class.
* Amain() method is not invoked on an applet, and an applet class will not
define main().
= Applets are designed to be embedded within an HTML page.
= When a user views an HTML page that contains an applet, the code for the
applet is downloaded to the user's machine.
= AJVMis required to view an applet.Lifecycle of Java Applet
Applet is initialized.
Applet is started.
Applet is painted.
Applet is stopped.
Applet is destroyed.
Applet Lifecyclejava.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4 life
cycle methods of applet.
public void init(): is used to initialized the Applet. It is invoked only once.
public void start(): is invoked after the init() method or browser is maximized. It is
used to start the Applet.
public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
public void destroy(): is used to destroy the Applet. It is invoked only once.
java.awt.Component class
The Component class provides 1 life cycle method of applet.
public void paint(Graphics g): is used to paint the Applet. It provides Graphics class
object that can be used for drawing oval, rectangle, arc etc.How to run an Applet?
There are two ways to run an applet
1.By html file.
2.By appletViewer tool (for testing purpose).
Simple example of Applet by html file:
To execute the applet by html file, create an applet and compile it. After that create an
html file and place the applet code in html file. Now click the htm! file.
//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
Simple example of Applet by appletviewer tool:
To execute the applet by appletviewer tool, create an applet that contains applet tag in
comment and compile it.
After that run it by: appletviewer First.java. Now Html file is not required but it is for testing
purpose only.
HFirst.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);
+o}
i*
*/ To execute the applet by appletviewer tool, write in command prompt:
:\>javac First.java
c:\>appletviewer First.javaDifference between Applet and Application programming
java Applet Java Application
User graphics Inherently graphical ‘Optional
Memory Java application ‘Mirumal java application
requirements requirements plus web requirements
browser
Distribution ‘Linked via HTML and ‘Loaded from the file
transported via HTTP system or by a custom
lass loading process
Environmental Browser client location and | command-line parameters
anput size, parameters embedded
in the host HTML
document
Method expected | init- initialization method | Main - startup method
by the virtual start-startup method stop
Machine pause/ deactive method
y-termination
method paint-drawing,
‘method
Typical y | Network server,
applications systems for the web, online | multimedia kiosks,
multimedia , | developer tools, appliance
‘web page animation and consumer
control and navigation.Parameter in Applet
We can get any information from the HTML file as a parameter. For this purpose,
Applet ass provides a method named getParameter().
Syntax:
1, public String getParameter(String parameterName)
Exaniple uta , Jet:
import java.applet. Applet:
import java.awt Graphics,
ublic class UseParam extends Applet
public void paint{Graphies )
String str=getParameter("msg");
String str got argmetay(*mse')
yh
myapplet.htm!
Sparam name="msg" value="Welcome to applet"