使用Java处理,我试图对PGraphics2D对象进行深度复制
PGraphics2D pg_render;
pg_render = (PGraphics2D) createGraphics(width, height, P2D);
PGraphics2D pg_postprocd = (PGraphics2D)pg_render.clone();它抛出一个CloneNotSupportedException:
未处理异常类型CloneNotSupportedException
然而,阅读医生似乎已经实现了克隆。
我需要有两个PGraphics2D对象的实例,这样我就可以在其中一个上应用后处理效果,而另一个保持干净,用于分析运动矢量等等。
发布于 2021-01-04 19:10:08
异常
PGraphics类本身并不实现Clonable。相反,它扩展了PImage,这是实际实现Cloneable接口的类。
这就是为什么您对pg_render.clone()的调用抛出了CloneNotSupportedException,因为PGraphics实际上并不支持克隆(但碰巧扩展了一个确实支持克隆的类)。
解决方案
下面的静态方法返回输入PGraphics对象的克隆。它用PGraphics制作了一个新的createGraphics()对象,克隆样式(样式包括当前填充颜色),最后克隆像素缓冲区。
代码
static PGraphics clonePGraphics(PGraphics source) {
final String renderer;
switch (source.parent.sketchRenderer()) {
case "processing.opengl.PGraphics2D" :
renderer = PConstants.P2D;
break;
case "processing.opengl.PGraphics3D" :
renderer = PConstants.P3D;
break;
default : // secondary sketches cannot use FX2D
renderer = PConstants.JAVA2D;
}
PGraphics clone = source.parent.createGraphics(source.width, source.height, renderer);
clone.beginDraw();
clone.style(source.getStyle()); // copy style (fillColor, etc.)
source.loadPixels(); // graphics buffer -> int[] buffer
clone.pixels = source.pixels.clone(); // in's int[] buffer -> clone's int[] buffer
clone.updatePixels(); // int[] buffer -> graphics buffer
clone.endDraw();
return clone;
}https://stackoverflow.com/questions/64884843
复制相似问题