0% found this document useful (0 votes)
45 views

APPLET

Applet is a special type of program that is embedded in a webpage to generate dynamic content. It runs inside the browser at the client side. An applet extends the Applet class and does not define a main method. The lifecycle of an applet includes initialization, start, paint, stop, and destruction methods. Examples are provided of applets that draw strings, set background colors, and perform calculations to demonstrate how applets work.

Uploaded by

Kousik manna
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

APPLET

Applet is a special type of program that is embedded in a webpage to generate dynamic content. It runs inside the browser at the client side. An applet extends the Applet class and does not define a main method. The lifecycle of an applet includes initialization, start, paint, stop, and destruction methods. Examples are provided of applets that draw strings, set background colors, and perform calculations to demonstrate how applets work.

Uploaded by

Kousik manna
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

APPLET

PREPARED BY
TANUSREE SAHA
What is Applet?

 Applet is a special type of program that is


embedded in the webpage to generate the
dynamic content. It runs inside the browser
and works at client side.

 It is a Java class that extends the


java.applet.Applet class. A main() 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.
Life Cycle of an 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

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.

 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?

way to run an applet


 By creation a html file with same name as Java

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.*;

 public class ColorApplet extends Applet {


 int i=0;
 Color c[]={Color.blue,Color.yellow};
 public void init(){
 setBackground(Color.red);
 }
 public void paint(Graphics g){
 try{
 Thread.sleep(5000);
 }catch(Exception e){e.printStackTrace();}
 setBackground(c[i++%2]);
 repaint();
 }
 }
Html
 <html>
 <body>
 <applet code="ColorApplet" width="300"

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

You might also like