Applets Applet Basics Applets are small java applications

Applets

Applet Basics • Applets are small java applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as part of a web document. • 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( ). Actually, few applets even have main( ) methods.

• Applets are primarily used in internet programming. • An applet like any application can do many things like arithmetic operations, display graphics, play sounds accept user input, create animation, play interactive games. • Applets cannot read from or write to files of local computer. • They can’t run any program from local computer. • They are restricted from using libraries from other languages as C, C++. • They can’t communicate with other servers on the network.

Steps involved in developing and testing an applet Building an applet code(. java file) Creating an executable applet(. class file) Designing a web page using HTML tags. Preparing <applet> tag & incorporating into webpage. • Testing the applet code. • •

Two Types of Applets 1. based directly on the Applet class • These applets use the Abstract Window Toolkit (AWT) to provide the graphic user interface. • This style of applet has been available since Java was first created. 2. 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 Architecture • Two key concepts 1. Applets are event driven: • An applet resembles a set of interrupt service routines. Here is how the process works. • An applet waits until an event occurs. • The run-time system 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.

• our applet should not enter a “mode” of operation in which it maintains control for an extended period. • Instead, it must perform specific actions in response to events and then return control to the run-time system. 2. The user initiates interaction with an applet • The user interacts with the applet as he/she wants. • These interactions are sent to the applet as events to which the applet must respond. • For ex: when the user clicks the mouse inside the applet’s window, a mouse-clicked event is generated.

An Applet Skeleton C: Javajdk 1. 6. 0_12binApplet. Skel. java -

Applet Initialization and Termination • When an applet begins, the following methods are called, in this sequence: 1. init( ) : This is where you should initialize variables. This method is called only once during the run time of your applet. 2. start( ) : This method is called after init( ). It is also called to restart an applet after it has been stopped. 3. paint( ) : • This method is called each time your applet’s output must be redrawn. • It is also called when the applet begins execution. • 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.

• When an applet is terminated, the following sequence of method calls takes place: 1. stop( ) : The stop( ) method is called when a web browser leaves the HTML document containing the applet. • You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. 2. destroy( ): This method is called when the environment determines that your applet needs to be removed completely from memory. • At this point, you should free up any resources the applet may be using. • The stop( ) method is always called before destroy( ).

Simple Applet Display Methods • To output a string to an applet, we use draw. String(), which is a member of the Graphics class. • It is called from within either update( ) or paint( ). general form: void draw. String(String msg, int x, int y) • To set the background color of an applet’s window, use set. Background( ). • To set the foreground color (the color in which text is shown, for example), use set. Foreground( ). • These methods are defined by Component, general forms: void set. Background(Color new. Color) void set. Foreground(Color new. Color)

• The class Color defines the constants shown here that can be used to specify colors: Color. black Color. magenta Color. blue Color. orange Color. cyan Color. pink Color. dark. Gray Color. red Color. gray Color. white Color. green Color. yellow Color. light. Gray

• You can obtain the current settings for the background and foreground colors by calling • get. Background( ) and get. Foreground( ), respectivly. Color get. Background( ) Color get. Foreground( ) Example Program : file: ///C: Javajdk 1. 6. 0_12binSample. java

Requesting Repainting • Whenever your applet needs to update the information displayed in its window, it simply calls repaint( ). • The repaint( ) method is defined by the AWT. • It causes the AWT run-time system to execute a call to your applet’s update( ) method, which, in its default implementation, calls paint( ). • For example, if part of your applet needs to output a string, it can store this string in a String variable and then call repaint( ).

• The repaint( ) method has four forms. 1. void repaint( ): This version causes the entire window to be repainted. 2. void repaint(int left, int top, int width, int height) : specifies a region that will be repainted. 3. void repaint(long max. Delay) 4. void repaint(long max. Delay, int x, int y, int width, int height) : These two forms are used whenever we need to update the window soon. Like when we are using animation.

• A Simple Banner Applet to demonstrate repaint() method C: Javajdk 1. 6. 0_12binSimple. Banner. java

Using the status Window • In addition to displaying information in its window, an applet can also output a msg to the status window of the browser/applet viewer on which it is running. • To do so, call show. Status( ) with the string that you want display. • The status window is a good place to give the user feedback about what is occurring in the applet, suggest options, or possibly report some types of errors. • The status window also makes an excellent debugging aid, because it gives you an easy way to output information about your applet. • C: Javajdk 1. 6. 0_12binStatus. Window. java
![The HTML APPLET Tag < APPLET [CODEBASE = codebase. URL] CODE = applet. File The HTML APPLET Tag < APPLET [CODEBASE = codebase. URL] CODE = applet. File](http://slidetodoc.com/presentation_image_h2/721f7c617b3b23288538007cf14344bd/image-18.jpg)
The HTML APPLET Tag < APPLET [CODEBASE = codebase. URL] CODE = applet. File [ALT = alternate. Text] [NAME = applet. Instance. Name] WIDTH = pixels HEIGHT = pixels [ALIGN = alignment] [VSPACE = pixels] [HSPACE = pixels]> [< PARAM NAME = Attribute. Name VALUE = Attribute. Value>] [< PARAM NAME = Attribute. Name 2 VALUE = Attribute. Value>]. . . [HTML Displayed in the absence of Java] </APPLET>

Passing Parameters to Applets • The APPLET tag in HTML allows you to pass parameters to your applet. • To retrieve a parameter, use the get. Parameter( ) method. It returns the value of the specified parameter in the form of a String object. C: Javajdk 1. 6. 0_12binParam. Demo. java • Improving the Banner Applet C: Javajdk 1. 6. 0_12binParam. Banner. java

get. Document. Base( ) & get. Code. Base( ) • Often, you will create applets that will need to explicitly load media and text. • Java will allow the applet to load data from the directory holding the HTML file that started the applet (the document base) and • the directory from which the applet’s class file was loaded (the code base). • These directories are returned as URL objects. file: ///C: Javajdk 1. 6. 0_12binBases. java

Applet. Context and show. Document( ) • Applet. Context is an interface that lets you get information from the applet’s execution environment. • To allow your applet to transfer control to another URL, you must use the show. Document( ) method defined by the Applet. Context interface. • The method show. Document(URL) displays the document at the specified URL. • The method show. Document(URL, String where) displays the specified document at the specified location within the browser window. • Valid arguments for where– “_self” (show in currentframe), “_parent” (show in parent frame), “_top” (show in topmost frame), and “_blank” (show in new browser window).

• file: ///C: Javajdk 1. 6. 0_12binACDemo. java • Methods of Appletcontext interface • . . Applet get. Applet. doc • The Audio. Clip Interface The Audio. Clip interface defines these methods: • play( ) (play a clip from the beginning), • stop( ) (stop playing the clip), • loop( ) (play the loop continuously). • After you have loaded an audio clip using get. Audio. Clip( ), you can use these methods to play it.

The Applet. Stub Interface • The Applet. Stub interface provides the means by which an applet and the browser (or applet viewer) communicate. • Your code will not typically implement this interface.
- Slides: 23