home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-23 | 6.2 KB | 228 lines |
- /*
- * QuickTime for Java SDK Sample Code
-
- Usage subject to restrictions in SDK License Agreement
- * Copyright: © 1996-1999 Apple Computer, Inc.
-
- */
- package MySGController;
-
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
-
- import quicktime.io.*;
- import quicktime.util.*;
- import quicktime.*;
- import quicktime.app.*;
- import quicktime.std.*;
- import quicktime.std.sg.*;
-
- import quicktime.std.movies.*;
- import quicktime.std.movies.media.*;
- import quicktime.std.StdQTConstants;
- import quicktime.app.display.QTCanvas;
- import quicktime.app.image.*;
- import quicktime.qd.*;
- import quicktime.app.sg.SGDrawer;
-
- import MyPlayMovie.PlayMovie;
-
-
- public class SGControlPanel extends Panel implements Runnable, Errors, StdQTConstants {
-
- //_________________________ INSTANCE VARIABLES
- public Button powerOn = new Button("Power");
- public Button recordButton = new Button("Record");
- public Button stopButton = new Button("Stop");
-
- public QDGraphics theGraphics;
- public SequenceGrabber mGrabber = null;
- public QTCanvas theQTCanvas;
- public SGVideoChannel mVideo;
- public SGDrawer mDrawer;
- public QTFile mFile;
- public boolean isPowerOn = false;
-
- static final int kWidth = 320;
- static final int kHeight = 240;
-
- //_________________________ CLASS METHODS
-
- public void showMovie() {
- PlayMovie pm = new PlayMovie("Captured Movie",mFile);
- pm.show();
- pm.toFront();
- }
-
-
- public void createNewMovie() {
- try {
- Container c = getParent();
- for (;;) {
- if (c instanceof Frame) break;
- c = c.getParent();
- }
-
-
- FileDialog fd = new FileDialog ((Frame) c, "Save Movie As...", FileDialog.SAVE);
- fd.show();
-
- if(fd.getFile() == null)
- throw new QTIOException (userCanceledErr, "");
-
- mFile = new QTFile(fd.getDirectory() + fd.getFile());
-
- }
- catch (QTException qte) {
- System.out.println("Error in makeMovie: ");
- qte.printStackTrace();
- }
- }
-
-
- // See the Comments below under the recordButton action listner to see why this run method is here.
- // Basically this is here as a workaround for using the SequenceGrabber on Windows Systems.
-
- public void run () {
- try {
- Thread.sleep(10);
- mDrawer.startTasking();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- public void powerUp() throws QTException {
-
- QDRect bounds = new QDRect(kWidth, kHeight);
-
- mGrabber = new SequenceGrabber();
-
- mVideo = new SGVideoChannel(mGrabber);
- //Bring up the sequence Grabber settings Dialog and make sure
- //you are receiving a clear image in the preview dialog.
- mVideo.settingsDialog ();
-
- mVideo.setBounds (new QDRect(kWidth, kHeight));
- mVideo.setUsage (seqGrabPreview | seqGrabRecord | seqGrabPlayDuringRecord);
-
- mGrabber.prepare(true,true);
- mDrawer = new SGDrawer(mVideo);
- theQTCanvas.setClient(mDrawer,true); //Uncomment to have the sequence grabber draw in the window
- mGrabber.startPreview();
-
- isPowerOn = true;
- }
-
-
- public void doAction(int what) {
- try {
- mGrabber.pause(what);
- }
-
- catch (QTException e) {
- e.printStackTrace();
- }
-
- }
-
- public SGControlPanel(QTCanvas myQTCanvas) throws QTException {
-
- theQTCanvas = myQTCanvas;
-
- setLayout(new FlowLayout());
- setBackground(getBackground());
- setLayout(new FlowLayout());
-
-
- add (powerOn);
- add (recordButton);
- add (stopButton);
-
- QTRuntimeException.registerHandler (new Handler());
-
-
- powerOn.addActionListener (new ActionListener () {
- public void actionPerformed (ActionEvent event) {
-
- try {
- powerUp();
- } catch (QTException err) {
- err.printStackTrace();
- }
- System.out.println("Powered UP");
- }
- });
-
- recordButton.addActionListener (new ActionListener () {
- public void actionPerformed (ActionEvent event) {
- try{
-
- if (isPowerOn == false) {
- System.out.println("You Must first Click The Power button");
- return;
- }
-
- mDrawer.stopTasking();
- mGrabber.stop();
- createNewMovie();
-
- if (mFile != null) {
- mGrabber.setDataOutput(mFile,seqGrabToDisk);
- mGrabber.startRecord();
-
- //This code of spawning a new thread is here as a workaround for MS Windows 98 Systems.
- //It appears as though a race condition could occur where the setDataOutput does not fully complete
- //execution before the task object for the sequence grabber would try to idle it.
- //As a result of the race condition a -9402 exception (cantDoThatInCurrentMode) would be thrown.
- //-9402 in some instances means that we are trying to set a parameter during a record operation
- //This is a harmless error, but because a runtime exception gets thrown, the task object
- //for the SGDrawer (which is responsible for calling SGIdle() internally ) would terminate the objects
- //tasker. Starting a new thread which simply starts the SGDrawer object idling seems to fix this problem.
- //
- new Thread (SGControlPanel.this).start();
- }
- } catch (QTException ee){
- ee.printStackTrace();
- }
- }
- });
-
- stopButton.addActionListener (new ActionListener () {
- public void actionPerformed (ActionEvent event) {
- try {
-
- if (isPowerOn == false) {
- System.out.println("You Must first Click the Power button");
- return;
- }
- mDrawer.stopTasking();
- mGrabber.stop();
- System.out.println("*** BEFORE START PREVIEW ***");
- mGrabber.startPreview();
- mDrawer.startTasking();
-
- showMovie();
-
- } catch (QTException ee) {
- ee.printStackTrace();
- }
- }
- });
-
- }
-
- //_________________________ Runtime Error Handling
- static class Handler implements QTRuntimeHandler {
- public void exceptionOccurred (QTRuntimeException e, Object eGenerator, String methodNameIfKnown, boolean unrecoverableFlag) {
- System.out.println (eGenerator + "," + methodNameIfKnown + ",unrecoverable=" + unrecoverableFlag);
- e.printStackTrace();
- throw e; // we don't handle this exception - just print stack trace and throw it
- }
- }
-
-
- }
-