I want to trasform a java.awt.Image realized from a Component using
PixelGrabber, but I always have a
NullPointerException. How can I doin in order to avoid it?
This is the code:
public static java.awt.Image getImage(Canvas c)
//creates an Image from graphics method of the Canvas
{
java.awt.Dimension d = c.getSize();
d.setSize(c.dimx,c.dimy);
java.awt.Frame f = new java.awt.Frame();
f.addNotify();
f.setSize(d.width+10,d.height+40);
c.setBackground(Color.white);
java.awt.Image i = f.createImage(d.width,d.height);
c.paint(i.getGraphics());
return i;
}
private static com.lowagie.text.Image getItextImage(Canvas c) throws
DocumentException,IOException
//transforms a java.awt.Image in an array of byte;
// this is necessary because com.lowagie.text.Image i =
com.lowagie.text.Image.getInstance(i0,null);
//don't correctly works.
{
java.awt.Dimension d = c.getSize();
java.awt.Image i0=getImage(c);
com.lowagie.text.Image i = com.lowagie.text.Image.getInstance(i0,null);
int[] pixels=new int[d.width*d.height];
java.awt.image.PixelGrabber pg=new
java.awt.image.PixelGrabber(i0,0,0,d.width,d.height,pixels,0,d.width);
try
{pg.grabPixels();}
// ^^^^^^^^^^^^^^^
//here I have a NullPointerException
catch (InterruptedException e)
{pixels=null;}
if ((pg.getStatus() & ImageObserver.ABORT) != 0)
{pixels=null;}
if (pixels!=null)
{
byte[] bytepixels=new byte[pixels.length*3];
for (int n=0;n<pixels.length;n++)
{
bytepixels[3*n]=(byte)(pixels[n]>>16);//red
bytepixels[3*n+1]=(byte)(pixels[n]>>8);//green
bytepixels[3*n+2]=(byte)(pixels[n]);//blue
}
i=com.lowagie.text.Image.getInstance(d.width,d.height,3,8,bytepixels);
}
return(i);
}
Exception occurred during event dispatching:
java.lang.NullPointerException
at sun.awt.image.OffScreenImageSource.sendPixels(Compiled Code)
at
sun.awt.image.OffScreenImageSource.produce(OffScreenImageSource.java:
168)
at
sun.awt.image.OffScreenImageSource.addConsumer(OffScreenImageSource.j
ava:44)
at
sun.awt.image.OffScreenImageSource.startProduction(OffScreenImageSour
ce.java:58)
at java.awt.image.PixelGrabber.grabPixels(Compiled Code)
at java.awt.image.PixelGrabber.grabPixels(PixelGrabber.java:215)
at output.pdf.getItextImage(Compiled Code)
Marco - 10 Jun 2005 11:29 GMT
>I want to trasform a java.awt.Image realized from a Component using
> PixelGrabber, but I always have a
> NullPointerException. How can I doin in order to avoid it?
N.B. With java 1.5 I haven't a NullPointerException as when I use 1.2, 1.3
and 1.4, but the length of the array pixels is 0... I prefer the
NullPointerException...
Chris Smith - 10 Jun 2005 15:29 GMT
> This is the code:
>
[quoted text clipped - 10 lines]
> return i;
> }
You're attempting to create an image from a non-realized component. You
can't reliably do that. Instead, just use this:
java.awt.Image i = Toolkit.getDefaultToolkit().createImage(...);

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Marco - 10 Jun 2005 16:47 GMT
> You're attempting to create an image from a non-realized component. You
> can't reliably do that.
No. the Image EXISTS, and I use it for create a pdf file, and the pdf file
contains all images as I want.
The NullPointerException is generated by the SECOND method, after
java.awt.image.PixelGrabber pg=new
java.awt.image.PixelGrabber(i0,0,0,d.width,d.height,pixels,0,d.width) and
pg.grabPixels()
But I think that it is possible that the cause is the TYPE of Image returned
by the FIRST method: f.addNotify();
call the "peer", that depend from OS (Windows, Linux, ecc). In fact, the
generated pdf file is not visualized in the same mode in Windows and in
Linux (the used font is not the same)
> Instead, just use this:
>
> java.awt.Image i = Toolkit.getDefaultToolkit().createImage(...);
If I read the correct page
(http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Toolkit.html#getDefaultToolkit()),
I read this:
Image createImage(byte[] imagedata)
Creates an image which decodes the image stored in the
specified byte array.
abstract Image createImage(byte[] imagedata, int imageoffset, int
imagelength)
Creates an image which decodes the image stored in the
specified byte array, and at the specified offset and length.
abstract Image createImage(ImageProducer producer)
Creates an image with the specified image producer.
abstract Image createImage(String filename)
Returns an image which gets pixel data from the specified
file.
abstract Image createImage(URL url)
Returns an image which gets pixel data from the specified
URL.
There isn't a createImage from Component...
Marco - 14 Jun 2005 08:35 GMT
>I always have a
> NullPointerException. How can I doin in order to avoid it?
[quoted text clipped - 4 lines]
>
> //creates an Image from graphics method of the Canvas
> private static com.lowagie.text.Image getItextImage(Canvas c) throws
> DocumentException,IOException
>
> {
> java.awt.Dimension d = c.getSize();
//This is the error: c is equals to (0,0), because the Component is not
visualized
d.setSize(c.dimx,c.dimy);
//This set d to the correct value, with public fields of my class that
extends Canvas.
//Now it is all ok, and works as I want.
> java.awt.Image i0=getImage(c);
//ecc. ecc.
Andrew Thompson - 14 Jun 2005 11:51 GMT
>>I always have a
>> NullPointerException. How can I doin in order to avoid it?
>>
>> This is the code:
[ Snip code snippets and written descriptions ]
I feel an SSCCE* would be a lot simpler for us to debug than
these 'bits and pieces' you are posting. Note that Chris
could have seen *exactly* where the errors were occuring
for himself - 3 days ago.
* <http://www.physci.org/codes/sscce.jsp>

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
Marco - 14 Jun 2005 12:47 GMT
> Note that Chris
> could have seen *exactly* where the errors were occuring
> for himself - 3 days ago.
No, Chris says that "You're attempting to create an image from a
non-realized component. You
can't reliably do that".
Instead, the error is (it is in my precedent message) that
"java.awt.Dimension d = c.getSize()" set c equals to (0,0), because the
Component is not visualized".
The Image EXISTS, only its size was (0,0), and after added
"d.setSize(c.dimx,c.dimy);" now it is all ok.
I don't know if Toolkit.getDefaultToolkit().createImage can create an Image
from a Component...
Thank you for your message.
Marco