posted 8 years ago
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
There are three kinds of actuaries: those who can count, and those who can't.