Applet

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

APPLETS

Introduction
• An applet is a Java program that runs in a Web browser. An applet
can be a fully functional Java application because it has the entire
Java API at its disposal.
• There are some important differences between an applet and a
standalone Java application, including the following −
• An applet is a Java class that extends the java.applet.Applet class.
• A main() method is not invoked on an applet, and an applet class
will not define main().
• Applets are designed to be embedded within an HTML page.
• When a user views an HTML page that contains an applet, the code
for the applet is downloaded to the user's machine.
Continue…………..
• A JVM is required to view an applet. The JVM can be either a plug-in
of the Web browser or a separate runtime environment.
• The JVM on the user's machine creates an instance of the applet
class and invokes various methods during the applet's lifetime..
• Advantage of Applet
• 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 platforms,
including Linux, Windows, Mac Os etc.
• Drawback of Applet
• Plugin is required at client browser to execute applet.
Two Types of Applets
• The first are those based directly on the Applet class. These
applets use the Abstract Window Toolkit (AWT) to provide the
graphic user interface (or use no GUI at all).
• The second type of applets are those based on the Swing class
JApplet. Swing applets use the Swing classes to provide the GUI.
Swing offers a richer and often easier-to-use user interface than
does the AWT.
Applet Basics
• All applets are subclasses (either directly or indirectly) of Applet.
Applets are not stand-alone programs. Instead, they run within
either a web browser or an applet viewer.
• Execution of an applet does not begin at main( ).
• Output to your applet’s window is not performed by
System.out.println( ). Rather, in non-Swing applets, output is
handled with various AWT methods, such as drawString( ), which
outputs a string to a specified X,Y location.
• To use an applet, it is specified in an HTMLfile. One way to do this
is by using the APPLET tag.
The Applet Class
Applet Architecture

• An applet is a window-based program.


• Applets are event driven.
• The user initiates interaction with an applet
Life Cycle of an Apple

•Four methods in the Applet class gives you the framework on which you
build any serious applet −

•init − This method is intended for whatever initialization is needed for


your applet. It is called after the param tags inside the applet tag have
been processed.
•start − This method is automatically called after the browser calls the
init method. It is also called whenever the user returns to the page
containing the applet after having gone off to other pages.
•stop − This method is automatically called when the user moves off the
page on which the applet sits. It can, therefore, be called repeatedly in
the same applet.
• destroy − This method is only called when the browser shuts down
normally. Because applets are meant to live on an HTML page, you
should not normally leave resources behind after a user leaves the
page that contains the applet.
• paint − Invoked immediately after the start() method, and also any
time the applet needs to repaint itself in the browser. The paint()
method is actually inherited from the java.awt.
Simple example of Applet by
html file:
• To execute the applet by html file, create an applet and compile it. After
that create an html file and place the applet code in html file.
Now click the 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:
• To execute the applet by appletviewer tool, create an applet that
contains applet tag in comment and compile it. After that run it by:
appletviewer First.java. Now Html file is not required but it is for testing
purpose only.
• //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> 
• */  
• To execute the applet by appletviewer
tool, write in command prompt:
• c:\>javac First.java c:\>appletviewer
First.java
Simple Applet Display
Methods
• void drawString(String message, int x, int y)
• Here, message is the string to be output beginning at x,y.
• In a Java window, the upper-left corner is location 0,0.
• To set the background color of an applet’s window, use
setBackground( ).
• To set the foreground color (the color in which text is shown, for
example), use setForeground( ).
• These methods are defined by Component, and they have the
following general forms:
• void setBackground(Color newColor)
• void setForeground(Color newColor)
• The following example sets the background color to green and the
text color to red:
• setBackground(Color.green);
• setForeground(Color.red);
• You can obtain the current settings for the background and
foreground colors by calling getBackground( ) and
getForeground( ), respectively and are shown here:
• Color getBackground( )
• Color getForeground( )
• /* A simple applet that sets the foreground and
• background colors and outputs a string. */
• import java.awt.*;
• import java.applet.*;
• /*
• <applet code="Sample" width=300 height=50>
• </applet>
• */
• public class Sample extends Applet{
• String msg;
• // set the foreground and background colors.
• public void init() {
• setBackground(Color.cyan);
• setForeground(Color.red);
• msg = "Inside init( ) --";
• }
• // Initialize the string to be displayed.
• public void start() {
• msg += " Inside start( ) --";
• }
• // Display msg in applet window.
• public void paint(Graphics g) {
• msg += " Inside paint( ).";
• g.drawString(msg, 10, 30);
• }
• }

You might also like