KEMBAR78
Java applet - java | PPTX
Java Applet
Objectives
• Definition
• Use of Java
• Advantages
• Methods
• Life Cycle
• Examples
• Drawback
What Is Applet?
An Applet is a special kind of java program that is designed to be transmitted
over the internet and automatically executed by java compatible web browser.
Why We Use Applet 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.
Advantages of Applet in 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.
How to run an Applet?
• By html file.
• By appletViewer tool (for testing purpose).
Applet Header Files
• import java.applet.*
• import java.awt.*
Methods
Function Use
init() is used 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
init()
The init() method is the first method to be
called
import java.applet.Applet;
public class First extends Applet {
public void init() {
}
}
paint()
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet {
public void paint(Graphics g){
g.drawString("Welcome To Our
Presentation", 100, 75);
}
}
Example :01
import java.applet.*;
import java.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 02
import java.applet.Applet;
import java.awt.*;
public class RECt extends Applet {
public void init() {
}
public void paint(Graphics g){
g.drawRect(70,50,300,200);
}
}
Example 03
import java.applet*;
import java.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);
}
}
Example 04
import java.applet.Applet;
import java.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);
}
}
Example 05
package applete;
import java.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);
}
}
Example 06
package applete;
import java.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");
}
}
Example 07
package applete;
import java.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){}
}
Example 08
Example 09
Drawback of Applet
• Plugin is required at client browser to execute applet

Java applet - java

  • 2.
  • 3.
    Objectives • Definition • Useof Java • Advantages • Methods • Life Cycle • Examples • Drawback
  • 4.
    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).
  • 8.
    Applet Header Files •import java.applet.* • import java.awt.*
  • 9.
    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() { } }
  • 11.
    paint() import java.applet.Applet; import java.awt.Graphics; publicclass First extends Applet { public void paint(Graphics g){ g.drawString("Welcome To Our Presentation", 100, 75); } }
  • 13.
    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); } }
  • 14.
    Example 02 import java.applet.Applet; importjava.awt.*; public class RECt extends Applet { public void init() { } public void paint(Graphics g){ g.drawRect(70,50,300,200); } }
  • 15.
    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){} }
  • 20.
  • 21.
  • 22.
    Drawback of Applet •Plugin is required at client browser to execute applet