0

I'm just learning Processing. My background is in ActionScript. I'm creating a custom class which draws complex objects. As part of this class, I call createShape() a couple of times. Things worked fine until I decided I needed the custom class to extend PShape. Now it will not recognize the createShape() syntax I'm using, and returns an error that says "createShape() expects parameters like: createShape(PApplet, PShape)"

Here's the function, which returns a PShape object. It works fine.

    class MyClass
    {
      ...

      PShape makeTriangle1( float w, color c )
      {
        strokeJoin(BEVEL);
        PShape t = createShape(TRIANGLE, 0, 0, w, 0, 0, topBar);
        t.setFill(c);
        return t;
      }
    }

But when I do this, it returns the error I've quoted above:

    class MyClass extends PShape
    {
      ...

      PShape makeTriangle1( float w, color c )
      {
        strokeJoin(BEVEL);
        PShape t = createShape(TRIANGLE, 0, 0, w, 0, 0, topBar);
        t.setFill(c);
        return t;
      }
    }

Any ideas? Thanks!

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Steve Bird
  • 67
  • 1
  • 8

1 Answers1

2

The PShape class has a function named createShape(). You can view it on line 1441 here:

static protected PShape createShape(PApplet parent, PShape src) {
    PShape dest = null;
    if (src.family == GROUP) {
      dest = parent.createShape(GROUP);
      PShape.copyGroup(parent, src, dest);
    } else if (src.family == PRIMITIVE) {
      dest = parent.createShape(src.kind, src.params);
      PShape.copyPrimitive(src, dest);
    } else if (src.family == GEOMETRY) {
      dest = parent.createShape(src.kind);
      PShape.copyGeometry(src, dest);
    } else if (src.family == PATH) {
      dest = parent.createShape(PATH);
      PShape.copyPath(src, dest);
    }
    dest.setName(src.name);
    return dest;
  }

Notice the arguments for that function are a PApplet and a PShape.

When you extend PShape and call createShape() inside that class, Processing thinks you're trying to call this function. Since your arguments don't match up, you get the error.

To fix this error, you could pass your sketch PApplet into the MyClass class and specifically call the sketch-level createShape() function:

int topBar = 100;

MyClass mc;

void setup() {
  size(500, 500);
  mc = new MyClass(this);
}

void draw() {
  background(0);
  shape(mc.makeTriangle1(100, #ff0000), 200, 200);
}


class MyClass extends PShape{

  PApplet sketch;

  public MyClass(PApplet sketch){
    this.sketch = sketch;
  }

  PShape makeTriangle1( float w, color c )
  {
    strokeJoin(BEVEL);
    PShape t = sketch.createShape(TRIANGLE, 0, 0, w, 0, 0, topBar);
    t.setFill(c);
    return t;
  }
}

However, I would strongly suggest that you refactor your code so that you don't need to extend PShape anymore. It seems very strange to extend a Processing class like that. Chances are you should be using composition instead of inheritance. In other words, your class has-a PShape, not your class is-a PShape.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Thank you. I'm approaching this the way that I would do in ActionScript, as that is my main frame of reference. It seems that's probably the root of my problem. – Steve Bird Feb 10 '16 at 20:53