O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling Dropped Picts on Mac OS X #68
Chapter 9, Drag-and-Drop
|
349
HACK
Next is the QTJ stuff. It’s OK if you don’t totally understand it—QTJ code
is pretty twisted. That’s why there’s a whole book on it. First, you open a
QTSession
, which initializes QuickTime and allocates resources. You have to
do this before any QTJ call. It can throw a
QTException
, as can most other
QTJ calls, so the whole thing is wrapped in a
try
-
catch
block.
Next, you create a
Pict
object from the byte array and pass that to
DataRef
,
which is a sort of generic media reference. In this case, you pass in flags to tell
the
DataRef exactly what it’s pointing to. The second and third arguments
indicate that we’re using the old-style Mac OS file type and that its value is
PICT. You might be wondering why being a Pict object isn’t self-descriptive
enough. It’s because many QTJ objects are just pointers to blocks of memory
and the functions that work with them. The
DataRef signature used here
takes a
QTHandleRef as an argument. QTHandleRef is subclassed by Pict, but it
is little more than a pointer; the only level on which the
DataRef understands
the
Pict, in fact, is as a block of memory. My point here: QTJ code is weird
and often C-like.
Create a
GraphicsImporter for the Pict format, and point it to the DataRef.
You have now read the Pict “file” from memory. Now, you can get the size
of the imported image and create a
QTImageProducer, with help from a
GraphicsImporterDrawer (a sort of QT-to-Java bridge for still images, also
used by QTJ’s Swing
JComponents). Since this is a normal, everyday
ImageProducer, you can create an image with Component.createImage( ) or
Toolkit.createImage( ).
Finally, you need to call
QTSession.close( ) to deallocate QuickTime
resources. It’s OK to do it many times in a program, although it’s probably
more efficient to
open( ) it once and close( ) it once; e.g., in your quit han-
dler or a shut-down hook.
Take a Breath and Run
The code works exactly as in the previous hack—all you’ve done is support
one more
DataFlavor. In Figure 9-6, I’ve dropped two images from Quick-
Time Player onto the Java app, and both are shown in their own windows.
Actually, this is a little more interesting than it looks because the larger
image isn’t a still image; it’s the current frame of an MPEG-4 movie that I
dragged and dropped into the Java app. The single frame is transported as a
Pict, and using QTJ lets you get it into the Java world. This hack only
scratches the surface of QTJ’s potential—if you want to do Java Media on
the Mac, you should check out QuickTime for Java: A Developer’s Note-
book, by Chris Adamson (yours truly), published by O’Reilly.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required