Sign in to follow this  

[java] Slow drawing

This topic is 3863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a Java game application (not a full screen one) that is running like sammy the snail. I want to draw some polygons, then a transparent blue on top which gives a fade/fuzzy appearance then the higer level polygons. This then gives the impression that the lower polygons are under water. The problem is that the transparent layer takes approx 20ms to execute. I feel that if I can reduce the 20ms that I will be able to get an increase in the frame rate. I have included some code snips and I would be obliged if someone could suggest a way to speed things up. One idea I have had is if I could fade the lower polygons then I would get somewhat the same effect. public class Game extends Panel implements MouseMotionListener, MouseListener, Runnable { ... Image offScreenImage; Graphics bufferGraphics; Graphics2D bufferGraphics2D; ... /*Implemented Interface, updates the screen. public void update(Graphics g) { if (offScreenImage == null) { offScreenImage = createImage(d.width, d.height); bufferGraphics = offScreenImage.getGraphics(); } Graphics2D g2 = (Graphics2D) g; bufferGraphics2D = (Graphics2D) bufferGraphics; bufferGraphics2D.setColor(new Color(102, 204, 255)); bufferGraphics2D.clearRect(0, 0, d.width, d.height); ... //draw the lower level polygons bufferGraphics2D.setColor(new Color(0, 191, 255, 150)); //notice has transparancy //this next line takes 20ms to execute bufferGraphics2D.fillRect(0, 0, d.width, d.height); .. //Draw the higher level polygons .. g2.drawImage(offScreenImage, 0, 0, null); //draw lightning fast Thanks in advance, Peter

Share this post


Link to post
Share on other sites
The problem is the Java2D graphics pipeline. It tries to do everything in OpenGL, but once you start to use alpha blending, all the drawing is move to software. If you want to use alpha blending you need to use OpenGL via LWJGL or JOGL. Or use a 2D library, like Slick, that wraps OpenGL.

Another 2D library is GTGE, although I don't know much about it.

Share this post


Link to post
Share on other sites
Thank you CaptainJester.

I have looked at the resources you suggested.

Is there anyting that I can do right now with my code? I have found your reply at http://www.gamedev.net/community/forums/topic.asp?topic_id=427570 for something similar. How could I incorporate this in my code?

Regards,

Peter

Share this post


Link to post
Share on other sites
Hi.

Did you try applying the translaccel flag? You can also try shifting from your translucent rectangle drawing to a transclucent image instead. Set its accelleration priority to maximum. Also, any reason you are not using BufferStrategy?

As a side note, you're also creating an excessive amount of new objects on each cycle. Your Colors could be declared outside your method so that they are not instanciated each cycle. This wont fix your problem, but everything helps, right?

Share this post


Link to post
Share on other sites
Since you are only using polygons, you could blend the color before drawing.

ie.

1. Draw rectangle that is water color.
2. Alpha blend water color with lower polygon color
3. Set blended color and draw lower polygons
4. Set normal polygon color and draw upper polygons

Note do not use an alpha value in the colors you set on the graphics context. Just apply the alpha to each of the red, blue and green components.

Share this post


Link to post
Share on other sites
Hi CaptainJester,

Thank you for the assistance.

I am a bit of a novice programmer and I have not figured out how I could convert to BufferStrategy. Do you think that this will make it a bit faster?

I have created my colour object outside, I just did it that way in the snippet for clarity.

I have about 100 polygons with many different colours to draw under the ocean so what seems to be the simplest is to draw a transparent ocean on top of these.

Do you think that the suggestion you gave in http://www.gamedev.net/community/forums/topic.asp?topic_id=427570 would be any faster?

Best regards and thanks again
Peter

Share this post


Link to post
Share on other sites
No, I don't think that way would be faster. However, since you are drawing polygons, it still might be faster to alpha blend each color you use once and then draw each ploygon with the blended color.

If you know the colors ahead of time, you could create a class that holds the ploygon, the regular draw color and the underwater draw color ahead of time. Then just set the appropriate color at draw time.

As Addictman said, you could also try to create an image with the your water color, including the alpha value and see if that is faster.

If you read through The Java Tutorial's Full Screen section, they talk about how to use BufferStrategy. http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html

Share this post


Link to post
Share on other sites
Just another thing that might help.

How often are your polygons altered? If not often (not often is less than each 10th cycle or less), you might be better off rendering them as images as well.

The problem with rendering lines, polygons, - shapes in general, is that they need to be rasterized before they can be rendered to your screen. Images are already rasterized and can skip this step. So in many cases, while images require immense amount of memory compared to basic shapes, they are often much faster to render.

If you want an example about what I mean, look up "intermediate images" on google. There is a decent tutorial and explanation on the sun website.

Rendering a large translucent "thing" is nevertheless a costly process. If I were you, (note that I dont know how many/large polygons you draw on any given moment), I would take a close look to see if you really really need to draw the translucent layer on each cycle. Perhaps only a few polygons changed since the last cycle? In such a case, you'd only have to redraw a smaller portion of your translucent layer. If you can find such a solution (again; it all depends on your polygons, their size, and how often they change), it will be well worth your while.

Share this post


Link to post
Share on other sites
Hi Addict Man & Captain Jester,

Thank you for your help. I am a novice programmer and it is great to get this assistance.

I guessed my self that the best way (in terms of speed)is to have my creatures drawn with a colour based on their level and on the game level. But I learnt loads along the way about blurring etc.

Thanks again
Peter

Share this post


Link to post
Share on other sites
Hey Peter,

This all sounds a little familiar. Java's Graphics2D and transparencies and alphas just don't play nice together. The more alphaing that you have to do it seems the longer it takes.

Getting the glow I found to be near impossible using the standard java library. As has been mentioned already using LWJGL or something along those lines really is the best way to go. Unfortunately as you know yourself time is a big constraint at the moment.

To get the illusion of depth and the under-water effect I simply rendered the creatures on the level below me with a lower alpha value. This way your using as little alphas as possible, rather than alphaing the whole screen which will just destroy your frame rates.

Hope this helps and is not to all over the place, in the middle of studying for an Operating Systems exam and my head is melted.

eoin

Share this post


Link to post
Share on other sites

This topic is 3863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this