RENUGADEVI.S, MSC.,CS
NADAR SARASWATHI COLLEGE
OF ARTS AND SCINCE. THENI.
applet
What is applet?
 An applet is a java program that can be
embedded into a webpage. It runs inside the
web browser and works at client side.
 An applet is embedded in an HTML page
using the applet or object tag and hosted on a
web server.
 Applet are used to make the website more
dynamic and entertaining.
Important points about
Applet
1. All applets are sub-classes(either directly or
indirectly) of java.applet.Applet class.
2. Applet are not stand-alone programs.
Instead, they run within either a web
browser or an applet viewer. JDK provides a
standard applet viewer tool called applet
viewer.
3. In general, execution of an applet does not
begin at main() method.
4. Output of an applet window is not
performed by System.out.println().
5. Rather with various AWT methods, such as
drawString().
Life cycle of an applet
 It is important to understand the order in
which the various methods shown in the
above image are called. When an applet
begins, the following methods are called, in
this sequence:
1.init()
2.start()
3.paint()
 When an applet is terminated, the following
sequence of method calls takes place:
1.stop()
2.destory()
 init()
The init() method is the first method to be
called. Where you should initialize variables.
This method is called only once during the
runtime of applet.
Start(): The start() method is called after
init(). It is also called to restart an applet after
it has been stopped. Note that init() is called
once. i.e.,When the first time an applet is
loaded whereas start() is called each time an
applet’s HTML document is displayed
onscreen. So, if a user leaves a web page and
comes back, the applet resumes execution at
start().
Paint():
The paint() method is called each time an
AWT-based applet’s output must be redrawn.
This situation can occur for several reasons.
For example, the window in which the applet
is running may be overwritten by another
window and then uncovered. ORThe applet
window may be minimized and then
restored.
 Paint() is also called when the applet
begins execution. Whatever the cause,
whenever the applet must redraw its output,
paint() is called.
 paint() method has one parameter of
type Graphics. This parameter will contain
the graphics context, which describes the
graphics environment in which the applet is
running.
Stop():
The stop() method is called when a web
browser leaves the HTML document
containing the applet-when it goes to
another page, example: When stop() is
called, the applet is probably running. Use
stop() to suspend threads that don’t need to
run when the applet is not visible. Restart
them when start() is called if the user returns
to the page.
destroy():
The destroy() method is called when
the environment determines that applet
needs to be removed completely from
memory. At this point , should free up any
resources the applet may be using. The
stop() method is always called before
destroy().
CREATING AN EXECUTABLE
APPLET
 Executable applet is nothing but the .class file
of the applet, which is obtained by compiling
the source code of the applet. Compiling an
applet is exactly the same as compiling an
application. Therefore, we can use the
The Applet class
 Every applet is an extension of the
java.applet.Applet class. The base Applet
class provides methods that a derived Applet
class may call to obtain information and
services from the browser context.
 These include methods that do the following-
 Get applet parameters
 Get the network location of the HTML file
that contains the applet.
 Get the network location of the applet class
directory.
 Print a status message in the browser
 Fetch an image
 Fetch an audio clip
 Play an audio clip
 Resize the applet
 Additionally, the Applet class provides an
interface by which the viewer or browser
obtains information about the applet and
controls the applet’s execution. The viewer
may-
 Request information about the author,
version, and copyright of the applet.
 Request a description of the parameters the
applet recognizes.
 Initialize the applet
 Destroy the applet
 Start the applet’s execution
 Stop the applet’s execution
The Applet class provides default
implementations of each of these methods.
Those implementations may be overridden as
necessary.
Invoking an Applet
 An applet may be invoked by embedding
directives in an HTML file and viewing the file
through an applet viewer or java enabled
browser.
 The <applet> tag is the basis for embedding
an applet in an HTML file.
Example program
 Draw smiley in applet window using java applet class
method.
Import java.applet.Applet;
Import java.awt.*;
Public class smileyexe extendsApplet{
Public void paint(Graphics g){
g.Set color(color,yellow);
g.filloval(20,20,150,150);
g.Set color(color,black);
g.filloval(50,60,15,25);
g.Set color(color,black);
g.filloval(120,60,15,25);
Int x[ ]={95,85,106,95};
Int y[ ]={85,104,104,85};
g.drawpolygon(x,y,4);
g.DrawArc(55,95,78,50,0,-180);
g.drawLine(50,126,60,116);
g.drawLine(128,115,139,126);
}
}
/* <appletcode=“smiley class” width=“200” height=“200”>
</applet>
*/
Thank you

