• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on double buffering

 
Shashank Gokhale
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I get the concept of double buffering, but there are a few questions.
1) Does the line Dimension d=size() get the dimensions of the entire screen or a JFrame?
2) From what I understand, the line offscreen = createImage(d.width, d.height) creates a blank image with the given dimensions. Am I correct?
3) Does offgc=offscreen.getGraphics() basically say that now we are going to access the graphics context of the image created by the previous line of code. So whenever anything is then drawn, this line causes the drawing to be made on the created image. Is this correct?
4) The lines offgc.fillRect(...), draws to that context, meaning on the created image. Correct?
5) What does paint(offgc) do, if the lines above it do the drawing?
5) drawImage then draws the secondary image back to the onscreen Image, correct?

So if Im understanding the whole process, what is happening is that new images replace the previous onscreen images each time that the update method is called, correct? So, nothing is being erased, the images on the screen are replaced with the images that have been create with createImage(), correct?
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The doucmentation about size should tell you that. Why are you using such an old‑fashioned class? Don't know about the other questions.

I shall move you to our GUIs forum.
 
Shashank Gokhale
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im just trying to follow along step by step from some examples of double buffering on the web, I am sure there is another way, but I need to know everything from the ground up. So I still need answers to whats happening in the code.
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing is double buffered by default. Don't use a Canvas. Use a JComponent of JPanel for custom painting. See Painting in AWT and Swing for more information.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us where that example comes from. Maybe we shall wish to avoid that site
 
Piet Souris
Rancher
Posts: 1635
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Shashank,

I guess the replies so far did not answer your questions directly. So here goes:

1) size() gives the dimension of the current object, so in this case the canvas.

2) yes

3) yes

4) yes

5) this is where I have to rely on my memory, always an uncertain thing to do.
Canvas is, as the others already indicated, part of the old AWT, and this
AWT is follwed up by Swing. If I recall correctly, if you issue a 'repaint()' command, then the method
'update(Graphics g)' is called. Normally, all that 'update()' does is clearing
your Canvas to the background colour and then call 'paint'.

So, what is happening here is that 'update' is overridden in such way, that it does all the updating itself,
including painting on the canvas, and therefor it does not call the actual paint() method.

It does this by:
'paint(offgc)'; it does indeed paint all the contents to your newly created image (instead of to the canvas),
and next it draws your just painted image 'offscreen' to the canvas.

Now, that is indeed the idea behind 'double buffering', and this was, again IIRC, the way to do it back then, under
the AWT. The other possibility was to do all this work in the 'paint' method, so that your 'update' method could simply
be calling 'paint' directly, since you would not need to clear to the background first.

I hope this makes the code clear.

As the other replies indicate, don't use the old AWT anymore, use the modern equivalents from Swing, i.e. JCanvas or JPanel.
And as Rob said, these are double buffered by default.

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!