Login  Register

Java repaint question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Java repaint question

Jon Harman-3
59 posts
Hi,

In my plugin I want to cycle through a set of enhancements.  My code to
do this is inside of the actionPerformed routine for the button press in
my customWindow.
I have tried lots of  things: repaint, repaintWindow, updateImage...
(for the imagePlus or the window or the custom canvas) but the image
refuses to update until the code exits the actionPerformed routine.  Is
there a way I can get it to repaint the image without leaving the button
press routine?

Jon
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Java repaint question

ctrueden
1670 posts
Hi Jon,

Did you try validate() followed by repaint()?

-Curtis

On Sat, Jun 21, 2008 at 3:50 PM, Jon Harman <[hidden email]>
wrote:

> Hi,
>
> In my plugin I want to cycle through a set of enhancements.  My code to do
> this is inside of the actionPerformed routine for the button press in my
> customWindow.
> I have tried lots of  things: repaint, repaintWindow, updateImage... (for
> the imagePlus or the window or the custom canvas) but the image refuses to
> update until the code exits the actionPerformed routine.  Is there a way I
> can get it to repaint the image without leaving the button press routine?
>
> Jon
>
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Java repaint question

Jon Harman-3
59 posts
Hi,

Just gave it a try, for both the customCanvas and customWindow: doesn't
work.

Jon

Curtis Rueden wrote:

> Hi Jon,
>
> Did you try validate() followed by repaint()?
>
> -Curtis
>
> On Sat, Jun 21, 2008 at 3:50 PM, Jon Harman <[hidden email]>
> wrote:
>
>> Hi,
>>
>> In my plugin I want to cycle through a set of enhancements.  My code to do
>> this is inside of the actionPerformed routine for the button press in my
>> customWindow.
>> I have tried lots of  things: repaint, repaintWindow, updateImage... (for
>> the imagePlus or the window or the custom canvas) but the image refuses to
>> update until the code exits the actionPerformed routine.  Is there a way I
>> can get it to repaint the image without leaving the button press routine?
>>
>> Jon
>>
>
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Java repaint question - can ImageJ be updated to swing components and images?

Doug S-3
8 posts
In reply to this post by Jon Harman-3
Jon

