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 / all java / quicktime for java / bouncingsprites / src / spritedemoapp.java
Encoding:
Java Source  |  2000-09-28  |  4.6 KB  |  168 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.io.IOException;
  11. import java.io.File;
  12.  
  13. import quicktime.*;
  14. import quicktime.qd.*;
  15. import quicktime.io.*;
  16. import quicktime.app.image.*;
  17. import quicktime.app.display.*;
  18. import quicktime.app.QTFactory;
  19. import quicktime.util.*;
  20.  
  21. import quicktime.app.anim.*;
  22. import quicktime.app.actions.*;
  23. import quicktime.std.image.*;
  24.  
  25. public final class SpriteDemoApp extends Frame {
  26.     public static void main (String[] args) {
  27.         try {
  28.             QTSession.open();
  29.             SpriteDemoApp win = new SpriteDemoApp ("QT in Java");
  30.             win.show();
  31.             win.toFront();
  32.         } catch (Exception e) {
  33.             QTSession.close();
  34.             e.printStackTrace();
  35.         }
  36.     }
  37.     
  38.     MovingQTCanvas c1;
  39.     MovingQTCanvas c2;
  40.     boolean c1Active;
  41.     
  42.     SpriteDemoApp (String s) throws Exception {
  43.         super(s);
  44.         setResizable (false);
  45.         setLayout(new BorderLayout());
  46.         setBounds (0, 0, 400, 220);
  47.         addWindowListener (new WindowAdapter() {
  48.             public void windowClosing (WindowEvent we) {
  49.                 c1.cleanup();
  50.                 c2.cleanup();
  51.                 QTSession.close();
  52.                 dispose();
  53.             }
  54.             public void windowClosed (WindowEvent we) {
  55.                 System.exit(0);
  56.             }
  57.         });
  58.                     
  59.         Compositor shipAnimation = new Compositor (new QDGraphics (new QDRect(200, 200)), 
  60.                                                     QDColor.yellow, 20, 1); 
  61.         addSprites (shipAnimation);
  62.         shipAnimation.getTimer().setRate(1);
  63.         
  64.         c1 = new MovingQTCanvas (shipAnimation);
  65.         add("West", c1);
  66.         c1.setClient (shipAnimation, true);
  67.         
  68.         c2 = new MovingQTCanvas (shipAnimation);
  69.         add("East", c2);
  70.  
  71.         Button b = new Button ("Click Here To Make Sprites Jump");
  72.         b.addActionListener (new ActionListener () {
  73.             boolean c1Active = true;
  74.             
  75.             public void actionPerformed (ActionEvent event) {
  76.                 if (c1Active) {
  77.                     c1.removeClient();
  78.                     c2.doSetClient();
  79.                 } else {
  80.                     c2.removeClient();
  81.                     c1.doSetClient();
  82.                 }
  83.                 c1Active = ! c1Active;
  84.             }
  85.         });
  86.         add("North", b);
  87.     }
  88.  
  89.     // this is used to change the rates when the clients are changed
  90.     static int r = 2;
  91.     
  92.     class MovingQTCanvas extends QTCanvas {
  93.         
  94.         MovingQTCanvas (Compositor anim) {
  95.             client = anim;
  96.         }
  97.         
  98.         Compositor client;
  99.         
  100.         void doSetClient() {
  101.             try {
  102.                 setClient (client, true);
  103.                 if (r++ % 3 == 0)
  104.                     client.getTimer().setRate(0);
  105.                 else {
  106.                     float rt = client.getTimer().getRate();
  107.                     if (rt == 0)
  108.                         client.getTimer().setRate (1);
  109.                     else
  110.                         client.getTimer().setRate (-client.getTimer().getRate());
  111.                 }
  112.             } catch (QTException e) {
  113.                 e.printStackTrace();
  114.             }
  115.         }
  116.         
  117.         public void cleanup () {
  118.             client = null;
  119.             removeClient();
  120.         }
  121.     }
  122.  
  123.     static void addSprites (Compositor sh) throws IOException, QTException {
  124.         File matchFile = QTFactory.findAbsolutePath ("images/Ship01.pct");    //this file must exist in the directory!!!    
  125.         ImageDataSequence isp = ImageUtil.createSequence (matchFile);
  126.         ImageDataSequence seq = ImageUtil.makeTransparent (isp, QDColor.blue);
  127.  
  128. // Build Sprites
  129.         Matrix matrix1 = new Matrix();
  130.         matrix1.setTx(20);
  131.         matrix1.setTy(20);
  132.         matrix1.setSx(0.8F);
  133.         matrix1.setSy(0.8F);
  134.         TwoDSprite s1 = new TwoDSprite(seq, 4, matrix1, true, 1);
  135.         sh.addMember (s1);
  136.         
  137.         Matrix matrix2 = new Matrix();    
  138.         matrix2.setTx(4);
  139.         matrix2.setTy(4);
  140.         TwoDSprite s2 = new TwoDSprite(seq, 1, matrix2, true, 10);
  141.         sh.addMember (s2);
  142.         
  143.         // This needs to be a 32bit QDGraphics so the blend mode will 
  144.         // be applied correctly to this sprite
  145.         File shipFile = QTFactory.findAbsolutePath ("images/Ship10.pct");
  146.         GraphicsImporterDrawer ip = new GraphicsImporterDrawer (new QTFile(shipFile));
  147.         QDRect r = new QDRect (ip.getDescription().getWidth(), ip.getDescription().getHeight());
  148.         ImageSpec si = ImageUtil.makeTransparent (ip, QDColor.blue, new QDGraphics (QDGraphics.kDefaultPixelFormat, r));
  149.         Matrix matrix3 = new Matrix();    
  150.         matrix3.setTx(50);
  151.         matrix3.setTy(50);
  152.         TwoDSprite s3 = new TwoDSprite(si, matrix3, true, 8, new GraphicsMode (QDConstants.blend, QDColor.green));
  153.         sh.addMember(s3);
  154.  
  155. // Build ActionList
  156.         SimpleActionList al = new SimpleActionList();
  157.         ImageSequencer is = new ImageSequencer (seq);
  158.         is.setLooping (ImageSequencer.kLoopForwards);
  159.         al.addMember (new NextImageAction (10, 1, is, s1));
  160.         al.addMember (new BounceAction (5, 1, sh, s1, 3, 2));
  161.         ImageSequencer is2 = new ImageSequencer (seq);
  162.         is2.setLooping (ImageSequencer.kLoopForwards);
  163.         al.addMember (new NextImageAction (20, 1, is2, s2));
  164.         al.addMember (new BounceAction (20, 1, sh, s2, 4, 3));
  165.         sh.addController(al);
  166.     }
  167. }
  168.