Applet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

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>
<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.
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.
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.
Common Methods Used in Displaying the output:

Applets are displayed in a window and they use the AWT to perform input and output functions.
Below are the common methods used for displaying the output-
➢ To output a string to an applet, use drawstring( ),which is a member of the Graphics
class. Typically, it is called from within either update( ) or paint( ).It has the following
general form:
void drawString(String message,int x,int y)
Here, message is the string to be output beginning at x,y.In a Java window,the upper-left
corner is location 0,0.The drawString( ) method will not recognize newline characters.If we want
to start a line of text in another line,we must do it manually,specifying the X,Y location where
we want the line to begin.
➢ To set the background color of an applets window,we use setBackground( )
To set the foreground color,we use setForeground( ).These methods are defined y
Component,and have the general forms:
void setBackground(Color newColor)
void setForeground(Color newColor)
Here, newColor specifies the new color.The class Color defines the following values that can be
used to specify colors:
✓ Color.black
✓ Color.magenta
✓ Color.blue
✓ Color.orange
✓ Color.cyan
✓ Color.pink
✓ Color.darkGray
✓ Color.red
✓ Color.gray
✓ Color.white
✓ Color.green
✓ Color.yellow
✓ Color.lightGray
For example,this sets the background color to blue and the text color to yellow:

setBackground(Color.blue);
setBackground(Color.yellow);
we can change these colors as and when required during the execution of the applet.The default
foreground color is black.The default background color is lightgray.
➢ We can obtain the current settings for background and foreground colors by calling
getBackground( ) and getForeground( ),respectively.They are also defined by Component
and bear the general form:
Color getBackground( )
Color getForeground( )

Program to change color of the foreground and background:


1. Create the applet with filename as MyApplet.java
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
public void init()
{
setName("My Applet");
}
public void paint(Graphics g)
{
setBackground(Color.Green);
setForeground(Color.RED);
g.drawString("Using Colors in Applet",50,100);
}
}

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="250" height="250">
</applet>
</body>
</html>
4. Execute the applet using applet viewer tool at command prompt as shown below:
appletviewer MyApplet.html

AppletViewer:MyApplet.class - X

Applet

Using Colors in Applet

Applet started.

You might also like