| | |
Implementing "double buffering" (without "paint()" method)
![]() |
Hi all,
I am working on the "Pacman" game and I want to use the "double buffering" technique for animation.
By now, I have used this code:
and there was by default used double buffering (because in "JPanel" there is double buffering technique used by default).
But sometimes later I realized that I want to do all my painting code in my own method, not in "paint()" one (I don't want to explain why, it is another long story, lol).
And there is a question: how can I implement the double-buffering myself?
And after some time I have written this solution:
My question is:
Is this the good technique (I mean in performance) to do all the drawing on some image and then draw that image at once to the screen, or is there some better way (in performance) to do this (maybe some pointer changing, page flipping, or something similar,...)?
Thanks.
Petike
I am working on the "Pacman" game and I want to use the "double buffering" technique for animation.
By now, I have used this code:
Java Syntax (Toggle Plain Text)
class GamePanel extends JPanel { public void paint(Graphics g) { // Do some drawing here... } }
But sometimes later I realized that I want to do all my painting code in my own method, not in "paint()" one (I don't want to explain why, it is another long story, lol).
And there is a question: how can I implement the double-buffering myself?
And after some time I have written this solution:
Java Syntax (Toggle Plain Text)
class GamePanel extends JPanel { private Image backBuffer; private Graphics backBufferGraphics = backBuffer.getGraphics(); public void myDrawingMethod() { // Get the painting area which is visible on the screen Graphics frontBufferGraphics = this.getGraphics(); // Some sample painting code (executing on the back buffer // which is not visible on screen) backBufferGraphics.drawLine(10, 10, 20, 20); backBufferGraphics.drawRect(3, 5, 40, 32); backBufferGraphics.drawPoint(20, 30); // .... // .... etc... // .... // And finally draw the whole graphics to the screen frontBufferGraphics.drawImage(backBuffer, 0, 0, this); } }
My question is:
Is this the good technique (I mean in performance) to do all the drawing on some image and then draw that image at once to the screen, or is there some better way (in performance) to do this (maybe some pointer changing, page flipping, or something similar,...)?
Thanks.
Petike
Petike
•
•
Join Date: Apr 2008
Posts: 971
Reputation:
Solved Threads: 146
It would be interesting to hear why you have rejected using paint() (or paintComponent()), because that's the "right" way to do it, and guarantees that you will handle things like windows being re-sized or covered/uncovered properly. Looking at your code as it stands, I expect that Swing will use paint() to overwrite whatever your method is doing at unpredictable (to your code) intervals.
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
If you're into Windows operational system ->
Study SUN example:
http://today.java.net/pub/a/today/20...solutions.html
For your use declare In constructor
and in your overrided method
invoke NATIVE method (at end) Remember abut
Study SUN example:
http://today.java.net/pub/a/today/20...solutions.html
For your use
java Syntax (Toggle Plain Text)
import com.sun.animation.SmoothAnimation;
java Syntax (Toggle Plain Text)
SmoothAnimation smoothAnimation;
java Syntax (Toggle Plain Text)
smoothAnimation = new SmoothAnimation();
java Syntax (Toggle Plain Text)
public synchronized void paintComponent(java.awt.Graphics g)
java Syntax (Toggle Plain Text)
smoothAnimation.vbLock();
Java Syntax (Toggle Plain Text)
VBLocker.dll
•
•
Join Date: Sep 2008
Posts: 1,563
Reputation:
Solved Threads: 196
This is an interesting read. http://java.sun.com/docs/books/tutor...doublebuf.html
>Is this the good technique (I mean in performance) to do all the drawing on some image and then draw that image at once to the screen
Yes, that is typically what is done. You can update portions of the image as you wish and paintComponent() just renders that image to the screen. Be sure to dispose() of Graphics references that you obtain from getGraphics() calls.
Yes, that is typically what is done. You can update portions of the image as you wish and paintComponent() just renders that image to the screen. Be sure to dispose() of Graphics references that you obtain from getGraphics() calls.
![]() |
Other Threads in the Java Forum
- Previous Thread: GridBagLayout - Problem resizing
- Next Thread: jdbc connection
| Thread Tools | Search this Thread |
addball android api append applet application apps array arrays automation awt binary bluetooth businessintelligence busy_handler(null) button card class client code collision component constructor crashcourse css csv database draw eclipse ee error eventlistener exception fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jni jpanel jtree julia jvm linux list loan machine map method methods migrate mobile netbeans oracle output phone physics plazmic problem program programming project radio recursion reporting scanner server service set sharepoint smart sms software sort sortedmaps sql string swing textfield threads tree trolltech unlimited utility webservices windows






