aspose file tools*
The moose likes Game Development and the fly likes Making translucent images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Making translucent images" Watch "Making translucent images" New topic
Author

Making translucent images

John Hindmarch
Greenhorn

Joined: Jun 16, 2011
Posts: 3
Hi there, first time posting, and fairly new to Java (and programming in general!) so be gentle...

I have a game where the background image fades in and out behind the action. I do this by drawing the background image and then drawing a translucent black rectangle over it each frame, with the alpha value going up and down according to a sine wave. However, this really slows the game down, so I was trying another method: creating an array (newbgrounds) of background images of different intensities before the game starts, so I just have to draw one opaque background image each frame.

Here's the code I'm using:



(Please ignore the actual maths of calculating the alpha value y, I'm not sure if its right, but can't check it 'til the rest works).
If I get rid of the loop (or just set it to i<1), it works as intended (I think!) and creates a single background image by combining the original image and the translucent black rectangle. However, as soon as I put it in a loop, I just get a flat black rectangle. I think it may be drawing cumulative translucent black rectangles on the same image, rather than starting with the original image (bg) each iteration and drawing a single translucent rectangle over the top...

Any hints/ideas?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

Hi John, welcome to CodeRanch!

You are correct. In each iteration, you're simply applying your rectangle over the same image. the createGraphics method simply provides you with an object that will draw onto the existing image. It doesn't create a new image.
Before you do your drawing, you will have to create a new image out of your existing base image.

I will see if I can do some tests using your first solution, dimming the image on the fly, and see how slow it gets.
John Hindmarch
Greenhorn

Joined: Jun 16, 2011
Posts: 3
Thanks for the quick reply Stephan.

I thought by including:



in each iteration, I'd be creating a new image equal to the original bg image, and only then creating the graphics object - clearly not...

I've done some image manipulation stuff in c++, and I would just use a loop to change the intensity of each pixel directly, is that something that can be done easily in Java?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

What you're currently doing is assigning a new image to the newbg variable, and then overriding that variable again with your original image, losing the newly created image, which promptly makes it eligible for garbage collection.

I'm not exactly sure if this is the proper way of doing things, but you can try calling copyData on your original image, and then use the result in the setData of your newly created image. Simply drop the newbg = bg line.
John Hindmarch
Greenhorn

Joined: Jun 16, 2011
Posts: 3
Aha, so I'm actually making changes to the original bg image? Because that's exactly what I didn't want to do! I'll look into copyData and setData, thanks again for your help.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

Oh, by the way. You might want to divide i by 200 instead of 100, because you'll want hundred increments between zero and Pi/2. If my math isn't off, this will result in a transparency between 0 and 1. Sin(Pi) is 0 after all. You only want a quarter of a full sine period.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making translucent images