Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: painting in Java to a (native) NSView possible?




On Sep 27, 2006, at 6:34 PM, Michael Hall wrote:

On Sep 27, 2006, at 8:27 PM, Rob Ross wrote:



On Sep 27, 2006, at 6:10 PM, Greg Guerin wrote:

Rob Ross wrote:

Is there a way to provide some kind of  wrapper/bridge object  in
Java (via JNI) to a native NSView class so that I can make Java2D
drawing calls to something like an  "NSViewJComponent" from Java,
treating it like a JComponent from within Java?

AFAIK there's JAWT and CocoaComponent: <http://developer.apple.com/technotes/tn2005/tn2147.html#TNTAG2>

-- GG

Thanks for responding.

I've looked at both of these, and they are actually the opposite of what I am trying to find. I want to do

Java2D ---do drawing---> JavaAWT ---> JNI ---> Cocoa ---> draw into NSView.

The JAWT and Cocoa components let you do

Cocoa -- do drawing --> NSView --> JNI --> JavaAWT --> draw into Java Component

You were asking about NSImage a bit back as I remember. What about converting the java to an image that Cocoa can render into the NSView?



Mike Hall mikehall at spacestar dot net



Haha, yes! This is where I start running in circles again.


The long story is, I'm trying to implement the Mac version of the TrayIcon API of the JDIC project :
https://jdic.dev.java.net/


It's basically an API to the System Tray on windows, and on the Mac it's an API to the Status Bar. Some of the notification actions in Windows OS(the balloon dialog that pops up over a tray icon) map to the Dock notification action, but most of the TrayIcon API is appropriate to the Mac Status Bar. I have a Cocoa test app that does everything I need, so the native part is working.

One problem I am having is that the API I have to implement only provides access to an Icon (javax.swing.Icon). That's just an interface, so it is a pretty opaque object; all you can really do is get the size of the Icon, and call paintIcon on it.

If I had access to the image URL, I could of course pass that from Java to the JNI method and create an NSImage (actually an NSBitmapImageRep), and I can pass that to my NSStatusItem as the image. Or if I see the image is multi-framed, I can use a Timer to periodically update that image and thus provide support for an animated GIF. Or I could also create a custom NSView with an NSImageViewer, and if my image is multi-framed, it would automatically animate it for me.

But, I do NOT have access to the image URL. From an Icon, I would like to at least get an Image, and maybe from that a BufferedImage, and have some hope of getting access to the image data. But, there are at least 8 implementations of Icon, and no guarantee what you get passed will be an ImageIcon. And even if it IS an ImageIcon, the Mac implementation (on my machine) shows that the actual class is an apple.awt.OSXImage, of which I have no official documentation. On Windows, it would either be a BufferedImage or a VolitleImage. From the BufferedImage I could do something with the ImageIO classes, however, there is no GIF writer in the JDK, so even if I can figure out the image is really a GIF, there's no way to write it to a stream or a ByteBuffer in order to pass the bytes to JNI.

I have to use the old-school ImageObserver method, and continually monitor the state of the Image, and when I see the FRAMEBITS flag, I have to create an offscreen Graphics in Java, paint that single new frame, then convert that to something I can pass to a JNI method(like a PNG), that will then install it as the image on the NSStatusItem.

So this is the only thing I can think of that will work, and be safe, only using public, documented APIs.

Some of the not-so-safe hacks I am also contemplating are :

the ImageIcon constructor takes a String description argument, that I do not think is used much. If you created the ImageIcon via a URL or file name, and do not specify the description field, it gets set to the URL or filename you used to construct the object. So I could get this value, see if the file/url exists, and pass that to JNI, and I'm in business.

Or, I could get a BufferedImage version of the ImageIcon by noting that OSXImage extends sun.awt.image.ToolkitImage, and that class has a public getBufferedImage() method. I think there may be a way of getting an IIOImage from a BufferedImage or one of it's subclasses, and then I could at least access the metadata that tells me how many frames the image contains. If it's just one, I can just draw it offscreen, and write it to a PNG (I can write to many different formats, just not GIF). If it's multiple frames, I'm in the same boat as above; I either update the image every time my ImageObserver tells me a new frame is available, or I can extract each frame and pass them to JNI as an array of PNGs, and then implement my own animation loops.

Or, finally, I could avoid this by just drawing in Swing/AWT, to a native NSView, and let Java handle a lot of this image processing for me. It would require however a means of drawing into an NSView from Java.


Rob Ross, Lead Software Engineer
E! Networks
email@hidden
---------------------------------------------------
"Beware of he who would deny you access to information, for in his heart he dreams himself your master." -- Commissioner Pravin Lal


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: painting in Java to a (native) NSView possible? (From: Greg Guerin <email@hidden>)
 >Re: painting in Java to a (native) NSView possible? (From: Rob Ross <email@hidden>)
 >Re: painting in Java to a (native) NSView possible? (From: Michael Hall <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.