/*     
 *     File	: cam.java
 *     Author	: Koen Martens (gmc@metro.cx)
 *     Creation	: 16-01-2003
 *     Desc.	: This applet requests the given image with a query string
 *     		  of the form '?id=#num#', where #num# is a number, 
 *     		  continuously. After loading the image, it it displayed
 *     		  and after the set delay the next image will be loaded.
 *     		  In effect, this can be used to provide webcam access
 *     		  if your camera software  can write an image somewhere in 
 *     		  your web document root.
 *
 *     		  Include the following html code in your web page:
 *     		  
 *     		  <applet code="cam.class" width=320 height=240>
 *     		  <param name=showversion value="0">
 *     		  <param name=delay value="5000">
 *     		  <param name=image value="http://www.metro.cx:8082/pic.jpg">
 *     		  <param name=downimage value="http://www.metro.cx/gfx/dow.jpg">
 *     		  </applet>
 *
 *     		  The parameter 'delay' is the delay between images (set to 0
 *     		  to use maximum bandwidth). The parameter 'image' is the url
 *     		  of the image that is updated continuously by your camera
 *     		  software. The parameter 'downimage' is the url of a picture
 *     		  that is showed if the image is not retrievable (eg. video
 *     		  server down). If the parameter 'showversion' is 1, a line
 *     		  with the applet version will be shown above the image.
 *
 */

import java.awt.* ;
import java.net.URL;

import javax.swing.*;
import java.awt.event.*;
import java.net.*;

public class cam extends JApplet implements Runnable {

  int     	delay = 167;
  int		showVersion=1;
  int		termination=0;
  int	  	down = 0, imgidx=0;
  boolean 	suspended = false;
  Thread  	spinThread ;
  Image   	camImage1,camImage2,downImage;
  Image[] 	images={camImage1,camImage2};
  long    	sequence = System.currentTimeMillis();
  MediaTracker 	mt = new MediaTracker(this);
  String 	imageName,downName,param;
  String 	versionString=new String("0.5");


  private Image loadImage(String url) {
    Image im=null;
    try {
      im = getImage(new URL(url));
      mt.addImage(im,0);
      try {
	mt.waitForAll();
      } catch (InterruptedException e) {
      }
      mt.removeImage(im,0);
    } catch(Exception e) {
      showStatus(e.toString());
    }
    return im;
  }
  
  public void init() {

    showStatus("Webcam applet v"+versionString+" by Koen Martens (gmc@metro.cx)");
	  
    param = getParameter("showversion");
    if (param != null)
	    showVersion=Integer.parseInt(param);
    
    imageName = getParameter("image");
    if (imageName == null)
	    imageName="pic.jpg";
   
    downName = getParameter("downimage");
    if (downName == null)
	    downName="down.jpg";
    downImage = loadImage(downName);
    
    param = getParameter("delay");
    if (param != null)
       delay = Integer.parseInt(param);
  
    if (delay < 10)
       delay = 10;

    images[imgidx]=loadImage(imageName+"?id="+sequence);
    sequence++;
    if(images[imgidx]==null) down=1; else
    if((images[imgidx]).getWidth(this)==-1) down=1; else down=0;

    imgidx=1-imgidx;
    images[imgidx]=loadImage(imageName+"?id="+sequence);
    sequence++;
    if(images[imgidx]==null) down=1; else
    if((images[imgidx]).getWidth(this)==-1) down=1; else down=0;

   }


  public void start() {
    if (spinThread == null) {
      spinThread = new Thread (this, "Spin");
      spinThread.start();
    }
  }


  public void run() {
    while (spinThread != null && termination!=1) {
      repaint();
      try {
	imgidx=1-imgidx;
        images[imgidx].flush();
    
        images[imgidx]=loadImage(imageName+"?id="+sequence);
        sequence++;
        if(images[imgidx]==null) down=1; else
        if((images[imgidx]).getWidth(this)==-1) down=1; else down=0;

        spinThread.sleep(delay);
      } catch (InterruptedException e) {
      }
    }
  }


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


  public void paint(Graphics g) {
      Image showImage=(down==0)?(images[1-imgidx]):downImage;

      if(showVersion==1)  
	g.drawString("Webcam applet v"+versionString+" by Koen Martens (gmc@metro.cx)",10,15);
      g.drawImage ( showImage ,
        0,
        (showVersion==1)?20:0,
        this );
      if(showVersion==1)  
	g.drawString("Webcam applet v"+versionString+" by Koen Martens (gmc@metro.cx)",10,15);
  }


  public void stop() {
    termination=1;
  }

}

