1
public void myMethod()
{
if (capture.isOpened()) {
    while (true) { //This is The main issue.
        capture.read(webcam_image);
        if (!webcam_image.empty()) {
            webcam_image = my_panel.detect(webcam_image);
            temp = my_panel.matToBufferedImage(webcam_image);
            my_panel.setimage(temp);
            my_panel.repaint();
            System.out.print("."); // It should prints "." but the above code doesn't works.
        } else {
            System.out.println(" --(!) No captured frame -- Break!");
            break;
        }
    }
}
}

This is invoking code of the above method... actually it's an ActionEvent which can be fire on menu is clicked.

if (e.getActionCommand().equals("goLive")) {
        System.out.println("Live...");
        myMethod();
}

I know actually it's problem of the infinite while loop but here I need to put this condition at any cost.

Gopal00005
  • 2,061
  • 4
  • 35
  • 54
  • 2
    Or a `SwingWorker`, see [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for more details – MadProgrammer Aug 26 '14 at 13:05

2 Answers2

2

The exact solution for this type of problem is Timer class. We can overcome this issue using the following code.

Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            myMethod();
        }
}, 0);

Thanks google, oracle and java doc

Gopal00005
  • 2,061
  • 4
  • 35
  • 54
1

Assuming that myMethod is called by an event listener (actionPerformed), the infinite loop is blocking the event dispatch thread. You can avoid this by using SwingWorker or executing your loop on another thread:

public void myMethod()
{
if (capture.isOpened()) {
    new Thread(new Runnable() { //Create a new thread and pass a Runnable with your while loop to it
        @Override public void run() {
            while (true) {
                capture.read(webcam_image);
                if (!webcam_image.empty()) {
                    webcam_image = my_panel.detect(webcam_image);
                    temp = my_panel.matToBufferedImage(webcam_image);
                    SwingUtilities.invokeLater(new Runnable() { //The following lines affect the GUI and must be executed on the event dispatch thread, so they should be wrapped inside a Runnable
                        @Override public void run() {
                            my_panel.setimage(temp);
                            my_panel.repaint();
                        }
                    }
                    try{
                        Thread.sleep(xxx); //consider waiting for a moment (e.g. 16ms)
                    } catch(InterruptedException e) { ... }
                    System.out.print(".");
                } else {
                    System.out.println(" --(!) No captured frame -- Break!");
                    break;
                }
            }
        }
    }).start(); //Let the thread loop in the background
}
}
J4v4
  • 780
  • 4
  • 9
  • Thanks a lot @J4v4 but here I am facing the problem of the final variables all the above variables are coming from the out of the method though this is not possible to me to set them all as final..Buddy what to do ? – Gopal00005 Aug 27 '14 at 09:00
  • 1
    @Gops this is just a very simple example. I might be wrong, but your code does not appear to be thread safe. I would like to see the entire code to provide a better solution. For example, you could avoid the final variables if you made a new class that implements Runnable where you could pass your variables in the constructor. – J4v4 Aug 27 '14 at 09:45