[java] Drawing with transparency and keyboard slow downs

Started by
1 comment, last by Stiby 23 years, 5 months ago
Hi all, I have been working on a simple top down shoot um'' up in Java and things are going quiet well but I have two problems to solve and any suggestions would be appriciated. 1. Draw images with transparency? I want to draw my sprites ideally depicting the transparent colour, or by usaing my sprite sets as gifs and using the gif format to specify the transparency colour but I am still getting a white box effect any ideas? (I am drawing image object using the createImage(int, int) method). 2. MY keyboard listener is ideal for taking keyboard input but my OS sends repeater messages once the key has been held down long enough which gives a visable slow down on the frame rate any ideas on an ideal solution to solve this probelm? Thanks to any one who has the time to reply to this messages! Phillip Stiby BSc(HONS) I program therefore cant spell!
StibyI program therefore cant spell!
Advertisement
If you are loading a GIF that has transparency Java will display it properly. Make sure that the GIF was done correctly by viewing it in another image viewer. If you are creating the image internally then you must set the alpha of the color you want to disappear. With Java2 you can use the AlphaComposite class to draw your sprites onto BufferedImages, with Java <= 1.1 you need to use the PixelGrabber and cycle through each pixel, setting the alpha as you find RGB values that you want to erase.

The keyboard slowdown you notice is natural in Java. Java is designed for desktop applications and not much else. Since events are part of AWT they both run in the same thread. Holding a key down floods the event queue and slows down any graphics. On linux any graphic operations stall untill the queue becomes empty, on windows the slowdown is not nearly as damaging. AWT & Swing are not thread-safe and therefore cannot utilize threads which are necassary for responsive, graphically intense games.
I''m part way through writing a game in Java, and I haven''t had any encounter with the problem you mention.
The game is a remake of a popular eighties game called Repton and runs in an area of 512*512, it''s a very busy game and you do have a lot of things moving round the screen and I detect no slow-down at all when I''m giving keyboard input. Running on a PII 400 it gets 40 fps and it''s all written in Java, as opposed to partly in C.
The only thing I can think of that you might be doing that might slow down the game is that you are reacting to the key-press in the same thread, ie. instead of calling repaint() or whatever when the key is pressed have repaint() called by a seperate thread running a timer object as often as you can make it, and in the key press listener code just set a flag to say this key has been pressed and let the game react in the seperate thread, this does however bring it''s own set of problems. I don''t mind clarifying this if you didn''t follow....

On the topic of alpha channels in images, I would say not to use them if you can find a way round them, the reason for this is that Java is a lot slower at creating 32-bit images and drawing them to the screen than it is at doing 24-bit images, which you would expect. There are ways to draw 2/3 layers of graphics without using 32-bit images so that they all appear correctly if the only alpha channels involved are those with a value of 255 or 0. If written properly the speed difference can be quite significant. (>25% increase in frame rate in my case)

Dewi Williams

This topic is closed to new replies.

Advertisement