Set Image Color as Transparent


/ Published in: Java
Save to your folder(s)

nifty little function when you want transparent images such as sprite sheets


Copy this code and paste it in your HTML
  1. public Image makeColorTrans(Image im,final Color c)
  2. {
  3. ImageFilter filter = new RGBImageFilter()
  4. {
  5. // the color we are looking for... Alpha bits are set to opaque
  6. public int markerRGB = c.getRGB() | 0xFF000000;
  7.  
  8. public final int filterRGB(int x, int y, int rgb) {
  9. if ( ( rgb | 0xFF000000 ) == markerRGB ) {
  10. // Mark the alpha bits as zero - transparent
  11. return 0x00FFFFFF & rgb;
  12. }
  13. else {
  14. // nothing to do
  15. return rgb;
  16. }
  17. }
  18. };
  19.  
  20. ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
  21. return Toolkit.getDefaultToolkit().createImage(ip);
  22. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.