flickering canvas

Hello,

i want to draw in a canvas which listens to mouse events. so far so good, but if i get a drag-event the canvas begins with flickering. i have read about a bufferstrategy but i failed to use it with the canvas. the error message was something about “need a valid peer”. so i am using a buffered image but the flickering is still there.
Here is the (pseudo-)code of the paint function of this canvas:


  public void paint(Graphics g){
    Graphics2D g2 = (Graphics2D)g;

    if(firstTime){
      // create BufferedImage
      // create Graphics from BufferedImage
      firstTime = false;
    } 

    //clear Image

    // run through vector
      // draw element of vector to Graphics of BufferedImage

    // Drawing the buffered image to the screen
    g2.drawImage(aBufferedImage, 0, 0, this);
  }

i would be happy if anyone has a hint for me.

thanks

have you called .setRepaint(false) on your canvas?

Hoi!

go with the bufferstrategy class. The reason you are getting invalid peer, is probably because you call createBufferStrategy before the canvas is added to its container, and the container is visible.

Ergo:

canvas.createBufferStrategy(2);
frame.add(canvas);
frame.setVisible(true);

is no good… But,

frame.add(canvas);
frame.setVisible(true);
canvas.createBufferStrategy(2);

should work fine :wink:

do you mean .setIgnoreRepaint(true) ?
yes i have set this, but i forgot to say that the mouse-listener calls on every drag-event .repaint() on this canvas.
do i need a special thread for calling .repaint()?

thanks Addictman,
that might be the problem.
i called createBufferStrategy(2) in the Constructor of the
Canvas-Subclass. :-[
so i will try it again with the bufferstrategy.

thanks again. :smiley:

Okay,
the BufferStrategy works now but the Canvas is still flickering. Can it be that the drag-event is fired to often? is it common to use a seperate thread for painting the graphics?

Yeah just don’t call repaint() that much. Use the program thread to manage how fast the screen is repainted, and let the Event firing thread run on its own.

that seems not to be the problem. ???
the thread works but the flickering is still there.

here the function of the threadclass:


  public void run(){
    while(true){
      aCanvas.repaint();
      try { Thread.sleep(WaitMillis); } catch (Exception e) {}
    }
  }

  
  public void setWaitMillis(int millis){
    WaitMillis=millis;
  }

and the mouselistener:


  public void mousePressed(MouseEvent e) {
    // Coordinate calculations
    canvas.setWaitMillis(50); // delegates to the thread
  }

  public void mouseReleased(MouseEvent e) {
    canvas.setWaitMillis(500); // delegates to the thread
  }

canvas:


  public void paint(Graphics g){
    gBS.clearRect(0, 0, width, height);

    //run through vector
       // draw element of vector

    // Draws the image to the screen.
    dblStrategy.show();
  }

repaint() queues up a draw in the EventQueue I believe. paint() is immediate. The problem is that when you call repaint(), it doesn’t happen immediately. You should be calling paint(g) instead of repaint(). let g be the Graphics for your screen graphics, which if you’re using the code suggested to you for BufferStrategy would look like this:

public void run(){ 
            strategy = canvas.getBufferStrategy();
            while(true){ 
                        aCanvas.paint(strategy.getDrawGraphics()); 
                        try { Thread.sleep(WaitMillis); } catch (Exception e) {} 
            } 
}

wow…i have tried it and it is absolute perfect. :o
thanks for that great help.


while (true) System.out.println( "WOW!!!!" );

glad it worked!

Haha your sentiments are exactly that of my profile message under my name on the left :wink: Only 1 character difference! ;D