Appletjava

  • 1.
    RENUGADEVI.S, MSC.,CS NADAR SARASWATHICOLLEGE OF ARTS AND SCINCE. THENI. applet
  • 2.
    What is applet? An applet is a java program that can be embedded into a webpage. It runs inside the web browser and works at client side.  An applet is embedded in an HTML page using the applet or object tag and hosted on a web server.  Applet are used to make the website more dynamic and entertaining.
  • 3.
    Important points about Applet 1.All applets are sub-classes(either directly or indirectly) of java.applet.Applet class. 2. Applet are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer. 3. In general, execution of an applet does not begin at main() method.
  • 4.
    4. Output ofan applet window is not performed by System.out.println(). 5. Rather with various AWT methods, such as drawString().
  • 5.
    Life cycle ofan applet
  • 6.
     It isimportant to understand the order in which the various methods shown in the above image are called. When an applet begins, the following methods are called, in this sequence: 1.init() 2.start() 3.paint()
  • 7.
     When anapplet is terminated, the following sequence of method calls takes place: 1.stop() 2.destory()  init() The init() method is the first method to be called. Where you should initialize variables. This method is called only once during the runtime of applet.
  • 8.
    Start(): The start()method is called after init(). It is also called to restart an applet after it has been stopped. Note that init() is called once. i.e.,When the first time an applet is loaded whereas start() is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start().
  • 9.
    Paint(): The paint() methodis called each time an AWT-based applet’s output must be redrawn. This situation can occur for several reasons. For example, the window in which the applet is running may be overwritten by another window and then uncovered. ORThe applet window may be minimized and then restored.
  • 10.
     Paint() isalso called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint() is called.  paint() method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running.
  • 11.
    Stop(): The stop() methodis called when a web browser leaves the HTML document containing the applet-when it goes to another page, example: When stop() is called, the applet is probably running. Use stop() to suspend threads that don’t need to run when the applet is not visible. Restart them when start() is called if the user returns to the page.
  • 12.
    destroy(): The destroy() methodis called when the environment determines that applet needs to be removed completely from memory. At this point , should free up any resources the applet may be using. The stop() method is always called before destroy().
  • 13.
    CREATING AN EXECUTABLE APPLET Executable applet is nothing but the .class file of the applet, which is obtained by compiling the source code of the applet. Compiling an applet is exactly the same as compiling an application. Therefore, we can use the
  • 14.
    The Applet class Every applet is an extension of the java.applet.Applet class. The base Applet class provides methods that a derived Applet class may call to obtain information and services from the browser context.  These include methods that do the following-  Get applet parameters  Get the network location of the HTML file that contains the applet.
  • 16.
     Get thenetwork location of the applet class directory.  Print a status message in the browser  Fetch an image  Fetch an audio clip  Play an audio clip  Resize the applet
  • 17.
     Additionally, theApplet class provides an interface by which the viewer or browser obtains information about the applet and controls the applet’s execution. The viewer may-  Request information about the author, version, and copyright of the applet.  Request a description of the parameters the applet recognizes.
  • 18.
     Initialize theapplet  Destroy the applet  Start the applet’s execution  Stop the applet’s execution The Applet class provides default implementations of each of these methods. Those implementations may be overridden as necessary.
  • 19.
    Invoking an Applet An applet may be invoked by embedding directives in an HTML file and viewing the file through an applet viewer or java enabled browser.  The <applet> tag is the basis for embedding an applet in an HTML file.
  • 20.
    Example program  Drawsmiley in applet window using java applet class method. Import java.applet.Applet; Import java.awt.*; Public class smileyexe extendsApplet{ Public void paint(Graphics g){ g.Set color(color,yellow); g.filloval(20,20,150,150); g.Set color(color,black); g.filloval(50,60,15,25); g.Set color(color,black); g.filloval(120,60,15,25); Int x[ ]={95,85,106,95}; Int y[ ]={85,104,104,85}; g.drawpolygon(x,y,4); g.DrawArc(55,95,78,50,0,-180);
  • 21.
  • 22.