|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
paint() The Java environment calls paint() whenever it suspects that the applets graphic space may have been obscured. As a result, paint() gets called far more often than you might expect. Experiment with the LifeCycle applet in various browsers to see when paint() gets called. Design your applets so that paint() is as efficient as possiblethis method is where your program will spend much of its time. destroy() Put up Navigators Java Console, open a page with the LifeCycle applet on it, and exit Navigator. If you watch the console closely, youll see LiveCycles destroy message just before the console itself disappears. In general, browsers will try to keep applets around (at least in their stopped state) as long as they can. When the browsers memory is full, or when the user exits the browser, the applets resources are released. Just before the browser destroys the applets memory, it calls destroy(). Use destroy() to release any resources your applet may have acquired. Wheres the Destructor? If youre experienced in C++, you may be puzzled by Javas lack of a destructor. Java relies on garbage collectionyou dont have to explicitly delete objects. Nevertheless, your C++ habits will stand you in good steadmost programmers prefer to release resources as soon as they know theyre done with them. The code you put into destroy() should, therefore, resemble code you might write in a C++ destructor; look through your constructor and init() code, identify any resources (such as object references) you acquired, and release them. If your applet acquired other resources during its lifetime (such as nodes on a linked list), release them by setting the references to null. You get a similar effect by writing a finalize() method. The garbage collector calls an objects finalize() method just before the objects memory is reclaimed. If you have a sophisticated applet with more than one class, you might want to write a finalize() method for each class that needs to dispose of resources. You can then use finalize() to dispose of system resources or perform other cleanup, and use destroy() to wrap up the applet itself. Troubleshooting HelloAppletEven with an applet as simple as this, a number of things could go wrong. Take a look at some common gotchas:
Sample AppletsSometimes programming is better caught than taught. This section shows three fairly advanced appletseach uses techniques that havent been described in detail in this book. If you have a programming background, most of the logic of these applets should be clear; look in the Java documentation for details about the API. If youd like a more detailed treatment of Java programming, read Using Java 1.2 (Que, 1998).
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |