0% found this document useful (0 votes)
77 views21 pages

Java Applet

Java Applets are embedded programs that run in web browsers to create dynamic content on the client side, offering advantages like reduced response time and cross-platform compatibility. They require a plug-in to execute and follow a specific life cycle including initialization, starting, painting, stopping, and destruction. Applets can display graphics, images, and handle events using Java's AWT classes, and can be run via HTML files or the applet viewer tool.

Uploaded by

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

Java Applet

Java Applets are embedded programs that run in web browsers to create dynamic content on the client side, offering advantages like reduced response time and cross-platform compatibility. They require a plug-in to execute and follow a specific life cycle including initialization, starting, painting, stopping, and destruction. Applets can display graphics, images, and handle events using Java's AWT classes, and can be run via HTML files or the applet viewer tool.

Uploaded by

Firaol Bonsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

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

 There are many advantages of applet. These are:


1. It works at client side so less response time.
2. Secured
3. It can be executed by browsers running
under many platforms, including Linux,
Windows, Mac Os etc.
Drawback of Applet

 Plug-in is required at client browser to execute


applet.
Life-cycle of Java Applet

 Applet is initialized.
 Applet is started.
 Applet is painted.
 Applet is stopped.
 Applet is destroyed.
[Link] class

 For creating any applet [Link] class


must be inherited.
 It provides 4 life cycle methods of applet:
1. public void init(): is used to initialized the
Applet. It is invoked only once.
Cont.

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:

//[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:

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

 [Link] class provides many methods for


graphics programming.
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.
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");
}

public void paint(Graphics g) {


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
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");
}
}
[Link]
<html>
<body>
<applet code="[Link]" width="300"
height="300">
</applet>
</body>
</html>

You might also like