Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com

Displaying Image in Applet

Displaying-Image-Applet

 

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.

o display an image in an applet, you can use the Image class and the Graphics class.

Here is an example of an applet that displays an image:

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Image;


public class MyImageApplet extends Applet {

    private Image image;


    public void init() {

        image = getImage(getCodeBase(), "image.jpg");

    }


    public void paint(Graphics g) {

        g.drawImage(image, 10, 10, this);

    }

}

In this example, the MyImageApplet class extends the Applet class and initializes an Image object in the init() method using the getImage() method. The getImage() method takes two arguments: the URL of the image and the base URL of the applet.

The paint() method overrides the Applet class's method and draws the image using the drawImage() method of the Graphics class. The drawImage() method takes four arguments: the image to be drawn, the x and y coordinates of the top-left corner of the image, and the ImageObserver object that is notified when the image is fully loaded.

Note that the init() method is called when the applet is initialized, and the paint() method is called whenever the applet needs to be repainted. The getCodeBase() method returns the URL of the directory containing the applet's class file.

You can also load images from a file on the local file system using the FileInputStream class and the ImageIO class:


import java.applet.Applet;

import java.awt.Graphics;

import java.awt.Image;

import java.io.File;

import java.io.FileInputStream;

import javax.imageio.ImageIO;


public class MyLocalImageApplet extends Applet {

    private Image image;


    public void init() {

        try {

            File file = new File("image.jpg");

            FileInputStream fis = new FileInputStream(file);

            image = ImageIO.read(fis);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }


    public void paint(Graphics g) {

        g.drawImage(image, 10, 10, this);

    }

}

In this example, the MyLocalImageApplet class loads the

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.*;  
  3.   
  4.   
  5. public class DisplayImage extends Applet {  
  6.   
  7.   Image picture;  
  8.   
  9.   public void init() {  
  10.     picture = getImage(getDocumentBase(),"sonoo.jpg");  
  11.   }  
  12.     
  13.   public void paint(Graphics g) {  
  14.     g.drawImage(picture, 30,30this);  
  15.   }  
  16.       
  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.

myapplet.html

  1. <html>  
  2. <body>  
  3. <applet code="DisplayImage.class" width="300" height="300">  
  4. </applet>  
  5. </body>  
  6. </html>  
download this example.

Post a Comment

© JAVA. The Best Codder All rights reserved. Distributed by