An applet is a Java program that runs in a web browser. Applets allow interactive components and enhancements to be added to webpages. Advantages include faster response times since applets run on the client-side, security, and platform independence. The main methods in an applet's lifecycle are init(), start(), stop(), and destroy(). Drawbacks include requiring the Java plugin to be installed in the client's browser.
What Is Applet?
AnApplet is a special kind of java program that is designed to be transmitted
over the internet and automatically executed by java compatible web browser.
5.
Why We UseApplet In Java?
• Java Applets are usually used to add small, interactive components
or enhancements to a webpage. These may consist of buttons,
scrolling text, or stock tickers, but they can also be used to display
larger programs like word processors or games.
6.
Advantages of Appletin Java
• There are many advantages of applet. They are as follows:
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many plateforms,
including Linux, Windows, Mac Os etc.
7.
How to runan Applet?
• By html file.
• By appletViewer tool (for testing purpose).
Methods
Function Use
init() isused to initialized the Applet. It is invoked only
once
start() is invoked after the init() method or browser is
maximized. It is used to start the Applet.
stop() is used to stop the Applet. It is invoked when
Applet is stop or browser is minimized.
destroy() is used to destroy the Applet. It is invoked only
once.
paint() is used to paint the Applet
10.
init()
The init() methodis the first method to be
called
import java.applet.Applet;
public class First extends Applet {
public void init() {
}
}
Example :01
import java.applet.*;
importjava.awt.*;
public class Drawline extends Applet {
public void init() {
}
public void paint(Graphics g){
g.drawLine(20,20,100,20);
g.drawLine(20,20,20,100);
}
}
Example 03
import java.applet*;
importjava.awt.*;
public class Backcolor extends Applet {
public void init(){
setBackground(Color.blue);
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.drawString("JAVA APPLET",40,100);
}
}
16.
Example 04
import java.applet.Applet;
importjava.awt.*;
import java.awt.Image;
public class Image_OP extends Applet {
Image picture;
public void init() {
picture=getImage(getDocumentBase(),"JKKNIU.jpg");
}
public void paint(Graphics g){
g.drawImage(picture,50,50,this);
}
}
17.
Example 05
package applete;
importjava.applet.Applet;
import java.awt.*;
public class Second extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome",150, 70);
g.drawRect(70,100,40,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.drawString("Welcome to my applete program",50, 50);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}
}
18.
Example 06
package applete;
importjava.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class EventApplet extends JApplet implements ActionListener{
JButton b;
JTextField tf;
public void init(){
tf=new JTextField();
tf.setBounds(30,40,150,20);
b=new JButton("Click");
b.setBounds(80,150,70,40);
add(b);add(tf);
b.addActionListener(this);
setLayout(null);
}
public void actionPerformed(ActionEvent e){
tf.setText("Welcome");
}
}
19.
Example 07
package applete;
importjava.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Painting extends Applet implements MouseMotionListener{
public void init(){
addMouseMotionListener(this);
setBackground(Color.red);
}
public void mouseDragged(MouseEvent me){
Graphics g=getGraphics();
g.setColor(Color.white);
g.fillOval(me.getX(),me.getY(),5,5);
}
public void mouseMoved(MouseEvent me){}
}