Redraw PGraphics object

PerPer
edited March 2017 in Library Questions

I got this simple voronoi sketch below. I can draw this Voronoi into an graphics object with the getGraphics() method in the ToxiclibsSupport class. But when i press the mouse and are updating the voronoi only the background updates. Not the PGraphics object.

How do I get the PGraphics-object to redraw?

import toxi.geom.*;
import toxi.geom.mesh2d.*;
import toxi.processing.*;

ToxiclibsSupport gfx;
Voronoi v;
PGraphics pg;

void setup() {
  size( 500, 500 );
  gfx = new ToxiclibsSupport( this );
  pg = createGraphics(width, height);

  drawVoronoi();
}

void drawVoronoi() {
  v = new Voronoi();
  for ( int i = 0; i < 150; i++ ) {
    v.addPoint( new Vec2D( random(width), random(height) ) );
  }

  for (int i = 0; i < v.getRegions().size(); i++) {
    Polygon2D p = v.getRegions().get(i);
    fill(random(255));
    gfx.polygon2D(p);
  }
}

void draw() {
  pg = gfx.getGraphics();
  image(pg, mouseX, mouseY, 200, 200);
}

void mousePressed() {
  drawVoronoi();
}

Answers

  • edited March 2017

    @Per -- Your ToxiclibsSupport object is being created with the default constructor ( this ) -- that means it draws straight to the sketch surface. Instead, pass this and your PGraphics target:

    In addition to providing new drawing commands, this class provides wrappers for using datatypes of the toxiclibs core package directly with Processing's drawing commands. The class can be configured to work with any PGraphics instance (incl. offscreen buffers). http://toxiclibs.org/docs/p5/toxi/processing/ToxiclibsSupport.html

    So:

    1. create your ToxiclibsSupport with the PGraphics object that you want to modify

      gfx = new ToxiclibsSupport( this, pg );
      
    2. wrap your drawVoronoi() in beginDraw()/endDraw() since it is now a PGraphics update routine

    3. call fill() as pg.fill()
    4. use background(pg) and image(pg) in draw. Notice that pg = gfx.getGraphics() is not necessary -- you are already drawing to pg with your gfx.polygon2D calls.

    Like this:

    import toxi.geom.*;
    import toxi.geom.mesh2d.*;
    import toxi.processing.*;
    
    ToxiclibsSupport gfx;
    Voronoi v;
    PGraphics pg;
    
    void setup() {
      size( 500, 500 );
      pg = createGraphics(width, height);
      gfx = new ToxiclibsSupport( this, pg );
      drawVoronoi();
    }
    
    void drawVoronoi() {
      pg.beginDraw();
      v = new Voronoi();
      for ( int i = 0; i < 150; i++ ) {
        v.addPoint( new Vec2D( random(width), random(height) ) );
      }
    
      for (int i = 0; i < v.getRegions().size(); i++) {
        Polygon2D p = v.getRegions().get(i);
        pg.fill(random(255));
        gfx.polygon2D(p);
      }
      pg.endDraw();
    }
    
    void draw() {
      background(pg);
      image(pg, mouseX, mouseY, 200, 200);
    }
    
    void mousePressed() {
      drawVoronoi();
    }
    

    You could also create multiple ToxiclibsSupport objects for mapping to different PGraphics layers, but that isn't necessary in this case.

    VoronoiPGraphics--screenshot

Sign In or Register to comment.