ImagePlus not drawing updates with in a loop

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

ImagePlus not drawing updates with in a loop

Richard Zorger
Hello,

I am making a plugin that uses imagePlus.insert() to add images to the canvas. These inserts are done within a loop. I want the imagePlus to be re-drawn for each iteration of the loop so that users can see each of the images as they are added. This works for the first time I run the loop but if I run it a second time it will not update the canvas within the loop. I have tried lots of different methods to let the imagePlus, imageProcesser and canvas know that there has been a change but nothing works. Here is some code:

        CustomCanvas cc = new CustomCanvas(imp3);
        CustomWindow cw = new CustomWindow(imp3, cc, 1);        
        ...
        ImagePlus impLoop;
        for (int i=numOldImages;i<numImages;i++){ // Insert each image in it's correct location in the gallery
                impLoop = IJ.openImage(galleryImages.get(i));
                ipLoop = impLoop.getProcessor();
                float thumbHeight = idealHeight/thumbFactor;
                float thumbWidth = idealWidth/thumbFactor;
                if (currentCol > numColumns){ // Make new row if there are more than numColumns open images
                        row = row + (int) thumbHeight;
                        column = 0;                
                        currentCol = 1;
                }                                
                currentCol++;
                galleryOrder.add(galleryImages.get(i));
                ipLoop = ipLoop.resize((int) thumbWidth);
                ip3.insert(ipLoop, column, row);

                imp3.setProcessor(ip3);  // This updates the gallery as each image is loaded for the 1st directory
               
                if (i == imageTally+numOldImages-1) {
                        row = row + (int) thumbHeight;
                        for (int k=0;k<maxNumCols - currentCol + 1;k++) { // Add spacers for empty slots in gallery
                                galleryOrder.add(" ");
                        }
                }

                System.gc();
                column = column + (int) thumbWidth;
               
                IJ.showStatus("Loading image: " + (i+1) +"/" + numImages + ", " + galleryImages.get(i));
                IJ.showProgress(i,numImages);        
        }

        IJ.showStatus("Finished loading images!");
        imp3.show();
        imp3.draw();
        cc.setDrawingSize(screen.width-panelWidth, row+gutter);        // redraws image
        cw.minimize(); // makes canvas match window size (with room for side panel)
        cw.setSize(ccSize);
        cc.setMagnification(1);    

The two calls imp3.setProcessor(ip3) and cc.setDrawingSize(screen.width-panelWidth, row+gutter) both cause the image to be updated, but neither can make it update in the loop.

Also a similar thing happens with the progress bar. It shows up once, then in subsequent running of the loop it is not visible. Is there a way I can setVisble on the progress bar?

thanks!

Richard

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: ImagePlus not drawing updates with in a loop

Michael Schmid
Hi Richard,

ImagePlus.setProcessor(ImageProcessor ip) updates the ImagePlus only if you have a new ImageProcessor (i.e., if it is a different object, irrespective of whether its contents have changed).
In your case, you use always the same ImageProcessor, so the operation is unnecessary except for the first time, and ImageJ recognizes that there is no new ImageProcessor.

To update the displayed ImagePlus, use the ImagePlus.updateAndDraw() method.

Michael
________________________________________________________________
On Apr 23, 2014, at 21:01, Richard Zorger wrote:

> Hello,
>
> I am making a plugin that uses imagePlus.insert() to add images to the canvas. These inserts are done within a loop. I want the imagePlus to be re-drawn for each iteration of the loop so that users can see each of the images as they are added. This works for the first time I run the loop but if I run it a second time it will not update the canvas within the loop. I have tried lots of different methods to let the imagePlus, imageProcesser and canvas know that there has been a change but nothing works. Here is some code:
>
>        CustomCanvas cc = new CustomCanvas(imp3);
>        CustomWindow cw = new CustomWindow(imp3, cc, 1);        
>        ...
>        ImagePlus impLoop;
>        for (int i=numOldImages;i<numImages;i++){ // Insert each image in it's correct location in the gallery
>                impLoop = IJ.openImage(galleryImages.get(i));
>                ipLoop = impLoop.getProcessor();
>                float thumbHeight = idealHeight/thumbFactor;
>                float thumbWidth = idealWidth/thumbFactor;
>                if (currentCol > numColumns){ // Make new row if there are more than numColumns open images
>                        row = row + (int) thumbHeight;
>                        column = 0;                
>                        currentCol = 1;
>                }                                
>                currentCol++;
>                galleryOrder.add(galleryImages.get(i));
>                ipLoop = ipLoop.resize((int) thumbWidth);
>                ip3.insert(ipLoop, column, row);
>
>                imp3.setProcessor(ip3);  // This updates the gallery as each image is loaded for the 1st directory
>
>                if (i == imageTally+numOldImages-1) {
>                        row = row + (int) thumbHeight;
>                        for (int k=0;k<maxNumCols - currentCol + 1;k++) { // Add spacers for empty slots in gallery
>                                galleryOrder.add(" ");
>                        }
>                }
>
>                System.gc();
>                column = column + (int) thumbWidth;
>
>                IJ.showStatus("Loading image: " + (i+1) +"/" + numImages + ", " + galleryImages.get(i));
>                IJ.showProgress(i,numImages);        
>        }
>
>        IJ.showStatus("Finished loading images!");
>        imp3.show();
>        imp3.draw();
>        cc.setDrawingSize(screen.width-panelWidth, row+gutter);        // redraws image
>        cw.minimize(); // makes canvas match window size (with room for side panel)
>        cw.setSize(ccSize);
>        cc.setMagnification(1);    
>
> The two calls imp3.setProcessor(ip3) and cc.setDrawingSize(screen.width-panelWidth, row+gutter) both cause the image to be updated, but neither can make it update in the loop.
>
> Also a similar thing happens with the progress bar. It shows up once, then in subsequent running of the loop it is not visible. Is there a way I can setVisble on the progress bar?
>
> thanks!
>
> Richard
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: ImagePlus not drawing updates with in a loop

