Double Buffering - more detail

G

Guest

Hi, I'm looking for some double-buffering help. I'm back with a different question than my previous one.

Within a derived Canvas class, PlotCanvas, I have declared:

Image offscreenImage;
Graphics offscreenGraphics;

What I originally had inside paint() was:

------------------------------

if(offscreenImage == null) {
offscreenImage = createImage(this.getSize().width, this.getSize().height);
offscreenGraphics = offscreenImage.getGraphics();
}

// Do lots of drawing to 'offscreenGraphics'

g.drawImage(offscreenImage,0,0,this);

------------------------------

This resulted in flickering. I did some debugging and realized that immediately upon entering paint(), the canvas was cleared. So
during all of that writing to 'offscreenGraphics', the Canvas wasn't showing the old image, and swapping at the last minute.

So, I took "lots of drawing to 'offscreenGraphics'" and put that into a function, pre_paint(), with a repaint() at the end of it, so
that all that was needed inside paint() was:

g.drawImage(offscreenImage, 0, 0, this);

I still had flickering. Any suggestions?

The code is online at:
http://www.angelfire.com/retro/there/lab73_exer2.zip

The interesting functions are in the PlotCanvas class: paint(), pre_paint(), maybe update(). The project was done in Eclipse.


Help is very much appreciated, TIA.
 
H

hiwa

Do the lots of drawing in the update() method, which should call
paint() at
its last line.
 
I

Ian Shef

Hi, I'm looking for some double-buffering help. I'm back with a
different question than my previous one.

Within a derived Canvas class, PlotCanvas, I have declared:
that all that was needed inside paint() was:

g.drawImage(offscreenImage, 0, 0, this);

I still had flickering. Any suggestions?
<snip>

Calling repaint(...) causes Canvas.update(Graphics g) to be called. This
method first clears the Canvas to the background color. Then and only then
is Paint(Graphics g) called. It is the clearing of the Canvas that causes
your flickering.

Your PlotCanvas class needs to override update(Graphics g) like this:

public void update(Graphics g){
paint(g);
}

Note: PlotCanvas.paint(Graphics g) had better paint its entire area.
Because the Canvas is no longer cleared to the background color, any area
that is not painted will show the previous image.

See

http://java.sun.com/products/jfc/tsc/articles/painting/index.html

and

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Canvas.html#update
(java.awt.Graphics)

if you want further details of the difference between paint(...) and update
(...).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top