Java Buffered Image Tutorial


Java Bufferd Image class is a subclass of Image class. It is used to handle and manipulate the image data. A BufferedImage is made of ColorModel of image data. All BufferedImage objects have an upper left corner coordinate of (0, 0).

This class supports three types of constructors.

The first constructor constructs a new BufferedImage with a specified ColorModel and Raster.

BufferedImage(ColorModel cm, WritableRaster raster, 
boolean isRasterPremultiplied, Hashtable<?,?> properties)

The second constructor constructs a BufferedImage of one of the predefined image types.

BufferedImage(int width, int height, int imageType)

The third constructor constructs a BufferedImage of one of the predefined image types: TYPE_BYTE_BINARY or TYPE_BYTE_INDEXED.

BufferedImage(int width, int height, int imageType, IndexColorModel cm)

Methods and Description

Sr.NoMethod & Function
1copyData(WritableRaster outRaster)
Computes an arbitrary rectangular region of the BufferedImage and copies it into a specified WritableRaster.
Return ColorModel of an image.
Returns the image as one large tile.
4getData(Rectangle rect)
Computes and returns an arbitrary region of the BufferedImage.
This method returns a Graphics2D, but is here for backwards compatibility.
Returns the height of the BufferedImage.
Returns the minimum x coordinate of this BufferedImage.
Returns the minimum y coordinate of this BufferedImage.
9getRGB(int x, int y)
Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.
Returns the image type.


The following example demonstrates the use of java BufferedImage class that draw some text on the screen using Graphics Object.

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JPanel {

   public void paint(Graphics g) {
      Image img = createImageWithText();
      g.drawImage(img, 20,20,this);

   private Image createImageWithText(){
      BufferedImage bufferedImage = new 
      Graphics g = bufferedImage.getGraphics();

      g.drawString("", 20,20);
      g.drawString("", 20,40);
      g.drawString("", 20,60);
      g.drawString("", 20,80);
      g.drawString("", 20,100);
      return bufferedImage;
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.getContentPane().add(new Test());

      frame.setSize(200, 200);



When you run the above example, following output screen would appear:

Java Buffered Image Tutorial