accessing a layer's methods

General discussion about the Processing Layers library.

accessing a layer's methods

Postby plutovman » Sun Oct 09, 2011 2:10 pm

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!
plutovman
Newbie
Newbie
 
Posts: 8
Joined: Wed May 26, 2010 2:00 am

Re: accessing a layer's methods

Postby Michael » Sun Oct 09, 2011 2:18 pm

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:

Code: Select all
((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.
Michael
Administrator
Administrator
 
Posts: 460
Joined: Sat May 29, 2010 8:24 am

Re: accessing a layer's methods

Postby plutovman » Mon Oct 10, 2011 2:48 am

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.
plutovman
Newbie
Newbie
 
Posts: 8
Joined: Wed May 26, 2010 2:00 am

Re: accessing a layer's methods

Postby Michael » Mon Oct 10, 2011 2:45 pm

The variable 'g' is a reference to the Processing PGraphics object which performs the drawing in Processing.
Michael
Administrator
Administrator
 
Posts: 460
Joined: Sat May 29, 2010 8:24 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests