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

An applet

 is a fully functioning small Java application


 is used for Web Design
 need to be embedded into HTML file
What is an Applet?

o Program that runs in


• appletviewer (test utility for applets)
• Web browsers that support them
• Executes when HTML (Hypertext Markup Language)
document containing applet is opened and
downloaded
 Java Application programs run in command windows
What Java Applet can do?

• display message and image on a web page


• draw picture on a web page
• receive input from the user through keyboard or mouse
• play Sound
Steps in Creating a Java Applet

Open the
Write the
Write a HTML
Java
Compile HTML to file in
Applet
it call the browser
Source
Applet and run
Code
it
Write the Java Applet Source Code

import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello world!", 50, 25);
}
}
Compile it

• Create HelloWorldApplet.class file


Write a HTML to call the Applet

<html>
…….
<applet CODE=“HelloWorldApplet.class"
WIDTH="250"
HEIGHT="220">
</applet>
……
</html>
Open the HTML file in browser and run it

• If the browser does not support applets, then use appletviewer to


run the applet
HelloWorldApplet

• import java.applet.*;
• import java.awt.*;

• public class HelloWorldApplet extends Applet


• {
• public void paint (Graphics g)
• {
• g.drawString ("Hello World", 25, 50);
• }
• }
Life Cycle of an applet

• When an applet is loaded, it undergoes a series of


changes in its state. The applet states include
1. Born or initialization state
2. Running state
3. Idle state
4. Dead or destroyed state
Life Cycle of an applet
Initialization state

• Applet enters this state when it is first loaded


• This is achieved by calling init() method
• The init( ) method is the first method to be called
• Initialize variables in this method
• This method is called only once during the life time of an applet
Running state

• • Applet enters into running state when it calls start() method • The
start( ) method is called after init( ) • It is also called to restart an
applet after it has been stopped • Where as init( ) is called only once,
start() method may be called any number of times • When a user
leaves a web page and comes back, the applet resumes execution

You might also like