0% found this document useful (0 votes)
44 views

Java Applet

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)
44 views

Java Applet

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.
java.applet.Applet class

 For creating any applet java.applet.Applet 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.
java.awt.Component 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:

//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{

public void paint(Graphics g){


g.drawString("welcome",150,150);
}
}
myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>
Simple example of Applet by appletviewer tool:

//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{

public void paint(Graphics g){


g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
Displaying Graphics in Applet

 java.awt.Graphics class provides many methods for


graphics programming.
Example of Graphics in applet:
import java.applet.Applet;
import java.awt.*;

public class GraphicsDemo extends Applet{

public void paint(Graphics g){


g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);

g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}}
myapplet.html
<html>
<body>
<applet code="GraphicsDemo.class" 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 java.awt.Graphics class provide a method
drawImage() to display the image.
Example of displaying image in applet:
import java.awt.*;
import java.applet.*;
public class DisplayImage extends Applet {

Image picture;

public void init() {


picture = getImage(getDocumentBase(),"sonoo.jpg");
}

public void paint(Graphics g) {


g.drawImage(picture, 30,30, this);
}

}
myapplet.html
<html>
<body>
<applet code="DisplayImage.class" width="300"
height="300">
</applet>
</body>
</html>
Animation in Applet
import java.awt.*;
import java.applet.*;
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++){
g.drawImage(picture, i,30, this);

try{Thread.sleep(100);}catch(Exception e){}
}
}
}
myapplet.html
<html>
<body>
<applet code="DisplayImage.class" width="300"
height="300">
</applet>
</body>
</html>
EventHandling in Applet
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class EventApplet extends Applet implements ActionListener{
Button b;
TextField tf;

public void init(){


tf=new TextField();
tf.setBounds(30,40,150,20);

b=new Button("Click");
b.setBounds(80,150,60,50);
add(b);add(tf);
b.addActionListener(this);

setLayout(null);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
}
myapplet.html
<html>
<body>
<applet code="EventApplet.class" width="300"
height="300">
</applet>
</body>
</html>

You might also like