We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am using Proclipsing in Eclipse and i have some trouble getting the correct parent for my class.
First i am drawing a rectangle (A) with the parent to PApplet . The rectangle (A) should be the parent of the rectangle (B) but the second rectangle isn't showing with my code. I am also not able to correctly register the "draw" method in my code.
import processing.core.PApplet;
public class Test extends PApplet {
public void settings() {
fullScreen();
}
public void setup() {
background(21);
clRect rectA= new clRect(this);
rectA.left = 50;
rectA.top = 50;
rectA.width = 500;
rectA.height = 500;
clRect rectB= new clRect(rectA);
rectB.left = 20;
rectB.top = 20;
rectB.width = 100;
rectB.height = 100;
}
public void draw() {
background(21);
}
public static void main(String _args[]) {
PApplet.main(new String[] { test.Test.class.getName() });
}
}
I am extending the rectangle class from another class which is supposed to be a controller class for multiple different classes.
import processing.core.PApplet;
public class clControl extends PApplet
{
public PApplet parent; //The parent object referred to
public float left;
public float top;
public float width;
public float height;
public clControl(PApplet parent)
{
this.parent = parent;
}
}
Finally this is the rectangle class which is causing problems for me. The first rectangle is drawn with no problem at all. The second rectangle is not able to register the "draw" method and drawing the rectangle is also a problem, it is not showing.
import processing.core.PApplet;
public class clRect extends clControl
{
public clRect(PApplet parent)
{
super(parent);
this.left = 0;
this.top = 0;
this.width = 100;
this.height = 200;
this.parent.registerMethod("draw", this);
}
public void draw()
{
this.parent.rect(this.left, this.top, this.width, this.height);
}
}
How to get this running? :(
Answers
Only one of your classes should extend PApplet. Why does clControl (which should be ClControl, btw) extend PApplet?
In order to use registerMethod() we gotta pass the PApplet's reference which owns the "canvas" we wish to "draw" into.
At statement
clRect rectB = new clRect(rectA);
, you're passing a PApplet which got no "canvas".That is, even though class clRect inherits from PApplet via clControl, it is "canvas"-less nonetheless!
In order to "ignite" some PApplet and make it "alive" w/ a "canvas" & an "Animation" Thread, we need to issue runSketch() passing the still "dead" PApplet as its argument.
Thank you for your comment KevinWorkman.
You are right, only one class should extend PApplet, i fixed this.
So how do i pass the parent so that rectangle (A) is the parent of rectangle (B)?
Oh hey GoToLoop
The runSketch() method is not visible for me :(
May you can show me any snippet or even better adjust the example code above?
Thank you very much for your great reply :)
For runSketch(), you can see many examples of it in this tag link:
http://forum.Processing.org/two/discussions/tagged?Tag=papplet.runsketch()
However, its purpose is to instantiate a PApplet w/ its own canvas.
That is, to have multiple windows. Each 1 being a sketch running at the same time as the others.
As I understand, in your case you still wanna "draw" to the same "canvas" rather than creating another 1.
Therefore, there's no reason to extend any of your classes w/ PApplet at all.
Rather, just use composition inheritance. Then ask the main sketch to pass its
this
to those.This class should do want you want and you can create a rectangle in setup with something like this.
new ClRect (this,10,10,80,60);