Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » scrolling an image in a canvas
scrolling an image in a canvas [message #504523] Mon, 21 December 2009 04:07 Go to next message
Eclipse UserFriend
Hi,
I'm trying to paint an image into a canvas with scrolling support, but the following code is not working:

   public void createPartControl(Composite parent) {
		
	// now create a canvas for this view
	this.canvas = new Canvas( parent, SWT.H_SCROLL | SWT.V_SCROLL );
	// this canvas must show the image when loaded
	this.canvas.addPaintListener( new PaintListener(){

	    @Override
	    public void paintControl(PaintEvent event) {
		// the system wants to show the canvas, so draw the image
		GC graphicContext = event.gc;
		if( image != null )
		    graphicContext.drawImage( image, 0, 0 );		
	    }
	    
	});


I've also tried to use a scrolled composite, but it does not even paint the image:

   public void createPartControl(Composite parent) {
		
        ScrolledComposite scolled = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
	// now create a canvas for this view
	this.canvas = new Canvas( scrolled, SWT.NONE );
	// this canvas must show the image when loaded
	this.canvas.addPaintListener( new PaintListener(){

	    @Override
	    public void paintControl(PaintEvent event) {
		// the system wants to show the canvas, so draw the image
		GC graphicContext = event.gc;
		if( image != null )
		    graphicContext.drawImage( image, 0, 0 );		
	    }
	    
	});


Anyone can help me on how to get the image supporting scrolling?

Thanks
Re: scrolling an image in a canvas [message #504566 is a reply to message #504523] Mon, 21 December 2009 09:32 Go to previous messageGo to next message
Eclipse UserFriend
see
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet48.java?view=co

"Luca Ferrari" <fluca1978@infinito.it> wrote in message
news:hgndru$h0v$1@build.eclipse.org...
> Hi,
> I'm trying to paint an image into a canvas with scrolling support, but the
following code is not working:
>
>
> public void createPartControl(Composite parent) {
>
> // now create a canvas for this view
> this.canvas = new Canvas( parent, SWT.H_SCROLL | SWT.V_SCROLL );
> // this canvas must show the image when loaded
> this.canvas.addPaintListener( new PaintListener(){
>
> @Override
> public void paintControl(PaintEvent event) {
> // the system wants to show the canvas, so draw the image
> GC graphicContext = event.gc;
> if( image != null )
> graphicContext.drawImage( image, 0, 0 );
> }
>
> });
>
>
> I've also tried to use a scrolled composite, but it does not even paint
the image:
>
>
> public void createPartControl(Composite parent) {
>
> ScrolledComposite scolled = new ScrolledComposite(parent,
SWT.H_SCROLL | SWT.V_SCROLL);
> // now create a canvas for this view
> this.canvas = new Canvas( scrolled, SWT.NONE );
> // this canvas must show the image when loaded
> this.canvas.addPaintListener( new PaintListener(){
>
> @Override
> public void paintControl(PaintEvent event) {
> // the system wants to show the canvas, so draw the image
> GC graphicContext = event.gc;
> if( image != null )
> graphicContext.drawImage( image, 0, 0 );
> }
>
> });
>
>
> Anyone can help me on how to get the image supporting scrolling?
>
> Thanks
Re: scrolling an image in a canvas [message #504710 is a reply to message #504566] Tue, 22 December 2009 00:01 Go to previous messageGo to next message
Eclipse UserFriend
Thanks,
I did that and of course ti works, but is a quite lot of work for a simple functionality like scrolling a composite content. Is there anything simpler to use in SWT? Something that can offer the JScrollPane of swing features?
Re: scrolling an image in a canvas [message #504713 is a reply to message #504710] Tue, 22 December 2009 05:17 Go to previous messageGo to next message
Eclipse UserFriend
Maybe
http://www.eclipse.org/articles/Article-Image-Viewer/Image_v iewer.html

Should be helpful?
Re: scrolling an image in a canvas [message #504793 is a reply to message #504713] Tue, 22 December 2009 12:22 Go to previous messageGo to next message
Eclipse UserFriend
Yes thanks, it is very helpful.
However it still empathizes how SWT is lacking (?) an equivalent component like JScrollPane. Is there a particular reason for that? Why ScolledComposite cannot just embed an image and provide the scrolling capabilities? I'm really curious to understand.

Thanks

P.S.
in the meantime I've succeeded implementing an image-view following your suggestions.
Re: scrolling an image in a canvas [message #504819 is a reply to message #504793] Tue, 22 December 2009 14:36 Go to previous messageGo to next message
Eclipse UserFriend
The ScrolledComposite provides the scrolling capabilities and can be used to scroll the image. In your example, you have to also set the bounds of the canvas for the image to be visible.
--> this.canvas.setBounds(image.getBounds());

Please see --
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet215.java?view=co

Re: scrolling an image in a canvas [message #504872 is a reply to message #504819] Tue, 22 December 2009 23:26 Go to previous messageGo to next message
Eclipse UserFriend
Uhm...this is not working for me:

  public void createPartControl(Composite parent) {
		
	// create a scrolling composite
	ScrolledComposite scroller = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL );
	
	// now create a canvas for this view
	this.canvas = new Canvas( scroller, SWT.NONE );
	scroller.setContent(scroller);
	
	// this canvas must show the image when loaded
	this.canvas.addPaintListener( new PaintListener(){

	    @Override
	    public void paintControl(PaintEvent event) {
		// the system wants to show the canvas, so draw the image
		GC graphicContext = event.gc;
		if( image != null ){
		    // draw the image at the current image position
		    graphicContext.drawImage( image, imageLeftCornerPosition.x, imageLeftCornerPosition.y );
		    canvas.setBounds( image.getBounds() );
		}
	    }
	    
	});


in fact I cannot see any scroll bar on the view. I cannot set the canvas bounds before the image is painted, could it be that the problem?
Re: scrolling an image in a canvas [message #504898 is a reply to message #504793] Wed, 23 December 2009 07:33 Go to previous messageGo to next message
Eclipse UserFriend
When the bounds of the canvas is zero, canvas doesn't have any visible area to be painted. So, it doesn't get the Paint event and the PaintListener is not called.

You can set the bounds of the canvas to a non-zero value outside the PaintListener so that it can receive the Paint events. You can reset it to the bounds of the image in the PaintListener.

ScrolledComposite scroller = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL );
final Canvas canvas = new Canvas( scroller, SWT.NONE );
scroller.setContent(scroller);
canvas.setBounds(display.getBounds());
canvas.addPaintListener( new PaintListener(){
public void paintControl(PaintEvent event) {
event.gc.drawImage(image, 0, 0);
canvas.setBounds(image.getBounds());
}
});




Re: scrolling an image in a canvas [message #505156 is a reply to message #504898] Mon, 28 December 2009 03:04 Go to previous messageGo to next message
Eclipse UserFriend
No way to obtain the scrolling:

  public void createPartControl(Composite parent) {
		
	// create a scrolling composite
	ScrolledComposite scroller = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL );
	
	// now create a canvas for this view
	this.canvas = new Canvas( scroller, SWT.NONE );
	scroller.setContent(scroller);
	this.canvas.setBounds( this.getSite().getShell().getDisplay().getBounds() );
	
	// this canvas must show the image when loaded
	this.canvas.addPaintListener( new PaintListener(){

	    @Override
	    public void paintControl(PaintEvent event) {
		// the system wants to show the canvas, so draw the image
		GC graphicContext = event.gc;
		if( image != null ){
		    // draw the image at the current image position
		    graphicContext.drawImage( image, imageLeftCornerPosition.x, imageLeftCornerPosition.y );
		    canvas.setBounds( image.getBounds() );
		}
	    }
	    
	});


I cannot see the scrolling bars even if I resize the view. I cannot understand why scrolling bars are not even shown. Any help?
Re: scrolling an image in a canvas [message #506874 is a reply to message #505156] Mon, 11 January 2010 04:00 Go to previous messageGo to next message
Eclipse UserFriend
I've made a simple change but it still does not work:

public void createPartControl(Composite parent) {
		
	// create a scrolling composite
	ScrolledComposite scroller = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL );
	
	// now create a canvas for this view
	this.canvas = new Canvas( scroller, SWT.NONE );
	this.canvas.setBounds( this.getSite().getShell().getDisplay().getBounds() );
[B]	scroller.setContent( this.canvas );[/B]
	
	
	// this canvas must show the image when loaded
	this.canvas.addPaintListener( new PaintListener(){

	    @Override
	    public void paintControl(PaintEvent event) {
		// the system wants to show the canvas, so draw the image
		GC graphicContext = event.gc;
		if( image != null ){
		    // draw the image at the current image position
		    graphicContext.drawImage( image, imageLeftCornerPosition.x, imageLeftCornerPosition.y );
		    canvas.setBounds( image.getBounds() );
		}
	    }
	    
	});


Now I've got a suspect: the paint listener draws the image at the
imageLeftCornerPosition.x, imageLeftCornerPosition.y
that are set both to zero. So this means that even if the scrollbar moves the image is always displayed at (0,0). Now my doubt is that the problem is with the pain listener, that forces the image to be shown in the canvas at the same position. Should I remove it? And if I remove how can I display the image when it is loaded?
Re: scrolling an image in a canvas [message #508854 is a reply to message #506874] Wed, 20 January 2010 09:57 Go to previous messageGo to next message
Eclipse UserFriend
I've found that if I remove the paint listener, and therefore I remove the painting at corner 0,0 the scroller seems to work:

 public void createPartControl(Composite parent) {
		
	// create a scrolling composite
	ScrolledComposite scroller = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL );
	
	// now create a canvas for this view
	this.canvas = new Canvas( scroller, SWT.NONE );
	this.canvas.setBounds( this.getSite().getShell().getDisplay().getBounds() );
	scroller.setContent( this.canvas );
	
	
	// this canvas must show the image when loaded
	this.canvas.addPaintListener( new PaintListener(){

	    boolean paint = true; 
	    @Override
	    public void paintControl(PaintEvent event) {
		// the system wants to show the canvas, so draw the image
		GC graphicContext = event.gc;
		if( image != null && paint ){
		    // draw the image at the current image position
		    graphicContext.drawImage( image, 0, 0 );
		    canvas.setBounds( image.getBounds() );
		    paint = false;
		}
	    }
	    
	});


The problem now is that the image scrolls, but cannot be repaint. For instance, if I scroll to right, the image moves, but when I scroll back to left, the image is not shown and so I got the background color. Any suggestion?
Re: scrolling an image in a canvas [message #508887 is a reply to message #508854] Wed, 20 January 2010 11:08 Go to previous message
Eclipse UserFriend
At last I got it, it seems that the canvas.setBounds inside the paint listener was disturbing the image repainting thru the scrolled composite. The final code working is the following:

 public void createPartControl(Composite parent) {
		
	// create a scrolling composite
	ScrolledComposite scroller = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL );
	
	// now create a canvas for this view
	this.canvas = new Canvas( scroller, SWT.NONE );
	scroller.setContent( this.canvas );
	
	
	// this canvas must show the image when loaded
	
	this.canvas.addPaintListener( new PaintListener(){

 
	    @Override
	    public void paintControl(PaintEvent event) {
		if( image != null )
		    event.gc.drawImage(image, 0, 0 );
	    }
	    
	});
Previous Topic:Customizing Traverse-keys for ColumnViewerEditor
Next Topic:Problem with ColumnViewerEditorActivationListener in a TableViewer
Goto Forum:
  


Current Time: Sun Jul 13 10:37:11 EDT 2025

Powered by FUDForum. Page generated in 0.10604 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top