APPLET
APPLET
PREPARED BY
TANUSREE SAHA
What is Applet?
Applet is initialized.
Applet is started.
Applet is painted.
Applet is stopped.
Applet is destroyed.
Lifecycle methods for Applet:
to repaint itself in the browser
Life cycle
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.
destroy − This method is only called when the browser
shuts down normally.
paint − Invoked immediately after the start() method, and
also any time the applet needs
java.applet.Applet class
once.
ava.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?
file then,
By appletViewer tool (for testing purpose).
Simple example of Applet by html file:
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome",150,150);
}
}
Html file with same name
<html>
<body>
<applet code="First.class" width="300" heigh
t="300">
</applet>
</body>
</html>
Another exmple-2
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet {
String s="";
public void init() {
s+="MY College name : "+this.getParameter ("College");
}
public void paint(Graphics g){
g.drawString(s, 100, 100);
}
}
html
<html>
<body>
<applet code="MyApplet.class" width="300"
height="300">
<param name="College" value="JISCE"/>
</applet>
</body>
</html>
Applet program to change
background color-3
import java.awt.*;
import java.applet.*;
height="300">
</applet>
</body>
</html>
Another program-4
import java.applet.Applet;
import java.awt.Graphics;
public class Number extends Applet {
public void paint(Graphics g){
int a=10;
int b=30;
int c=a+b;
String s="Sum"+String.valueOf(c);
g.drawString(s, 100, 100);
}
}
Html
<html>
<body>
<applet code="Number.class" width="300"
height="300">
</applet>
</body>
</html>
Thank you