0

I am using Java/Processing.org to create a drawing application.

I need to be able to reset the PGraphics object quite a lot. This is fine if I am only drawing rects,lines ect to the PGraphics object. But I need to be able to draw images to it.

Is there any way to reset the PGraphics object without calling:

graphic=createGraphics(700, 700, JAVA2D); 

or is there some other way around this issue?

Here is some sample code to highlight the issue. It should crash after about 40+ clicks...

PImage img;
PImage main_image;

PGraphics graphic;

void setup(){
  size(700,700);
 img=loadImage("img.png"); 
  graphic=createGraphics(700, 700, JAVA2D); 
}

void draw(){

 graphic.beginDraw();
 graphic.image(img,mouseX,mouseY,10,10); 

 graphic.endDraw();
 image(graphic,0,0);


}

void mouseClicked(){

  graphic=createGraphics(700, 700, JAVA2D); 
}
4

2 回答 2

1

已解决::: 我每次只调用以下函数时都没有调用 createGraphic:

graphic = setAlpha(graphic);

这是功能:

PGraphics setAlpha(PGraphics g){
  g.loadPixels();
 for (int x = 0; x < g.width; x++ ) {
    for (int y = 0; y < g.height; y++ ) {

      // Calculate the 1D pixel location
      int loc = x + y*g.width;

  g.pixels[loc]=color(0,0);
 }  
 } 
 g.updatePixels();
 return g;
}
于 2012-05-25T07:02:45.630 回答
0

PGraphics 有一个功能已经做到了这一点:clear().

参考

清除缓冲区内的像素。此函数仅适用于使用 createGraphics() 函数创建的 PGraphics 对象。与主图形上下文(显示窗口)不同,使用 createGraphics() 创建的附加图形区域中的像素可以完全或部分透明。此函数清除 PGraphics 对象中的所有内容,以使所有像素 100% 透明。
于 2017-03-02T02:30:15.440 回答