Transparancy [fairly simple]

Hello, im trying to work with transparancy. So i have copy’d the following code:
now this 0xFF00FF00 used to be 0xFF0000 for blue. But i have changed to make it work with purple/pink. However this does not seem to work.
Can anybody help me out with this simple question?


				private Image setTransparency(BufferedImage image){
		ImageFilter filter = new RGBImageFilter(){
			public final int filterRGB(int x, int y, int rgb){
				return (rgb << 8) & 0xFF00FF00;						//0xFF(255.)00(0.)FF(255.)00(0) r,g,b,t 
			}
		};
		ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
		return Toolkit.getDefaultToolkit().createImage(ip);
	}

One second, i’ll post all methods needed =D.

The color of course:

The actual transparent method, similar to yours:


	private static BufferedImage makeTransparent(BufferedImage tmpImage) {
		  int h = tmpImage.getHeight(null);
		  int w = tmpImage.getWidth(null);
		  BufferedImage resultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		  int transparentColor = tmpImage.getRGB(0,0);  
		  for (int y = 0; y < h; y++)
		    for (int x = 0; x < w; x++)  {
		      int color = tmpImage.getRGB(x, y);
		      if (color == transparentColor) {
		    	  color = color & 0x00FFFFFF; // clear the alpha flag
		      }
			      resultImage.setRGB(x, y, color);
		    }
		  return resultImage;
	}

The method where you would actually pass the images through transparency in the method above(Example commented out)


	public static void makeTransparents() throws Exception {
		try {
		    //BufferedImage tmp0 = ImageIO.read(new File("storage/sprites/interfaces/hud0.png"));
		   //hud0 = makeTransparent(tmp0);
		    SystemOut.append("[Transparent Image Created.");
			SystemOut.append("==== FINISHED CALLING TRANSPARENT IMAGES ====");
		} catch (FileNotFoundException e3) {
			System.out.println("File not found.");
			e3.printStackTrace();
		}
	}

The whole TransparentImage class (if needed):


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import javax.imageio.ImageIO;
import server.system.print.SystemOut;

public class TransparentImage {

	public static BufferedImage hud0; //Define the BufferedImage
	public static int tps = 0;
	public static boolean loaded = false;

	public static SystemOut so = new SystemOut(); //Ignore
	
	public TransparentImage() {
	}
	
	public static void makeTransparents() throws Exception {
		try {
		    BufferedImage tmp0 = ImageIO.read(new File("storage/sprites/interfaces/hud0.png")); //Specify the file path.
		   hud0 = makeTransparent(tmp0); //Pass that BufferedImage through our "makeTransparent()" method
		    SystemOut.append("[Transparent Image Created.");
			SystemOut.append("==== FINISHED CALLING TRANSPARENT IMAGES ====");
		} catch (FileNotFoundException e3) {
			System.out.println("File not found.");
			e3.printStackTrace();
		}
	}
	
	private static BufferedImage makeTransparent(BufferedImage tmpImage) {
		  int h = tmpImage.getHeight(null);
		  int w = tmpImage.getWidth(null);
		  BufferedImage resultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		  int transparentColor = tmpImage.getRGB(0,0);  
		  for (int y = 0; y < h; y++)
		    for (int x = 0; x < w; x++)  {
		      int color = tmpImage.getRGB(x, y);
		      if (color == transparentColor) {
		    	  color = color & 0x00FFFFFF; // clear the alpha flag
		      }
			      resultImage.setRGB(x, y, color);
		    }
		  return resultImage;
	}
	
}

Thanks a lot! why wasnt my function working though?
and what does this line do:


rgb << 8

Ive made with your code the following sprite code(first time im using sprite sheets. seems useful! :slight_smile: )


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;


public class Sprite {
	BufferedImage imgSheet;
	//TODO -> use the Offset 
	private int xOffset, yOffset, x, y, size;
	
	
	Sprite( int x , int y, int offset, int size){
		init();
		this.x = x;
		this.size = size;
		this.y=y;
		setImage();
		xOffset = x+offset;
		yOffset = y+offset;
	}
	
