FileDocCategorySizeDatePackage
GraphicImportMatrix.javaAPI DocExample5014Wed Nov 10 13:03:24 GMT 2004com.oreilly.qtjnotebook.ch05

GraphicImportMatrix

public class GraphicImportMatrix extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        try {
            QTSessionCheck.check();

            File graphicsDir = new File ("graphics");
            QTFile pngFile1 = new QTFile (new File (graphicsDir, "1.png"));
            QTFile pngFile2 = new QTFile (new File (graphicsDir, "2.png"));
            GraphicsImporter gi1 = new GraphicsImporter (pngFile1);
            GraphicsImporter gi2 = new GraphicsImporter (pngFile2);
            
            // define some matrix transforms on importer 1
            QDRect bounds = gi1.getBoundsRect();
            // combine translation (movement) and scaling into
            // one call to rect
            QDRect newBounds =
                new QDRect (bounds.getWidth()/4,
                            bounds.getHeight()/4,
                            bounds.getWidth()/2,
                            bounds.getHeight()/2);
            Matrix matrix = new Matrix();
            matrix.rect(bounds, newBounds);
            // rotate about its center
            matrix.rotate (30,
                           (bounds.getWidth() - bounds.getX())/2,
                           (bounds.getHeight() - bounds.getY())/2);
            gi1.setMatrix (matrix);

            // draw somewhere
            QDGraphics scratchWorld = new QDGraphics (gi2.getBoundsRect());
            System.out.println ("Scratch world: " + scratchWorld);
            // draw background
            gi2.setGWorld (scratchWorld, null);
            gi2.draw();
            // draw foreground
            gi1.setGWorld (scratchWorld, null);
            gi1.draw();

            int bufSize =
                QTImage.getMaxCompressionSize (scratchWorld, 
                                               scratchWorld.getBounds(),
                                               0,
                                               StdQTConstants.codecNormalQuality,
                                               StdQTConstants4.kPNGCodecType,
                                               CodecComponent.anyCodec);
            byte[] compBytes = new byte[bufSize];
            RawEncodedImage compImg = new RawEncodedImage (compBytes);
            ImageDescription id = QTImage.compress(scratchWorld,
                                                   scratchWorld.getBounds(),
                                                   StdQTConstants.codecNormalQuality,
                                                   StdQTConstants4.kPNGCodecType,
                                                   compImg);
            System.out.println ("rei compressed from gw is " +
                                compImg.getSize());

            /* works, but please kludge less
            try {
                FileOutputStream out = new FileOutputStream ("matrix.png");
                byte compImgBytes[] = compImg.getBytes();
                out.write(compImgBytes, 0, compImgBytes.length);
                out.flush();
                out.close();
            } catch (java.io.IOException ioe) {
                ioe.printStackTrace();
            }
            */

            System.out.println ("exporting");
            GraphicsExporter exporter =
                new GraphicsExporter (StdQTConstants4.kQTFileTypePNG);
            exporter.setInputPtr (compImg, id);
            QTFile outFile = new QTFile (new File ("matrix.png"));
            exporter.setOutputFile (outFile);
            exporter.doExport();
            System.out.println ("did export");

        } catch (QTException qte) {
            qte.printStackTrace();
        }
        System.exit(0);