Implementing "double buffering" (without "paint()" method)

Reply

Join Date: Feb 2009
Posts: 4
Reputation: petike is an unknown quantity at this point 
Solved Threads: 0
petike's Avatar
petike petike is offline Offline
Newbie Poster

Implementing "double buffering" (without "paint()" method)

 
0
  #1
Apr 7th, 2009
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:
  1. class GamePanel extends JPanel
  2. {
  3. public void paint(Graphics g)
  4. {
  5. // Do some drawing here...
  6. }
  7. }
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:
  1. class GamePanel extends JPanel
  2. {
  3. private Image backBuffer;
  4. private Graphics backBufferGraphics = backBuffer.getGraphics();
  5.  
  6. public void myDrawingMethod()
  7. {
  8. // Get the painting area which is visible on the screen
  9. Graphics frontBufferGraphics = this.getGraphics();
  10.  
  11. // Some sample painting code (executing on the back buffer
  12. // which is not visible on screen)
  13. backBufferGraphics.drawLine(10, 10, 20, 20);
  14. backBufferGraphics.drawRect(3, 5, 40, 32);
  15. backBufferGraphics.drawPoint(20, 30);
  16. // ....
  17. // .... etc...
  18. // ....
  19.  
  20. // And finally draw the whole graphics to the screen
  21. frontBufferGraphics.drawImage(backBuffer, 0, 0, this);
  22. }
  23. }


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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 971
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Implementing "double buffering" (without "paint()" method)

 
0
  #2
Apr 7th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Implementing "double buffering" (without "paint()" method)

 
0
  #3
Apr 7th, 2009
If you're into Windows operational system ->
Study SUN example:
http://today.java.net/pub/a/today/20...solutions.html
For your use
  1. import com.sun.animation.SmoothAnimation;
declare
  1. SmoothAnimation smoothAnimation;
In constructor
  1. smoothAnimation = new SmoothAnimation();
and in your overrided method
  1. public synchronized void paintComponent(java.awt.Graphics g)
invoke NATIVE method (at end)
  1. smoothAnimation.vbLock();
Remember abut
  1. VBLocker.dll
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Implementing "double buffering" (without "paint()" method)

 
0
  #4
Apr 7th, 2009
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,433
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: Implementing "double buffering" (without "paint()" method)

 
0
  #5
Apr 7th, 2009
>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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC