AWT Images

Working with Images -


In the Java 2D API an image is typically a rectangular two-dimensional array of pixels, where each pixel represents the color at that position of the image and where the dimensions represent the horizontal extent (width) and vertical extent (height) of the image as it is displayed.
java.awt.Image and java.awt.image.BufferedImage are the important classes you need to learn to work with images.
  • The java.awt.Image class is the superclass that represents graphical images as rectangular arrays of pixels.
  • The java.awt.image.BufferedImage class, which extends the Image class to allow the application to operate directly with image data (for example, retrieving or setting up the pixel color). Applications can directly construct instances of this class.
The BufferedImage class manages the image in memory and provides methods for storing, interpreting, and obtaining pixel data. Since BufferedImage is a subclass of Image it can be rendered by the Graphics and Graphics2D methods that accept an Image parameter.

Here are the basic operations you can performed on images -

1- Loading an image -

You can load any external image into Applet by using getImage() method of Applet class. It has following two versions -
Image getImage(URL url)
Image getImage(URL url, String imageName) 
        
The first version takes url (path of the external image) of the image and returns that image on to Applet, and second version takes the url and image name as parameters and return that image on to Applet.

2- Drawing an Image -

After loading the image we need to display it. Graphics class provides a drawImage() method that is used to display the image.
It has several versions; the most commonly used version is as follows -
    
boolean Graphics.drawImage(Image img, int top, int left, ImageObserver observer);

This displays the image passed in img object with its upper-left corner specified by left and top. observer is a reference to a class that implements the ImageObserver interface. This interface is implemented by all AWT components. An image observer is an object that can monitor an image while it loads.

In addition to this we can also specify the width and height of the image drawn by using this version of drawImage() method -

boolean Graphics.drawImage(Image img, int top, int left, int width, int height ImageObserver observer);
    
Where width and height of drawn image is specified by width and height parameters.

Example -

import java.awt.*;
import java.applet.*;
public class Demo1 extends Applet
{
    public void paint(Graphics g)
    {
        Image img = getImage(getCodeBase(),"A.jpg"); 
 /* getCodeBase() method return the url of directory which contain this applet.
     Image should be in the same folder where Applet program stored.   */
        g.drawImage(img, 10,10,200, 100 this);
    }
}

Output -


Program Description -

Above program will draw an image named A.jpg stored in the same folder where your program is stored. We get the url of the image by using getCodeBase() method that returns the url of directory which contains this applet.
Suppose the program is stored at C:\java location. getCodeBase() will return the same.
g.drawImage(img, 10,10, this); will draw an image represented by img object, whose starting point is (10,10) and width and height of image is 200 and 100 pixels respectively. The last parameter should be an object of the class that implements ImageObserver interface and As Applet class already implements the ImageObserver interface, so we passed here ?this? i.e object of current class(Applet class).

Creating and Drawing to an Image -

To create your own images you can use BuffredImage class which is a subclass of Image class. This subclass describes an Image with an accessible buffer of image data.
It has several constructors but the most commonly used constructor is as follows -
BufferedImage(int width,int height,int imageType) 
Constructs a BufferedImage of one of the predefined image types. 
Parameters- 
width - width of the created image
height - height of the created image
imageType - Type of the created image that can be used to specify for example a RGB image (colored image) 
or Gray (Black and white ) images. 
There are  several constants defined for this. Some of them are as follows - 
For a black and white image you can use - public static final int TYPE_BYTE_GRAY - 
It represents a unsigned byte grayscale image. It has a constant value 10.
For colored image you can use - public static final int TYPE_INT_ARGB - 
It rpresents an image with 8-bit RGBA color components packed into integer pixels. It has a constant value 1.

Methods of BufferedImage class -

It has several methods some most frequently used are as follows -
  • getGraphics() - This method returns Graphics2D object, but here used for backward compatibility. Here it is declared to return Graphics object. It has the following syntax-
    public Graphics getGraphics()
    The object return by this method can be used to draw on BufferedImage.
  • createGraphics() - This method returns Graphics2D object and more convenient compared to getGraphics() because it is declared to return Graphics2D objects. It has the following syntax-
    public Graphics2D createGraphics()
  • getType() - It returns the image type. It has the following syntax-
    public int getType()
  • getRGB() - Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace. It has the following syntax-
    public int getRGB(int x,int y)

Example -

import java.awt.*;
import java.applet.*;
import java.awt.image.BufferedImage;
public class Demo1 extends Applet
{
    public void paint(Graphics g)
    {
	Image img = getImageDemo();
	g.drawImage(img, 10,10, this);
    }
	public Image getImageDemo()
	{
         BufferedImage bi = new BufferedImage(300, 300, 1);
        Graphics gbi = bi.getGraphics();
        Image img = getImage(getCodeBase(),"A.jpg");

        gbi.drawImage(img, 10, 10, 200, 100, this);
        gbi.drawString("This is just a BufferedImage Demo",10, 120);
        gbi.drawRect(10,130,200,100);
        return bi;
                    }
}
    

Output -


Program Description -

BufferedImage bi = new BufferedImage(300, 300, 1); this line of code creates a Buffered Image of size 300 *300 and the third parameter i.e. 1 indicates the image type and 1 is used for colored image.
The next line Graphics gbi = bi.getGraphics(); is used to get Graphics object so that we can draw something on this Buffered Image.
And in next three lines we draw an Image, a String and a rectangle on that Buffered Image. And finally return that image to paint method. In paint() method we finally drawn this buffered image on applet.

Double Buffering -

While drawing some complicated images directly on screen you may notice that it takes some time to draw image on screen. You will probably even notice visible artifacts of how your picture is drawn. And in case of Animations some flickering may occur.
To avoid such conditions programmer use the concept of Double Buffering.
In this concept you need to create an off-screen image, draw whatever you want using that image graphics object then, in one step, call drawImage() using the target window's graphics object and the off screen image.
The screen surface where your image will be visible is called primary surface and off-screen image used for drawing things is referred to as back buffer.

Example -

import java.awt.*;
import java.applet.*;
import java.awt.image.BufferedImage;
public class Demo1 extends Applet
{

  public void paint(Graphics g)
  {
    int aw = getSize().width;
    int ah = getSize().height;
    Image offScreenImage = createImage(aw,ah);  // create an image
   Graphics gimg = offScreenImage.getGraphics();  // get offScreenImage Graphics object to draw on it.
   Image img = getImage(getCodeBase(),"A.jpg");  // get a local image from computer.
   gimg.drawImage(img, 10, 10, 200, 100, this);  // draw that image on offScreenImage
   g.drawImage(offScreenImage, 0, 0, this);   // finally draw offScreenImage on visible window.
  }
}

Output -