Bobby Getka Bobby Getka - 7 months ago 6
Java Question

Is it possible to draw a rectangle from inside a java class in processing?

I want to have a java object that I would call object.draw() from inside the main draw() method so that every time the processing sketch draws it draws the java object.

Answer

Yes.

You just need to pass the PApplet instance into the Java class, and then use that to do your drawing.

public class MyShape{
   PApplet papplet;

   public MyShape(PApplet papplet){
      this.papplet = papplet;
   }

   public void draw(){
      papplet.ellipse(50, 50, 25, 25);
   }
}

And then in your sketch, you would use the this keyword to pass the sketch into your object:

MyShape myShape;

void setup(){
   myShape = new myShape(this);
}

void draw(){
   myShape.draw();
}

More info can be found in the reference.