Here you can find the source of makeColorTransparent(Image image, final Color transparentColor)
Makes the given color in the given Image transparent.
Parameter | Description |
---|---|
image | The Image to modify. |
transparentColor | The color to make transparent. |
public static Image makeColorTransparent(Image image, final Color transparentColor)
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany * Technical University Darmstadt, Germany * Chalmers University of Technology, Sweden * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/* ww w . jav a2 s. c o m*/ * Technical University Darmstadt - initial API and implementation and/or initial documentation *******************************************************************************/ 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; public class Main { /** * <p> * Makes the given color in the given {@link Image} transparent. * </p> * <p> * Fore more details have a look at * <a href="http://stackoverflow.com/questions/665406/how-to-make-a-color-transparent-in-a-bufferedimage-and-save-as-png">http://stackoverflow.com/questions/665406/how-to-make-a-color-transparent-in-a-bufferedimage-and-save-as-png</a>. * </p> * @param image The {@link Image} to modify. * @param transparentColor The color to make transparent. * @return A new created {@link Image} where the color is transparent. */ public static Image makeColorTransparent(Image image, final Color transparentColor) { ImageFilter filter = new RGBImageFilter() { @Override public final int filterRGB(int x, int y, int rgb) { if (rgb == transparentColor.getRGB()) { return rgb & 0xFFFFFF; // Set fully transparent but keep color } return rgb; } }; ImageProducer ip = new FilteredImageSource(image.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); } }