Java Swing – Transparent Images

I have been asked several times about how to make certain pixels in an Image Transparent or to make Images appear on Swing Interfaces with a transparent background. Notice the phone image in the interface below. There have been tons of questions about how this was achieved as these phone images are not with transparent background.


Well the technique is pretty simple. You just have to write a ImageFilter and filter the image pixels through it. Here the whole working code

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;

* This class is used for making an Image in which a designated color is made
* transparent.
* @author Faisal Feroz
public class TransparencyUtil {

* This method makes the color in image transparent.
* @param im The image who's color needs to be made transparent.
* @param color The color which needs to be filtered out of the image.
* @return  Image from which the color has been removed.
public static Image makeColorTransparent (Image im, final Color color) {

ImageFilter filter = new RGBImageFilter() {

// the color we are looking for... Alpha bits are set to opaque
public int markerRGB = color.getRGB() | 0xFF000000;

public final int filterRGB(int x, int y, int rgb) {
if ( ( rgb | 0xFF000000 ) == markerRGB ) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
else {
// nothing to do
return rgb;

ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);

Java Swing – Transparent Images

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s