| Andreas Rozek |
|
Applet_05"Applet_05" demonstrates the built-in multimedia capabilities of an applet. Warning: some web browsers tested by the author seem not to be capable of handling the code of this applet (despite a number of recent changes to eliminate subtle timing issues). Thus, in case that you cannot start the graphical animation which is part of Applet_05, please download source code, image and sound file, compile it locally and run the applet by means of Sun's "appletviewer". Please, also consider my "Hints for Reading" and the "List of Recent Changes"!
ImagesFrom within an applet, images may be loaded using Image Bitmap = getImage(URL); or Image Bitmap = getImage(URL, Name); and then drawn inside a java.awt.Canvas using one of the available drawImage methods of java.awt.Graphics. Unfortunately, images are always loaded asynchronously - and only if required. As a consequence, it is wise to enforce bitmap loading using: prepareImage(Bitmap, ImageObserver); The second argument of prepareImage specifies an object which implements the ImageObserver interface Depending on whether the (asynchronous) process of image loading should be tracked or not, either an AWT component (or the applet itself), an user-defined ImageObserver implementation or even null may be specified. AnimationsSequential drawing of slightly varying images ("frames") at the same place in regular intervals can be used to produce the illusion of motion ("key frame animation"). Instead of using the Animator applet which is part of the Java SDK programming examples, "Applet_05" implements its own way of animation. The main trick in this example is to bundle all frames in a single image which can then be transferred using a single TCP connection (rather than requiring multiple connections) - this approach may considerably reduce loading time. The applet uses the imageUpdate method from the java.awt.image.ImageObserver interface to detect when the asynchronous image loading process has been completed and key frames may be extracted. The main (animation) work is done within a separate thread - upon a click on the button labelled "Start Animation" a single frame of the rotating globe animation is drawn every 10th of a second: try {
while (Animation_running) { // animator loop
AppletArena.show_Image(Globe[ImageId]);
Animator.sleep(100);
ImageId = (ImageId + 1) % 36;
}
} catch (InterruptedException Signal) {
/**** just catch the exception - don't do anything special ****/
};
The try-catch-clause becomes necessary because of the sleep method. Audio ClipsJava provides a number of (high-level) functions to load and play java.applet.AudioClips. You may either load and play them by means of a single method invocation play(URL); or - as shown in this example - load an AudioClip explicitly and then start and stop the playback AudioClip Music = getAudioClip(URL); Music.loop(); Music.stop(); The latter approach is recommended as it allows an applet to stop a running audio playback when the underlying web page becomes invisible and start over as soon as it becomes visible again - this procedure allows to save processor capacity for other applets which are running in the foreground and therefore need any available resources. Source CodeThe source code of this applet is available for download:
Additionally, the following files are required to run "Applet_05":
|
| http://www.Andreas-Rozek.de/Java/JavaKurs/Applet_05/Applet_05.html | (last Modification: 01.05.2002) |