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!