0

JAVA2D处理中唯一PGraphics可以使用透明背景显示对象的渲染模式(与P2Dand相对P3D)?我已经尝试在其他两种模式(P3D, P2D)下重写我的代码,但结果是PGraphics不透明的背景(默认为黑色)---也许我错过了一种可以让它们变得透明的方法?png我在对象中显示的sPGraphics按预期变成透明的;PGraphics这是不透明的背景。我正在做一个项目,目前已选择JAVA2D以保持我的PGraphics透明性,但想知道我是否可以交替使用P3D它的功能。

从网上环顾四周,相信JAVA2D这是唯一可以在早期版本的处理(1.*)中使 PGraphics 透明的模式,我想知道这是否在版本 2 中发生了变化?

提前感谢您的澄清和帮助!

更新:

下面是我的代码示例(用 Java 中的 Eclipse 编写,Processing 作为组件)。vk 的回复在处理 pde 中效果很好(与我png的 s 一起),但我一直未能成功地让它在我的 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;
}
}

注意: 我不确定在第二类save()中是否需要最后一个函数makeMotifArray()(只是另一种保持透明度的尝试)。

4

1 回答 1

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);
}
于 2013-06-30T01:09:18.567 回答