Java-Loading Images

Loading Images

Basic image handling in java is easy. The java.awt provides Image class. This is used to represent the common behavior of an image. To display an image in your applet. You must load the image into your java program. So you have to tell where to find them. Applet class provides a method calling getImage, which loads an image and automatically create an instance of an Image class for you.

Drawing Image

The common way to display a image, java provides drawImage( ) method.

drawImage (img im, x, y, this);

The above code draws image im in its original dimension with the top left corner at the given x and y positions this refers the current object.



Program Source

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;

public class Javaapp extends Applet{
    private Image im;

     public void init() 
         im = getImage(getCodeBase(), "ford.jpg");

     public void paint(Graphics g) 
         g.drawImage(im, 10, 10, this);

Leave a Comment