|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
ConstructorsSeasoned C++ programmers are probably looking for a constructor in which they can initialize their variables. Java provides one, probably for us old C++ salts. The constructor is named after the applet. For our Hello1 applet it would be a method named Hello1(). Although some might not agree (personal habits vary widely), the constructor is a good place in which to initialize variables. Assigning values to variables in the constructor and declaring variables at the top of the applet separates them for clarity and gives you one place to check when things arent what they should be.
Heres what the beginning of an applet with applet variable declarations and variable initialization in the constructor would look like: public class Hello1 extends Applet { int x, y; int nHorizontal, nVertical; // Hello1 Class Constructor //------------------------------------------------------- public Hello1() { x = 5; y = 20; nHorizontal = x * 2; nVertical = y * 3; } // Place other methods here. } The init() MethodThe next of the common methods that will be called is the init() method. The init() method is called by the Virtual Machine when an applet is first loaded or reloaded. Use this method to perform whatever initialization your applet needs, such as initializing data structures, loading images or fonts, creating frame windows, setting the layout manager, or adding user interface components. The init() method is where most programs retrieve parameters. Actually, Visual J++ creates startup code inside of the init() method that calls a method named usePageParams(). The usePageParams() method loads and processes all the HTML parameters. The usePageParams() method is not an override of a method. Its just a convenient method that Microsofts Visual J++ adds. The start() MethodIn a multithreaded applet, the start() method comes next in the sequence of events. Its in this method that the main applet is created and started. You can add your own code in this method, but youll need some basic things. Following is a bare-bones start() method for a multithreaded applet: public void start() { if( m_Params == null ) { m_Params = new Thread( this ); m_Params.start(); } // Place additional applet start code here } The run() MethodAfter the start() method is called, the thread begins execution. The program will remain in the run() method until an interruption is detected. In the run() method, repaint() and sleep() are usually called. For most applets, repaint() wont need to be called every time you go through the loop. Youll most likely call the repaint() method only when the applets display will change as a result of something your programs code has done. The value passed to the sleep() method determines the amount of time the applet waits before executing the code in the run() method. Its value is in milliseconds. If the value passed to sleep() is 1000, the sleep() method waits one second before returning control to the applet. If the value is 500, it waits a half second before returning control. A good default value is 50. A bare-bones run() method follows: public void run() { while( true ) { try { // Do stuff here Thread.sleep(50); } catch (InterruptedException e) { // Place exception-handling code here in case an // InterruptedException is thrown by Thread.sleep(), // meaning that another thread has interrupted this one stop(); } } } The stop() MethodWhen the applet is interrupted, usually by the browser going to another site, the stop() method is called. The thread is destroyed, and you have the opportunity to perform additional steps you might want to take for applet cleanup. Heres a bare-bones stop() method: public void stop() { if( m_Params != null ) { m_Params.stop(); m_Params = null; } // Place additional applet stop code here } The destroy() MethodThe destroy() method is called automatically when the applet is terminated. Its still a good place to put all of your cleanup code because all the exit code will be organized in one place. For this reason, your stop() method should call destroy() after the thread is stopped. You can avoid any code except for the call to destroy() in the stop() method. You might have some difficulty because destroy() might not be executed as early in the cleanup process as youd like. A good example from my own programming is a situation in which I have sockets that are open. The applet is far more robust when the sockets are closed immediately as users exit the applet. If theres any kind of delay in closing the sockets, users can actually come back to the HTML page and rerun the applet. If the sockets are slow to close, the newly instantiated applet will fail when it tries to open these sockets that havent yet closed. Its okay to explicitly call this method.
|
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. |