"Reload Image 10 sec after successful loading" in Java with AWT
Loading the image one time works as done in the following steps (for code see end of post):
- load the URI
- with the Toolkit load the image from the URI
- use a MediaTracker to check the loading(waitForAll())
- change the viewports size according to the image
- (repaint() tried here, didn't change anything)
The resizing will be done in "no time" and after that it takes a second or two that the image actually is shown (maybe because of my slow computer). If I put a while(true) around that without letting him wait for some time, he will load like crazy and probably never take the time to show anything (on all computers not just mine). So I want him to wait. There is not much happening on the webcam, so every 10 seconds might be a nice interval.
The two parts of my question are:
How do I let the loop wait in a smart way? Inside it's own thread? Just putting a
Thread.wait(10000);
at the bottom of the loop will also stop the picture from being displayed.How can I check if the picture is displayed and not just loaded like the MediaTracker does?
I know that I actually don't ask how to solve a problem but how to implement a solution, which might be the wrong one in the first place. For that case I add the context, too: I want to display Images from a webcam that I can get through an URL as a JPG which will be rewritten on the server in an unknown interval. This connection to the Webcam might be smart or stupid, but I can't change it anyway. Not my server. I just want to write a client to show the pictures.
The code for reference and Great Justice (inside an actionPerformed()):
...
// now use an URI even if Toolkit.getImage() uses URL, as
// recommended here:
// http://download.oracle.com/javase/6/docs/api/java/net/URL.html
URI adress = null;
try {
adress = new URI("http://194.95.48.67/record/current.jpg");
} catch (URISyntaxException e1) {
throw new Error(e1);
}
displaystate = STATE_LOADING;
repaint();
System.out.println("load image from: " + adress);
try {
image = getToolkit().getImage(adress.toURL());
} catch (MalformedUR开发者_JAVA技巧LException e1) {
throw new Error(e1);
}
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try {
mt.waitForAll();
} catch (InterruptedException e) {
// nothing
}
filename = adress.toString();
imagesize.width = image.getWidth(null);
imagesize.height = image.getHeight(null);
if (imagesize.width == -1 || imagesize.height == -1) {
displaystate = STATE_LOADERROR;
} else {
viewport.x = 0;
viewport.y = 0;
viewport.width = imagesize.width;
viewport.height = imagesize.height;
setSize(viewport.width, viewport.height);
displaystate = STATE_DISPLAYING;
}
If you call Thread.sleep
the current thread can't do anything meanwhile. So if you do this from the main thread (as I assume, as you question if a second thread should be used), the GUI cannot update and thus not repaint the image. This is why it hangs if you put the sleep at the end of the loop.
Second thing is that repaint
doesn't repaint immediately. It simply posts an event in the AWT event loop that will be processed eventually, and only then the component is repainted.
The simplest way to do it is like this:
Make a second thread that loads the image, and then stores in in a member variable. After that it calls repaint
, and then sleeps.
In the paint method you take the image and draw it on screen. This will happen in the main GUI thread, so the other thread's sleep won't harm.
I try to answer to the first question:
try to use a timer and when timer expired then you can refresh the display
精彩评论