accessing a layer’s methods

Store Forums Processing Layers General Discussion accessing a layer’s methods

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #548
    plutovman
    Member

    Hello,
    I’m a big fan of your processing.org layers library. Thank you.

    I have a sketch with a couple of layers, and I can’t seem to figure out how to access a layer’s methods.

    So, for instance, I have a layer definition

    public class L_PolyMesh extends Layer {

    void setup();{…}
    void draw(){…}
    void drawPolygons(){…}

    }

    and in the main program, I create an instance of the layer

    void setup() {
    layers = new AppletLayers(this);
    L_PolyMesh canvas_mesh = new L_PolyMesh (this);
    layers.addLayer(canvas_mesh);
    l_mesh = layers.getLayer(0);
    }

    My problem is that when I try to do:

    l_mesh.drawPolygons();

    I get a warning that says “The function drawPolygons does not exist”. I’m sure I’m missing something, but I can’t figure out what that is. Your help would be appreciated.

    Thanks!

    #1140
    Michael
    Keymaster

    How is the variable “l_mesh” declared? Is it declared as type Layer?

    If so, then you need to cast it to the type L_PolyMesh:

    ((L_PolyMesh)l_mesh).drawPolygons(); 

    That is, you have to tell the compiler that your object is not only a Layer, but is specifically a L_PolyMesh object.

    Alternatively, you can declare l_mesh as type L_PolyMesh. Hope that makes sense.

    #1141
    plutovman
    Member

    Sneaky. That worked. My knowledge of OOP is a bit hazy, but I’m trying. Thanks for your help.
    If I may ask another question, can you explain what this bit of code does to make the layers render?

    void paint(java.awt.Graphics g) {
    // This method MUST be present in your sketch for layers to be rendered!
    if (layers != null) {
    layers.paint(this);
    } else {
    super.paint(g);
    }
    } // end of paint()

    Specifically what is ‘g’? I’ve seen in some old bits of code that inside layers definitions, I’ll see lines like:

    img = g.get()
    g.save(someimage)

    Thanks again for your help.

    #1142
    Michael
    Keymaster

    The variable ‘g’ is a reference to the Processing PGraphics object which performs the drawing in Processing.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.