Basics of applet

Applet

An applet represents java byte code embedded in a web page to generate the dynamic content.

Uses of Applets

Applets are used on internet for creating dynamic web pages. There are two types of web pages: Static and Dynamic. Static web pages provides some information to the user but the user cannot interact with the web pages. Dynamic web pages interact with the user at the runtime.
Another use of applets is for creating animation and games where the images can be displayed or moved giving a visual impression that they are alive.

Life cycle of Java Applet

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

Lifecycle methods for Applet

1. public void init()
2. public void start()
3. public void stop()
4. public void destroy()

Example
This file save as test.java

import java.awt.*;
import java.applet.Applet;
public class test extends Applet
{
public void init()
{
setBackground(Color.yellow);
}
public void paint(Graphics g)
{
g.drawString(“Hello”, 50, 100);
}
}

Now, this byte code embedded into a HTML page.
This file save as New.html

<html>
<body>
<applet code=”test.class” width=”300″ height=”300″>
</applet>
</body>
</html>

This html page contains the applet which can be opened in the browser.

Leave a Reply

Your email address will not be published. Required fields are marked *