next up previous
Next: Image Producing Up: QuickTime for Java Previous: Drawing An Image File

Quicktime to Java Imaging

The code in this section shows how to create a java.awt.Image from a QuickTime source. The QTImageProducer is used to produce this image's pixel data from the original QuickTime source.

The QuickTime image could come from any one of the following sources:

In the sample code, the user is prompted to open an image file from one of 20 or more formats that QuickTime's GraphicsImporter can import. The program then uses the QTImageProducer to create a java.awt.Image that is then drawn in the paint() method of the Frame. You prompt the user to select an image file and import that image into QuickTime. You then create a GraphicsImporteDrawer that uses the GraphicsImporter to draw.

This object produces pixels for the QTImageProducer:

QTFile imageFile =
QTFile.standardGetFilePreview(QTFile.kStandardQTFileTypes);
GraphicsImporter myGraphicsImporter = 
     new GraphicsImporter (imageFile);
GraphicsImporterDrawer myDrawer = 
     new GraphicsImporterDrawer(myGraphicsImporter);

You create a java.awt.Image from the pixels supplied to it by the QTImageProducer:

QDRect r = myDrawer.getDisplayBounds();
imageSize = new Dimension (r.getWidth(), r.getHeight());
QTImageProducer qtProducer = new QTImageProducer (myDrawer,
                                        imageSize);
javaImage = 
   Toolkit.getDefaultToolkit().createImage(qtProducer);

Note that to do Java drawing, your application uses the
java.awt.Graphics.drawImage(...) calls, which must be defined in a paint() method. When using a QTCanvas client, this detail is taken care of by the QTCanvas itself by establishing the QTCanvas QTDrawable client relationship, as the above code demonstrates.

In the paint() method of the frame, the image that we produced in the above code is drawn, using this drawImage call. This method will correctly resize the image to the size of the Frame.

public void paint (Graphics g) {
Insets i = getInsets();
Dimension d = getSize();
int width = d.width - i.left - i.right;
int height = d.height - i.top - i.bottom;
g.drawImage (javaImage, i.left, i.top, width, height, this);
}

This returns the size of the source image, so the pack() will correctly resize the frame:

public Dimension getPreferredSize () {
return imageSize;
}


next up previous
Next: Image Producing Up: QuickTime for Java Previous: Drawing An Image File
Dave Marshall
10/4/2001