Search code examples
eclipseimagecanvasrcp

Fit a bigger image into smaller Canvas


My question : I have created and filled a Canvas in Eclipse RCP application as follows :

Canvas myCanvas = new Canvas(comImageViewer,  SWT.H_SCROLL | SWT.V_SCROLL);
      myCanvas.setLocation(0, 0);
      myCanvas.setSize(583, 432);
      myCanvas.setLayout(new FormLayout());
      myCanvas.addPaintListener( new PaintListener() {  
          public void paintControl(PaintEvent e) {  
              GC gc = e.gc;  
              gc.drawImage( image,10,10); // Draw the moon  
              }  
              });  

and by definition, this canvas has V_Scroll and H_Scroll bars, but they do not scroll the image displayed at run time. Now, My Requirements are:

The Image is too big for small sized Canvas. So, How can I make the loaded image :

  1. either fit into the canvas.
  2. Or make the Canvas's Scroll bars(which I have added or any) such employed that they can scroll up-down the image displayed in canvas at runtime.

Solution

  • Take a look at this snippet which explains how to scroll an Image on SWT Canvas.