This loop works fine in Main function but when copy this loop code in under button pressed action then it does not work. It shows image only at the last iteration otherwise it shows blank.
public void buttonPressed() {
BufferedImage im = null;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int j=100;
for(int i=0;i<j;i++) {
im = camera.captureImage();
tracker.setImage(im);
if (previous.x == -1) {
// previous = tracker.searchCorrelation();
previous = tracker.trackMarker();
} else {
// previous =tracker.searchCorrelation(previous);
previous = tracker.sayemSearch(previous);
}
if (previous.x != -1) {
System.out.println("prevX" + previous.x + "prevY" + previous.y);
im = trackedImage(im, previous);
}
System.out.println("prevX" + previous.x);
// System.out.println("prevY"+previous.y);
imgpanel.setImage(im);
System.out.println("set");
//break;//When i write break then it works
}
}
You are blocking the event thread, so no repainting is done. If you want to display every image you will have to manually call
repaint()on the panel or the frame after everysetImage.