26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

292 Methods Chapter 6<br />

Method When the method is called and its purpose<br />

public void init()<br />

This method is called once by the appletviewer or browser when an applet is<br />

loaded for execution. It performs initialization of an applet. Typical actions performed<br />

here are initialization of instance variables and GUI components of the<br />

applet, loading of sounds <strong>to</strong> play or images <strong>to</strong> display (see Chapter 18, Multimedia)<br />

and creation of threads (see Chapter 15, Multithreading).<br />

public void start()<br />

This method is called after the init method completes execution and every time the<br />

user of the browser returns <strong>to</strong> the HTML page on which the applet resides (after<br />

browsing another HTML page). This method performs any tasks that must be completed<br />

when the applet is loaded for the first time in<strong>to</strong> the browser and that must be<br />

performed every time the HTML page on which the applet resides is revisited. Typical<br />

actions performed here include starting an animation see (Chapter 18, Multimedia)<br />

and starting other threads of execution (see Chapter 15, Multithreading).<br />

public void paint( Graphics g )<br />

This method is called after the init method completes execution and the start<br />

method has started executing <strong>to</strong> draw on the applet. It is also called au<strong>to</strong>matically<br />

every time the applet needs <strong>to</strong> be repainted. For example, if the user covers the applet<br />

with another open window on the screen then uncovers the applet, the paint<br />

method is called. Typical actions performed here involve drawing with the Graphics<br />

object g that is au<strong>to</strong>matically passed <strong>to</strong> the paint method for you.<br />

public void s<strong>to</strong>p()<br />

This method is called when the applet should s<strong>to</strong>p executing—normally, when the<br />

user of the browser leaves the HTML page on which the applet resides. This method<br />

performs any tasks that are required <strong>to</strong> suspend the applet’s execution. Typical actions<br />

performed here are <strong>to</strong> s<strong>to</strong>p execution of animations and threads.<br />

public void destroy()<br />

This method is called when the applet is being removed from memory—normally,<br />

when the user of the browser exits the browsing session. This method performs any<br />

tasks that are required <strong>to</strong> destroy resources allocated <strong>to</strong> the applet.<br />

Fig. Fig. 6.18 6.18 JApplet methods that the applet container calls during an applet’s<br />

execution .<br />

Method repaint is also of interest <strong>to</strong> many applet programmers. The applet’s paint<br />

method normally is called by the applet container. What if you would like <strong>to</strong> change the<br />

appearance of the applet in response <strong>to</strong> the user’s interactions with the applet? In such situations,<br />

you may want <strong>to</strong> call paint directly. <strong>How</strong>ever, <strong>to</strong> call paint, we must pass it the<br />

Graphics parameter it expects. This requirement poses a problem for us. We do not have<br />

a Graphics object at our disposal <strong>to</strong> pass <strong>to</strong> paint. (We discuss this issue in Chapter 18,<br />

Multimedia.) For this reason, class JApplet provides method repaint. The statement<br />

repaint();<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!