import cs134lib.*;

import objectdraw.ActiveObject;
import objectdraw.VisibleImage;
import javax.swing.*;
import java.awt.*;

/*
 * An active object that implements a video camera.
 */
public class VideoCamera extends ActiveObject {

  private VideoCameraListener viewer;  // object to notify about new images

  private boolean filming;       // are we still using the camera?

  private static final int FREQ = 1000 / 24;  // frame rate

  /*
   * Provide the constructor with an object that will be told
   * whenever new images are captured.  The object must implement
   * the VideoCameraListener interface.
   */
  public VideoCamera(VideoCameraListener theViewer) {
    viewer = theViewer;
    filming = true;
    this.start();
  }

  public void run() {

    Camera c = new Camera();
    c.activate();

    while (filming) {

      Image image = c.getImage();
      viewer.setImage(image);
      pause(FREQ);

    }

    c.deactivate();

  }

  /*
   * Stop filming.
   */
  public void cut() {
    filming = false;

  }
}
