Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:44:53+00:00 2026-06-01T10:44:53+00:00

Possible Duplicate: I am making some parts of java image transparent by some code,

  • 0

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();

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:44:55+00:00Added an answer on June 1, 2026 at 10:44 am

    Use PhiLho;s solution in How to make a color transparent in a BufferedImage and save as PNG

      private Image TransformColorToTransparency(BufferedImage image, Color c1, Color c2)
      {
        // Primitive test, just an example
        final int r1 = c1.getRed();
        final int g1 = c1.getGreen();
        final int b1 = c1.getBlue();
        final int r2 = c2.getRed();
        final int g2 = c2.getGreen();
        final int b2 = c2.getBlue();
        ImageFilter filter = new RGBImageFilter()
        {
          public final int filterRGB(int x, int y, int rgb)
          {
            int r = (rgb & 0xFF0000) >> 16;
            int g = (rgb & 0xFF00) >> 8;
            int b = rgb & 0xFF;
            if (r >= r1 && r <= r2 &&
                g >= g1 && g <= g2 &&
                b >= b1 && b <= b2)
            {
              // Set fully transparent but keep color
              return rgb & 0xFFFFFF;
            }
            return rgb;
          }
        };
    
        ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
          return Toolkit.getDefaultToolkit().createImage(ip);
      }
    

    Or, http://marxsoftware.blogspot.com/2011/04/making-white-image-backgrounds.html

       public static Image makeColorTransparent(final BufferedImage im, final Color color)  
       {  
          final ImageFilter filter = new RGBImageFilter()  
          {  
             // the color we are looking for (white)... Alpha bits are set to opaque  
             public int markerRGB = color.getRGB() | 0xFFFFFFFF;  
    
             public final int filterRGB(final int x, final int y, final int rgb)  
             {  
                if ((rgb | 0xFF000000) == markerRGB)  
                {  
                   // Mark the alpha bits as zero - transparent  
                   return 0x00FFFFFF & rgb;  
                }  
                else  
                {  
                   // nothing to do  
                   return rgb;  
                }  
             }  
          };  
    
          final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);  
          return Toolkit.getDefaultToolkit().createImage(ip);  
       }  
    }
    

    Obviously, the difference between your problem and the second solution is here:

    public int markerRGB = color.getRGB() | 0xFF000000;   
    

    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

    class TransparentFilter extends RGBImageFilter {
    
        final static int aShift=24;
        final static int rShift=16;
        final static int gShift=8;
        final static int bShift=0;
        final static int aMask=0xff<<aShift;
        final static int rMask=0xff<<rShift;
        final static int gMask=0xff<<gShift;
        final static int bMask=0xff<<bShift;
        final static int rgbMask=rMask|gMask|bMask;
    
        // this fudge value is used in place of the harder (but proper) task of
        // performing statistical analysis of the saturation values (and
        // possibly distances) of highly-saturated pixels contained in the image
        float saturationFudge;
    
        TransparentFilter(float saturationFudge) {
            this.saturationFudge=saturationFudge;
            canFilterIndexColorModel=true;
        }
    
        public int filterRGB(int x, int y, int argb) {
            // separate the three colour channels (ignore incoming alpha)
            int r=(argb&rMask) >>> TransparentFilter.rShift;
            int g=(argb&gMask) >>> TransparentFilter.gShift;
            int b=(argb&bMask) >>> TransparentFilter.bShift;
            // convert to hsb so that we can use saturation to
            // establish the alpha (transparency) value of the pixel
            float[] hsb=Color.RGBtoHSB(r,g,b,null);
            float fa=255f*hsb[1]/this.saturationFudge;
            int a=Math.max(0,Math.min(255,Math.round(fa)))<<TransparentFilter.aShift;
            return a|(argb&TransparentFilter.rgbMask);
        }
    
    }
    

    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:

    1. OS version 32/64-bit?
    2. JRE/JDK version 32/64-bit?
    3. GPU: hardware and driver?
    4. Display: 256-bit/16-bit/24-bit?

    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 ??:

    1. Java Swing transparency drawing issues
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Making a generic property I'm not quite sure how to do that,
Possible Duplicate: CSS Opacity Property I'm making a layer on an image which has
Possible Duplicate: Make error: missing separator I've made many makefiles, but today I was
Possible Duplicate: Making sure a web page is not cached, across all browsers I
Possible Duplicate: Java, GUI builder or hand coding? I have been making GUI hand-coded
Possible Duplicate: How costly is Reflection? For sake of making my code generic I
Possible Duplicate: Shutting down a computer using Java I am making a personal program
Possible Duplicate: PHP take all combinations I'm thinking of making something in PHP that
Possible Duplicate: Determine device (iPhone, iPod Touch) with iOS I am making a game
Possible Duplicate: What is the !! (not not) operator in JavaScript? What does the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.