0% found this document useful (0 votes)
25 views7 pages

Applets and Advanced Applets

An applet is a Java program that runs within a web browser, enhancing website interactivity and is embedded using the APPLET or OBJECT tag. The applet lifecycle includes methods like init(), start(), paint(), stop(), and destroy(), which manage its execution and resource handling. Applets face security restrictions, such as limitations on file access and network connections, and can utilize the Graphics class for rendering visual elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

Applets and Advanced Applets

An applet is a Java program that runs within a web browser, enhancing website interactivity and is embedded using the APPLET or OBJECT tag. The applet lifecycle includes methods like init(), start(), paint(), stop(), and destroy(), which manage its execution and resource handling. Applets face security restrictions, such as limitations on file access and network connections, and can utilize the Graphics class for rendering visual elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Applets and Advanced Applets

What is Applet?
An applet is a Java program that can be embedded into a web page. It runs
inside the web browser and works at client side. An applet is embedded in
an HTML page using the APPLET or OBJECT tag and hosted on a web
server.
Applets are used to make the website more dynamic and entertaining.
Important points :
1. All applets are sub-classes (either directly or indirectly)
of java.applet.Applet class.
2. Applets are not stand-alone programs. Instead, they run within either a
web browser or an applet viewer. JDK provides a standard applet
viewer tool called applet viewer.
3. In general, execution of an applet does not begin at main() method.
4. Output of an applet window is not performed by System.out.println().
Rather it is handled with various AWT methods, such as drawString().
Java applets are one of three kinds of Java programs:
 An application is a standalone program that can be invoked from the
command line.
 An applet is a program that runs in the context of a browser session.
 A servlet is a program that is invoked on a server program, and it runs
in the context of a web server process.
Life cycle of an applet :
 When an applet begins, the following methods are called, in this
sequence:

5acXjzUk
1. init( )
2. start( )
3. paint( )
 When an applet is terminated, the following sequence of method
calls takes place:
1. stop( )
2. destroy( )
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 that 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.
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.
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.
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.
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( ).

Sample applet Program:


// A Hello World Applet
// Save file as HelloWorld.java

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

// HelloWorld class extends Applet


public class HelloWorld extends Applet
{

5acXjzUk
// Overriding paint() method
@Override
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}

}
There are two standard ways in which you can run an
applet :
1. Executing the applet within a Java-compatible web browser.
2. Using an applet viewer, such as the standard tool, applet-
viewer. An applet viewer executes your applet in a window.
This is generally the fastest and easiest way to test your
applet.
 Using java enabled web browser : To execute an applet in a web
browser we have to write a short HTML text file that contains a tag
that loads the applet. We can use APPLET or OBJECT tag for this
purpose. Using APPLET, here is the HTML file that executes
HelloWorld :
<applet code="HelloWorld" width=200 height=60>
</applet>
NOTE : Chrome and Firefox no longer supports NPAPI (technology
required for Java applets).
 Using appletviewer : This is the easiest way to run an applet. To
execute HelloWorld with an applet viewer, you may also execute the
HTML file shown earlier. For example, if the preceding HTML file
is saved with
RunHelloWorld.html, then the following command line will run
HelloWorld :

appletviewer RunHelloWorld.html

5acXjzUk
Restrictions imposed on Java applets
Due to security reasons, the following restrictions are imposed on Java applets:
1. An applet cannot load libraries or define native methods.
2. An applet cannot ordinarily read or write files on the execution host.
3. An applet cannot read certain system properties.
4. An applet cannot make network connections except to the host that it
came from.
5. An applet cannot start any program on the host that’s executing it.
Commonly used methods of Graphics class:

1. public abstract void drawString(String str, int x, int y): is used to draw the specified
string.
2. public void drawRect(int x, int y, int width, int height): draws a rectangle with the
specified width and height.
3. public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle
with the default color and specified width and height.
4. public abstract void drawOval(int x, int y, int width, int height): is used to draw oval
with the specified width and height.
5. public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with
the default color and specified width and height.
6. public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line
between the points(x1, y1) and (x2, y2).
7. public abstract boolean drawImage(Image img, int x, int y, ImageObserver
observer): is used draw the specified image.
8. public abstract void drawArc(int x, int y, int width, int height, int startAngle, int
arcAngle): is used draw a circular or elliptical arc.
9. public abstract void fillArc(int x, int y, int width, int height, int startAngle, int
arcAngle): is used to fill a circular or elliptical arc.
10. public abstract void setColor(Color c): is used to set the graphics current color to the
specified color.
11. public abstract void setFont(Font font): is used to set the graphics current font to the
specified font.

5acXjzUk
Displaying Image in Applet

Applet is mostly used in games and animation. For this purpose image is required
to be displayed. The java.awt.Graphics class provide a method drawImage() to
display the image.

Syntax of drawImage() method:

1. public abstract boolean drawImage(Image img, int x, int y,


ImageObserver observer): is used draw the specified image.

How to get the object of Image:

The java.applet.Applet class provides getImage() method that returns the object of Image. Syntax
1. public Image getImage(URL u, String image){}

Other required methods of Applet class to display image:

1. public URL getDocumentBase(): is used to return the URL of the


document in which applet is embedded.
2. public URL getCodeBase(): is used to return the base URL.
Animation in Applet
Applet is mostly used in games and animation. For this purpose image is required to be moved.

Example of animation in applet:

import java.awt.*;
import java.applet.*;
public class AnimationExample extends Applet {

Image picture;

public void init() {


picture =getImage(getDocumentBase(),"bike_1.gif");
}

5acXjzUk
public void paint(Graphics g) {
for(int i=0;i<500;i++){
g.drawImage(picture, i,30, this);

try{Thread.sleep(100);}catch(Exception e){}
}
}
}
EventHandling in Applet
As we perform event handling in AWT or Swing, we can perform it in applet also. Let's see th
simple example of event handling in applet that prints a message by click on the button.

Example of EventHandling in applet:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class EventApplet extends Applet implements ActionListener{
Button b;
TextField tf;

public void init(){


tf=new TextField();
tf.setBounds(30,40,150,20);

b=new Button("Click");
b.setBounds(80,150,60,50);

add(b);add(tf);
b.addActionListener(this);

setLayout(null);

5acXjzUk
}

public void actionPerformed(ActionEvent e){


tf.setText("Welcome");
}
}
In the above example, we have created all the controls in init() method because it is invoked onl
once.

myapplet.html
<html>
<body>
<applet code="EventApplet.class" width="300" height="300">
</applet>
</body>
</html>

5acXjzUk

You might also like