Skip to main content

Practical-47

Write an applet program to implement the concept of passing parameter to applet.


Introdution

In this tutorial, we will learn how to write an applet program to implement the concept of passing parameter to applet. Parameters may be in the form of strings, numbers, or booleans. We will also learn how to make modifications in the parameter's value from within the program as per the requirement of the application.

Code

import java.applet.*;
import java.awt.*;
public class Example extends Applet{
String name;
int num;
boolean flag;
public void init(){
name=getParameter("Name");
num=Integer.parseInt(getParameter("Number"));
flag=Boolean.parseBoolean(getParameter("Flag"));
// number can be modified
num=num+1;
}
public void paint(Graphics g){
g.drawString("Name = "+name,50,50);
g.drawString("Number = "+num,50,100);
g.drawString("Flag = "+flag,50,150);
}
}

Output

Name = Amit
Number = 1
Flag = true
Explanation

The init() method is used to initialize the parameter's initial values and the paint() method is used to draw the string for the output. The getParameter() method is used to assign the value of the parameter to the corresponding argument. The getParameter value is passed as a parameter in the HTML page which contains the applet.