Applet Programming in Java

  • Applets are small java programs used in internet computing.
  • Can b transported from one computer to another
  • And run using applet viewer or java enabled browser.
Like APIs it can perform arithmetic operations, display graphics, play sounds ,accept user inputs, create animation etc..

  • Applets r window based programs 
  • Applets are event driven
  • An applet is embedded in a Web page
  • Applets can be loaded from local disk or from an application (Web) server
We can embed applets into web pages in 2 ways:
  1. We can write our own applet and embed them into the web pages- Local Applets
-an applet developed locally and stored in a local system. No need of internet or n/w connection to access such applets. 

 2. We can down load an applet from a remote s/m and then embed it into the web page-Remote Applets.
-developed by some one else and stored on a remote s/m connected to the net.                                                               
Java applet is executed by a browser:

  • The browser provides the JVM  
  • No main() for initiating the execution.
  • Cannot run independently
  • Cannot read from or write to the files in the local system
  • Cannot run any program from he local system
  • Constructors tend to be empty
  • Are restricted from using libraries from other languages like C and C++
  • Almost always graphical
  • Applets must provide a class that extends Applet
 The Applet Class
 To build an applet, create a subclass of Applet, and override certain methods:
  • init() 
  • start() 
  • stop()  
  • destroy()  
  • paint()
Initialization State
Applets enters in initialization when it is first loaded.
This is achieved by calling the init() method of Applet.
This method is called only once during the runtime of an Applet
public void init()


 Running State
Applet enters in running state when the system calls the start() method of Applet class.
It occurs automatically after initialization.
After idle state also start can occur.

                      public void start()
                       { …    }
Idle or Stopped State
Applet becomes idle when it is stopped from running.
It occurs automatically when we leave the page containing the currently running applet.
By using stop() also it can b performed

                             public void stop()
                        { …    }
Dead state
An applet is in dead state when it is removed from memory.
This  occurs automatically by invoking the destroy() method
Like initialization destroy occurs only once.
If the applet has created any resources like threads we may override the destroy() method to clean up these resources.
        public void destroy()
               { …    }
Display state
An applet is in display state whenever it has to perform some output operations on the screen.
This happens just after applet enters into the running state.
The paint() method is called for this purpose
This method is called when each time when applets output
This is defined in Container class of AWT.
            public void paint(Graphics g)
             { … Display Statements   }
Example of java applet

import java.awt.*;
import java.applet.Applet;
public class FirstApplet extends Applet
             String msg;
              public void init()
                msg="initialized in init()  ";
   public void start()
           msg+="inside start ";
   public void paint(Graphics g)

Here HTML code: just see the syntax 
The name of html is FirstApplet.html
and code

<applet code=FirstApplet.class width=500 height=400>