Sprite Alpha

Ok, one question with possibley two parts:

First, basicaly: How can I set the alpha (or transparency) of a sprite held as a BufferedImage in Graphics2D?

Secondly: What I want to be able to do is fade in and out my particles neatly as they are created/destroyed.

I sort of invision the particle eventualy having some sort of ‘Stop’ system like:

Age=0, Alpha=0.0;
Age=50, Alpha=1.0;
age 200, Alpha=0.0;

Where my sprite Render() method then interperlates between those values for any given life of the sprite.

Ahh, part three of two:
It would also be usefull to change the color of the sprite using the above method - is it possible, given a grey (or colored) image, to tint the image to a color?

Thanks :slight_smile:

Either use a GIF with transparency or a PNG with an alpha channel.

What’s the question?

It is not possible directly with the 2D API like it is with OpenGL. Instead you would have to read the data out of the BufferImage, change it to the colour you want and write it back.

Ahh, ok. What I ment was modifying the overall opacity of an image during the Render() method of my game loop to any value I like, rather than having an image with pixles that are transparrent, which I can do fine.

The second question was, assuming that you could dynamicly alter the overall alpha of an image (gif, png, jpeg whatever). So if this was then done over time, the image could be faded in and out. Would this be possible in the wayi described.

It would seem not then.

You might be able to do it with java.awt.image.ColorConvertOp. I am not that familiar with the image sub package so I don’t know how it works. The only way I know how is to pull the data out of the image and transform the colours and then put it back.

yeh, I’ve had a look through the docs and all the methiods seem rally longwinded.

I think that I’m going to have to move to JOGL if I want to get the results that I want to get with this project.


glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
//BOOM

Yes, OpenGL will be a good idea. You might want to go with LWJGL instead as it is more frequently updated.

Guys, you’re making this harder than it needs to be:


g.setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); // Set blending mode and alpha

g.drawImage(...);

g.setPaintMode(); // Restore previous state

You might want a different blend mode for particles depending on what effect you’re after.

Something like this:


private float[] ages = new float[]{0,50,200};
private float[] alphas = new float[]{0,1,0};

public float getAlphaForAge(float age)
{
   for(int i=1; i<ages.length; i++)
   {
      if(age > ages[i])
         continue;

      float loAge = ages[i-1];
      float hiAge = ages[i-0];
      float ratio = (age - loAge) / (hiAge - loAge);
   
      float loAlpha = alphas[i-1];
      float hiAlpha = alphas[i-0];
      return loAlpha + ratio * (hiAlpha - loAlpha);
   }

   return alphas[alphas.length-1];
}

Or your can precalculate it into a float[200]…

If your doing what I’m thinking you are doing then you can try this, this allows you to change the transparency on an image, preferably a png:


        /** Image transperancy value*/
	float transperancy = 0;

	public BufferedImage drawAlpha() {
		/** Load Image, note that it is better if you preload the image else where, this is just to show you how its loaded. */
		BufferedImage loaded = ImageIO.read(new File("images/buildings/g_u_1.png"));

		/** Create alpha image */
		BufferedImage aimg = new BufferedImage(loaded.getWidth(), loaded.getHeight(), BufferedImage.TRANSLUCENT);  

		 /** Get image graphics */ 
		Graphics2D g = aimg.createGraphics();

		/** Set the Graphics composite to Alpha */
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transperancy));  

		/** Draw the loaded image into the prepared reciver image */
		g.drawImage(loaded, null, 0, 0);  

		/** Release system resource in this graphics */
		g.dispose();
		
		return aimg;
	}

And this is how you would draw it, the transparent value has a range from 1.0 ~ 0.0, hope it helps.:


public void paint(Graphics2D g) { 
      g.drawImage(drawAlpha(), x, y, width, height, null);
}

whats with all this filtering bullshit when we can just use the AlphaComposite class?!