100% found this document useful (1 vote)
68 views4 pages

Applet

dsd

Uploaded by

kirthimadishetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
68 views4 pages

Applet

dsd

Uploaded by

kirthimadishetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Unit _IV

APPLETS

Applets: An applet is a Java program that runs in a web browser.

Introduction: 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 Secured and can be
executed by browsers running under many platforms, including Linux, Windows, Mac Os
etc.Plug-in is required at client browser to execute applet
Java Plug-in is required in the browser at client side to execute applet.Java Plug-in is a
software product that serves as a bridge between a browser and an external Java Runtime
Environment(JRE).

Applets Example: Every Applet application must import two packages


1. java.awt.*:Imports the Abstract Window Toolkit(AWT)classes.Applets interact with the
user(either directly or indirectly)through the AWT.The AWT contains support for a
window-based,graphical user interface.
2. The java.applet.*:Imports the applet package,which contains the class Applet.Every
applet that you create must be a subclass of Applet class.

Steps to create an Applet example:

1. Create the applet with filename as HelloWorldApplet.java

import java.applet.Applet;
import java.awt.Graphics;

//Every applet must extend from java.applet.Applet class


public class HelloWorldApplet extends Applet
{

//paint method is called every time applet redisplay its output


public void paint(Graphics g)
{
g.drawString("Hello World",50,50);
}
}

2. Compile the above HelloWorldApplet java applet.


javac HelloWorldApplet.java

3. Create a html file,example “HelloWorldApplet.html” and place the applet code in html
file.
<html>
<body>

1
<applet code="HelloWorldApplet.class" width="300" height="300">
</applet>
</body>
</html>
4. There are two ways to run an applet.
 Executing the Applet within Java-compatible web browser.For executing an
Applet in an web browser,create short HTML file in the same directory and Run
the HTML file.
 Using an Applet viewer,such as the standard tool,applet viewer.An applet viewer
executes your applet in a window.
5. Execute the applet using applet viewer tool at command prompt as shown below-
appletviewer HelloWorldApplet.html

Applet Viewer:HelloWorldApplet.class - X

Applet

Hello World

Applet started

class in the program must be declared as public, because it will be accessed by code that
is outside the program.

Every Applet application must declare a paint() method. This method is defined by AWT
class and must be overridden by the applet. The paint( ) method is called each time when an
applet needs to redisplay its output.

Another important thing to notice about applet application is that,execution of an applet


doesnot begin at main( )method.In fact an applet application does not have any main( ) method.

Lifecycle of Java Applet

1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.

2
Whenever an applet is created,it undergoes a series of changes from initialization to
destruction.
Various stages of an applet life cycle are depicted in the figure above.

1. Initial State:When a new applet is born or created,it is activated by calling init( )


method,At this stage,new objects to the applet are created,initial values are set,images
are loaded and the colors of the images are set.An applet is initialized only once in its
lifetime.
2. Running State:An applet achieves the running state when the system calls the start()
method. This occurs as soon as the applet is initialized. An applet may also start when
it is in idle state.
3. Idle State: An applet comes in idle state when its execution has been stopped either
implicitly or explicitly. An applet is implicitly stopped when we leave the page
containing the currently running applet. An applet is explicitly stopped when we call
stop() method to stop its execution.
4. Dead State:An applet is in dead state when it has been removed from the
memory.This can be done by using destroy( ) method.
5. Paint() : Apart from the above stages,Java applet also possess paint( ) method.This
method helps in drawing,writing and creating colored backgrounds of the applet.It
takes an argument of the graphics class.To use The graphics,imports the package
java.awt.Graphics.

Applet Class:Applet class provides all necessary support for applet execution,such as
initializing and destroying of applet.It also provide methods that load and display images
and methods that load and play audio clips.
Most applets override these four methods.These four methods forms Applet
lifecycle.
public void init(): init() is the first method to be called. This is where variable are initialized.
This method is called only once during the runtime of applet.

public void start(): start( ) method is called after init( ).This method is called to restart an applet
after it has been stopped.

3
public void stop(): stop( ) method is called to suspend thread that does not need to run when
applet is not visible.

public void destroy(): destroy( ) method is called when your applet needs to be removed
completely from memory.

Program to show functions of Applet class:

1. Create the applet with filename as MyApplet.java

import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
public void init()
{
setName("MyApplet");
}
public void paint(Graphics g)
{
g.drawRoundRect(10,30,120,120,2,3);
}
}
2. Compile the above MyApplet.java applet.
Javac MyApplet.java
3. Create the html file,example “MyApplet.html” and place the applet code in html file.

<html>
<body>
<applet code="MyApplet.class" width="300" height="300">
</applet>
</body>
</html>
4. Execute the applet using applet viewer tool at command prompt as shown below:
Appletviewer MyApplet.html

Applet Viewer.MyApplet.class - X

Applet

Applet started.

You might also like