	void setImage(){
		
	}
	
	void init(){
		try {
			imgSheet = ImageIO.read(new File("sheet.png"));
		} catch (IOException e) {
			System.out.println("Error while loading image sheet! \n Program will now abort.");
			e.printStackTrace();
		}
		
	}
	//Returns the corresponding image
	public Image getImage(){
		return setTransparency(imgSheet.getSubimage(x*size+xOffset, y*size+yOffset, size, size));
	}
	
	private Image setTransparency(BufferedImage image){
        int h = image.getHeight(null);
        int w = image.getWidth(null);
        BufferedImage resultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        int transparentColor = image.getRGB(0,0);  
        for (int y = 0; y < h; y++)
          for (int x = 0; x < w; x++)  {
            int color = image.getRGB(x, y);
            if (color == transparentColor) {
               color = color & 0x00FFFFFF; // clear the alpha flag
            }
               resultImage.setRGB(x, y, color);
          }
        return resultImage;
	}
}

Java Operators:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Haven’t used that operator myself yet :confused:

Shifting. If you had met binary then you’ll know. That means to shift all bits to left for 8 steps according to the “arrow”. So N >> 1 equals N / 2.

I remember my teacher saying something about that, only that was with microcontrollers and in c >>. But thanks for the heads-up!

Anyways i have a small problem while trying to draw text. If the text does not have pink in it, then the black will become transparent. And sometimes the pink doesnt get transparent. I use rather much code, i hope somebody can be bothered to see my problem!


import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class TextChar {
	
	BufferedImage imgSheet;
	Image Char;

	private int xPosition, yPosition;
	
	
	TextChar(int xPosition, int yPosition, int x, int y, int offset, int size){
		this.xPosition = xPosition;
		this.yPosition = yPosition;
		try {
			imgSheet = ImageIO.read(new File("charSheet.png"));
		} catch (IOException e) {
			System.out.println("Error while loading image sheet! \n Program will now abort.");
			e.printStackTrace();
		}

		Char = setTransparency(imgSheet.getSubimage(x*size+x+offset, y*size+y+offset, size, size));
	}
	TextChar(int xPosition, int yPosition, int x, int y, int offset, int size, int useless){
		this.xPosition = xPosition;
		this.yPosition = yPosition;
		try {
			imgSheet = ImageIO.read(new File("charSheet.png"));
		} catch (IOException e) {
			System.out.println("Error while loading image sheet! \n Program will now abort.");
			e.printStackTrace();
		}

		Char = imgSheet.getSubimage(x*size+x+offset, y*size+y+offset, size, size);
	}
	
	private Image setTransparency(BufferedImage image){
        int h = image.getHeight(null);
        int w = image.getWidth(null);
        BufferedImage resultImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        int transparentColor = image.getRGB(0,0);  
        for (int y = 0; y < h; y++)
          for (int x = 0; x < w; x++)  {
            int color = image.getRGB(x, y);
            if (color == transparentColor) {
               color = color & 0x00FFFFFF; // clear the alpha flag
            }
               resultImage.setRGB(x, y, color);
          }
        return resultImage;
	}
	
	public void draw(Graphics g){
		g.drawImage(Char, xPosition, yPosition, null);
	}
}

And my Line class, which will make for each char an object:


import java.awt.Graphics;
import java.util.ArrayList;


