Possible Duplicate:
I am making some parts of java image transparent by some code, it works fine on the laptop I made, but not on others, Why?
Image test : the orignal image,
Image testt : the image after applying transparency
In my laptop, the color I want to make transparent becomes transparent, test as well as testt are rendered, but else where the testt image is not drawn, the test is drawn but testt is not.
The complete code for drawing a simple image with transparency:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.Serializable;
public class test extends Applet implements Runnable
{
public static Image makeColorTransparent(Image im, final Color color)
{
ImageFilter filter = new RGBImageFilter()
{
public int markerRGB = color.getRGB() | 0xFF000000; //color to make transparent
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);
}
Image test;
public void init()
{
setSize(600,600);
}
public void update (Graphics g) //overriding the update for double buffering
{
// initialize buffer
Image dbImage = createImage (this.getSize().width, this.getSize().height);
Graphics dbg = dbImage.getGraphics ();
// clear screen in background
dbg.setColor (getBackground());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void paint(Graphics g)
{
test = getImage(getCodeBase (), "tt.gif");
Image testt = makeColorTransparent(test, Color.white);
g.drawImage (testt,0,0, this);
}
@Override
public void run() {
// TODO Auto-generated method stub
repaint();
}
}
Use PhiLho;s solution in How to make a color transparent in a BufferedImage and save as PNG
Or, http://marxsoftware.blogspot.com/2011/04/making-white-image-backgrounds.html
Obviously, the difference between your problem and the second solution is here:
where instead of white color, you are looking for black color to set as a transparency color.
MIT’s solution http://web.mit.edu/javalib/www/examples/if/start.html
Still, if all solutions don’t work properly on test systems, we can suspect other possible culprits which we don’t have enough information based on your original post:
Also, we don’t know what you have observed between those systems since no screenshots are provided.
The worst is you have to start using an image with transparent mask already set to solve you issue rather than relying on coding its transparency bit (unless if you are trying to create a Java based photo-editing application which needs a transparency filter).
Possible unsolved related issues ??: