292 Methods Chapter 6 Method When the method (Web server extensions)

292 Methods Chapter 6 Method When the method is called and its purpose public void init() This method is called once by the appletvieweror browser when an applet is loaded for execution. It performs initialization of an applet. Typical actions performed here are initialization of instance variables and GUI components of the applet, loading of sounds to play or images to display (see Chapter 18, Multimedia) and creation of threads (see Chapter 15, Multithreading). public void start() This method is called after the initmethod completes execution and every time the user of the browser returns to the HTML page on which the applet resides (after browsing another HTML page). This method performs any tasks that must be completed when the applet is loaded for the first time into the browser and that must be performed every time the HTML page on which the applet resides is revisited. Typical actions performed here include starting an animation see (Chapter 18, Multimedia) and starting other threads of execution (see Chapter 15, Multithreading). public void paint( Graphics g ) This method is called after the initmethod completes execution and the start method has started executing to draw on the applet. It is also called automatically every time the applet needs to be repainted. For example, if the user covers the applet with another open window on the screen then uncovers the applet, the paint method is called. Typical actions performed here involve drawing with the Graphicsobject gthat is automatically passed to the paintmethod for you. public void stop() This method is called when the applet should stop executing normally, when the user of the browser leaves the HTML page on which the applet resides. This method performs any tasks that are required to suspend the applet s execution. Typical actions performed here are to stop execution of animations and threads. public void destroy() This method is called when the applet is being removed from memory normally, when the user of the browser exits the browsing session. This method performs any tasks that are required to destroy resources allocated to the applet. Fig. 6.18 JAppletmethods that the applet container calls during an applet s execution . Method repaint is also of interest to many applet programmers. The applet s paint method normally is called by the applet container. What if you would like to change the appearance of the applet in response to the user s interactions with the applet? In such situations, you may want to call paint directly. However, to call paint, we must pass it the Graphicsparameter it expects. This requirement poses a problem for us. We do not have a Graphicsobject at our disposal to pass to paint. (We discuss this issue in Chapter 18, Multimedia.) For this reason, class JAppletprovides method repaint. The statement repaint(); Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply