Way2Java

Images & Audio Clips

Loading and Running Images

Images, audio clips and animation requires special focus as all the images are not drawn at a time. Either image drawing or downloading takes a long time as part by part is written or downloaded.

Here, we use mainly two methods – one that creates an image object and the other that draws the image. getImage() method of java.applet.Applet returns an object of image and this object is drawn on the applet window using drawImage() method of java.awt.Graphics.

To do with images, Java supports two types of image formats – GIF (Graphics Interchange Format) and JPEG (Joint Photographic Experts Group).

Images & Audio Clips program to load and run an image bird1.gif
import java.awt.*;    
import java.awt.*;                             // for Image class
import java.applet.*;     
public class DrawingBird extends Applet  
{
  public void paint( Graphics g ) 
  {
    Image img1 = getImage(getCodeBase(), "bird2.gif");
    g.drawImage(img1, 40, 50, this);       
  }
}

Following is the HTML file to run the above applet (File Name: DB.html).



Screenshot of DB.html of tutorial Images & Audio Clips

Image img1 = getImage(getCodeBase(), “bird1.gif”);

getImage() method defined in Applet class returns an object of Image (of java.awt package). getImage() method takes two parameters, the first one is the address of the image as URL object and the second one is the image itself as a string. The returned Image object, img1, contains the image with address. getCodeBase() method of Applet class returns the address of the image, bird1.gif, as an URL object.

g.drawImage(img1, 40, 50, this);

drawImage() method of Graphics class draws the image on the applet window and takes four parameters. The first one is the image to be drawn as an object of Image class, the second and third are the x and y coordinates of where the image is to be drawn and the last one is an object of ImageObserver of java.awt package.

Let us see how the drawImage() method gets the reference of ImageObserver. ImageObserver interface is implemented by Component class, an indirect super class of Applet. The applet is inheriting the reference of ImageObserver from Component class. Following is the method signature of drawImage() method as defined in Graphics class.

public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)

The above drawImage() method is overloaded 6 times with different parameters.

Following is the hierarchy of Applet class.




Developing Animation

For developing animation programs, refer Image and Animation topic.

Playing Audio-clips

Playing music in an applet is very trivial. The audio files can be of any type – like .au, .wav or .mp3 etc. To support audio, the java.applet package comes with an interface AudioClip. An object of AudioClip interface can be obtained using the Applet method getAudioClip(). The AudioClip interface contains three methods to play the music – play() which plays only once the music, loop() which plays continuously and stop() method to stop the music.

Using AudioClip Methods

In the following program, all the three methods play(), loop() and stop() of AudioClip are used. To call these methods, three buttons are created and when a button clicked, the appropriate method is called.

File Name: PlayLoopStop.java

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 

public class PlayLoopStop extends Applet implements ActionListener
{
  AudioClip clip;
  public void init()
  {
    clip = getAudioClip(getDocumentBase(),"spacemusic.au");
    Button pb = new Button("PLAY");
    Button cb = new Button("CONTINUOUS");
    Button sb = new Button("STOP");
    pb.addActionListener(this);
    cb.addActionListener(this);
    sb.addActionListener(this);

    add(pb);  add(cb);  add(sb);
  }
  public void actionPerformed(ActionEvent e)
  {
    String str = e.getActionCommand();
    if(str.equals("PLAY"))
      clip.play();
    else if(str.equals("CONTINUOUS"))
      clip.loop();
    else if(str.equals("STOP"))
      clip.stop();
  }
}

File Name: Music.html


clip = getAudioClip(getDocumentBase(),"spacemusic.au");

The getAudioClip() method of Applet returns an object of AudioClip, clip. The method requires two parameters, the first one is the address of the "spacemusic.au" file as a URL object and the second is the name of the audio file. Place "spacemusic.au" file in the current directory (where your applet exists). getDocumentBase() method of Applet returns an object of URL. The URL gives the address of the audio file.

play() method plays the music only once and then stops automatically. loop() method plays the music continuously. stop() method stops the music played with loop() method.

Note: Place some audio player software like Media Player or VLC Player are loaded. Keep the speakers on.