SWT canvas performance problem

Hi !

I’m trying to draw on a SWT canvas and I am experiencing performance issue.

What I am trying to do exactly is painting a simple jpeg image (larger than the Canvas) and drag it with the right mouse button (i.e. make it scroll the way the mouse moves when right button is pressed).

When picture moves, it flickers. Image rendering is not heavy however since it’s just the copy of the image part which is supposed to be viewable.
I wonder if some buffer strategy could help here since it would just do what I’m already doing : simply copy an image part.

Any clue ?

Thanks for your help

PS : Here is some code

Mouse move processing : (“pressed” is a boolean tracking right mouse button state)


            public void mouseMove(MouseEvent e)
            {
                  if (pressed)
                  {
                        x-=e.x-origX;
                        if (x<0) x=0;
                        else if (x>backgroundPic.getBounds().width) x=backgroundPic.getBounds().width;                  
                        origX=e.x;
                        
                        y-=e.y-origY;
                        if (y<0) y=0;
                        else if (y>backgroundPic.getBounds().height) y=backgroundPic.getBounds().height;
                        origY=e.y;

                        ((Canvas)e.getSource()).redraw();
                  }
                  
            }

Paint listener :


            canvas.addPaintListener(new PaintListener(){
                  public void paintControl(PaintEvent e) {
                        GC gc=e.gc;
                        Canvas canvas=(Canvas)e.getSource();
                        int drawWidth=Math.min(canvas.getSize().x, backgroundPic.getBounds().width-x);
                        int drawHeight=Math.min(canvas.getSize().y, backgroundPic.getBounds().height-y);
                        gc.drawImage(backgroundPic, x, y, drawWidth, drawHeight, 0, 0, Math.min(canvas.getSize().x, drawWidth), Math.min(canvas.getSize().y, drawHeight));                        
                  }
            });


Since nobody bothered answering - I wonder if my question was dumb or to clever :slight_smile: -, I do it myself with information gathered on SWT website (mailing lists archive and newsgroup).

Setting the SWT.NO_BACKGROUND of the Canvas is the trick. Using Canvas’ scroll method is also better since it should be OS optimized and it automatically sets GC clipping.

Hope this will help someone.