import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; public class ImageRefresh extends Applet implements Runnable { protected int refreshMillis; protected URL imageURL = null; protected Image theImage = null; private Object lock = new Object(); private boolean stopped = false; /* * The following is a sample html that contains this applet: * * * * * * * * * * * * * A description of what the parameters do: * refreshMillis -- the number of milliseconds to wait between updating the image * imageURL -- the relative URL of the image to be displayed * width -- should be the exact width of the image * height -- should be the exact height of the image */ public void init() { try { refreshMillis = Integer.parseInt(getParameter("refreshMillis")); imageURL = new URL(getDocumentBase(), getParameter("imageURL")); } catch (Exception e) { e.printStackTrace(System.out); } Thread go = new Thread(this); go.start(); } public void run() { while (!stopped) { Image tempImage = getReadyImage(); synchronized (lock) { theImage = tempImage; } paint(getGraphics()); try { Thread.sleep(refreshMillis); } catch (Exception e) {} } } private Image getReadyImage() { Image returnThis = null; try { URLConnection conn = imageURL.openConnection(); conn.setUseCaches(false); conn.connect(); InputStream in = (InputStream) conn.getContent(); byte[] data = new byte[conn.getContentLength()]; for (int i = 0; i < data.length; i++) data[i] = (byte) in.read(); returnThis = Toolkit.getDefaultToolkit().createImage(data); MediaTracker mt = new MediaTracker(this); mt.addImage(returnThis, 0); mt.waitForID(0); } catch (Exception e) { e.printStackTrace(System.out); } return returnThis; } public void destroy() { stopped = true; } public void paint(Graphics g) { synchronized (lock) { if (theImage == null) return; try { g.drawImage(theImage, 0, 0, null); } catch (Exception e) { e.printStackTrace(System.out); } } } }