import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.applet.Applet;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.URL;

public class tbViewer extends Applet implements ActionListener, WindowListener {

    public static void main(String[] args) {
        Frame F=new Frame("WebCam Monitor");
        tbViewer C=new tbViewer();
        F.setSize(500, 480);
        F.addWindowListener(C);
        F.add(C);
        C.init();
        C.start();
        F.setVisible(true);
    }

    // parameters (these values are discarded later)
    String  imgLoc          = null;
    String  imgLocDefault   = "http://webbot.bradley.edu/cgi-bin/labcam.cgi";
    int     timSlMs         = 1000;
    String  titleStr        = null;
    int     titleSize       = 24;
    Color   titleCol        = new Color(0,0,128);

    // GUI
    tbImage WC;
    Button BUT;

    public void init() {

        try {
            imgLoc=getParameter("image-location");
            if (imgLoc==null) throw new Exception("missing applet parameter");
        } catch (Exception e) {
            imgLoc=imgLocDefault;
        }

        try {
            timSlMs=Integer.parseInt(getParameter("image-refresh"), 10);
        } catch (Exception e) {
            timSlMs=1000;
        }
        
        try {
            titleStr=getParameter("title-text");
            if (titleStr==null) throw new Exception("missing applet parameter");
        } catch (Exception e) {
            titleStr="WebCam Monitor";
        }

        try {
            titleSize=Integer.parseInt(getParameter("title-size"), 10);
        } catch (Exception e) {
            titleSize=24;
        }

        try {
            titleCol=new Color(Integer.parseInt(getParameter("text-color"),16));
        } catch (Exception e) {
            titleCol=new Color(0,0,128);
        }

        BUT=new Button("pause");
        BUT.addActionListener(this);

        // Image canvas
        WC=new tbImage(imgLoc, timSlMs);

        setLayout(new BorderLayout(20,20));

        if (titleSize>0) {
            Label title=new Label(titleStr, Label.CENTER);
            title.setFont(new Font("Helvetica", Font.BOLD, titleSize));
            title.setForeground(titleCol);
            add("North",  title);
        }
        add("East",  new Label(""));
        add("Center", WC);
        add("South", BUT);
        add("West",  new Label(""));
        setVisible(true);
    }

    public void start() {
        WC.start();

        // Transmitted image canvas
    }

    public void stop() {
        WC.stop();
    }


// INTERFACE ActionListener -------
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==BUT) {
            if (BUT.getLabel().equals("pause")) {
                WC.pause();
                BUT.setLabel("resume");
            } else {
                WC.resume();
                BUT.setLabel("pause");
            }
                        
        }
        else ;
    }

// INTERFACE WindowListener -------
    public void windowClosing(WindowEvent e)      { stop(); WC.stop(); System.exit(0); }
    public void windowClosed(WindowEvent e)       {  }
    public void windowOpened(WindowEvent e)       {  }
    public void windowIconified(WindowEvent e)    { WC.pause();  }
    public void windowDeiconified(WindowEvent e)  { WC.resume(); }
    public void windowActivated(WindowEvent e)    {  }
    public void windowDeactivated(WindowEvent e)  {  }
}









class tbImage extends Canvas implements Runnable {

    // parameters
    private String imgLoc;
    private int    tmSlMs;

    // variables
    private MediaTracker MT     = null;
    private Image    webcam     = null;
    private Thread   process    = null;
    private boolean  pause      = false;

    tbImage(String loc, int wait) {
        super();
        imgLoc=loc;
        tmSlMs=wait;
        MT=new MediaTracker(this);
        start();
    }

    public void start() {
        if (process!=null) return;
        process=new Thread(this,"Image Update");
        process.start();
    }

    public void pause() {
        pause=true;
    }

    public void resume() {
        pause=false;
    }

    public void stop() {
        if (process==null) return;
        Thread tokill=process;
        process=null;
        tokill.interrupt();
        try {
            tokill.join(100);
        } catch (Exception e) {
        }
    }

    public void run() {
        while(Thread.currentThread()==process) {
            if (pause) {
                try {
                    Thread.currentThread().sleep(100);
                } catch (Exception e) { }
            } else {
                try {
                    // remove the previous image from cache
                    if (webcam!=null) webcam.flush();
                    // get a new image by location
                    webcam = getToolkit().getImage(new URL(imgLoc));
                    // wait for downloading the image
                    // -- extra time added to thread.sleep()
                    MT.addImage(webcam,0);
                    MT.waitForAll();
                    MT.removeImage(webcam,0);
                } catch(Exception e) {
                    webcam=null;
                }
                repaint();
                try {
                    Thread.currentThread().sleep(tmSlMs);
                } catch (Exception e) { }
            }
        }
        process=null; // redundant but just in case, to make sure
    }

    public void paint(Graphics g) {
        final Rectangle r=getBounds();
        g.drawRect(1,1,r.width-2,r.height-2);
        if (webcam!=null) {
            int wi=webcam.getWidth(this);
            int hi=webcam.getHeight(this);
            int w1=(r.width -wi)/2;
            int h1=(r.height-hi)/2;

            int w2=w1+wi;
            int h2=h1+hi;
            g.clearRect(2, 2, r.width-3,h1-3);
            g.clearRect(2, h1,w1-3,hi);
            g.clearRect(w2,h1,w1-3,hi);
            g.clearRect(2,h2+1,r.width-3,h1-3);

            g.drawImage(webcam, w1, h1, this);
        }
    }

    public void update(Graphics g) {
        paint(g);
    }
}


syntax highlighted by Code2HTML, v. 0.8.11