Search Your Topic

Thursday, 25 May 2017 06:11

Displaying Image in Applet

Written by
Rate this item
(0 votes)

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:
  1. public Image getImage(URL u, String image){}  

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:

  1. import java.awt.*;  
  2. import java.applet.*;  
  5. public class DisplayImage extends Applet {  
  7.   Image picture;  
  9.   public void init() {  
  10.     picture = getImage(getDocumentBase(),"sonoo.jpg");  
  11.   }  
  13.   public void paint(Graphics g) {  
  14.     g.drawImage(picture, 30,30this);  
  15.   }  
  17.   }  
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.



  1. <html>  
  2. <body>  
  3. <applet code="DisplayImage.class" width="300" height="300">  
  4. </applet>  
  5. </body>  
  6. </html>  
Read 1316 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email :

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

Java Training