Preloading the Images & Playing Sound

The following applet pre-loads the pictures in the init() method. It also sounds an audio clip (a duck quack) when the applet is initialized. See the code for reference.

Code for Preload.java

Click here for the code --> Preload code

I have created a preload option for the slide show object (see section below) that allows the slide show to be "shown" which forces the computer running the applet to load the pictures into memory. When the button is pressed, the show method is called, except that the preload option is set to false.

I have also include the command to sound an audio file. Since I play the file in the actionPerformed() method, the sound is played when user presses the button. You can have a sound play using this code.

Things to note:

  • Try to keep the size of your media files (pictures, sounds, etc) small. Files that are too large take a long time to load up and may not perform as you expect as a result.
  • Remember that some files are not standard and cannot be played by some computer systems. For example Midi files cannot be played by all systems.

Code for SlideShow.java

Click here for the code --> Slide Show code

I had to modify the slide show object's show method. It now allows the user to specify the border size around the picture, although I do have the size of the picture hardcoded to the applet size.

 

Java
Sample Code