javapngprocessingjava-2dpgraphics

Is JAVA2D the only rendering mode to display transparent PGraphics objects in Processing?


Is JAVA2D the only rendering mode in Processing in which PGraphics objects can be displayed with a transparent background (as opposed to P2D and P3D)? I have tried rewriting my code in the other two modes (P3D, P2D) but the backgrounds of the PGraphics turn out opaque (black by default)---perhaps I'm missing a method that would turn them transparent? The pngs I'm displaying in the PGraphics objects are turning out transparent as expected; it's the backgrounds of the PGraphics that are opaque. I am working on a project and have currently selected JAVA2D in order to keep my PGraphics transparent, but wonder if I can alternately use P3D and its features instead.

From looking around online believe JAVA2D was the only mode that could render PGraphics transparent in earlier versions of Processing (1.*), and I'm wondering if this has changed with version 2?

Thanks in advance for your clarification and help!

UPDATE:

Below is a sample of my code (written in Eclipse in Java with Processing as a component). v.k.'s reply works great (with my pngs) in the Processing pde, but I've been unsuccessful getting it to work in my project in Eclipse. Thoughts? :

Main class:

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);
    }
}

Second class:

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;
}
}

NOTE: I'm not certain that last save() function is necessary in makeMotifArray() in the second class (just another attempt to preserve transparency).


Solution

  • The code below worked for me in 1.5.1 and 2.0b8... If I got you right it is doing what you need... Check it out:

    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);
    }