Chapter1 Java Programming COMP417 Applet Applet Java program

  • Slides: 16
Download presentation
Chapter-1 Java Programming COMP-417 Applet

Chapter-1 Java Programming COMP-417 Applet

Applet: Java program that are typically embedded in xhtml documents. Also called web pages

Applet: Java program that are typically embedded in xhtml documents. Also called web pages when a java -enabled web browsers loads a web page containing an applet, the applet downloads into the browses and executes. Applet Containers: the application in which the applet executes is known as the applet containers. It is the applet containers responsibility to load the applet class creates an instance of the applet and manages its life cycle. The JDK includes one called the "applet viewer" for testing applet as you develop them and before you embed them in web pages.

Applet Life-Cycle Methods init() Begin Born start() stop() Running Idle start() paint() destroy() Dead

Applet Life-Cycle Methods init() Begin Born start() stop() Running Idle start() paint() destroy() Dead End

Contd. . • There are five applet method that are called by the applet

Contd. . • There are five applet method that are called by the applet container from the time the applet is loaded to the time it’s terminated by the browser. • These methods correspond to various aspects of an applet’s life cycle. • Which are inherited into your applet classes from class Applet or Japplet.

Contd. . • The methods are listed below: 1. init() 2. start() 3. paint(Graphics)

Contd. . • The methods are listed below: 1. init() 2. start() 3. paint(Graphics) 4. stop() 5. destroy()

1. init (): called once by the applet containers when an applet is loaded

1. init (): called once by the applet containers when an applet is loaded for execution. This method initializes an applet. Typical action performed here are initializing fields, creating GUI components, loading sounds to play, loading images to display and creating threads. 2. start( ) : Called by the applet container after method init( ) completes execution. In addition, if the user browses to another website and later returns to the applet’s XHTML page, method ‘start’ is called again. This method perform any tasks that must be performed when the applet is loaded for the first time and that must be performed every time the applet’s XHTML page is revisited. Action performed have might include starting an animation or starting other threads of execution.

3. paint ( ) : Called by the applet container after methods init() and

3. paint ( ) : Called by the applet container after methods init() and start(). Method paint() is also called when the applet needs to be repainted. If the user covers the applet with another open window on the screen and later uncovers it, the paint method is called. Typical action performed here involve is drawing with the Graphics object g that is passing to the paint method by the applet container. 4. stop( ) : This method is called by the applet container when the user leaves the applet's web page by browsing to another web page. Since it’s possible that the user might return to the web page containing the applet, method stop performs tasks that might be required to suspend the applet’s execution, so that the applet does not use computer processing time when it’s not displayed on the screen. Typical action performed here would stop the execution of animations and threads.

5. destroy( ) : This method is called by the applet container when the

5. destroy( ) : This method is called by the applet container when the applet is being removed from memory. This occurs when the user exits the browsing session by closing all the browser windows and may also occurs at the browser’s discretion when the user has browsed to other web page. This method performs any tasks that are required to clean up resources allocated to the applet.

init() Begin Born start() Running paint()

init() Begin Born start() Running paint()

init() Begin Born start() stop() Running Idle start() paint()

init() Begin Born start() stop() Running Idle start() paint()

init() Begin Born start() stop() Running Idle start() paint() destroy() Dead End

init() Begin Born start() stop() Running Idle start() paint() destroy() Dead End

init() Begin Born start() stop() Running Idle start() paint() destroy() Dead End

init() Begin Born start() stop() Running Idle start() paint() destroy() Dead End

program to demonstrate applet life cycle

program to demonstrate applet life cycle

import java. applet. Applet; import java. awt. *; public class Life. Cycle extends Applet

import java. applet. Applet; import java. awt. *; public class Life. Cycle extends Applet { public void init() { System. out. println(" init()"); } public void start() { System. out. println(" start()"); } public void paint(Graphics g) { g. draw. String("Welcome student", 200); System. out. println(" paint()"); }

public void stop() { System. out. println(" stop()"); } public void destroy() { System.

public void stop() { System. out. println(" stop()"); } public void destroy() { System. out. println(" destroy()"); } } /* <HTML> <BODY> <APPLET CODE ="Life. Cycle. class", WIDTH="800" HEIGHT="500"></APPLET> </BODY> </HTML>*/