Applet in Java
Applet in Java
• Applet is a special type of program that is embedded in the webpage to generate the dynamic
content.
• It runs inside the browser and works at client side.
Advantage of Applet
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os
etc.
Drawback of Applet
Hierarchy of Applet:
[Link] class:
public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be
used for drawing oval, rectangle, arc etc.
<html>
<body>
<applet code="[Link]" width="300" height="300">
</applet>
</body>
</html>
Simple example of Applet by appletviewer tool:
• To execute the applet by appletviewer tool, create an applet that contains applet tag in comment
and compile it.
• After that run it by: appletviewer [Link]. Now Html file is not required but it is for testing
purpose only.
//[Link]
import [Link];
import [Link];
public class First extends Applet{
public void paint(Graphics g){
[Link]("welcome to applet",150,150);
}
}
/*
<applet code="[Link]" width="300" height="300">
</applet>
*/
//[Link]
<html>
<body>
<applet code="[Link]" width="300" height="300">
</applet>
</body>
</html>
Animation in Applet:
import [Link].*;
import [Link].*;
public class AnimationExample extends Applet {
Image picture;
public void init() {
picture =getImage(getDocumentBase(),"bike_1.gif");
}
for(int i=0;i< 500;i++) {
[Link](picture, i,30, this);
try{
[Link](100);
}
catch(Exception e){}
}
}
}
//[Link]
<html>
<body>
<applet code="[Link]" width="300" height="300">
</applet>
</body>
</html>
EventHandling in Applet
• An event is a change in the state of an object triggered by some action such as Clicking a
button, Moving the cursor, Pressing a key on the keyboard, Scrolling a page, etc.
• In Java, the [Link] package provides various event classes to handle these actions.
Classification of Events:
import [Link].*;
import [Link].*;
import [Link].*;
public class EventApplet extends Applet implements ActionListener{
Button b;
TextField tf;
public void init(){
tf=new TextField();
[Link](30,40,150,20);
b=new Button("Click");
[Link](80,150,60,50);
add(b);
add(tf);
[Link](this);
setLayout(null);
}
public void actionPerformed(ActionEvent e){
[Link]("Welcome");
}
}
Events in Java can be broadly classified into two categories based on how they are generated:
1. Foreground Events: Foreground events are the events that require user interaction to generate.
Examples of these events include Button clicks, Scrolling the scrollbar, Moving the cursor, etc.
2. Background Events: Events that don't require interactions of users to generate are known as
background events. Examples of these events are operating system failures/interrupts, operation
completion, etc.
Painting in Applet
import [Link].*;
import [Link].*;
import [Link].*;
public class MouseDrag extends Applet implements MouseMotionListener{
public void init(){
addMouseMotionListener(this);
setBackground([Link]);
}
public void mouseDragged(MouseEvent me){
Graphics g=getGraphics();
[Link]([Link]);
[Link]([Link](),[Link](),5,5);
}
public void mouseMoved(MouseEvent me){}
}
Parameter in Applet
• We can get any information from the HTML file as a parameter.
For this purpose, Applet class provides a method named getParameter().
Syntax:
public String getParameter(String parameterName)