This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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...
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.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Joined: Jun 16, 2011
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?
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.
Joined: Jun 16, 2011
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.
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.