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 › redrawing image to PGraphics2D causes ArrayIndOOB
Page Index Toggle Pages: 1
redrawing image to PGraphics2D causes ArrayIndOOB (Read 1108 times)
redrawing image to PGraphics2D causes ArrayIndOOB
Nov 2nd, 2009, 6:15am
 
I'm using a PGraphics2D, pg, as my own backbuffer. My PApplet is added to a class extending JPanel. To handle resizing the PApplet and the images contained in it, I added a ComponentListener to my JPanel. componentResized() sets the size() of the PApplet and calls a method to redraw some stuff then when all of that is done, we go to my method to update the whole backbuffer before draw() gets a hold of anything. Occasionally I will get an ArrayIndexOutOFBoundsException looking like this.

Code:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
       at processing.core.PPolygon.coverage(PPolygon.java:606)
       at processing.core.PPolygon.scanline(PPolygon.java:526)
       at processing.core.PPolygon.render(PPolygon.java:335)
       at processing.core.PGraphics2D.endShape(PGraphics2D.java:401)
       at processing.core.PGraphics.endShape(PGraphics.java:1153)
       at processing.core.PGraphics.imageImpl(PGraphics.java:2293)
       at processing.core.PGraphics2D.imageImpl(PGraphics2D.java:1410)
       at processing.core.PGraphics.image(PGraphics.java:2221)
       at processing.core.PGraphics.image(PGraphics.java:2197)


followed by a reference to a line in my code where I am simply adding an image that already exists and was not altered in the process, aside from being resize by the image(x,y,wid,hight) method. I'm stumped. I will also occasionally get a null pointer exception when I resize. . .

Heres the spot where the Exception is happening (keep in mind that this Exception happens during one of the image() calls, these images should already be set):