Richard Zorger
In reply to this post by Richard Zorger
Hi Michael,

Thank you for your response! Unfortunately I have tried the ImagePlus.updateAndDraw() method and it did not work. I have also tried:
- Setting the ImageProcessor to a different ImageProcessor and then setting it back with imp3.setProcessor()
- ImageWindow winID = imp3.getWindow(); then winID.updateImage(imp3); and winID.show();
- imp3.changes = true;
- WindowManager.repaintImageWindows()

Is there anything else you can think of that I can try? Is there some other field like ImagePlus.changes that ImageJ looks at when deciding whether to update the image?

thanks
Richard


>Hi Richard,
>
>ImagePlus.setProcessor(ImageProcessor ip) updates the ImagePlus only if you have a new ImageProcessor (i.e., if it is a >different object, irrespective of whether its contents have changed).
>In your case, you use always the same ImageProcessor, so the operation is unnecessary except for the first time, and >ImageJ recognizes that there is no new ImageProcessor.
>
>To update the displayed ImagePlus, use the ImagePlus.updateAndDraw() method.
>
>Michael

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: ImagePlus not drawing updates with in a loop

dscho
Hi Richard,

On Fri, 25 Apr 2014, Richard Zorger wrote:

> Unfortunately I have tried the ImagePlus.updateAndDraw() method and it
> did not work.

Maybe your Event Dispatch Thread is stuck? If that is the case, *nothing*
gets drawn, so you should be able to verify this theory by starting the
"Monitor Memory..." command first; it will not update either during the
time the Event Dispatch Thread is blocked.

Ciao,
Johannes

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: ImagePlus not drawing updates with in a loop

Richard Zorger
The memory monitor updates properly so it does not seem the Event Dispatch Thread is stuck. Any thing else you can think of I can check?

Thanks
Richard

----- Original Message -----
From: "Johannes Schindelin" <[hidden email]>
To: "Richard Zorger" <[hidden email]>
Cc: [hidden email]
Sent: Friday, April 25, 2014 5:04:07 PM
Subject: Re: ImagePlus not drawing updates with in a loop

Hi Richard,

On Fri, 25 Apr 2014, Richard Zorger wrote:

> Unfortunately I have tried the ImagePlus.updateAndDraw() method and it
> did not work.

Maybe your Event Dispatch Thread is stuck? If that is the case, *nothing*
gets drawn, so you should be able to verify this theory by starting the
"Monitor Memory..." command first; it will not update either during the
time the Event Dispatch Thread is blocked.

Ciao,
Johannes

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: ImagePlus not drawing updates with in a loop

Richard Zorger
In reply to this post by dscho
Okay I figured it out.

For any one who might be interested in the future, use an inherited java.awt.Component method to get g:

Graphics g = cc.getGraphics();
cc.update(g);

Ciao,
Richard

----- Original Message -----
From: "Johannes Schindelin" <[hidden email]>
To: "Richard Zorger" <[hidden email]>
Cc: [hidden email]
Sent: Friday, April 25, 2014 5:04:07 PM
Subject: Re: ImagePlus not drawing updates with in a loop

Hi Richard,

On Fri, 25 Apr 2014, Richard Zorger wrote:

> Unfortunately I have tried the ImagePlus.updateAndDraw() method and it
> did not work.

Maybe your Event Dispatch Thread is stuck? If that is the case, *nothing*
gets drawn, so you should be able to verify this theory by starting the
"Monitor Memory..." command first; it will not update either during the
time the Event Dispatch Thread is blocked.

Ciao,
Johannes

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html