APPLET
By
M. Sri Nandhini
Introduction
 Applet is small java program that can be
easily transported over the network from
one computer to other.
 Used in internet applications.
 Embedded in an html page, can be
downloaded from the server and run on the
client, so as to do a specific kind of job.
 An applet is a program that runs in the
context of a browser session.
Applets, Webpage,
Client, Server
 Applets are programs stored on a web
server, similar to web pages.
 When an applet is referred to in a web page
that has been fetched and processed by a
browser, the browser generates a request
to fetch the applet program, the executes
the program in the browser’s execution
context on the client host.
Types of java programs
 Stand Alone Program
 Run on a single machine
 Compiler-javac
 Interpreter-java
 Web Based Program
 Compiler-javac
 Interpreter-applet viewer or web browser
 Must Subclass of Applet
 Import java.awt
Application vs Applet
Java Applications
• Stand-alone program
• Runs independently
• Has a main() method
• No HTML file
• Run using JDK’s
java interpreter
Java Applet
• Embedded program
• Runs in a Web
browser
• No main() method
• Requires an HTML
file
• Run using JDK’s
applet viewer.
Applet Class
Applet Example
import java.applet.*;
import java.awt.*;
public class FirstApplet extends applet{
public void paint(Graphics g)
{
g.drawString(“Hello World”,10,50);
}
}
Output
Applet Life Cycle
 An applet may move from one state to
another dependent upon a set of default
behaviours inherited in the form of methods
from ‘Applet’ class.
 These states are
 Born
 Running
 Idle
 Dead
Applet state diagram
Life Cycle of Applet
 Init()-
 Creates the objects needed by the applet
 Sets up initial values, load font and images or
set up colors.
 Called only once during the lifetime of an
applet.
 Start()-
 Moves to this phase automatically after the
initialization state.
 If the applet is stopped or it goes to idle state
start() method must be called in order to force
the applet again to the running state.
Life Cycle of Applet
Paint()-
 This method is called each time to draw
and redraw the output of an applet.
Stop()-
 Idle state, once it is stopped from
running.
 Destroy()-
 An applet goes to dead state when it is
destroyed by invoking the destroy() of
applet class.
 It results in complete removal of applet
from the memory.
Common Methods
 drawString()-
 Member of graphics class used to output a
string to an applet.
 It is typically called from within the paint() or
update() method.
 Void drawString(String msg,int a, int b).
 setBackground() & getBackground()-
 Belongs to component class used to set
and get the background color.
 Void setBackground(Color anyColor)
Common Methods
 Predefined constants for each color such as
Color.red can be used.
 setForeground()& getForeground()-
 Set and gets the color of the text to be
displayed on the foreground of the applet
window.
 Void setForeground(Color anyColor)
 showStatus()-
 Display any string In the status window of
the browser.
 Void showString(Stringtext).
Applet Execution
 An applet program is a written as a
subclass of the java. Applet class or the
javax.swing.Japplet class. There is no main
method: you must override the start
method. Applet objects uses AWT for
graphics. Japplet uses SWING.
 It is a graphics object that runs in a thread
object, so every applet can perform
graphics, and runs in parallel to the browser
process.
Applet Execution
When the applet is loaded,these methods are
automatically invoked in order:
 The init() method is invoked by the Java
Virtual Machine.
 The start() method.
 The paint()method.
 The applet is now running and rendered on
the web page.
Applet Security
For security reasons, applets that are loaded
over the network have several restrictions.
 An applet cannot ordinarily read or write
files on the computer that its executing on.
 An applet cannot make network
 connections except to the host that it came
from.
Summary
 An applet is a java class.
 Its code is downloaded from a web server.
 It is run in the browser’s environment on the
client host.
 It is invoked by a browser when it scans a
web page and encounters a class specified
with the APPLET tag. For security reason ,
the execution of an applet is normally
subject to restrictions:
 Applet cannot access files in the file system
on the client host.
 Applet cannot make network connection