Code:
 private void updateScreenImage() {
       //Draw to BackBuffer, you don't want to see that rescaling happen
       //long startTime = System.currentTimeMillis();
       pg.beginDraw();
       pg.background(BACKGROUND_COLOR);
       pg.smooth();
       pg.stroke(100);//GRID_TEXT_COLOR);
       pg.image(bgrnd.get(), OFFSET, OFFSET, width-OFFSET-5, height-OFFSET-5);
       pg.image(hist.get(), OFFSET, OFFSET, width-OFFSET-5, height-OFFSET-5);
       pg.image(grid.get(), OFFSET-5, OFFSET, grid.width, grid.height);
       pg.image(vertLabel.get(),0,0,vertLabel.width, vertLabel.height);
       pg.image(titleLabel.get(),OFFSET,5,titleLabel.width,titleLabel.height);
       pg.noFill();
       pg.rect(OFFSET-1, OFFSET-1, width-OFFSET-4, height-OFFSET-4);

       pg.endDraw();


and here's my componentResized() method:
Code:
if(this.isShowing()){
           histogram.noLoop();
           ((ProcessingHistogram)histogram).resetGrid(this.getWidth() - 1, this.getHeight() - 1);
           histogram.loop();

           resizedWhileHidden = false;
       }


and finally, here is my resetGrid() method, All I'm really doing here, is redrawing to a couple Pgraphics2D buffers and redrawing a line grid, to overlay my graph just so it doesn't get stretched in a resize:
Code:
public void resetGrid(int wid, int hig){
       size(wid, hig); //reset size first

       titleLabel = new PGraphics2D();
       titleLabel.setParent(this);
       titleLabel.setPrimary(false);
       titleLabel.setSize(width-OFFSET-5, OFFSET-10);
       titleLabel.beginDraw();
       titleLabel.background(BACKGROUND_COLOR);
       titleLabel.image(miniHuey,(int)(titleLabel.width-miniHuey.width)-5,2);
       titleLabel.stroke(50);
       titleLabel.noFill();
       titleLabel.rect((float)(titleLabel.width-miniHuey.width)-5,2,miniHuey.width,miniHuey.height);
       titleLabel.textFont(TITLE_TEXT_FONT);
       titleLabel.fill(GRID_TEXT_COLOR);
       titleLabel.text("HISTOGRAM VIEW",4,titleLabel.height-8);

       titleLabel.endDraw();
       //make it the right size so we don't have to rescale later
       grid = createImage(width-OFFSET, height-OFFSET-5, ARGB);

       int numGridLines = ((110-Math.abs(top))/10);
       gridLineOffset = (grid.height)/numGridLines; //the space between

       vertLabel = new PGraphics2D();
       vertLabel.setParent(this);
       vertLabel.setPrimary(false);
       vertLabel.setSize(OFFSET, height-5);
       vertLabel.beginDraw();
       vertLabel.background(BACKGROUND_COLOR);
       vertLabel.fill(GRID_TEXT_COLOR);
       vertLabel.stroke(100);//GRID_COLOR);
       vertLabel.textFont(GRID_TEXT_FONT);
       vertLabel.textAlign(RIGHT);
       //draw straightVertical line
       vertLabel.line(vertLabel.width-4,OFFSET,vertLabel.width-4,vertLabel.height-5);
       //draw top vertical hash
       vertLabel.line(vertLabel.width-7, OFFSET, vertLabel.width-4, OFFSET);
       //draw top label
       vertLabel.text("-"+abs(top),vertLabel.width-7,OFFSET+5);

       grid.loadPixels();//gotta load em before you mess with em
       //draw the hash marks as grid-lines
       for(int i = 1; i<numGridLines; i++){
           //draw vertical hashes to match
           vertLabel.line(vertLabel.width-7, i*gridLineOffset+OFFSET,
                                           vertLabel.width-4, i*gridLineOffset+OFFSET);
           //draw db level labels
           vertLabel.text("-"+(10*i+abs(top)),vertLabel.width-7,i*gridLineOffset+OFFSET+5);
           for(int j=0; j<grid.width;j++){
               if(j%6!=0 && j%6!=1 && j%6!=2){
                   grid.pixels[(i*gridLineOffset)*grid.width+j] = GRID_COLOR;
               }
           }
       }
       grid.updatePixels();//put em back where you found em
       vertLabel.endDraw();
                                   
       pg = new PGraphics2D();     //
       pg.setParent(this);         //  
       pg.setPrimary(true);       //  
       pg.setSize(width, height);//resize backBuffer\\
       updateScreenImage();//there's been resizing, so update the backbuffer
       //redraw();
   }


Please tell me If I'm doing anything weird here causing the error. It's all pretty straight forward I hope.

Thanks!
Re: redrawing image to PGraphics2D causes ArrayIndOOB
Reply #1 - Nov 2nd, 2009, 11:17am
 
well, I know no one has responded, but I have found that if I just draw everything to the screen instead of the PGraphics2D, pg, I do not get the exceptions. does PGraphics2D not like drawing images onto itself?
Re: redrawing image to PGraphics2D causes ArrayIndOOB
Reply #2 - Nov 12th, 2009, 9:21am
 
also if pg is a PGraphics instead of PGraphics2D, all is well....
Re: redrawing image to PGraphics2D causes ArrayIndOOB
Reply #3 - Nov 12th, 2009, 11:31am
 
Do you not need createGraphics() with PGraphics2D?  It seems only to be mentioned in the docs for its parent class, PGraphics, but I don't know.  Also: polygon coverage OOB sounds like it might not have the right width/height, or it might be trying to go beyond its borders?
Re: redrawing image to PGraphics2D causes ArrayIndOOB
Reply #4 - Nov 12th, 2009, 12:05pm
 
yeah, I was reading the docs for PGraphics2D and it said to instantiate it using it's constructor and then explicitly setting parent, size, etc... so far I'm not having issues with the PGraphics so I'm just going to use that. I was only assuming I'd get some speed increase with the PGraphics2D
Re: redrawing image to PGraphics2D causes ArrayIndOOB
Reply #5 - Nov 12th, 2009, 1:00pm
 
Hmm, I've been working on a project using a PGraphics offscreen buffer as well, and I don't get any framerate increase by converting to PGraphics2D...if anything it goes down by a couple fps.
Page Index Toggle Pages: 1