home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / imagecompositing / src / javatextdrawer.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  3.8 KB  |  146 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.  
  9. import java.util.*;
  10.  
  11. import quicktime.std.StdQTConstants;
  12. import quicktime.std.image.GraphicsImporter;
  13. import quicktime.app.*;
  14. import quicktime.app.players.MoviePlayer;
  15. import quicktime.std.image.GraphicsMode;
  16.  
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import java.awt.image.*;
  20.  
  21. import quicktime.*;
  22. import quicktime.qd.*;
  23. import quicktime.std.*;
  24. import quicktime.io.*;
  25. import quicktime.std.image.*;
  26. import quicktime.util.*;
  27. import quicktime.std.movies.*;
  28. import quicktime.std.movies.media.*;
  29. import quicktime.app.actions.*;
  30. import quicktime.app.anim.*;
  31. import quicktime.app.display.*;
  32. import quicktime.app.image.*;
  33. import quicktime.app.players.QTPlayer;
  34. import quicktime.app.actions.*;
  35.  
  36. import quicktime.app.spaces.*;
  37. import quicktime.util.QTUtils;
  38.  
  39. import java.io.*;
  40.  
  41. import quicktime.std.StdQTConstants;
  42. import quicktime.std.image.GraphicsImporter;
  43. import quicktime.std.movies.*;
  44. import quicktime.io.*;
  45. import quicktime.util.*;
  46.  
  47. public class JavaTextDrawer implements Compositable, Listener, Notifier {
  48.     
  49.     public JavaTextDrawer (Paintable renderer, Dimension size, boolean makeTransparent) {
  50.         fSize        = size;
  51.         fRenderer     = renderer;
  52.         fMakeTransparent = makeTransparent;
  53.     }
  54.     
  55.     private ImageDataSequence     ids;
  56.     private NotifyListener         fListener;
  57.     private Paintable            fRenderer;
  58.     private GraphicsMode        fGraphicsMode;
  59.     private Dimension            fSize;
  60.     private boolean                fMakeTransparent;
  61.     private Object                fInterest;
  62.         
  63.     public void setGraphicsMode(GraphicsMode mode) {
  64.         fGraphicsMode = mode;    
  65.     }
  66.     
  67.     public GraphicsMode getGraphicsMode() {
  68.         return fGraphicsMode;
  69.     }
  70.     
  71.     // need to make sure we don't get a null pointer exception at this stage
  72.     // this is OK to return NUL BECAUSE we are a notifier
  73.     // normally sprites expect valid description and image data
  74.     public EncodedImage getImage() {
  75.         return (ids != null ? ids.getImage() : null);
  76.     }
  77.     
  78.     public ImageDescription getDescription() {
  79.         return (ids != null ? ids.getDescription() : null);
  80.     }
  81.     
  82.     public void removedFrom(Object interest) {
  83.         fInterest = null;
  84.     }
  85.     
  86.     public void addedTo(Object interest) {
  87.         try {
  88.             fInterest = interest;
  89.             
  90.             this.doWork();
  91.  
  92.             if (fListener != null)
  93.                 fListener.notifyComplete();
  94.         }
  95.         catch (Exception e) {
  96.             e.printStackTrace();
  97.         }
  98.     }
  99.     
  100.     public void doWork() {
  101.         try {
  102.                     // if we don't have an interest registered then qid can't draw its image
  103.             if (fInterest != null) {
  104.                     // qid is only kept as a local as we only need it to produce
  105.                     // a single frame then we throw it away
  106.                 QTImageDrawer qid = new QTImageDrawer (fRenderer, fSize, Redrawable.kSingleFrame);            
  107.                     //this will capture the result of the Java drawing
  108.                 qid.addedTo (fInterest);
  109.                     
  110.                     // get the image data from the java drawing in
  111.                     // a QT format
  112.                 ids = new ImageDataSequence (qid.getDescription());
  113.                 ids.addMember (qid.getImage()); 
  114.                 
  115.                 if (fMakeTransparent) {
  116.                     ids = ImageUtil.makeTransparent (ids, QDColor.white);                
  117.                 }
  118.                 
  119.                 if (fListener != null)
  120.                     fListener.notifyComplete();
  121.             }
  122.         }
  123.         catch (Exception e) {
  124.             e.printStackTrace();
  125.         }
  126.     }
  127.     
  128.     // this method will be called by the Compositor when it
  129.     // detects that a Notifier has been added to it
  130.     public boolean addNotifyListener (NotifyListener nl) {
  131.         //we set ourselves as the Notifier for this listener
  132.         // if the listener doesn't like us it returns false
  133.         // otherwise we can assume that the listener is able
  134.         // to deal with us        
  135.         if (nl.setNotifier (this) == false)
  136.             return false;
  137.         
  138.         fListener = nl;
  139.         
  140.         if (ids != null) {
  141.             nl.notifyComplete();
  142.         }
  143.         
  144.         return true;
  145.     }
  146. }