home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / compositedeffects / src / compositedeffects.java next >
Encoding:
Java Source  |  2000-09-28  |  6.9 KB  |  190 lines

  1. /*
  2.  * QuickTime for Java SDK Sample Code
  3.  
  4.    Usage subject to restrictions in SDK License Agreement
  5.  * Copyright: © 1996-1999 Apple Computer, Inc.
  6.  
  7.  */
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.awt.image.*;
  11. import java.io.*;
  12.  
  13. import quicktime.qd.*;
  14. import quicktime.*;
  15. import quicktime.std.StdQTConstants;
  16. import quicktime.std.image.*;
  17. import quicktime.std.movies.*;
  18. import quicktime.io.*;
  19. import quicktime.util.*;
  20.  
  21. import quicktime.app.QTFactory;
  22. import quicktime.app.display.*;
  23. import quicktime.app.image.*;
  24. import quicktime.app.players.*;
  25. import quicktime.std.image.GraphicsMode;
  26. import quicktime.app.time.*;
  27. import quicktime.app.anim.*;
  28. import quicktime.app.actions.*;
  29.  
  30. public class CompositedEffects extends Frame implements QDConstants, StdQTConstants {        
  31.     private static boolean isWin = QTSession.isCurrentOS (QTSession.kWin32);
  32.  
  33.     public static void main (String args[]) {
  34.         try { 
  35.             QTSession.open();
  36.             CompositedEffects pm = new CompositedEffects("QT in Java");
  37.             pm.pack();
  38.             pm.show();
  39.             pm.toFront();
  40.         } catch (Exception e) {
  41.             e.printStackTrace();
  42.             QTSession.close();
  43.         }
  44.     }
  45.     int kWidth = 350;
  46.     int kHeight = 250;
  47.         
  48.     CompositedEffects (String title) throws Exception {
  49.         super (title);
  50.         
  51.         setBackground (Color.lightGray);
  52.         
  53.         QTCanvas myQTCanvas = new QTCanvas(QTCanvas.kInitialSize, 0.5f, 0.5f);
  54.         add("Center", myQTCanvas);
  55.  
  56. // set the Background Color to white so that the Java text will appear transparent
  57. // white is a color that provides a reliable transparent background for different pixel depths.
  58.         myQTCanvas.setBackground (Color.white);
  59.         
  60.         Dimension d = new Dimension (kWidth, kHeight);
  61.         QDRect r = new QDRect(d);
  62.         QDGraphics gw = new QDGraphics (r);
  63.  
  64. // this is the compositor which will contain the bgPict, effect, text and a member compositor
  65.         Compositor comp = new Compositor (gw, QDColor.green, new QDGraphics (r), 10, 1);
  66.         
  67. // add the background picture to the Comp -> load it into memory so it draws quicker
  68.         QTFile backgroundFile = new QTFile (QTFactory.findAbsolutePath("pics/water.pct"));
  69.         ImagePresenter background = makeImagePresenter (backgroundFile, r);
  70.         comp.addMember (background, Layerable.kBackMostLayer);
  71.  
  72. // add the effect in front of the background pict
  73.         CompositableEffect e = new CompositableEffect ();
  74.         AtomContainer effectSample = new AtomContainer();
  75.         effectSample.insertChild (new Atom(kParentAtomIsContainer), kEffectWhatAtom, 1, 0, EndianOrder.flipNativeToBigEndian32(kWaterRippleCodecType));
  76.         e.setEffect (effectSample);
  77.         e.setDisplayBounds (new QDRect (0, kHeight - 100, kWidth, 100));
  78.         comp.addMember (e, 2);
  79.  
  80. // add the Text in front of the pict and ripples
  81. // set its transparency (to the bgColor of the QTCanvas) so that only the text is seen
  82.         Paintable jt = new JavaText ();
  83.         QTImageDrawer qid = new QTImageDrawer (jt, new Dimension (110, 22), Redrawable.kSingleFrame);
  84.         qid.setGraphicsMode (new GraphicsMode (transparent, QDColor.white));
  85.         qid.setLocation (200, 20);
  86.         comp.addMember (qid, 1);
  87.  
  88. // add the contained Compositor - yellow is bgColor which is then NOT drawn
  89. // add a Dragger so that member of this compositor can be dragged around
  90. // when any modifier key is pressed when the mousePressed event is generated
  91.         Compositor sh = new Compositor (new QDGraphics (new QDRect(160, 160)), QDColor.yellow, 8, 1); 
  92.         addSprites (sh);
  93.         sh.setLocation (190, 90);
  94.         sh.setGraphicsMode (new GraphicsMode (transparent, QDColor.yellow));
  95.         sh.getTimer().setRate(1);
  96.         comp.addMember (sh, 1);
  97.         sh.addController(new SWController (new Dragger (MouseResponder.kAnyModifiersMask, MouseResponder.kAnyModifiers), true));
  98.     
  99. // add a Dragger to the main Compositor to enable dragging of all its members around
  100.         comp.addController(new SWController (new Dragger (MouseResponder.kNoModifiersMask), true));
  101.         
  102. // make a DirectGroup as the top level container space 
  103.         DirectGroup dg = new DirectGroup (d, QDColor.white);
  104.  
  105. // add    the Compositor to the DirectGroup parent
  106.         dg.addMember (comp, 2);
  107.  
  108. // make a movie and add it in front of the Composited image
  109. // resizing the movie to make it a little smaller
  110.         QTDrawable mov = QTFactory.makeDrawable (new QTFile (QTFactory.findAbsolutePath ("jumps.mov")));
  111.         mov.setDisplayBounds (new QDRect(20, 20, 120, 106));
  112.         dg.addMember (mov, 1);
  113.  
  114. // Set the DirectGroup as the client of the QTCanas                
  115.         myQTCanvas.setClient (dg, true);
  116.         
  117. // set the rates of the compositor and parent DirectGroup so you see
  118. // it "playing" when it is first shown
  119.         comp.getTimer().setRate(1);
  120.         dg.getTimer().setRate(1);
  121.  
  122. // add the control panel to control the rates of the 
  123. //    DirectGroup
  124. //        -> Its Compositor member
  125. //            -> The Compositor's Compositor 
  126. // the movie can be controlled directly by the user
  127.         ControlPanel cp = new ControlPanel(comp.getTimer(), (QTPlayer)mov, dg, sh);
  128.         add (cp, "North");
  129.         cp.setDisplay();
  130.  
  131. // add a WindowListener to close the program down
  132.         addWindowListener (new WindowAdapter () {
  133.             public void windowClosing (WindowEvent e) {
  134.                 QTSession.close();
  135.                 dispose();
  136.             }
  137.             public void windowClosed (WindowEvent e) { 
  138.                 System.exit(0);
  139.             }
  140.         });
  141.     }
  142.  
  143.     private Movie makeMovie (QTFile f) throws IOException, QTException {
  144.         OpenMovieFile movieFile = OpenMovieFile.asRead(f);
  145.         Movie m = Movie.fromFile (movieFile);
  146.         m.getTimeBase().setFlags (loopTimeBase);    
  147.         return m;
  148.     }
  149.     
  150.     private ImagePresenter makeImagePresenter (QTFile file, QDRect size) throws Exception {
  151.         GraphicsImporterDrawer if1 = new GraphicsImporterDrawer (file);
  152.         if1.setDisplayBounds (size);
  153.         return ImagePresenter.fromGraphicsImporterDrawer (if1);
  154.     }    
  155.  
  156. // makes the Sprites for the child Compositor     
  157.     void addSprites (Compositor sd) throws IOException, QTException {
  158.         File matchFile = QTFactory.findAbsolutePath ("images/Ship01.pct");    //this file must exist in the directory!!!    
  159.         ImageDataSequence isp = ImageUtil.createSequence (matchFile);
  160.         ImageDataSequence seq = ImageUtil.makeTransparent (isp, QDColor.blue);
  161.  
  162. // Build Sprites        
  163.         Matrix matrix1 = new Matrix();
  164.         matrix1.setTx(20);
  165.         matrix1.setTy(20);
  166.         matrix1.setSx(0.8F);
  167.         matrix1.setSy(0.8F);
  168.         TwoDSprite s1 = new TwoDSprite(seq, 4, matrix1, true, 1);
  169.         sd.addMember (s1);
  170.         
  171.         Matrix matrix2 = new Matrix();    
  172.         matrix2.setTx(4);
  173.         matrix2.setTy(4);
  174.         TwoDSprite s2 = new TwoDSprite(seq, 1, matrix2, true, 10);
  175.         sd.addMember (s2);
  176.  
  177. // Build ActionList
  178.         SimpleActionList al = new SimpleActionList();
  179.         ImageSequencer is = new ImageSequencer (seq);
  180.         is.setLooping (ImageSequencer.kLoopForwards);
  181.         ImageSequencer is2 = new ImageSequencer (seq);
  182.         is2.setLooping (ImageSequencer.kLoopForwards);
  183.         al.addMember (new NextImageAction (20, 1, is2, s2));
  184.         al.addMember (new BounceAction (20, 1, sd, s2, 4, 3));
  185.         al.addMember (new NextImageAction (7, 1, is, s1));
  186.         al.addMember (new BounceAction (5, 1, sd, s1, 3, 2));
  187.         sd.addController(al);
  188.     }
  189. }
  190.