How are the images used correctly in 2D? (gif or png?)

Im wondering, how is the transparency made to games usually?

If I use png, the graphics work perfectly: I can use translucency (that half-transparent thing), but the really big minus is, that my game’s fps dropped to 1-3, which is way too slow.
–> The game with png images

If I use gif, the fps is good, something like 50-70, but the picture looks like it would have been made with chainsaw.
–> The game with gif images
(notice the tree line near the sky and the bushes at the bottom, ball and players arent gifs, because those are small enough pictures to use pngs)

It feels like Im missing something…

PNG can also do indexed images.

So, the question isn’t if you should use gif or png… instead it’s if you should use full alpha or bitmasked transparency. Bitmasked is of course faster… but… well there is no reason that it needs to look that bad. Your screenshot looks like you only set the fully transparent bits to transparent and the rest to opaque (and it was also blended to a white background before). Well, that doesn’t work (apparently).

Usually you would need to use some kind of treshold - 50%. Less than that = opaque and more than that = transparent. Your drawing programm should do stuff like that automatically, if you save a full alpha image as gif or 8bit png. However, you could also do that at runtime (if you want to give the user the ability to switch between fullalpha and bitmasked). All you need to do is copying the image over to a bitmasked image.

well, just in case you don’t know, there are 3 different types of transparencies in Java:

  • Opaque - all pixels are visible (no transparent pixels)
  • Bitmask - all pixels are either 100% visible or 100% transparent (no middle-ground here)
  • Translucent - pixels can be partially visible and partially transparent

Now there’s a difference between GIF and PNG. GIFs can only save in the opaque or bitmask mode. It doesn’t support translucent pixels. PNG, however, supports all 3 transparencies. The problem with your FPS is hardware acceleration.

Hardware acceleration makes use of your video card and speeds up Image processing. One way to create an accelerated Image is to use this method from the GraphicsConfiguration class:


createCompatibleImage(width,height,transparency);

That last parameter is an int provided by the Transparency class. You can use either Transparency.OPAQUE, Transparency.BITMASK, or Transparency.TRANSLUCENT

The problem is Java only supports acceleration for BITMASK and OPAQUE - which is why your PNG with translucent pixels chokes the FPS. Not to mention it takes alot of computing power anyway to composite translucent pixels.

There are a few things you can try to get around your problem:

  • Don’t use transparency for your mountains. If the background image doesn’t change in some way, there’s no reason for the transparency in the mountains. Save the mountains with the same color background that the real background uses
  • Enable the OpenGL pipeline (Java 1.5 only) by passing this option in the command-line: -Dsun.java2d.opengl=true
  • Use an OpenGL library (LWJGL, Xith3D, jME, etc.) - These libraries give you access to OpenGL’s capabilities. The OpenGL is all hardware accelerated and OpenGL can handle translucency much more decently than average Java

Actually the sky is moving, so thats the reason why the mountains are translucent. But I figured I could raise the clouds a little bit, so I could use even jpg-images as the mountains.

[quote]- Enable the OpenGL pipeline (Java 1.5 only) by passing this option in the command-line: -Dsun.java2d.opengl=true

  • Use an OpenGL library (LWJGL, Xith3D, jME, etc.) - These libraries give you access to OpenGL’s capabilities. The OpenGL is all hardware accelerated and OpenGL can handle translucency much more decently than average Java
    [/quote]
    Thanks, that looks like something that will be useful. Ill be back after I have checked those.

I know that this game is also going to be played/tested with slower machines than mine, so that might be a good thing to do also :slight_smile:

Problem solved, the solution was to activate the hardware acceleration (-Dsun.java2d.opengl=true), now the game runs smoothly even with all those translucent images, tho I think I do as you suggested and change the format etc.

But another problem: The game wont go to full-screen mode anymore.

Of course the problem might be in the class which I use to go into full-screen mode, but if this is a common problem, Id appreciate to know it (and/or the solution) :slight_smile:

I’ve had this problem too, and as far as I know this is a known bug. But there is a way around it.

Instead of setting the DisplayMode manually, you should use the GraphicsDevice.getDisplayModes (returns an array of all available displaymodes that your hardware supports) and then pick a suitable one from that list. Simple as that 8)

This is a link to my post about OpenGL and Fullscreen exclusive mode
http://192.18.37.44/forums/index.php?topic=4953.msg45074#msg45074

Thx very much. I havent tested that yet, but I choose that mode manually, so I think that solution will work :slight_smile: