
/*
 * @(#) Cine.java 1.0 96/9/18
 * Copyright (c) 1996 Lawrence Rodrigues
 */

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Hashtable;
import java.lang.Math;

/** Cine Class.
  * @version 1.0  18 sep 1997
  * @author Lawrence Rodrigues
  **/

public class Cine extends Panel implements Runnable{
   static int startValue = 60,minCycles = 20,maxCycles = 360;
   protected  int frameDelay;
   private Scrollbar frameSlider;
   private TextField frameValueField;
   private Button startButton,stopButton, resetButton;
   protected boolean startStop = true;
   private Choice cinePort;
   protected int curCinePortNum =1;
   protected DisplayController vpMgr;
   protected int width, height;
   private Thread cineThread;

   public  Cine(int wd, int hgt){
      width = wd; height = hgt;
      createUI();
      setSize(width, height);
      initCine();
      cineThread = new Thread(this);
   }

   private  void createUI() {
      GridBagLayout    gb = new GridBagLayout() ;
      GridBagConstraints c = new GridBagConstraints();
      setLayout(gb);
      setBackground(Color.lightGray);

      c.gridheight = 1; c.weighty = 1.0; c.weightx = 1.0;
      c.gridwidth = GridBagConstraints.REMAINDER;
      c.insets = new Insets(5,5, 5,5);
      Panel selPanel = new Panel();
      Label cinePanelLabel = new Label("Cine", Label.LEFT);
      Label cineLabel = new Label("Port", Label.LEFT);
      cinePort = new Choice();
      cinePort.setBackground(Color.gray);
      cinePort.addItem("UL");
      cinePort.addItem("UR");
      cinePort.addItem("BL");
      cinePort.addItem("BR");
      cinePort.addItemListener(
      new ItemListener() {
          public void itemStateChanged(ItemEvent e){
             int portNum = ((Choice)(e.getSource())).getSelectedIndex();
             if(portNum != vpMgr.getCinePortNum()){
                curCinePortNum = portNum;
                vpMgr.setCinePort(curCinePortNum);
             }
             else cinePort.select(curCinePortNum);
          }
       }
      );
      selPanel.add(cinePanelLabel);
      selPanel.add(cineLabel);
      cinePort.select(1);

      selPanel.add(cinePort);
      gb.setConstraints(selPanel, c);
      add(selPanel);
      c.gridwidth = 1; c.weightx = 0.0;
      Label frameLabel= new Label("Frames/min", Label.LEFT);
      c.fill = GridBagConstraints.HORIZONTAL;
      gb.setConstraints(frameLabel, c);
      add(frameLabel);

      frameSlider = new Scrollbar(Scrollbar.HORIZONTAL,
                               startValue,1,minCycles,maxCycles);
      frameSlider.addAdjustmentListener(
         new AdjustmentListener(){
           public void adjustmentValueChanged(AdjustmentEvent e){
              int sliderVal = frameSlider.getValue();
	            frameDelay = (int)(100000/(1+(int)(sliderVal*10/6)));
              frameValueField.setText(Integer.toString(sliderVal));
           }
        }
      );
      c.weightx = 1.0;
      gb.setConstraints(frameSlider, c);
      add(frameSlider);

      frameValueField  = new  TextField(Integer.toString(startValue),3);
      frameValueField.setBackground(Color.gray);
      c.gridwidth = GridBagConstraints.REMAINDER;
      c.weightx = 0.0;
      gb.setConstraints(frameValueField, c);
      add(frameValueField);
      c.fill = GridBagConstraints.NONE;
      Panel reportBtns= new Panel();
      reportBtns.setLayout(new GridLayout(1,3));
      startButton = new Button("Start");
      startButton.addActionListener(
         new ActionListener(){
            public void actionPerformed(ActionEvent e){
               if (startStop == false) return;
               startButton.setEnabled(false);
               stopButton.setEnabled(true);
               if(cineThread == null){
                  cineThread = new Thread(Cine.this);
               }
               if(cineThread.isAlive() ==true){
                  cineThread = new Thread(Cine.this);
               }
               //running = true;
               cineThread.start();
               startStop = false;
           }
         }
      );
      stopButton = new Button("Stop");
      stopButton.addActionListener(
        new ActionListener(){
          public void actionPerformed(ActionEvent e){
             if (startStop == true) return;
             startButton.setEnabled(true);
             stopButton.setEnabled(false);
             cineThread.stop();
             cineThread = new Thread(Cine.this);
             startStop = true;
          }
        }
      );

      resetButton = new Button("Reset");
      resetButton.addActionListener(
        new ActionListener(){
          public void actionPerformed(ActionEvent e){
             reset();
             cineThread = new Thread(Cine.this);
         }
        }
      );
      reportBtns.add(startButton);
      reportBtns.add(stopButton);
      reportBtns.add(resetButton);
      gb.setConstraints(reportBtns, c);
      add(reportBtns);
   }

   private void initCine(){
      frameDelay = (int)(100000/(1+(int)(startValue*10/6)));
      frameSlider.setValue(startValue);
      frameValueField.setText(Integer.toString(startValue));
      stopButton.setEnabled(false);
      startButton.setEnabled(true);
      startStop =true;
   }

   public void paint(Graphics g) {
      Rectangle r = getBounds();
      g.setColor(Color.cyan);
      g.draw3DRect(1,1, r.width-2, r.height-2, true);
   }

   public void run(){
      while(true){
         try{
           vpMgr.cineDispImage();
           if(frameDelay <10) frameDelay =10;
           cineThread.sleep(frameDelay);
         } catch (InterruptedException e){}
      }
   }
   public void registerForDisplayCallback(DisplayController callbackObj){
      vpMgr = callbackObj;
   }

   public void stopCine(){
      if (startStop == true) return;
      stopButton.setEnabled(false);
      startButton.setEnabled(true);
      cineThread.stop();
      cineThread = new Thread(this);
      startStop = true;
   }
   public void reset(){
      if(cineThread != null)
      if(cineThread.isAlive() ==true)cineThread.stop();
      initCine();
      vpMgr.cineReset();
   }
}