public class TextLine {
	private ArrayList<TextChar> characters;
	//TextChar(int xPosition, int yPosition, int x, int y, int offset, int size)
	TextLine(int x, int y, String line){
		characters = new ArrayList<TextChar>();
	    char[] stringArray;

	    stringArray = line.toCharArray();
	    for(int i = 0; i<stringArray.length;i++){
	    	switch(stringArray[i]){
	    	case 'a':
	    		characters.add(new TextChar(x+i*6,y,0,0,1,7));
	    		break;
	    	case 'b':
	    		characters.add(new TextChar(x+i*6,y,1,0,1,7));
	    		break;
	    	case 'c':
	    		characters.add(new TextChar(x+i*6,y,2,0,1,7));
	    		break;
	    	case 'd':
	    		characters.add(new TextChar(x+i*6,y,3,0,1,7));
	    		break;
	    	case 'e':
	    		characters.add(new TextChar(x+i*6,y,4,0,1,7));
	    		break;
	    	case 'f':
	    		characters.add(new TextChar(x+i*6,y,5,0,1,7));
	    		break;
	    	case 'g':
	    		characters.add(new TextChar(x+i*6,y,6,0,1,7));
	    		break;
	    	case 'h':
	    		characters.add(new TextChar(x+i*6,y,7,0,1,7));
	    		break;
	    	case 'i':
	    		characters.add(new TextChar(x+i*6,y,8,0,1,7));
	    		break;
	    	case 'j':
	    		characters.add(new TextChar(x+i*6,y,9,0,1,7));
	    		break;
	    	case 'k':
	    		characters.add(new TextChar(x+i*6,y,0,1,1,7));
	    		break;
	    	case 'l':
	    		characters.add(new TextChar(x+i*6,y,1,1,1,7));
	    		break;
	    	case 'm':
	    		characters.add(new TextChar(x+i*6,y,2,1,1,7,1));
	    		break;
	    	case 'n':
	    		characters.add(new TextChar(x+i*6,y,3,1,1,7,1));
	    		break;
	    	case 'o':
	    		characters.add(new TextChar(x+i*6,y,4,1,1,7));
	    		break;
	    	case 'p':
	    		characters.add(new TextChar(x+i*6,y,5,1,1,7));
	    		break;
	    	case 'q':
	    		characters.add(new TextChar(x+i*6,y,6,1,1,7));
	    		break;
	    	case 'r':
	    		characters.add(new TextChar(x+i*6,y,7,1,1,7));
	    		break;
	    	case 's':
	    		characters.add(new TextChar(x+i*6,y,8,0,1,7));
	    		break;
	    	case 't':
	    		characters.add(new TextChar(x+i*6,y,9,0,1,7));
	    		break;
	    	case 'u':
	    		characters.add(new TextChar(x+i*6,y,0,2,1,7));
	    		break;
	    	case 'v':
	    		characters.add(new TextChar(x+i*6,y,1,2,1,7));
	    		break;
	    	case 'w':
	    		characters.add(new TextChar(x+i*6,y,2,2,1,7));
	    		break;
	    	case 'x':
	    		characters.add(new TextChar(x+i*6,y,3,2,1,7));
	    		break;
	    	case 'y':
	    		characters.add(new TextChar(x+i*6,y,4,2,1,7,1));
	    		break;
	    	case 'z':
	    		characters.add(new TextChar(x+i*6,y,5,2,1,7,1));
	    		break;

	    	}
	    }
	}
	
	public void draw(Graphics g){
		for(int i = 0; i<characters.size(); i++){
			TextChar m = (TextChar)characters.get(i);
            m.draw(g);
		}
	}
}

Screenshot of the problem (i have used 2 ways, right is the normal one, and left is the one with another constructor for some of the problematic chars, which wont make it transparent)

http://img189.imageshack.us/img189/3500/transparencybug.png

http://img189.imageshack.us/img189/3500/transparencybug.png <- Bigger size. on the other one you wont be able to see it

Crap! what a big screen you have there :smiley:
My real study is microcontroller so I know that before +=.

btt, your code looks fine. Need deep thought to find out :clue:

Uploading a Image atm sec.

Could be the image :o
If you can see it:

Look real close, it looks like you’re first chars are spaced fine, than towards the edge you’re cutting them a little closer:
Not giving enough space between chars for the transparency to fill it.

So how would i fix this? do i need bigger letters? (The horror of making them all over again! ><)
Also why is my black getting transparent then? any ideas?