Uses of Applets
Applets can be used for multiple purposes. Some of the uses of applets are:
- Applets are used on Internet for creating dynamic web pages. There are two types of web pages: Static and Dynamic. Static web pages provide some information to the user but the user cannot interact with the web page other than viewing the information. Dynamic web pages interact with the user at the runtime. For example, a student can type his hall ticket number in a text field and click the retrieve button to get back his result from his University server. Applets are useful to provide such interaction with the user at 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.
In fact, applets are one of the main reasons for the popularity of java on Internet. In 1995, when javasoft people demonstrated some animation using an applet on HotJava browser, developers started believing that it is possible to perform animation in the browsers.
Now-a-days most of the websites on Internet are dependent on other software like PhotoShop, Flash, Dreamweaver, etc . For creating, editing, and providing animation to the images. But for validating the form data, scripting languages like JavaScript and PHP are better. Hence the use of applets is diminishing rapidly.
<APPLET> tag
<APPLET> tag is useful to embed an applet into an HTML page. It has the following form:
<APPLET CODE = “name of the applet class file”
CODEBASE=”path of the applet class file”
HEIGHT= maximum width of applet in pixels
ALIGN= alignment (LEFT, RIGHT, TOP, BOTTOM, MIDDLE)
ALT= alternate text to be displayed>
<PARAM NAME= parameter name VALUE= its value>
</APPLET>
The <PARAM> tag is useful to define a variable (parameter) and its value inside the HTML page which can be passed to the applet. The applet can access the parameter value using getParameter() method , as:
String value= getParameter(“Pname”);
Here, pname is the parameter name and its value is retrieved by the above method into String type variable: value.
A Simple Applet
Let us create an applet that displays ‘Hello applet’ in the applet frame. To display a message, we can take the help of paint() method of Component class of java.awt package. Remember all the methods of the applet and the applet class itself should be declared ‘public’, otherwise they are not available to the browser to execute.
Program 1: This is java program that creates an applet with yellow background color and a message ‘Hello Applets”.
// A simple applet
import java.awt.*;
import java.applet.*;
public class myapp extends Applet
{
// set a background color frame
public void init()
{
setBackground(Color.yellow);
}
//display message in applet window
public void paint(Graphics g)
{
g.drawString("Hello Applets!",50,100);
}
}
Run: javac myapp.java
Now, MyApp.class is created. This byte code should be embedded into a HTML page using <APPLET> tag, as shown below:
<! myapp.htmlat embeds myapp applet>
<html>
<applet code="myapp.class" height=300 width=400>
</applet>
</html>
Save the above code with the name: myapp.html. This HTML page contains the applet which can be opened in the browser, or an applet viewer supplied by the Sun Microsystems Inc., can be used to test the applet. For this purpose, open any browser and in the browser’s address bar, type .html file name along with the directory path. The applet opens in the browser. Or, give the command at system prompt as:
Run: Appletviewer myapp.html
OUTPUT.