Compositing Graphics

Matrix transformations are nice, but you can do more with image drawing. QuickDraw supports a number of graphics modes so that instead of just copying pixels from a source to a destination, you can combine them to create interesting visual effects. The graphics mode defines the combination: blending, translucency, etc.

How do I do that?

Specifying a graphics mode for drawing is trivial. Create a GraphicsMode object and call setGraphicsMode( ) on the GraphicsImporter. In the included example, GraphicImportCompositing.java, the mode is set with the following code:

// draw foreground
GraphicsMode alphaMode =
  new GraphicsMode (QDConstants.blend,
                    QDColor.green);
gi1.setGraphicsMode (alphaMode);

Note

Run this with ant-ch05-graphic-importcompositing.

This is another headless app, producing the composite.png file as shown in Figure 5-8. Notice that where the images overlap, the 2 can now show through the 1.

Drawing with blend graphics mode

Figure 5-8. Drawing with blend graphics mode

What just happened?

The “blend” GraphicsMode instructs QuickDraw to average out colors where they overlap. In this case, 1’s black pixels are lightened up by averaging when averaged with cyan, and the green is slightly tinted where it overlaps with cyan or black.

The QDColor.green is irrelevant in this case, but change the first argument to QDConstants.transparent and suddenly the result is very different, as shown in Figure ...

Get QuickTime for Java: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.