【发布时间】:2013-06-27 05:34:11
【问题描述】:
JAVA2D 是 Processing 中唯一可以以透明背景显示PGraphics 对象的渲染模式(与P2D 和P3D 不同)?我已经尝试在其他两种模式下重写我的代码(P3D,P2D),但PGraphics 的背景变成不透明(默认为黑色)---也许我错过了一种可以将它们变成的方法透明?我在PGraphics 对象中显示的pngs 按预期变为透明; PGraphics 的背景是不透明的。我正在做一个项目,目前选择了JAVA2D 以保持我的PGraphics 透明,但想知道我是否可以交替使用P3D 及其功能。
从网上看,JAVA2D 是唯一可以在早期版本的 Processing (1.*) 中呈现 PGraphics 透明的模式,我想知道这是否在版本 2 中发生了变化?
提前感谢您的澄清和帮助!
更新:
下面是我的代码示例(用 Java 编写的 Eclipse 和 Processing 作为组件)。 v.k. 的回复在处理 pde 中效果很好(使用我的 pngs),但我一直未能成功地让它在我的 Eclipse 项目中工作。想法? :
主类:
package tester;
import java.util.ArrayList;
import processing.core.*;
public class GraphicsMain extends PApplet{
int screenWidth = 800;
int screenHeight = 500;
PGraphicsMaker pgm = new PGraphicsMaker(this);
ArrayList<PGraphics> motifArray = new ArrayList<PGraphics>();
public void setup() {
size(screenWidth,screenHeight,P3D);
background(0);
motifArray = pgm.makeMotifArray();
if (motifArray.get(0) == null) {
System.out.println("The motif array is unfilled!");
}
}
public void draw() {
for (int i = 0; i < motifArray.size(); i ++) {
image(motifArray.get(i),200+(10*i),100);
}
}
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "GraphicsMain" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
二等:
package tester;
import java.util.ArrayList;
import processing.core.*;
public class PGraphicsMaker {
String filepath = "/a/filepath/here/";
PApplet parent;
int populationSampleSize = 16;
int stageWidth = 100;
int stageHeight = 400;
ArrayList<PGraphics> motifArray;
PImage pic;
PImage pic2;
public PGraphicsMaker(PApplet pa) {
parent = pa;
pic = parent.loadImage(filepath + "small_jones6.png");
pic2 = parent.loadImage(filepath + "small_dresser10.png");
}
public ArrayList makeMotifArray() {
String filepathTempPngs = "/a/filepath/here/tempPngs/";
ArrayList<PGraphics> motifArray = new ArrayList<PGraphics>();
for (int i = 0; i < populationSampleSize; i++) {
motifArray.add(parent.createGraphics(stageWidth,stageHeight,parent.P3D));
motifArray.get(i).beginDraw();
motifArray.get(i).background(255, 255, 255, 0);
motifArray.get(i).image(pic,10,10,100,90);
motifArray.get(i).image(pic2,10,50,50,50);
motifArray.get(i).endDraw();
motifArray.get(i).save(filepathTempPngs + "motif" + i + ".png");
}
return motifArray;
}
}
注意:
我不确定在第二类的makeMotifArray() 中是否需要最后一个save() 函数(只是保持透明度的另一种尝试)。
【问题讨论】:
标签: java png processing java-2d pgraphics