Home » Displaying image in Applet

Displaying image in Applet

by Online Tutorials Library

Displaying Image in Applet

Applet is mostly used in games and animation. For this purpose image is required to be displayed. The java.awt.Graphics class provide a method drawImage() to display the image.

Syntax of drawImage() method:

  1. public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image.

How to get the object of Image:

The java.applet.Applet class provides getImage() method that returns the object of Image. Syntax:

Other required methods of Applet class to display image:

  1. public URL getDocumentBase(): is used to return the URL of the document in which applet is embedded.
  2. public URL getCodeBase(): is used to return the base URL.

Example of displaying image in applet:

In the above example, drawImage() method of Graphics class is used to display the image. The 4th argument of drawImage() method of is ImageObserver object. The Component class implements ImageObserver interface. So current class object would also be treated as ImageObserver because Applet class indirectly extends the Component class.

myapplet.html

You may also like