Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run.

Similar presentations


Presentation on theme: "Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run."— Presentation transcript:

1 Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run as part of a web document. After an applet arrives on the client, it has limited access to resources, so that it can produce and arbitrary multimedia user interface and run complex computations without introducing the risk of viruses or breaching data integrite. Applet begins with two import statements. The first imports the Abstract Window Toolkit(AWT) classes. Applets interact with the user through the AWT, not throug the console based I/O classes. The AWT contains support for a window – based, graphical interface. The Applet class must be declared as public, because it will be accessed by code that is outside the program. Inside paint() is a call to drawString(), which is a member of the Graphics class. This method outputs a string beginning at the specified X,Y location. It has the following general form: Void drawString(String msg, int x, int y)

2 In general, you can quickly iterate through applet development by using these three steps:
Edit a Java Source file. Compile your program. 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. Applet do not need a main() method. Applet must be run under an applet viewer or a Java –compatible brower. User I/O is not accomplished with Java’s stream I/O classes. Instead, applets use the interface provided by the AWT. Applet Architecture Applets override a set of methods that provides the basic mechanism by which the browser or applet viewer interface 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.

3 Applet Initialization and Termination
It is important to understand the order in which the various methods shown in the skeleton are called. When an applet begins the AWT calls the following methods, in this sequence: init() start() paint() When an applet is terminated, the following sequence of method calls takes place: stop() destory() init() The init() method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet.

4 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. 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. You 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.

5 destroy() The destroy() 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(). Overriding update() In some situtions, your applet may need to override another method defined by the AWT, called update() . This method is called when your applet has requested that a portion of its window be redrawn. Requesting Repainting As a reneral rule, an applet writes to its window only when its update() or paint() method is called by the AWT. 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().

6 [<PARAM NAME=AttibuteName VALUE=AttibuteValue>]
The repaint() method has four forms. Void repaint() Void repaint(int left,int top, int width,int height) Void repaint(long maxDelay) Void repaint(long maxDelay, int x, int y, int width, int height) The HTML Applet Tab The APPLET tag is used to start an applet from both an HTML document and from an applet viewer. The syntax for the standard APPLET tag is shown here. Bracketed items are optional. <APPLET [CODEBASE=codebaseUTL] CODE=appletfile [ALT=alternateText] [NAME=appletInstanceName] WIDTH=pixels HIEGHT=pixels [ALIGN=alignment] [VSPACE=pixels] [HSPACE=pixels]> [<PARAM NAME=AttibuteName VALUE=AttibuteValue>] [<PARAM NAME=AttibuteName2 VALUE=AttibuteValue>] [HTML Displayed in the absence of Java] </APPLET>

7 CODE: Code is a required attribute that gives the name of the file containing your applet’s compiled .class file. WIDTH and HEIGHT: Width and Height are required attributes that gives the size of the applet display area. PARAM NAME and VALUE : The PARAM tag allows you to specify applet-specific arguments in an HTML page. Applets access their attributes with the getParameter() method.

8

9


Download ppt "Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run."

Similar presentations


Ads by Google