We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
@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:
So:
create your ToxiclibsSupport with the PGraphics object that you want to modify
wrap your
drawVoronoi()
inbeginDraw()
/endDraw()
since it is now a PGraphics update routinefill()
aspg.fill()
background(pg)
andimage(pg)
in draw. Notice thatpg = gfx.getGraphics()
is not necessary -- you are already drawing to pg with yourgfx.polygon2D
calls.Like this:
You could also create multiple ToxiclibsSupport objects for mapping to different PGraphics layers, but that isn't necessary in this case.