exception to the server host from which it
originated.

Applet (1)

  • 1.
  • 2.
    Introduction  Applet issmall java program that can be easily transported over the network from one computer to other.  Used in internet applications.  Embedded in an html page, can be downloaded from the server and run on the client, so as to do a specific kind of job.  An applet is a program that runs in the context of a browser session.
  • 3.
    Applets, Webpage, Client, Server Applets are programs stored on a web server, similar to web pages.  When an applet is referred to in a web page that has been fetched and processed by a browser, the browser generates a request to fetch the applet program, the executes the program in the browser’s execution context on the client host.
  • 4.
    Types of javaprograms  Stand Alone Program  Run on a single machine  Compiler-javac  Interpreter-java  Web Based Program  Compiler-javac  Interpreter-applet viewer or web browser  Must Subclass of Applet  Import java.awt
  • 5.
    Application vs Applet JavaApplications • Stand-alone program • Runs independently • Has a main() method • No HTML file • Run using JDK’s java interpreter Java Applet • Embedded program • Runs in a Web browser • No main() method • Requires an HTML file • Run using JDK’s applet viewer.
  • 6.
  • 7.
    Applet Example import java.applet.*; importjava.awt.*; public class FirstApplet extends applet{ public void paint(Graphics g) { g.drawString(“Hello World”,10,50); } }
  • 8.
  • 9.
    Applet Life Cycle An applet may move from one state to another dependent upon a set of default behaviours inherited in the form of methods from ‘Applet’ class.  These states are  Born  Running  Idle  Dead
  • 10.
  • 11.
    Life Cycle ofApplet  Init()-  Creates the objects needed by the applet  Sets up initial values, load font and images or set up colors.  Called only once during the lifetime of an applet.  Start()-  Moves to this phase automatically after the initialization state.  If the applet is stopped or it goes to idle state start() method must be called in order to force the applet again to the running state.
  • 12.
    Life Cycle ofApplet Paint()-  This method is called each time to draw and redraw the output of an applet. Stop()-  Idle state, once it is stopped from running.  Destroy()-  An applet goes to dead state when it is destroyed by invoking the destroy() of applet class.  It results in complete removal of applet from the memory.
  • 13.
    Common Methods  drawString()- Member of graphics class used to output a string to an applet.  It is typically called from within the paint() or update() method.  Void drawString(String msg,int a, int b).  setBackground() & getBackground()-  Belongs to component class used to set and get the background color.  Void setBackground(Color anyColor)
  • 14.
    Common Methods  Predefinedconstants for each color such as Color.red can be used.  setForeground()& getForeground()-  Set and gets the color of the text to be displayed on the foreground of the applet window.  Void setForeground(Color anyColor)  showStatus()-  Display any string In the status window of the browser.  Void showString(Stringtext).
  • 15.
    Applet Execution  Anapplet program is a written as a subclass of the java. Applet class or the javax.swing.Japplet class. There is no main method: you must override the start method. Applet objects uses AWT for graphics. Japplet uses SWING.  It is a graphics object that runs in a thread object, so every applet can perform graphics, and runs in parallel to the browser process.
  • 16.
    Applet Execution When theapplet is loaded,these methods are automatically invoked in order:  The init() method is invoked by the Java Virtual Machine.  The start() method.  The paint()method.  The applet is now running and rendered on the web page.
  • 17.
    Applet Security For securityreasons, applets that are loaded over the network have several restrictions.  An applet cannot ordinarily read or write files on the computer that its executing on.  An applet cannot make network  connections except to the host that it came from.
  • 18.
    Summary  An appletis a java class.  Its code is downloaded from a web server.  It is run in the browser’s environment on the client host.  It is invoked by a browser when it scans a web page and encounters a class specified with the APPLET tag. For security reason , the execution of an applet is normally subject to restrictions:
  • 19.
     Applet cannotaccess files in the file system on the client host.  Applet cannot make network connection exception to the server host from which it originated.