views:

57

answers:

3

Say I have a button and want to change the background button to a gradient, is it better/faster to draw a gradient using Java2D or just get an image with that gradient and put it in the background of the button?

+1  A: 

The only way to tell if something is faster is to measure it. As changing a button's background color varies by look & feel, I like to implement the Icon interface in order to decorate a button, as suggested in this example.

trashgod
+1  A: 

Best answer: Measure it and see.

Realistic answer: Doesn't really matter. Screen refresh rate is usually the more important factor.

Best solution: Use some form of double buffering to make the question irrelevant.

Stargazer712
A: 

I agree that it is important to measure it and see. From my experience, painting a gradient is likely going to be slower than painting an image. However, if you are only talking about a small button, it may not make enough difference to worry about. There are some tricks to speed up gradients. For instance, you can paint it to a BufferedImage and whenever you need to paint your gradient use g.drawImage(Image img, int x, int y, ImageObserver observer) instead of creating a new gradient. Even more, if your gradient is straight up and down, you can store the gradient in a BufferedImage of 1 pixel width. Then you can paint the gradient across the entire width of your background using g.drawImage(Image img, int x, int y, int width, int height, ImageObserver observer).

However, worrying about which is faster is generally pointless though if one takes 1 millisecond and the other takes 0.1 millisecond (unless you are trying to call it 1000 times a second of course). For more information I would recommend the book Filthy Rich Clients, especially chapters 4, 5, and 7.

Jay Askren