Re: Jgen-devel digest, Vol 1 #326 - 2 msgs
Eric Bloch <[email protected]> Thu, 9 Oct 2003 20:28:51 -0700 (PDT)
| Newsgroups | gmane.comp.java.jgenerator.devel |
|---|---|
| Message-ID | <[email protected]> |
-
> This method currently is not used in JGenerator, but I remember that
> it used to work fine.
> If it does not work for you, you may try to fix it.
>
This code below for turning an Image into a BufferedImage
works for me in another application.
// This method returns a buffered image with the contents of an
image
public static BufferedImage toBufferedImage(Image image, int offx,
int offy, int w, int h) {
if (image instanceof BufferedImage) {
return (BufferedImage)image;
}
// This code ensures that all the pixels in the image are
loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels; for this
method's
// implementation, see e665 Determining If an Image Has
Transparent Pixels
//boolean hasAlpha = hasAlpha(image); XXX
boolean hasAlpha = false;
// Create a buffered image with a format that's compatible with
the screen
BufferedImage bimage = null;
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
//try {
// Determine the type of transparency of the new buffered
image
int transparency = Transparency.OPAQUE;
if (hasAlpha) {
transparency = Transparency.BITMASK;
}
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(w, h, transparency);
//} catch (HeadlessException e) {
// The system does not have a screen
//}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha) {
type = BufferedImage.TYPE_INT_ARGB;
}
bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, offx, offy, null);
g.dispose();
return bimage;
}
}
=====
Eric Bloch
1437 10th Avenue, San Francisco, CA, USA 94122
Email: [email protected]
Web page: http://www.10th-avenue.com
Phone: 415-731-0589
__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php