toxiclibs - toxiclibsSupport, PGraphics, setGraphics() - trying to use off screen buffer

edited August 2016 in Library Questions

hello,

I am fairly new to Processing and in the early stages of learning Java.
Thank you for this excellent library - it was crucial to the first major piece of software that I have written (used for an installation a couple of weeks ago).
What I am trying to do is use an instance of PGraphics (called 'frame' in the code below) to draw to the off screen buffer and then save as a .png - for many reasons, but in this case to use transparency (drawing lines and shapes(triangles) on a transparent background).
As soon as I either specify 'frame' in the constructor: "gfx = new ToxiclibsSupport (this, frame);" or use setGraphics(): "gfx.setGraphics(frame);" I get a draft rendering of the drawing - just black lines on a white background, no images, no shapes - for both the off screen buffer and the display.
I have tried everything to the limits of my current knowledge and abilities including using the P2D renderer (specified in size() and in the createGraphics () for 'frame'). As soon as I remove 'frame' from the ToxiclibsSupport constructor or comment out setGraphics(), everything returns to normal, so clearly it is the addition of this PGraphics instance. I have included a very simplified example of my code.

Thank you for any help,

ToxiclibsSupport gfx;
PGraphics frame;


void setup () {

  frame = createGraphics (width, height); 
  gfx = new ToxiclibsSupport(this, frame);
  //gfx.setGraphics(frame);

}


void draw () {

  frame.save (imageFileName + nf (frameCount++, 3) + ".png");

}



used in a function:


void drawTriangles () {

  frame.beginDraw();

  for (int i = 0 ; i < triangleList.size () ; i++ ) {
    if (triangleList.get(i).on) {
      noStroke();
      colorSelect(i);
      gfx.polygon2D(triangleList.get(i).triangle);
    } else {
      noFill();
      stroke(white);
      gfx.polygon2D(triangleList.get(i).triangle);
    }
  }

  frame.endDraw();
  image(frame,0,0);  
}   

Answers

  • Dunno whether that's the problem, however frame is a field of class PApplet.
    Maybe rename it to something else, like pg or whatever? :-??

  • I tried using 'fr' - no change.

  • Worth a try at least... However your posted code accesses unknown members, such as triangleList & colorSelect(). It helps a lot to post all the "actors" involved. :-\"

  • ok, here is revised code:

    ToxiclibsSupport gfx;
    PGraphics fr;
    
    void setup () {
      fr = createGraphics (width, height); 
      gfx = new ToxiclibsSupport(this, fr);
      //gfx.setGraphics(fr);
    }
    
    
    
    
    used in a function:
    
    
    void drawTriangles () {
    
      fr.beginDraw();
    
      for (int i = 0 ; i < triangleList.size () ; i++ ) {
        if (triangleList.get(i).on) {
          noStroke();
          colorSelect(i);
          gfx.polygon2D(triangleList.get(i).triangle);
        } else {
          noFill();
          stroke(white);
          gfx.polygon2D(triangleList.get(i).triangle);
        }
      }
    
      fr.endDraw();
      image(fr,0,0);  
    }   
    
    
    
    
    supporting functions and classes:
    
    
    
    class TriangleList {
      int v1, v2, v3;
    
      char col;
      boolean on = false;
    
      Vec2D vec1 = new Vec2D ();
      Vec2D vec2 = new Vec2D ();
      Vec2D vec3 = new Vec2D ();
    
      Polygon2D triangle = new Polygon2D ();
    
      public TriangleList (int V1, int V2, int V3) {
        v1 = V1;
        v2 = V2;
        v3 = V3;
      }
    }
    
    
    
    
    void colorSelect (int i) {
      char c = triangleList.get(i).col;
      switch (c) {
      case 'r':
        fill(red);
        break;
      case 'g':
        fill(green);
        break;
      case 'b':
        fill(blue);
        break;
      }
    }
    
  • Sorry dunno much about toxiclibs. And got no idea what that class ToxiclibsSupport is! X_X

    Seems like it's not compatible w/ Processing 3 either! :-<

    Also I can't spot where triangleList was declared at? :-@

  • edited August 2016

    This program uses toxiclibs extensively - eg. "gfx.polygon2D....." - everything works as it is supposed to until now - trying to use PGraphics with it.

    http://toxiclibs.org/docs/p5/toxi/processing/ToxiclibsSupport.html#setGraphics(processing.core.PGraphics)

    http://toxiclibs.org/docs/core/

    triangle list has been declared:

    ArrayList <TriangleList> triangleList = new ArrayList <TriangleList>();

    http://www.creativeapplications.net/processing/working-with-toxiclibs-processing-tutorial

    https://forum.processing.org/two/discussions/tagged/toxiclibs

  • edited August 2016

    Since your posted code isn't runnable, it doesn't even got the import statements after all, I've decided to take a look at the library by myself, and then come up w/ my own example. Here it is: :ar!

    /**
     * Toxic Triangle (v1.3)
     * GoToLoop (2016-Aug-16)
     *
     * forum.Processing.org/two/discussion/17857/
     * toxiclibs-toxiclibssupport-pgraphics-
     * setgraphics-trying-to-use-off-screen-buffer#Item_8
     */
    
    import toxi.geom.Triangle2D;
    import toxi.geom.Vec2D;
    
    import toxi.processing.ToxiclibsSupport;
    ToxiclibsSupport gfx;
    PGraphics fr;
    
    import java.util.List;
    final List<Triangle> triangles = new ArrayList<Triangle>();
    
    static final color WHITE = -1, BLACK = 0;
    static final color RED = #FF0000, GREEN = #008000, BLUE = #0000FF;
    
    void setup() {
      size(800, 600);
      noLoop();
    
      gfx = new ToxiclibsSupport(this, fr = createGraphics(width, height));
    
      fr.beginDraw();
      fr.smooth(3);
      fr.strokeWeight(2.5);
      fr.endDraw();
    
      createTriangles();
      println(triangles);
    }
    
    void draw() {
      drawTriangles();
      background(fr);
    }
    
    void createTriangles() {
      final Vec2D v1 = new Vec2D(), v2 = new Vec2D(), v3 = new Vec2D();
    
      triangles.add(new Triangle(RED, false
        , v1.set(400, 100), v2.set(600, 150), v3.set(200, 300)));
    
      triangles.add(new Triangle(GREEN, true
        , v1.set(100, 100), v2.set(300, 150), v3.set(150, 300)));
    
      triangles.add(new Triangle(BLUE, true
        , v1.set(400, 550), v2.set(700, 350), v3.set(150, 350)));
    }
    
    void drawTriangles() {
      fr.beginDraw();
      fr.background(BLACK);
    
      for (final Triangle trig : triangles) {
        if (trig.on) {
          fr.noStroke();
          fr.fill(trig.c);
        } else {
          fr.noFill();
          fr.stroke(WHITE);
        }
    
        gfx.triangle(trig.trig);
      }
    
      fr.endDraw();
    }
    
    class Triangle {
      final Triangle2D trig;
      final color c;
      boolean on;
    
      @ SafeVarargs Triangle(color colour, boolean active, Vec2D... vecs) {
        c = colour;
        on = active;
        trig = new Triangle2D(vecs[0], vecs[1], vecs[2]);
      }
    
      @ Override String toString() {
        return trig.toString();
      }
    }
    
  • totally works now - all I had to do was prepend the fill/noFill, stroke/noStroke functions with the fr object. Your example also provides some other insights (as always) into efficient Java coding. Many thanks GoToLoop excellent :)

Sign In or Register to comment.