
//Title:       Generic Workspace
//Version:     
//Copyright:   Copyright (c) 1998
//Author:      Chris Stauffer
//Company:     MIT AI Lab
//Description: description
package edu.mit.ai.vsam;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Vector;
import java.net.URL;

public class TimeLapseApplet extends Applet implements Runnable{
  TrackFS trackFS;
  Vector images=new Vector();
  Thread displayThread;
  boolean isStandalone = false;
  Panel mainPanel = new Panel();
  BorderLayout borderLayout1 = new BorderLayout();


  //Construct the applet

  public TimeLapseApplet() {
  }
//Initialize the applet

  public void init() {
    try {
      jbInit();
      trackFS=new TrackFS("d:\\");
      trackFS.loadFilelist();
      for (int i=0; i<trackFS.trackFiles.size(); i++) {
        TrackFile tf=(TrackFile)trackFS.trackFiles.elementAt(i);
        String baseName=tf.getAbsolutePath().substring(0,tf.getAbsolutePath().length()-4);
        if (baseName.endsWith("A")) {
          //String imageName="file://"+baseName+"i.jpg";
          //URL url=new URL(imageName);
          //Image tempImage=Toolkit.getDefaultToolkit().getImage(url);
          String imageName=baseName+"t.jpg";
          Image tempImage=this.getToolkit().getImage(imageName);
          images.addElement(tempImage);
          System.out.println("i="+i+"... "+imageName+" image:"+tempImage);
        }
      }
      displayThread=new Thread(this);
      displayThread.start();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.setSize(400,300);
    this.setLayout(borderLayout1);
    this.add(mainPanel, BorderLayout.CENTER);
  }

  public void run() {
    int fps=10;
    int frameCount=0;
    int delay = (fps > 0) ? (1000 / fps) : 100;
    long startTime = System.currentTimeMillis();
    boolean done=false;
    System.out.println("running display thread.  images "+images.size());
    if (Thread.currentThread()==displayThread) {
      while (!done) {
        startTime += delay;
        try {displayThread.sleep(Math.max(0,startTime-System.currentTimeMillis()));
        } catch (Exception e) {};
        int frameNumber=(frameCount++%images.size());
        Image tempImage=(Image)images.elementAt(frameNumber);
        System.out.println("new frame"+frameNumber+" image:"+tempImage);
        mainPanel.getGraphics().drawImage(tempImage,0,0,mainPanel.getBounds().width,mainPanel.getBounds().height,this);
        //mainPanel.getGraphics().drawRect(0,0,100,100);
      }
    }

  }

//Start the applet
  
  public void start() {
  }
//Stop the applet
  
  public void stop() {
  }
//Destroy the applet
  
  public void destroy() {
  }
//Get Applet information
  
  public String getAppletInfo() {
    return "Applet Information";
  }
//Get parameter info
  
  public String[][] getParameterInfo() {
    return null;
  }
//Main method
  
  public static void main(String[] args) {
    TimeLapseApplet applet = new TimeLapseApplet();
    applet.isStandalone = true;
    Frame frame = new Frame();
    frame.setTitle("Applet Frame");
    frame.add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  }
}

 