|
|
Java Applets #4
You are here: irt.org | Articles | Java | Java Applets #4 Published on: Saturday 25th April 1998 By: Tarique Sani
IntroductionThis is the concluding article of this series, the much advertised Java FAQ to be maintained by myself and my mentor friend Rajesh Dhawan is already on line! There is not much code left to be explained in the KTN applet but it is the part which gives the applet its interactivity - so without further ado... The Kalptaru Net Applet- the paint( ) methodAs show in the previous article the paint() method is the place where the actual drawing on the applet canvas is done, here is how (hope you have your API reference at hand):
The paint() method when called draws an Image referenced by the index plus if selected is true then shows the animated green arrow and the tag line associated with the Image, if selected is false then the mirror image is drawn. As you will see later selected is toggled by mouseEnter() and mouseExit() event handlers. The start() stop() and destroy() MethodsThese three small bits of code smoothen the running of the applet:
The start() method starts the thread t which is created if found to be null else it resumes it, the stop() method suspends the thread and the destroy() method stops the threads and makes it null (thus doing the clean up job). The run() MethodThis is where the thread we created is controlled from:
The run method first sleeps for the period specified in the delay parameter, then increments the index variable if the index is greater than the number of items on our list it is reset to zero, then the run method calls the repaint() thus in effect it redraws the applet after every n second gap, n being the delay we specified. The mouse event handlersThere are three mouse events which are handled by the applet mouseEnter(), mouseExit() and mouseDown():
The mouseEnter() toggles selected to true, suspends the thread (we don't want the display to change while the mouse is over the applet) and calls repaint(). mouseExit() is exactly opposite of mouseEnter(). Finally the mouseDown() shows the appropriate document in the proper frame that we supplied in the parameter list. The last wordThat is all that is needed to make the applet work, BUT there is a lot of flickering or flashing between repaints to avoid this we just over ride the update() method and make it call paint() directly without clearing the applet canvas first (clearing the applet canvas is the default behaviour of the update() method).
Complete Source CodeThe complete source code for the complete KTN applet is available. Feedback on 'Java Applets #4'
View the profile on Tarique Sani and the list of other Articles by Tarique Sani. |
-- div -->
|