Understanding Applet (Part-I)

Discussion in 'Java' started by techgeek.in, Apr 17, 2010.

  1. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in

    Introduction



    Java can be used to create two types of programs: applications and applets. An application is a program that runs on a computer, under the operating system of that Computer just like the one created using C or C++.

    An applet is an application that is accessed on the Internet Server designed to be transmitted over the Internet and executed by a Java-compatible Web browser. An applet is actually a tiny Java program, dynamically downloaded across the network, just like an image, sound file, or video clip. After an applet arrives on the client, it has limited access to resources, so that it can produce an arbitrary multimedia user interface and run complex computations without introducing the risk of viruses or breaching data integrity.

    The point to be noted is that an applet is an intelligent program, not just an animation or media file. In other words, an applet is a program that can react to user input and dynamically change not just run the same animation or sound over and over.

    A simple Applet program:-
    Code:
        import java.awt.*;
        import java.applet.*;
        
       public class SimpleApplet extends Applet 
           {
              public void paint(Graphics g) 
                {
                  g.drawString("A Simple Applet", 20, 20);
                }
           }
    
    
    :discuss:
    • Applets are not executed by the console-based Java run-time interpreter. Rather, they are executed by either a Web browser or an applet viewer. Since all applets run in a window, it is necessary to include support for that window. Applets interact with the user through the Abstract Window Toolkit (AWT) classes. The AWT contains support for a window-based, graphical interface. It contains numerous classes and methods that allow you to create and manage windows. The AWT classes are contained in the java.awt package. Therefore all applets must import java.awt. It is one of Java’s largest and sophisticated packages but this applet makes very limited use of the AWT.
    • Every applet that you create must be a subclass of class Applet which provides the necessary support for applets. The Applet class is contained in the java.applet package. Applet contains several methods that give detailed control over the execution of your applet. Thus, all applets must import java.applet.
    • All applets must be declared as public, because it will be accessed by code that is outside the program.
    • Output to an applet’s window is not performed by System.out.println( ). Rather, it is handled with various AWT methods, such as drawstring. Input is also handled differently in an applet than in an application.
    • paint( ) method is defined by the AWT and must be overridden by the applet. paint( ) is called each time that the applet must redisplay or redraw its output. This situation can occur for several reasons. For example, the window in which the applet is running can be overwritten by another window and then uncovered. Or, the applet window can be minimized and then restored. paint( ) is also called when the applet begins execution. The paint( ) method has one parameter of type Graphics. This parameter contains the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required.
      Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
      This method outputs a string specified as its argument beginning at the specified X,Y location. It has the following general form:
      void drawString(String message, int x, int y)

    • Execution of an applet does not begin at main( ). An applet begins execution when the name of its class is passed to an applet viewer or to a network browser.
    • After writing the source code of an applet, compile it just the same way as we compile java applications. Running an applet involves a different process.
    :auto:

    Running An Applet


    There are two ways in which you can run an applet:

    1. Execute the applet within a Java-compatible Web browser.
    2. Use an applet viewer, such as the standard SDK tool, appletviewer. An applet viewer executes your applet in a window. This is generally the fastest and easiest way to test your applet.
    3. To execute an applet in a Web browser, you need to write a short HTML text file that contains the appropriate APPLET tag. Here is the HTML file that executes simpleapplet:
      <applet code="simpleapplet" width=200 height=60>
      </applet>


      After you create this file, execute your browser and then load this file. This causes simpleapplet to be executed.
    4. To execute simpleapplet with an applet viewer, you may also execute the HTML file shown earlier. For example, if the preceding HTML file is called RunApp.html, then the following command line will run simpleapplet:
      C:\>appletviewer RunApp.html
    5. However, a more convenient method exists that speeds up testing.
      Include a comment at the head of Java source code file that contains the APPLET tag. This documents our code with a prototype of the necessary HTML statements and we can test our compiled applet merely by starting the appletviewer with your Java source code file. If you use this method, the SimpleApplet source file looks like this:
    Code:
    import java.awt.*;
    import java.applet.*;
    /*
    <applet code="simpleapplet" width=500 height=500>
    </applet>
    */
    public class simpleapplet extends Applet
    {
        public void paint(Graphics g)
        {
            setBackground(Color.pink);
            g.drawString("A Simple Applet Program",50,20);
        }
            
    }
    
    
    :drum: We can develop an applet in the following way:


    1. Create a java source file.
    2. Compile it.
    3. Execute the applet viewer, specifying the name of your applet’s source file. The applet viewer will encounter the APPLET tag within the comment and execute your applet.
    :driving:

    Applet Architecture



    As an applet is a window based program so its architecture is different from that of console based programs.

    • Applets are event driven. An applet resembles a set of interrupt service routines. An applet waits until an event occurs. The AWT notifies the applet about an event by calling an event handler that has been provided by the applet. Once this happens, the applet must take appropriate action and then quickly return control to the AWT.
    • The user initiates interaction with an applet. in a nonwindowed program, when the program needs input, it will prompt the user and then call some input method, such as readLine( ). This is not the way it works in an applet. Instead, the user interacts with the applet as he or she wants, when he or she wants. These interactions are sent to the applet as events to which the applet must respond. For example, when the user clicks a mouse inside the applet’s window, a mouse-clicked event is generated. If the user presses a key while the applet’s window has input focus, a keypress event is generated. Applets can contain various controls, such as push buttons and check boxes. When the user interacts with one of these controls, an event is generated.
    • The applet is not in control of the thread of execution; it simply responds when the browser or viewer tells it to. For this reason, the methods you write must take the necessary action and return promptly--they are not allowed to enter time-consuming (or infinite) loops. In order to perform a time-consuming or repetitive task, such as animation, an applet must create its own thread, over which it does have complete control.

    :skull:

    An Applet Skeleton



    All applets override a set of methods that provides the basic mechanism by which the browser or applet viewer interfaces to the applet and controls its execution. Four of these methods—init( ), start( ), stop( ), and destroy( ) are defined by Applet. Another,
    paint( ), is defined by the AWT Component class. Default implementations for all of these methods are provided. Applets do not need to override those methods they do not use.
    Code:
    import java.awt.*;
    import java.applet.*;
    /*<applet code="first" height=500 width=500>
    </applet>
    */
     
    public class first extends Applet
    {
        String msg="";
        public void init()
        {
            setBackground(Color.pink);
            msg+=" Inside Init";
        }
        public void start()
        {
            msg+=" Inside Start";
        }
        public void paint(Graphics g)
        {
            msg+=" Inside paint";
            g.drawString(msg,50,50);
                
        }
        public void stop()
        {
            msg+=" Inside stop";
        }
        public void destroy()
        {
            msg+=" Inside destroy";
        }
        
        
    }
    
    Now write first.html with the following code:
    Code:
    <html>
    <head>
    </head>
    <applet code="first" height=1000 width=1000>
    </applet>
    </html>
    
    Now execute this html in the browser it gives the following output:
    [​IMG]

    Applet Initialization and Termination



    :daisy: When an applet begins, the AWT calls the following methods, in this sequence:
    1. init( )
    2. start( )
    3. paint( )
    :death: When an applet is terminated, the following sequence of method calls takes place:
    1. stop( )
    2. destroy( )
    init( ):- The init( ) method is the first method to be called. This is where we should initialize variables. This method is called only once during the run time of your applet.

    start( ):- The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Whereas init( ) is called once—the first time an applet is loaded—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 your 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. Or the 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. The 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. This context is used whenever output to the applet is required.

    stop( ):- The stop( ) method is called when a web browser leaves the HTML document containing the applet when it goes to another page, for example. We should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.

    destroy( ):- The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. This frees up any resources the applet may be using. The stop( ) method is always called before destroy( ).
     
    Last edited by a moderator: Jan 21, 2017
  2. Prateek.sem

    Prateek.sem New Member

    Joined:
    Apr 26, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Very helpful..
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. TheGopi

    TheGopi New Member

    Joined:
    Aug 16, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    chennai
  6. sbsl

    sbsl Banned

    Joined:
    Sep 20, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    good information.... applets are programs .we used in java for animation?
     
  7. shravs

    shravs New Member

    Joined:
    May 5, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    manglore
    thanx a lot for information it is really good and helpfull for starter
     
  8. suman52

    suman52 New Member

    Joined:
    Dec 16, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    it's the very good
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice