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 9:18 PM, Rob Ross wrote:


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.



I'm not entirely sure I followed this so I might be off base. But there is a trick to get an Image from an Icon that I think I originally got from gl4java where you use a Graphics subclass.
Something like...


Code that wants to manipulate Icon as an image ( gui.FileSystemModel from my application code - This to scale the icon as an image)

tmpIcon = (Icon)((CachedIcon)icons.get(extension)).getIcon (); // = (Icon)new ImageIcon(f.getPath());
if (tmpIcon == null) throw new NullPointerException ("FilePreviewrer: icon is not cached");
IconGraphics icon2img = new IconGraphics(getGraphics());
tmpIcon.paintIcon(this,icon2img,0,0);
Image iconimg = icon2img.getImage();
int imageWidth = iconimg.getWidth(this);
int imageHeight = iconimg.getHeight(this);
buffimg = new BufferedImage (imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);
Graphics icong = buffimg.createGraphics();
icong.drawImage(iconimg,0,0,this);
resicon = new ImageIcon(buffimg.getScaledInstance (32,32,Image.SCALE_DEFAULT));


The pertinent parts of the IconGraphics Graphics subclass

		public IconGraphics(Graphics g) {
			this.g = g;
		}

* Set the desired dimensions for the rendered Icon and then
* invoke paintIcon() passing it this as the graphics
* The icon's image will be saved when passed to the drawImage() method
* of this class. The JVM does the conversion to Image before invoking this
* method. There is no other way I know of, pure java, to extract a Image from a Icon
* so this kluge seems required.
* I got the idea from the GLGraphics class of GL4Java.


is the comments. Off-hand for this...
Image iconimg = icon2img.getImage();
I'm not sure what initialized the returned image from the graphics subclass
public Image getImage() { return i; }


But anyhow, the scaling isn't perfect but it works.

IconGraphics is part of my utility package utillib.widgets.IconGraphics, so apparently I thought this useful enough that it would see some re-use.


Mike Hall mikehall at spacestar dot net http://www.spacestar.net/users/mikehall http://sourceforge.net/projects/macnative



Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
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>)
 >Re: painting in Java to a (native) NSView possible? (From: Rob Ross <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.