Applet Programming
Applet Programming
INTRODUCTION TO APPLET
Applets are small java programs that are used in internet computing. They can be transported
over the internet from one computer to another computer and run using the appletviewer or any
web browser that supports java. Applet can be used to perform arithmetic operations, display
graphics, play sounds, accept user input, create animation and play interactive games like any
other application.
Since applets are used in internet computing. So we can embed applet into web pages in two
ways as.
We can write our own applet and embed them into web pages
We can download an applet form a remote computer system and embed it into the web
pages
Types of applet
Local applet
Applets developed in a local computer are known as local applet. To run local applet in local
computer, we don’t need internet. It simply searches the directories in the local system and
locates and loads the specified applet.
Remote applet
Applets which are developed by someone else and stored on a remote computer connected to the
internet are known as remote applet. To run the remote applet to the local computer, the local
system must be connected with internet. We can download remote applet onto the local computer
via internet and run it.
For example, accessing local and remote applet from the html file as shown below:
import java.awt.Graphics;
import java.applet.Applet;
public class HelloApplet extends Applet{
1
Instructor: Sunil Bahadur Bist sunilbdrbist@gmail.com URL: www.sunilbist.com.np
ADVANCED OBJECT ORIENTED PROGRAMMING [BIT/BCA] PURBANCHAL UNIVERSITY
2
Instructor: Sunil Bahadur Bist sunilbdrbist@gmail.com URL: www.sunilbist.com.np
ADVANCED OBJECT ORIENTED PROGRAMMING [BIT/BCA] PURBANCHAL UNIVERSITY
3
Instructor: Sunil Bahadur Bist sunilbdrbist@gmail.com URL: www.sunilbist.com.np
ADVANCED OBJECT ORIENTED PROGRAMMING [BIT/BCA] PURBANCHAL UNIVERSITY
1. Born State: When the class is executed, the init () function is automatically invoked. At this
stage an Applet is said to be born. This state is also called initialization state. During the applet
life cycle, the born state occurs only once. The init function is defined within an Applet. This
function is used to initialize instance members, to load the required fonts, to initialize objects, to
load pictures, to load back colors, etc.
2. Running State: After initialization, this state will automatically occur by invoking the start ()
method of applet class which again calls the run () method and which calls the paint () method.
The running state also occurs from idle state when the applet is reloaded.
3. Idle State: The idle state will make the execution of the applet to be halted temporarily.
Applet moves to this state when the currently executed applet is minimized or when the user
switches over to another page. At this point the stop () method is invoked. From the idle state the
applet can move to the running state.
4. Dead State: When the applet programs terminate, the destroy function is invoked which
makes an applet to be in dead state.
}
public void paint(Graphics g){
g.drawString(sum,10,30);
}
}
Now create an html code to make it runnable as,
<html>
4
Instructor: Sunil Bahadur Bist sunilbdrbist@gmail.com URL: www.sunilbist.com.np
ADVANCED OBJECT ORIENTED PROGRAMMING [BIT/BCA] PURBANCHAL UNIVERSITY
<head>
<title>Addition Applet!</title>
</head>
<body>
<applet code="HelloApplet.class" width="400" height="400">
</applet>
</body>
</html>
Now you need to compile the java code and it will create .class file, finally goto the location
where your .class and html file saved from command prompt type: appletviewer htmlfilename
then you will see the result to the screen.
5
Instructor: Sunil Bahadur Bist sunilbdrbist@gmail.com URL: www.sunilbist.com.np
ADVANCED OBJECT ORIENTED PROGRAMMING [BIT/BCA] PURBANCHAL UNIVERSITY
6
Instructor: Sunil Bahadur Bist sunilbdrbist@gmail.com URL: www.sunilbist.com.np
ADVANCED OBJECT ORIENTED PROGRAMMING [BIT/BCA] PURBANCHAL UNIVERSITY
Q. Write an applet program that pass name of your JAVA lecturer name from the applet
parameter tag and display in the applet screen
Soln:
<html>
<head>
<title>Java Lecturor</title>
</head>
7
Instructor: Sunil Bahadur Bist sunilbdrbist@gmail.com URL: www.sunilbist.com.np
ADVANCED OBJECT ORIENTED PROGRAMMING [BIT/BCA] PURBANCHAL UNIVERSITY
<body>
<applet code="Parameter.class" width="400" height="200">
<param name="parName" value="Sunil Bahadur Bist">
</applet>
</body>
</html>
Now to handle the parameter send from the applet we use java applet code as:
import java.applet.*;
import java.awt.*;
public class Parameter extends Applet{
public void paint(Graphics g){
String name = getParameter("parName");
String message="Lecturor: "+name;
g.drawString(message, 20, 20);
}
}
Q. Create an applet program to count the no of click clicked by the user as shown in the figure
below:
Soln:-
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Counter extends Applet {
private TextField txtCount;
private int count = 0;
@Override
public void init() {
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
add(new Label("Counter: "));
txtCount = new TextField("0", 10);
txtCount.setEditable(false);
add(txtCount);
Button btnCount = new Button("Count");
add(btnCount);
txtCount.setText(count + "");
}
});
}
}
Q. Write an applet program to show the following screen when the user input integer the values to
the text box the output will be generated as shown in the figure:
Soln:
import java.applet.Applet;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.TextField;
9
Instructor: Sunil Bahadur Bist sunilbdrbist@gmail.com URL: www.sunilbist.com.np
ADVANCED OBJECT ORIENTED PROGRAMMING [BIT/BCA] PURBANCHAL UNIVERSITY
try {
x= Integer.parseInt(txtN1.getText());
y= Integer.parseInt(txtN2.getText());
} catch (Exception e) {}
z=x+y;
g.drawString("THE SUM IS : "+String.valueOf(z), 25, 100);
}
public boolean action(Event event, Object object){
repaint();
return true;
}
}
Now design web page as shown below,
<html>
<head>
<title>User Input Addition Applet!</title>
</head>
<body>
<applet code="Sum.class" width="400" height="130"></applet>
</body>
</html>
10
Instructor: Sunil Bahadur Bist sunilbdrbist@gmail.com URL: www.sunilbist.com.np