I had a similar problem, and I'm not positive its the same problem but
what I was doing was customizing an ImageJ ImageWindow with an ImagePlus
displayed and put swing components into it. The problem was that ( for
whatever reason :'( ) ImageJ Windows are based on the historically older
( and by now outdated ) AWT package in JAVA that takes care of GUI
programming. The way I was doing it at least you can't mix swing and
AWT. I think you can include AWT in swing but not swing with a base
component ( the ImageWindow ). What I did was just start from scratch
and make my custom window done completely in swing and it wasn't too
bad. If you need the functionality of the ImagePlus or ImageProcessor
classes you can store your images in an ImagePlus class and then just
export the image itself for display in your all swing Window. The
ImageProcessor classes have a createImage() method that puts your
ImagePlus back into a JAVA image.  Since ImageJ is based on AWT ,  these
methods will return AWT images. This may be fine for your application
and AWT images can be displayed easily in a custom swing based window (
usually through a AWT canvas thats added ultimately to a JFrame class ).
If you need functionality for a BufferedImage instead ( it allows you to
access pixels as a raster, an array of pixel values ) then you have to
beat around the bush a bit more and to a trick to convert an Image to a
BufferedImage. This is explained below from
http://en.allexperts.com/q/Java-1046/Image-BufferedImage-Conversion.htm

the easiest way to make a usable
BufferedImage is to create a blank buffered
image and then draw the old Image into it
using a Graphics.  I know it sounds lame, but
that is the only really portable way.  You can
see an example and explanation here:
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Supplements/Chapter11/...
<http://www.particle.kth.se/%7Elindsey/JavaCourse/Book/Part1/Supplements/Chapter11/bufferedImage.html>

What I did instead of converting to BufferedImage, is if you are reading
image files off of your computer, just load them as BufferedImages in
the first place,
then its trivial to cast in the other direction using the typical JAVA
casting:

BufferedImage bImage;
// then load it from file
Image AWTImage = (Image) bImage;

then you use the AWT Image to create you ImagePlus object and access
ImageJ's functionality
If your image is changing a lot its pretty indirect.
What really should have been done is to write ImageJ using swing to
begin with
*I don't know when ImageJ was first concieved
but it wouldn't be terribly hard to rewrite the Image classes and
ImageWindow classes in the more modern and vastly superior JAVA swing
package.*

Hope that helps
Doug Snyder




Jon Harman wrote:

> Hi,
>
> In my plugin I want to cycle through a set of enhancements.  My code
> to do this is inside of the actionPerformed routine for the button
> press in my customWindow.
> I have tried lots of  things: repaint, repaintWindow, updateImage...
> (for the imagePlus or the window or the custom canvas) but the image
> refuses to update until the code exits the actionPerformed routine.  
> Is there a way I can get it to repaint the image without leaving the
> button press routine?
>
> Jon
>
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Java repaint question

Albert Cardona
298 posts
In reply to this post by Jon Harman-3
>   Is there a way I can get it to repaint the image without leaving the
button press routine?


I suspect what you need is to call the repaint properly, not on button
press.
The problem is: repainting on the EventDispatchThread is not a good idea.
All events are executed within such thread. You can see it by calling:
System.out.println(Thread.currentThread().getName());

So you have to queue the repaint event:

java.awt.EventQueue.invokeLater(new Runnable() {
   public void run() {
       imp.updateAndDraw();
   }
});


The above will dispatch the repaint event and put it at the end of the
queue. Then your button is repainted, the action finishes, and finally
the screen is repainted properly.


If you want to execute the repaint before the actionPerformed routine
finishes, you can't. But you can design it differently: you can spawn a
new thread immediately when the actionPerformed is called (so that the
actionPerformed method returns immediately), and then do whatever calls
from the context of that thread. Declare the ActionEvent final so you
can access it from within the inner Thread. Be careful to set the Thread
priority lower than the EventDispatchThread (for example, by calling
setPriority(Thread.NORM_PRIORITY); ).

Hope that helps.

Albert

--
Albert Cardona
http://www.mcdb.ucla.edu/Research/Hartenstein/acardona
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Java repaint question

simon andrews (BI)
26 posts
In reply to this post by Jon Harman-3
 

> -----Original Message-----
> From: ImageJ Interest Group [mailto:[hidden email]] On
> Subject: Java repaint question
>
> Hi,
>
> In my plugin I want to cycle through a set of enhancements.  
> My code to do this is inside of the actionPerformed routine
> for the button press in my customWindow.
> I have tried lots of  things: repaint, repaintWindow, updateImage...
> (for the imagePlus or the window or the custom canvas) but
> the image refuses to update until the code exits the
> actionPerformed routine.  Is there a way I can get it to
> repaint the image without leaving the button press routine?

Since your ActionPerformed method is still active when you want the
update to happen you'd probably have to structure your plugin so that
your button push is in a separate thread and then let that thread sleep
after issuing the update so that the original ImageJ thread gets a
chance to process the request.

For getting the image to update I use a validate() followed by a
repaint(), which seemed to be the miniumum necessary to force an update
to a custom canvas (though I suspect it depends on exactly what you're
changing).

Simon.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Java repaint question - can ImageJ be updated to swing components and images?

Jon Harman-3
59 posts
In reply to this post by Doug S-3
Hi,

Wayne and others have suggested that a solution to my repaint problem is
to create a new thread using code similar to the image processing demo.
  I have done that and it works.

I use two panels in my plugin.  An old one done in AWT and a new one
using swing. I can switch between the two without problems.

I agree that having to know both AWT and swing intricacies is a pain,
but I have not had any problems with my swing panel. It exists in a
CustomWindow in a CustomCanvas and uses an ImagePlus. I originally
started with the Panel_Window demo then changed the panel to swing.  So
as far as my experience goes ImageJ is compatible with swing.

Jon

Doug S wrote:

> Jon
>
> I had a similar problem, and I'm not positive its the same problem but
> what I was doing was customizing an ImageJ ImageWindow with an ImagePlus
> displayed and put swing components into it. The problem was that ( for
> whatever reason :'( ) ImageJ Windows are based on the historically older
> ( and by now outdated ) AWT package in JAVA that takes care of GUI
> programming. The way I was doing it at least you can't mix swing and
> AWT. I think you can include AWT in swing but not swing with a base
> component ( the ImageWindow ). What I did was just start from scratch
> and make my custom window done completely in swing and it wasn't too
> bad. If you need the functionality of the ImagePlus or ImageProcessor
> classes you can store your images in an ImagePlus class and then just
> export the image itself for display in your all swing Window. The
> ImageProcessor classes have a createImage() method that puts your
> ImagePlus back into a JAVA image.  Since ImageJ is based on AWT ,  these
> methods will return AWT images. This may be fine for your application
> and AWT images can be displayed easily in a custom swing based window (
> usually through a AWT canvas thats added ultimately to a JFrame class ).
> If you need functionality for a BufferedImage instead ( it allows you to
> access pixels as a raster, an array of pixel values ) then you have to
> beat around the bush a bit more and to a trick to convert an Image to a
> BufferedImage. This is explained below from
> http://en.allexperts.com/q/Java-1046/Image-BufferedImage-Conversion.htm
>
> the easiest way to make a usable
> BufferedImage is to create a blank buffered
> image and then draw the old Image into it
> using a Graphics.  I know it sounds lame, but
> that is the only really portable way.  You can
> see an example and explanation here:
> http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Supplements/Chapter11/...
> <http://www.particle.kth.se/%7Elindsey/JavaCourse/Book/Part1/Supplements/Chapter11/bufferedImage.html>
>
>
> What I did instead of converting to BufferedImage, is if you are reading
> image files off of your computer, just load them as BufferedImages in
> the first place,
> then its trivial to cast in the other direction using the typical JAVA
> casting:
>
> BufferedImage bImage;
> // then load it from file
> Image AWTImage = (Image) bImage;
>
> then you use the AWT Image to create you ImagePlus object and access
> ImageJ's functionality
> If your image is changing a lot its pretty indirect.
> What really should have been done is to write ImageJ using swing to
> begin with
> *I don't know when ImageJ was first concieved
> but it wouldn't be terribly hard to rewrite the Image classes and
> ImageWindow classes in the more modern and vastly superior JAVA swing
> package.*
>
> Hope that helps
> Doug Snyder
>
>
>
>
> Jon Harman wrote:
>> Hi,
>>
>> In my plugin I want to cycle through a set of enhancements.  My code
>> to do this is inside of the actionPerformed routine for the button
>> press in my customWindow.
>> I have tried lots of  things: repaint, repaintWindow, updateImage...
>> (for the imagePlus or the window or the custom canvas) but the image
>> refuses to update until the code exits the actionPerformed routine.  
>> Is there a way I can get it to repaint the image without leaving the
>> button press routine?
>>
>> Jon
>>
>
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Java repaint question - can ImageJ be updated to swing components and images?

simon andrews (BI)
26 posts
On 23 Jun 2008, at 15:43, Jon Harman wrote:

> Hi,
>
> Wayne and others have suggested that a solution to my repaint  
> problem is to create a new thread using code similar to the image  
> processing demo.  I have done that and it works.
>
> I use two panels in my plugin.  An old one done in AWT and a new  
> one using swing. I can switch between the two without problems.
>
> I agree that having to know both AWT and swing intricacies is a  
> pain, but I have not had any problems with my swing panel. It  
> exists in a CustomWindow in a CustomCanvas and uses an ImagePlus. I  
> originally started with the Panel_Window demo then changed the  
> panel to swing.  So as far as my experience goes ImageJ is  
> compatible with swing.

There are all kinds of gotchas with mixing AWT and swing.  Try for  
instance embedding an AWT canvas into a JScrollPane and watch what  
happens!

In the application I developed (http://www.bioinformatics.bbsrc.ac.uk/ 
projects/focalpoint/) I had all kinds of problems because I was  
embedding AWT based ImageCanvas objects into a Swing based layout.  I  
ended up fixing this by making my own version of ImageCanvas which  
extended JPanel rather than Canvas.  You actually need very few  
changes to make this work and everything ran much more smoothly  
afterwards.

Simon.