【问题标题】:Is JAVA2D the only rendering mode to display transparent PGraphics objects in Processing?JAVA2D 是唯一在处理中显示透明 PGraphics 对象的渲染模式吗?
【发布时间】:2013-06-27 05:34:11
【问题描述】:

JAVA2D 是 Processing 中唯一可以以透明背景显示PGraphics 对象的渲染模式(与P2DP3D 不同)?我已经尝试在其他两种模式下重写我的代码(P3DP2D),但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


    【解决方案1】:

    以下代码适用于 1.5.1 和 2.0b8...

    PGraphics p;
    
    void setup(){
      size(400,400,P3D);
      p = createGraphics(400,400,P3D);
      smooth();
      stroke(180,90,210);
    
    }
    
    void draw(){
      background(180,90,210);
      p.beginDraw();
      p.background(220, 180, 40,0);
      p.noFill();
      p.stroke(220, 180, 40);
      p.translate(width/2 - (width/2 - mouseX), height/2, 400-mouseY*5);
      p.sphere(100);
      p.endDraw();
      pushMatrix();
      translate(width/2, height/2, -200);
      rotateY(radians(frameCount));
      rotateX(radians(frameCount/2));
      box(100);
      popMatrix();
      image(p,0,0);
    }
    

    【讨论】:

    • 嗨@v.k。 - 我在处理 pde 中尝试了您的代码,甚至替换了我的pngs,效果很好!然而,奇怪的是,当我在 Eclipse 中尝试代码时(我正在编写我的项目 - 使用处理作为组件)它不起作用。我已经用 Eclipse 的代码更新了我上面的问题。想法?
    • 更新:我发现在 Eclipse 中,我使用的是 2010 年的 Processing core.jar 库版本,该版本带有我不再使用的 Proclipsing 版本,但我使用的库外部参考。我的 Processing pde 副本使用的是更新版本的核心,因此您的代码可以在那里工作,但不能在 Eclipse 中工作。我更新了 Eclipse 中引用的核心库和 OpenGL 库,现在一切正常。谢谢!
    • 嘿,很高兴知道它有帮助。干杯。
    猜你喜欢
    • 2018-01-27
    • 1970-01-01
    • 2013-07-05
    • 2018-05-11
    • 2010-11-21
    • 2018-03-16
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多