We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › outofboundsexception lesson 101
Page Index Toggle Pages: 1
outofboundsexception lesson 101 (Read 479 times)
outofboundsexception lesson 101
Mar 1st, 2006, 12:38am
 
hi,

apologies if this gets covered every time a newbie runs into it, i did search the board first tho...

i made an amended version of the pixelarray example sketch, had an idea for some pdf vector stuff i wanted to try, and this first experiment works quite nicely, for a short time, then it grinds to a halt:

java.lang.ArrayIndexOutOfBoundsException: 40001

at Temporary_5229_6325.draw(Temporary_5229_6325.java:27)

at processing.core.PApplet.display(PApplet.java:1326)

at processing.core.PGraphics.requestDisplay(PGraphics.java:520)

at processing.core.PApplet.run(PApplet.java:1142)

at java.lang.Thread.run(Unknown Source)

you can see it at http://www.demiology.com/p5/pixelArrayModified
just click and drag horizontally to create straight line patterns, akin to the old 1-pixel wide full height selection in photoshop being transformed for instant patterns...


the code:
import processing.pdf.*;

PImage a;
color cp;
int[] aPixels;

void setup()
{
 size(200, 200);
 noFill();
 noStroke();
 framerate(30);
 a = loadImage("updoc.jpg");
 aPixels = new int[width*height];
 for(int i=0; i<width*height; i++) {
   aPixels[i] = a.pixels[i];
 }
}

void draw()
{
 background(51);
 image(a,0,0);
 if(mousePressed) {
     //beginRecord(PDF, "frame-####.pdf");
   for (int i=0; i<height; i++) {
     cp = aPixels[i*width + mouseX];
    stroke(cp);
     line(0,i,width,i);
   }
    //endRecord();
 }

}

thanks if you can tell me what it is i should be doing here!

d
Re: outofboundsexception lesson 101
Reply #1 - Mar 1st, 2006, 12:53am
 
mouseX is probably greater than "width", which will happen if you hold the mouse down and drag outside the window.

instead of:
cp = aPixels[i*width + mouseX];

you should check to make sure that mouseX is ok, so:
int mx = constrain(mouseX, 0, width-1)];
cp = aPixels[i*width + mx];
Re: outofboundsexception lesson 101
Reply #2 - Mar 1st, 2006, 1:45am
 
that was quick Smiley
thanks man!
Page Index Toggle Pages: 1