scrolling an image in a canvas [message #504523] |
Mon, 21 December 2009 09:07 |
Luca Ferrari 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 |
Grant Gayed 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 #508854 is a reply to message #506874] |
Wed, 20 January 2010 14:57 |
Luca Ferrari 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?
|
|
|
|
Powered by
FUDForum. Page generated in 0.04328 seconds