0% found this document useful (0 votes)
11 views8 pages

Applet in Java

Java Applets are embedded programs that run in a web browser to create dynamic content, operating on the client side for reduced response time. They require a plugin for execution and follow a defined lifecycle with methods for initialization, starting, stopping, and destruction. Applets can display graphics, handle events, and utilize parameters from HTML files, making them useful for interactive applications and animations.

Uploaded by

pratuyshswain884
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Applet in Java

Java Applets are embedded programs that run in a web browser to create dynamic content, operating on the client side for reduced response time. They require a plugin for execution and follow a defined lifecycle with methods for initialization, starting, stopping, and destruction. Applets can display graphics, handle events, and utilize parameters from HTML files, making them useful for interactive applications and animations.

Uploaded by

pratuyshswain884
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Applet

• 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

• Plugin is required at client browser to execute applet.

Hierarchy of Applet:

Lifecycle of Java Applet:


1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
Lifecycle methods for Applet
The [Link] class 4 life cycle methods and [Link] class provides 1 life cycle
methods for an applet. [Link] class
1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is maximized. It is used to start
the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.

[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.

How to run an Applet?


There are two ways to run an applet
1. By html file.
2. By appletViewer tool (for testing purpose).

Simple example of Applet by html file


To execute the applet by html file, create an applet and compile it. After that create an html file and
place the applet code in html file. Now click the html file.
//[Link]
import [Link];
import [Link];
public class First extends Applet{
public void paint(Graphics g){
[Link]("welcome",150,150);
}
}
//[Link]

<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>
*/

Displaying Graphics in Applet


public abstract void drawString(String str, int x, int y): is used to draw the specified string.
public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and
height.
public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the default
color and specified width and height.
public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the specified
width and height.
public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the default color
and specified width and height.
public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1,
y1) and (x2, y2).
public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw
the specified image.
public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used
draw a circular or elliptical arc.
public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used to
fill a circular or elliptical arc.
public abstract void setColor(Color c): is used to set the graphics current color to the specified color.
public abstract void setFont(Font font): is used to set the graphics current font to the specified font.

Example of Graphics in applet:


import [Link]; import [Link].*;
public class GraphicsDemo extends Applet{
public void paint(Graphics g){
[Link]([Link]); [Link]("Welcome",50, 50);
[Link](20,30,20,300);
[Link](70,100,30,30);
[Link](170,100,30,30);
[Link](70,200,30,30);
[Link]([Link]);
[Link](170,200,30,30);
[Link](90,150,30,30,30,270);
[Link](270,150,30,30,0,180);
}
}

//[Link]
<html>
<body>
<applet code="[Link]" width="300" height="300">
</applet>
</body>
</html>

Displaying Image in Applet


Applet is mostly used in games and animation. For this purpose image is required to be displayed.
The [Link] class provide a method drawImage() to display the image.
Syntax of drawImage() method:
public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw
the specified image.
How to get the object of Image
The [Link] class provides getImage() method that returns the object of Image.
Syntax:
Public Image getImage(URL u, String image){}
Other required methods of Applet class to display image:
public URL getDocumentBase(): is used to return the URL of the document in which applet is
embedded.
public URL getCodeBase(): is used to return the base URL.

Example of displaying image in


applet: import [Link].*;
import [Link].*;
public class DisplayImage extends Applet {
Image picture;
public void init() {
picture = getImage(getDocumentBase(),“[Link]");
}
public void paint(Graphics g) {
[Link](picture, 30,30, this);
}
}
[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.

Event Handling Mechanism


Event handling is a mechanism that allows programs to control events and define what should happen
when an event occurs. Java uses the Delegation Event Model to handle events. This model consists of two
main components:
• Source: Events are generated from the source. There are various sources like buttons, checkboxes,
list, menu-item, choice, scrollbar, text components, windows, etc., to generate events.
• Listeners: Listeners are used for handling the events generated from the source. Each of these
listeners represents interfaces that are responsible for handling events.

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)

Example of using parameter in Applet:


import [Link];
import [Link];
public class UseParam extends Applet{
public void paint(Graphics g){
String str=getParameter("msg");
[Link](str,50, 50);
}
}
//[Link]
<html>
<body>
<applet code="[Link]" width="300" height="300">
<param name="msg" value="Welcome to applet">
</applet>
</body>
</html>

You might also like