Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: making the color dissapear.

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Thanked 0 Times in 0 Posts

    Default making the color dissapear.

    hello, i'm new to this forum, i hope to find some answers
    my problem is that i have a .png file, and i read the content to a BufferedImage object, then i move that into a
    pixel array, the file is a picture of characters that need to be in the game, the problem is that when i paint them,
    i can see their background from the file and not the background of the game, i am looking for help as to how to
    make the background transparent. i have looked over the web for solutions, but things just won't work,
    i have managed to set the Alpha number of the specific color to zero. but still i see in the game panel the gray
    color of java's usual background, here is the code:
     // target - the image, rgba - the color to kill.
      public void cleanImage(BufferedImage target,int r,int g,int b,int a)
    	  int col = ((a << 24) | (r << 16) | (g << 8) | b);
    	  int trans = ((0 << 24) | (r << 16) | (g << 8) | b);
    	  int [] pixels = ((DataBufferInt)target.getRaster().getDataBuffer()).getData();
    	  ColorModel cm = target.getColorModel();
    	  SampleModel sm = target.getRaster().getSampleModel();
    	  for (int i = 0;i<pixels.length;i++)
    			if (pixels[i] == col) pixels[i] = trans;//-1118482;
    	  DataBuffer db = new DataBufferInt(pixels,target.getHeight()*target.getWidth());
    		WritableRaster wr = Raster.createWritableRaster(sm,db,null);
    		target = new BufferedImage(cm, wr, false, null);

  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default Re: making the color dissapear.

    Does the image itself have a transparent background? PNG-8 and PNG-32 are the only PNGs that allow transparent backgrounds. If your image is a PNG-24, the first thing you'll need to do is probably save it as one of the others. I'm not sure if Java changes the transparent backgrounds when it imports it.
    After that, I'm not too sure how to get java to make a background transparent. I just wanted to tell you something that you would want to be mindful of.

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Why does the Color class have two variables for each color?
    By Melawe in forum Java Theory & Questions
    Replies: 5
    Last Post: May 10th, 2012, 04:21 PM
  2. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM
  3. Trying to output color....HELP PLEASE!
    By toppcon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2011, 10:52 PM
  4. JTable Row Color
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2010, 03:59 PM
  5. Color Problem
    By aussiemcgr in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 03:53 PM