Flashing graphics

I'm trying to recreat the space invaders game. Everything is working out fine except my alien space ship images "flicker". I'm using a swing timer and the repaint() function. I've changed the update() function so that is is just drawing a buffered filled white rectangle so that not every pixel needs to be updated. I'm still getting flickering though. Any help would be appreciated. Thanks

[401 byte] By [danielnya] at [2007-10-3 5:07:34]
# 1
I just wanted to mention I'm not buffering the alien ships. I don't think that would help because they move every time the paint function is called.
danielnya at 2007-7-14 23:13:36 > top of Java-index,Security,Cryptography...
# 2

I've changed the update() function so that is is just drawing a buffered filled white rectangle so that not every pixel needs to be updated.

Oh–oh. Overriding the update method is common in AWT graphics but we don't do it in Swing (according to the designers recommendation).

Swing gives you double buffering for free so drawing is easier than in the AWT. You can still draw to an offscreen image (in your event code, eg, a timer) and draw it in the paintComponent method but it is not always required or desirable. Another technique is to do limited repaints. See the four–argument repaint method in the Component class.

Use the paintComponent method in Swing graphics unless you want/need to draw over components and borders. See the paint method in the JComponent class for details. This is another difference between Swing and the AWT.

74philipa at 2007-7-14 23:13:36 > top of Java-index,Security,Cryptography...
# 3
I'm sorry, I'm not sure I understand you. You talk about AWT and swing but I don't understand. Are you saying don't use swing? What? Thanks
danielnya at 2007-7-14 23:13:36 > top of Java-index,Security,Cryptography...
# 4
I think I got it now. I changed to paintComponent instead of regular paint. What excatly is the difference?
danielnya at 2007-7-14 23:13:36 > top of Java-index,Security,Cryptography...
# 5

Are you saying don't use swing?

No. Use either one as you like.

I changed to paintComponent instead of regular paint. What excatly is the difference?

For this we look in the javadocs. The j2se 1.5 version docs [url=http://java.sun.com/j2se/1.5.0/docs/api/index.html]are located here[/url]. In the left frame scroll down to the JComponent class and click on the link. The JComponent class will load into the main frame. Scroll down to the Methods Summary section and down to the paint method. Click the link which will take you to the description of the paint method in the Method Detail section.

74philipa at 2007-7-14 23:13:36 > top of Java-index,Security,Cryptography...
# 6
Thanks but I already read those. That didn't mean jack to me. Anybody got "simplier" language, detail explenation? I'm trying to find out why its faster. Is it not repainted everything? Thanks
danielnya at 2007-7-14 23:13:36 > top of Java-index,Security,Cryptography...