Processing Test with the PGraphics

To simplify the use of a dynamic mask with image, I try to use the PGraphics class as an off screen buffer to store the image for a subsequent mask operation. The foreground image is the live video input from the webcam. The mouse drag operation will draw a dynamic mask to reveal the webcam image. It makes use of the fact that the PGraphics class is a subclass of PImage. The mask function can directly take the PGraphics instance as input. Here is a sample screen shot.
 

import processing.video.*;
 
PGraphics pg;
Capture cap;
 
void setup() {
  size(640, 480);
 
  cap = new Capture(this, width, height);
  cap.start();
 
  pg = createGraphics(width, height);
  pg.beginDraw();
  pg.background(0);
  pg.smooth();
  pg.noStroke();
  pg.fill(255);
  pg.endDraw();
}
 
void draw() {
  if (!cap.available()) 
    return;
  cap.read();
  background(0);
  pg.beginDraw();
  if (mousePressed) 
    pg.ellipse(mouseX, mouseY, 40, 40);
  pg.endDraw();
  cap.mask(pg);
  image(cap, 0, 0);
}