TextPresenter.java
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.util.Vector;
import quicktime.app.image.ImagePresenter;
import quicktime.qd.QDRect;
import quicktime.qd.QDFont;
import quicktime.qd.QDGraphics;
import quicktime.qd.QDDrawer;
import quicktime.qd.QDColor;
import quicktime.qd.QDConstants;
import quicktime.std.image.GraphicsMode;
import quicktime.QTException;
/**
* A graphics utility class that images a block of text loaded from disk and draws it in an image presenter
*
* @author Michael Hopkins
* @author Levi Brown
* @author Apple Computer, Inc.
* @version 2.0 01/25/2000
*
*/
public class TextPresenter
{
/**
* Constructor for the TextPresenter class
* @param filePath relative path of file where text is located
* @param size size of area where text is to be clipped to
*/
public TextPresenter( String filePath, Dimension size ) throws FileNotFoundException
{
this( TextPresenter.readTextFile(filePath), size );
}
/**
* Constructor for the TextPresenter class
* @param filePath relative path of file where text is located
* @param size size of area where text is to be clipped to
*/
public TextPresenter( String[] text, Dimension size )
{
final QDRect area = new QDRect( size );
theText = text;
try
{
gw = new QDGraphics( area );
gw.beginDraw( new QDDrawer()
{
public void draw( QDGraphics gw ) throws QTException
{
gw.textSize( 12 );
gw.textFont( QDFont.getFNum( "Times" ) );
gw.textFace( QDConstants.normal );
gw.setForeColor( QDColor.black );
gw.setBackColor(QDColor.white);
gw.eraseRect(area);
String aLine = theText[0];
if( gw.textWidth( aLine, 0, aLine.length() ) > area.getWidth() )
gw.textSize( 10 );
for ( int i = 0; i < theText.length; ++i )
{
if ( i != 0 )
aLine = theText[ i ];
gw.moveTo( 0, (i+1) * 12 );
gw.drawTextScaled( aLine.length(), aLine, 1.0f, 1.0f );
}
}
});
imagePres = ImagePresenter.fromGWorld( gw );
imagePres.setGraphicsMode( new GraphicsMode( QDConstants.transparent, QDColor.white ));
}
catch ( QTException e )
{
e.printStackTrace();
}
}
/**
* Reads in a text file to a String array.
* @param filePath the path and name of the text file to read.
* @return the string array containing the contents of the file,
* one line per element of the array.
* @throws java.io.FileNotFoundException if the given file can not
* be found to be read.
*/
public static String[] readTextFile(String filePath) throws FileNotFoundException
{
if (filePath != null)
{
try
{
BufferedReader reader = new BufferedReader( new FileReader(filePath));
Vector lines = new Vector();
String aLine = reader.readLine();
while( aLine != null )
{
lines.addElement( aLine );
aLine = reader.readLine();
}
String[] lineArray = new String[ lines.size() ];
lines.copyInto( lineArray );
return lineArray;
}
catch( IOException exc )
{
exc.printStackTrace();
}
}
return new String[0];
}
/**
* Returns the image presenter object that contains the rendered text
*/
public ImagePresenter getPresenter()
{
return imagePres;
}
protected QDGraphics gw;
protected String[] theText;
protected ImagePresenter imagePres;
}