Images & Audio Clips

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

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

Images & Audio Clips

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.



Leave a Reply

Your email address will not be published. Required fields are marked *