Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 09:07 Go to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
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 14:32 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
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 05:01 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
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 10:17 Go to previous messageGo to next message
Dmitry K is currently offline Dmitry KFriend
Messages: 17
Registered: July 2009
Junior Member
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 17:22 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
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 19:36 Go to previous messageGo to next message
Lakshmi P ShanmugamFriend
Messages: 279
Registered: July 2009
Location: India
Senior Member
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



Lakshmi P Shanmugam
Re: scrolling an image in a canvas [message #504872 is a reply to message #504819] Wed, 23 December 2009 04:26 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
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 12:33 Go to previous messageGo to next message
Lakshmi P ShanmugamFriend
Messages: 279
Registered: July 2009
Location: India
Senior Member
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());
}
});






Lakshmi P Shanmugam
Re: scrolling an image in a canvas [message #505156 is a reply to message #504898] Mon, 28 December 2009 08:04 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
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 09:00 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
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 14:57 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
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 16:08 Go to previous message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
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: Thu Apr 25 04:22:53 GMT 2024

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

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

Back to the top