图像未在重绘时更新

时间:2014-09-19 23:17:50

标签: java image japplet

所以file image.png每2秒就会被覆盖一次。我想要一个applet在浏览器中显示图像。它确实显示图像,但问题是在计算机上更新图像文件后,图像永远不会在applet中更新。我做错了什么?

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);

  } 
}  

1 个答案:

答案 0 :(得分:0)

放弃applet,并用html和JavaScript完成。它以这种方式工作得更好。谢谢Andrew。我真的很想让applet工作,但是......无论如何。

<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>