- Small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run as part of a web document.
- Have limited access to the resources of the client machine to which it arrives, so that it can produce a GUI (Graphical User Interface) and run complex computations without introducing the risk of viruses or breaching data integrity.
Eg., An online log-antilog calculator or compound-interest, a calculator which we download (probably from Google server) and run on our machine, to get the result for our inputs.
Two types of applets:
- Applets based on Applet class which use the AWT(Abstract Window Toolkit) to provide the GUI (used when a very simple user interface is needed)
- Applets based on swing class JApplet which offers a richer and easier to use interface that does the AWT. However, JApplet inherits Applet.
Two ways of developing applets:
- Using JNLP (Java Network Launch Protocol). They are not needed for simple applications but for real-world applets, JNLP is the best choice.
- Specifying an applet directly in an HTML file without the use of JNLP
Important differences between applets and other programs:
- All applets are subclasses of the class Applet
- They are not standalone programs but they run within either a web browser or an appletviewer (a tool provided by JDK itself)
- Applet programs do not start execution at main(). Instead, they begin execution when the name of its class is passed to an appletviewer or to a network browser.
- Output to the applet window is not by System.out.println() but using AWT methods like drawstring°.
- Applets interact with the user through the AWT and not through the console-based 1/0 classes
Applets follow event-driven programming (and not procedural programming). An applet will wait until an event occurs. Once an event occurs, the Java Runtime system notifies the applet about the event, by calling an event handler that has been provided by the applet. In response to the notification, the applet must take appropriate action and then quickly return. It should not maintain control for a long period of time. In situations, where the applet needs to perform a repetitive task on its own (eg., displaying a scrolling message), an additional thread of execution must be started.
In console-based programs, when the program needs input, it will prompt the user and read the input data. But in applets, the user interacts with the applet when he wants. These interactions are sent to the applet as events to which the applet must respond. Eg., when the user clicks the mouse while the applet's window has input focus, a mouse click event is generated.
- void init( ): - For any applet program, this is the method that is called first when it begins its execution and called only once during the runtime of an applet. Variables can be initialized here.
- void start( ): -called automatically after init() (i.e., when the applet first begins execution). Also called when the applet should restart after it has been stopped (ie., called each time an applet's HTML document is displayed on the screen. So, if a user leaves a web page and comes back, the applet resumes execution at the start())
- void stop( ): -called by the browser to suspend execution of the applet. Once stopped, an applet is restarted only when the browser calls start( ) (i.e., called when a web browser leaves the HTML document containing the applet - for example when it goes to another page)
- void destroy( ): -called by the browser just before an applet is terminated (i.e., when the environment determines that the applet needs to be removed completely from memory). It can be overridden if any clean up has to be done prior to its destruction.
Note: Default implementations for all these methods are provided. So, applet programs need not override those methods which they do not use.
Methods of AWT Component class:
- void paint( ): -takes one parameter of type Graphics which describes the Graphics environment. It is called by the JVM implicitly in two circumstances — one when the first time applet window is created and displayed and the other when the window is resized (by dragging the border with the mouse) by the user or when the window is minimized and then restored. This method should be overridden to change the way a Component is drawn.
- void repaint( ): -called whenever the applet window has to be redrawn (updated) with the new data. (i.e, in the middle of the coding, if the paint() method has to be called, to draw some graphics on the frame or applet window, it is permitted to call only the repaint() method and not the paint() method directly.)
- void update( ): clears the earlier drawings or contents on the applet window. On the cleared surface, the paint() method draws afresh with the latest graphics. If the programmer is allowed to call paint() method directly, there is every possibility that he may forget to call the update( ) method explicitly. To avoid this and to make Java as a robust language, the designers are not allowed to call paint() directly.