How do I create a class which would contain a PGraphics property

edited March 2017 in Questions about Code

I would like every drawn shape to be an object which I can interact with later to change the fill, stroke etc. I would also create an ArrayList so I can sort the layers by sorting the arrayList.

I've been told to create a class for the drawn objects which would contain a PGraphics property, and also a draw method. While dragging the mouse, I can clear the latest Shape's PGraphic and draw it again.

I just have no idea how!

PS: I will implement other shapes the user can draw but they too need to be objects so I can manipulate them

Here is my code:

ArrayList<PGraphics> layer = new ArrayList();
float startX, startY;

void setup() {
  size(500, 500);
  cursor(CROSS);
  background(255);
  smooth();
}

void draw() {
  background(255);
  for(PGraphics pg : layer) {
    image(pg, 0, 0);
  }
}
void mousePressed() {
  startX = mouseX;
  startY = mouseY;
  PGraphics pg = createGraphics(width, height);
  layer.add(pg);
}

void mouseDragged() {
  // get most recent layer
  PGraphics pg = layer.get(layer.size() - 1);
  shapeLayer.beginDraw();
  shapeLayer.clear();
  shapeLayer.rectMode(CORNERS);
  shapeLayer.rect(startX, startY, mouseX, mouseY);
  shapeLayer.endDraw();
}

Answers

Sign In or Register to comment.