0% found this document useful (0 votes)
15 views10 pages

Understanding Java Applets Basics

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
0% found this document useful (0 votes)
15 views10 pages

Understanding Java Applets Basics

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

Java Applet

A Java applet is a small Java program designed to be embedded within an HTML


web page and executed within a web browser or an applet viewer. Applets were
historically used to add dynamic and interactive content to websites, such as
animations, games, and graphical user interfaces (GUIs).

Key characteristics of Java applets:


 Embedded in web pages:
Applets are included in HTML using tags like <applet> or <object>, similar to
how images or other media are embedded.
 Client-side execution:
The applet code is downloaded from a web server to the user's computer and
executed by the Java Virtual Machine (JVM) integrated within the web browser.
 Security sandbox:
Applets typically run within a security sandbox, which restricts their access to
local system resources for security reasons, preventing potential malicious
actions.
 Life cycle:
Applets have a defined life cycle managed by the browser or applet viewer,
including methods like init(), start(), paint(), stop(), and destroy().
 Subclass of [Link]:
All Java applets must extend the Applet class (or JApplet for Swing-based
applets) from the [Link] package.

Key Points:
 Applet Basics: Every applet is a child/subclass of the [Link] class.
 Not Standalone: Applets don’t run on their own like regular Java programs.
They need a web browser or a special tool called the applet viewer (which
comes with Java).
 No main() Method: Applets don't start with main() method.
 Display Output: Applets don't use [Link]() for displaying the
output, instead they use graphics methods like drawString() from the AWT
(Abstract Window ToolKit) .
Java Applet Life Cycle:-

The below diagram demonstrates the life cycle of Java Applet:

It is important to understand the order in which the various methods shown in the
above image are called.
 When an applet begins, the following methods are called, in this sequence:
o init( )
o start( )
o paint( )
 When an applet is terminated, the following sequence of method calls takes
place:
o stop( )
o destroy( )
Let’s look more closely at these methods.
1. init( ): The init( ) method is the first method to be called. This is where you
should initialize variables. This method is called only once during the run time of
your applet.
2. start( ): The start( ) method is called after init( ). It is also called to restart an
applet after it has been stopped.
Note: init( ) is called once i.e. when the first time an applet is loaded whereas
start( ) is called each time an applet’s HTML document is displayed onscreen.
So, if a user leaves a web page and comes back, the applet resumes execution at
start( )
3. paint( ): The paint( ) method is called each time an AWT-based applet’s
output must be redrawn. This situation can occur for several reasons. For
example, the window in which the applet is running may be overwritten by
another window and then uncovered. Or the applet window may be minimized
and then restored.

 paint( ) is also called when the applet begins execution. Whatever the cause,
whenever the applet must redraw its output, paint( ) is called.
 The paint( ) method has one parameter of type Graphics. This parameter will
contain the graphics context, which describes the graphics environment in
which the applet is running. This context is used whenever output to the applet
is required.
 paint() is the only method among all the methods mention above (which is
parameterized).
4. stop( ): The stop( ) method is called when a web browser leaves the HTML
document containing the applet, when it goes to another page.
For example: When stop( ) is called, the applet is probably running. You should
use stop( ) to suspend threads that don’t need to run when the applet is not
visible. You can restart them when start( ) is called if the user returns to the page.
5. destroy( ): The destroy( ) method is called when the environment determines
that your applet needs to be removed completely from memory. At this point, you
should free up any resources the applet may be using. The stop( ) method is
always called before destroy( ).

Key Packages for Java Applets


 [Link]: Base class for applets.
 [Link]: Used for drawing on the applet screen.
 [Link]: Provides GUI components and event-handling mechanisms.
Features of Applets over HTML
 Displaying dynamic web pages of a web application.
 Playing sound files.
 Displaying documents
 Playing animations

1.

import [Link].*; //[Link]

import [Link].*;

public class AppImage extends Applet

public void paint(Graphics g)

[Link]("My Applet",100,100);

<html> //[Link]

<head>

<title>

Hello Applet Program

</title>

</head>

<body>

<applet code="[Link]" width=400 height=400>

</body>

</applet>
</html>

C:\Users\hp\Desktop>javac [Link]

C:\Users\hp\Desktop>appletviewer [Link]

[Link] [Link].*;

import [Link].*;

public class AppImage extends Applet

public void init()

setBackground([Link]);

setForeground([Link]);

public void paint(Graphics g)


{

[Link]("My Applet",100,100);

3.

import [Link].*;

import [Link].*;

<applet code="[Link]" width=400 height=400>

</applet>

public class AppImage extends Applet

public void init()

setBackground([Link]);

setForeground([Link]);

public void paint(Graphics g)

[Link]("My Applet",100,100);

C:\Users\hp\Desktop>javac [Link]

C:\Users\hp\Desktop>appletviewer [Link]

[Link] class method(Color and Font):

import [Link].*;
import [Link].*;

/*<applet code="[Link]" width=800 height=500>

</applet>*/

public class AppImage1 extends Applet

public void init()

setBackground([Link]);

setForeground([Link]);

public void paint(Graphics g)

//Color c=new Color(10,20,30);//object/static member(0,0,0)show black color

Font f=new Font("TimesNewRoman",3,30);//string,bold,size

//[Link](c);

[Link](f);

//[Link]([Link]);

[Link]("My Applet",100,100);

5.

import [Link].*;

import [Link].*;

//<applet code="[Link]" width=800 height=500>

//</applet>
public class AppImage1 extends Applet

public void init()

setBackground([Link]);

setForeground([Link]);

public void paint(Graphics g)

//Color c=new Color(10,20,30);//object/static member(0,0,0)show black color

Font f=new Font("TimesNewRoman",3,30);//string,bold,size

//[Link](c);

[Link](f);

//[Link]([Link]);

[Link]("My Applet",40,50);//(String str,int x,int y)

[Link](40,70,100,40);

[Link](40,170,30,60);

[Link](90,170,30,60);

[Link](40,240,140,240);(int x1,int x2,int x3,int x4)

[Link](40,260,60,100,60,60);

}*/

6.

import [Link].*;

import [Link].*;
//<applet code="[Link]" width=800 height=500>

//</applet>

public class AppImage1 extends Applet

public void init()

setBackground([Link]);

setForeground([Link]);

public void paint(Graphics g)

int x[]={200,250,280,290,200};

int y[]={100,200,300,400,500};

//Color c=new Color(10,20,30);//object/static member(0,0,0)show black color

Font f=new Font("TimesNewRoman",3,30);//string,bold,size

//[Link](c);

[Link](f);

//[Link]([Link]);

[Link]("My Applet",40,50);

[Link](40,70,100,40);

[Link](40,170,30,60);

[Link](90,170,30,60);

[Link](40,240,140,240);

[Link](40,260,60,100,60,60);

[Link](x,y,5);
}

You might also like