Applet Programming
Applet Programming
Types of Applet:
Local Applet
Remote Applet
Local Applet:
An applet developed locally and stored in a local system that is known as a Local
Applet. When a web page is trying to find a local applet, it doesn’t need to use the
Internet and the local system doesn’t require the Internet Connection.
Remote Applet:
A remote applet is that which is developed by someone else and stored on a
remote computer connected to the Internet. If our system is connected to the
Internet, we can download the remote applet onto our system via the Internet.
1. Local Applet is stored on our computer. 1. Remote Applet is not stored on our
computer.
The various methods involved in the life cycle of Java Applet has been
depicted by the below diagram.
there are 4 main methods which are mandatory for any Java Applet to
override.
1. public void init(): This is the very first method to be invoked during
the life cycle of an applet. In this method, the variable that will be
used further in the applet is initialized. One thing you must note here
is that this method can be invoked only once per applet life cycle.
2. public void start(): This is the second method that is invoked just
after the init() method is called by the browser. Each time a user
revisits the web page containing the applet, start() method is invoked
and the applet is started.
3. public void stop(): This method is invoked whenever a user leaves the
web page containing applet. In other words, the stop() method is used
to suspend the threads which are not required when the applet is in
the background or is not visible on the screen. These can be easily
resumed using the start() method.
4. public void destroy(): Finally, we have the destroy() method which is
invoked in order to completely remove an applet from the memory.
This method is invoked only once per applet life cycle and all the
engaged resources must be freed up before this method is called.
One more method that is mostly used along with the above four is paint().
import java.applet.Applet;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Graphics;
After you enter the source code for HelloWorld.java, compile in the same way
that you have been compiling java programs(using javac command).
There are two standard ways in which you can run an applet :
1. By html file.
2. By appletViewer tool (for testing purpose).
HelloWorld.html
<html>
<body>
<applet code=" HelloWorld.class" width="300" height="300">
</applet>
</body>
</html>
The width and height statements specify the dimensions of the display area
used by the applet. The APPLET tag contains several other options. After you
create this html file, you can use it to execute the applet.
2. 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 HelloWorld.html,then the following command line will run
HelloWorld :
appletviewer HelloWorld.html
Output:
appletviewer with java source file : If you include a comment at the head
of your Java source code file that contains the APPLET tag then your code is
documented with a prototype of the necessary HTML statements, and you
can run your compiled applet merely by starting the applet viewer with your
Java source code file. If you use this method, the HelloWorld source file
looks like this :
import java.applet.Applet;
import java.awt.Graphics;
/*
*/
With this approach, first compile HelloWorld.java file and then simply run
below command to run applet :
appletviewer HelloWorld.java
Output:
Adding Applet to HTML File: The <applet> tag in HTML was used to embed
Java applets into any HTML document.
Syntax:
<applet attribute1 attribute2....>
<param parameter1>
<param parameter2>
....
</applet>
Attributes: The <applet> tag takes a number of attributes, with one of the
most important being the code attribute. This code attribute is used to link
a Java applet to the concerned HTML document. It specifies the file name of
the Java applet.
<html>
</applet>
</html>
codebase URL Indicates the base URL of the applet if the code
attribute is relative
To pass the parameters to the Applet we need to use the param tag.
To retrieve a parameter's value, we need to use
the getParameter() method of Applet class.
Param Tag
The <param> tag is a sub tag of the <applet> tag. The <param> tag contains
two attributes: name and value which are used to specify the name of the
parameter and the value of the parameter respectively. For example, the
param tags for passing name and age parameters looks as shown below:
Now, these two parameters can be accessed in the applet program using
the getParameter() method of the Applet class.
getParameter() Method
The getParameter() method of the Applet class can be used to retrieve the
parameters passed from the HTML page. The syntax
of getParameter() method is as follows:
Let’s look at a sample program which demonstrates the <param> HTML tag
and the getParameter() method:
import java.awt.*;
import java.applet.*;
/*
<applet code="MyApplet" height="300" width="500">
<param name="name" value="Rama" />
<param name="age" value="25" />
</applet>
*/
public class MyApplet extends Applet
{
String n;
String a;
public void init()
{
n = getParameter("name");
a = getParameter("age");
}
public void paint(Graphics g)
{
g.drawString("Name is: " + n, 20, 20);
g.drawString("Age is: " + a, 20, 40);
}
}
Output of the above program is as follows:
import java.awt.*;
import java.applet.*;
int x=10;
int y=25;
int add=x+y;
String s="add:"+String.valueOf(add);
Output:
For any kinds of computation on the input field, we must convert it to the
right form and the results again converted back to strings for display.
import java.awt.*;
import java.applet.*;
/*<applet Code="UserInput.class" Width=400 Height=300>
</applet>*/