Uncaught error fetching image with PixelGrabber

A

Alexandre

public Contenu()
{
super();
setVisible(true);
setBounds(50,50,1024,768);
setDoubleBuffered(true);
Toolkit tk = Toolkit.getDefaultToolkit();
fond = tk.getImage("solgalactic.jpg");
MediaTracker mt = new MediaTracker(this);
mt.addImage(fond, 0);
try{mt.waitForAll(); } catch(Exception e){e.printStackTrace();}
int[] pix = new int[fond.getWidth(this)*fond.getHeight(this)];

PixelGrabber pg = new PixelGrabber(fond, 0, 0,
fond.getWidth(this),fond.getHeight(this), pix, 0,
fond.getWidth(this)*fond.getHeight(this));

//the bug comes in this try catch block
try{pg.grabPixels();}catch(ArrayIndexOutOfBoundsException
e){e.printStackTrace();}catch(Exception e){e.printStackTrace();}

}

While executing the previous code I get this blocking uncaught error.

Uncaught error fetching image:
java.lang.ArrayIndexOutOfBoundsException: 786432
at java.awt.image.PixelGrabber.setPixels(PixelGrabber.java:563)
at sun.awt.image.PixelStore32.replayLines(PixelStore32.java:41)
at sun.awt.image.PixelStore.replay(PixelStore.java:224)
at sun.awt.image.PixelStore.replay(PixelStore.java:153)
at sun.awt.image.InputStreamImageSource.updateFromStore(InputStreamImageSource.java:282)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:235)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:221)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:189)

can anyone help me ?
 
K

Knute Johnson

Alexandre said:
PixelGrabber pg = new PixelGrabber(fond, 0, 0,
fond.getWidth(this),fond.getHeight(this), pix, 0,
fond.getWidth(this)*fond.getHeight(this));
can anyone help me ?

Yes. The last parameter, int scansize, is "the distance from one row of
pixels to the next in the array". In other words the width of the
image. Take out the *fond.getHeight(this) and it should work just fine.

On another note is there some reason you are not using the modern
methods for reading image files and gathering pixel data? What are you
ultimately going to do with this pixel data?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top