Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Screen fading in Java2D

Started by DeusFacticius Dec 8, 2006 at 11:19 AM 0 replies 1.2k views
Original Post
DeusFacticius
DeusFacticius
so i've spent a few months now writing a Java2D pure-java game. one of the requirements of the project is to run completely within the security bounds of an applet, meaning no native library bindings (JOGL, LWJGL), and most likely no runtime JVM performance flags (although we can at least ask users to manually set these themselves). the project assumes a minimum of java 1.5. now the problem: performance is horrible as expected but it is enough to run our real-time game. recently i ran into a technical hurdle -- screen fading for screen transitions. as it is, i've implemented it as a Rectangle2D being drawn over the screen while interpolating its alpha values. doing so causes the frame rate to drop to about 1~3fps, where the normal frame rate is around 60~80fps. does anyone have any ideas for an alternative method of performing a screen fade? the only other solution i've thought of is to make an image the same dimensions as the display, and using a Composite effect to achieve the fading, however without being guaranteed that the sun.java2d.transaccel flag will be set, this might yield even worse performance.
CaptainJester
CaptainJester
You could try bitshifting the pixel data.

1. Do all drawing to an offscreen image. (You probably already do this)
2. Make sure the offscreen image is a BufferedImage
3. Get the pixels:
BufferedImage image = //create image
byte data[] = (byte[])image.getRaster().getDataElements(0, 0, width, height(), null);
3a. Check the data type so the cast to byte[] is correct:
image.getTransferType() == DataBuffer.TYPE_BYTE
4. Iterate over data shifting it 1 bit:
data = data >>> 1;
5. Put data back into image:
image.getRaster().setDataElements(0, 0, width, height, data);
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.