Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Question on double buffering

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 77540
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3291
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 77540
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us where that example comes from. Maybe we shall wish to avoid that site
 
Saloon Keeper
Posts: 5289
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic