0
public class Signal2NoiseRatio
{
    public ImagePlus SingleSNR(ImagePlus imagePlus) throws InterruptedException
    {

        new Thread()
        { 
          @Override public void run() 
          { 
              JFrame imageFrame = new JFrame("ROI");
              Container imageFrame_Container = imageFrame.getContentPane();
              IIImagePanel imagePanel = new IIImagePanel();
              imageFrame_Container.add(imagePanel);
              imagePanel.setImage(imagePlus.getImage());
              imagePanel.getDisplayedImage();
              imageFrame.setVisible(true);
              final SNRSingleImageListener sNRSingleListener = new SNRSingleImageListener(imagePanel);  
              imagePanel.addMouseListener(sNRSingleListener);
              imagePanel.addMouseMotionListener(sNRSingleListener);
          }
        }.start();


        new Thread() 
    { 
      @Override public void run() 
      { 

          for (int i = 0; i <= 2000; i++)
          { 
             System.out.println("schleife "+i);
                     // ask if useractions are done ..
          }

          synchronized( Signal2NoiseRatio.this ) 
          { 

             Signal2NoiseRatio.this.notifyAll(); 

          }
      }
    }.start();


        synchronized (this) 
        {
        this.wait();
            // if userinteractions are done, go on
        }


        return imagePlusToProcess;
    }
}

The first new Thread() perform a frame which presents an image in it. My intention was to present the image in a new thread to wait for some user interactions on the image. But the code leads the frame to a white window and the image is not visible and the frame is not usable.

In the second thread I want ask in a short interval if the user actions are done.

It is not really a nice solution but it is be possible? what's wrong here?

Thank you stackoverflow!

6
  • You IIImagePanel class works without the threads? Commented Aug 14, 2012 at 12:46
  • 1
    One issue is that you cannot create or access Swing components from any thread other than the event dispatch thread. Secondly, your notifyAll() call will never execute, as it is inside a synchronized block. I believe for what you're looking for, neither of those synchronized blocks are necessary. Commented Aug 14, 2012 at 12:48
  • IIImagePanel works without the threads. Commented Aug 14, 2012 at 13:36
  • @Rob I: That's not correct. The corresponding call to wait will release the lock so notify/notifyAll can be called. It is by design. Commented Aug 14, 2012 at 14:28
  • @Tudor - thanks, of course you're right, I always get myself in trouble with the low-level sync methods. Commented Aug 14, 2012 at 14:44

2 Answers 2

1

I can see a few problems here:

1.If this

synchronized (this) 
{
    this.wait();
    // if userinteractions are done, go on
}

is happening on the UI thread, then you will block it from receiving user input (or doing anything else) until the object is signaled.

2.This part seems pretty much overcomplicated:

    new Thread() {
        @Override
        public void run() {

            for (int i = 0; i <= 2000; i++) {
                System.out.println("schleife " + i);
                // ask if useractions are done ..
            }

            synchronized (Signal2NoiseRatio.this) {

                Signal2NoiseRatio.this.notifyAll();

            }
        }
    }.start();

    synchronized (this) {
        this.wait();
        // if userinteractions are done, go on
    }

Just use:

    Thread t = new Thread() {
        @Override
        public void run() {

            for (int i = 0; i <= 2000; i++) {
                System.out.println("schleife " + i);
                // ask if useractions are done ..
            }

        }
    }.start();

    t.join();

Unless you're signaling more than what you had above. But again, this is redundant, since starting a thread only to wait for it to finish does not make much sense...

Sign up to request clarification or add additional context in comments.

4 Comments

What easier way can I implement to use a new Frame in the method SingleSNR?? I cannot make it run. My aim is to wait for the user interactions before the method returns.
@haxenmaxen: So you want something like a modal frame? So it appears but it you cannot access the parent frame until the child is closed?
hmm I think you get it partial. the method SingleSNR gets a image to process and should return the new processed image. therefore the user should select a rectangle on the image. the selected area will be calculated and processed. for the rectangle selection I need an new Frame with the image. The method should wait for the users selected rectangles and calculate something after. after all it should return the new image.
@haxenmaxen: AFAIK you don't really need a new thread for this. Just create the new frame object on the parent frame's thread and make it visible. It should work like this.
0

Problem solved.

The invoker of the method was an AWT-Thread. I created a new Thread for the invoker and so the AWT-Thread was not blocked and the then frame and image could present correctly.

Thank you all for your help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.