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 / zoo tutorial / module 7- switching compositors / source / textpresenter.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  6.2 KB  |  187 lines

  1. import java.awt.Dimension;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.FileReader;
  7. import java.util.Vector;
  8.  
  9. import quicktime.app.image.ImagePresenter; 
  10.  
  11.  
  12. import quicktime.qd.QDFont;
  13. import quicktime.qd.QDDrawer;
  14. import quicktime.qd.QDColor; 
  15. import quicktime.qd.QDConstants;
  16.  
  17. import quicktime.qd.QDGraphics;
  18. import quicktime.qd.QDRect;
  19.  
  20. import quicktime.QTException; 
  21.  
  22. import quicktime.std.image.GraphicsMode;
  23.  
  24.  
  25.  
  26. /**
  27.  * QTZoo Module 3 - Using QuickTime to draw text
  28.  * A graphics utility class that images a block of text loaded from disk 
  29.  * and draws it in an image presenter
  30.  *
  31.  * @author Michael Hopkins
  32.  * @author Levi Brown 
  33.  * @author Apple Computer, Inc.
  34.  * @version 2.0 01/25/2000
  35.  *
  36.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  37.  *    
  38.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  39.  *                ("Apple") in consideration of your agreement to the following terms, and your
  40.  *                use, installation, modification or redistribution of this Apple software
  41.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  42.  *                please do not use, install, modify or redistribute this Apple software.
  43.  *
  44.  *                In consideration of your agreement to abide by the following terms, and subject
  45.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  46.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  47.  *                reproduce, modify and redistribute the Apple Software, with or without
  48.  *                modifications, in source and/or binary forms; provided that if you redistribute
  49.  *                the Apple Software in its entirety and without modifications, you must retain
  50.  *                this notice and the following text and disclaimers in all such redistributions of
  51.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  52.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  53.  *                Apple Software without specific prior written permission from Apple.  Except as
  54.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  55.  *                are granted by Apple herein, including but not limited to any patent rights that
  56.  *                may be infringed by your derivative works or by other works in which the Apple
  57.  *                Software may be incorporated.
  58.  *
  59.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  60.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  61.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  62.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  63.  *                COMBINATION WITH YOUR PRODUCTS.
  64.  *
  65.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  66.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  67.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  68.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  69.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  70.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  71.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  72.  * 
  73.  */
  74. public class TextPresenter
  75. {
  76.     /**
  77.      *  Constructor for the TextPresenter class
  78.      *  @param filePath relative path of file where text is located
  79.      *  @param size size of area where text is to be clipped to
  80.      */
  81.     public TextPresenter( String filePath, Dimension size )  throws FileNotFoundException
  82.     {
  83.         this( TextPresenter.readTextFile(filePath), size );
  84.     }
  85.     
  86.     /**
  87.      *  Constructor for the TextPresenter class
  88.      *  @param filePath relative path of file where text is located
  89.      *  @param size size of area where text is to be clipped to
  90.      */
  91.     public TextPresenter(String[] text, Dimension size)
  92.     {    
  93.         final QDRect area = new QDRect( size.width, size.height );
  94.         theText = text;
  95.         
  96.         try
  97.         {
  98.             gw = new QDGraphics( area );
  99.             
  100.             gw.beginDraw( new QDDrawer()
  101.                             {
  102.                                 public void draw( QDGraphics gw ) throws QTException
  103.                                 {
  104.                                     gw.textSize( 12 );
  105.                                     gw.textFont( QDFont.getFNum("Times") ); 
  106.                                     gw.textFace( QDConstants.normal );
  107.                                     gw.setForeColor( QDColor.black );
  108.                                       
  109.                                      gw.setBackColor( QDColor.white );
  110.                                     gw.eraseRect( area );
  111.                                     
  112.                                     
  113.                                     String aLine = theText[0];
  114.                                     
  115.                                     if ( gw.textWidth( aLine, 0, aLine.length() ) > area.getWidth() )
  116.                                         gw.textSize( 10 );
  117.                                     
  118.                                       
  119.                                     for ( int i = 0; i < theText.length; ++i )
  120.                                      {
  121.                                          if ( i != 0 )
  122.                                              aLine = theText[ i ];
  123.                                           gw.moveTo( 0, (i+1) * 12 );
  124.                                          gw.drawTextScaled( aLine.length(), aLine, 1.0f, 1.0f );
  125.                                        }
  126.                                   }
  127.                             }); 
  128.                               
  129.             imagePres = ImagePresenter.fromGWorld( gw );
  130.             imagePres.setGraphicsMode( new GraphicsMode( QDConstants.transparent, QDColor.white ));
  131.         }
  132.         catch ( QTException e )
  133.         {
  134.             e.printStackTrace();
  135.         }
  136.     }
  137.     
  138.     /**
  139.      * Reads in a text file to a String array.
  140.      * @param filePath the path and name of the text file to read.
  141.      * @return the string array containing the contents of the file,
  142.      * one line per element of the array.
  143.      * @throws java.io.FileNotFoundException if the given file can not
  144.      * be found to be read.
  145.      */
  146.     public static String[] readTextFile( String filePath ) throws FileNotFoundException
  147.     {
  148.         if (filePath != null)
  149.         {
  150.             try
  151.             {
  152.                 BufferedReader reader = new BufferedReader( new FileReader( filePath ));
  153.                 Vector lines = new Vector();
  154.                 
  155.                 String aLine = reader.readLine();
  156.                 while( aLine != null )
  157.                 {
  158.                     lines.addElement(aLine);
  159.                     aLine = reader.readLine();
  160.                 }
  161.                 
  162.                 String[] lineArray = new String[ lines.size() ];
  163.                 lines.copyInto(lineArray);
  164.                 return lineArray;
  165.             }
  166.             catch (IOException exc)
  167.             {
  168.                 exc.printStackTrace();
  169.             }
  170.         }
  171.         
  172.         return new String[0];
  173.     }
  174.     
  175.     /**
  176.      * Returns the image presenter object that contains the rendered text
  177.      */
  178.     public ImagePresenter getPresenter()
  179.     {
  180.         return imagePres;
  181.     }
  182.     
  183.     protected QDGraphics      gw;
  184.     protected String[]          theText;
  185.     protected ImagePresenter imagePres;
  186. }
  187.