So the file image.png is getting overwritten every 2 seconds. I want an applet to display the image in a browser. It does display the image, but the problem is the image never gets updated in the applet after the image file was updated on the computer. What am I doing wrong?
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JApplet;
public class ImageUpdate extends JApplet {
Image picture;
Timer timer = new Timer();
int delay = 2000; //2 second
int period = 4000; //4 seconds
public void init() {
picture = getImage(getDocumentBase(),"image.png");
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
repaint();
System.out.println("image updated");
}
}, delay, period);
}
public void paint(Graphics g) {
g.drawImage(picture, 0,0, this);
}
public void update(Graphics g)
{
super.paint(g);
}
}
Gave up on the applet, and did it in html and JavaScript. It works so much better this way. Thanks Andrew. I had really wanted to get the applet to work, but ... whatever.
<html>
<head>
<script language="JavaScript"><!--
function refreshIt() {
if (!document.images) return;
var date = new Date();
document.images['image'].src = 'http://localhost:8080/image.jpg?ts=' + date.getTime();
setTimeout('refreshIt()',2000); // refresh every 2000ms
}
//--></script>
</head>
<body onLoad=" setTimeout('refreshIt()',2000)">
<img src="http://localhost:8080/image.jpg" name="image">
</body>
</html>