PGraphics pg; //define a buffer int dim = 200; //screen dimension void setup() { size(dim, dim); //size up the screen background(102); //set the background pg = createGraphics(dim, dim, P3D); //create a buffer } void draw() { image(pg, 0, 0); //draw the buffer } int x1,y1,x2,y2; //coordinates of a line void mouseDragged(){ pg.beginDraw(); //start writing to the buffer pg.background(102); pg.stroke(255); for(int i=0; i<1000; i++){ //draw 1000 lines x1 = int(random(dim)); y1 = int(random(dim)); x2 = int(random(dim)); y2 = int(random(dim)); pg.line(x1, y1, x2, y2); } pg.endDraw(); //end writing to the buffer }