Reputation: 191
So if I have a class
class A extends PApplet{
float x;
float y;
public A(float x, float y){
this.x = x;
this.y = y;
}
public void draw(){
ellipse(x,y,10,10);
}
}
and another class
class B extends PApplet{
A a = new A(12,19);
public void draw(){
ellipse(25,25,25,25);
}
}
only the ellipse in B
will be drawn. Is this an issue with having two draw methods? If not, what is the issue? Is there a way to do this kind of thing so that both the ellipses show up? Is this possibly an issue to do with threads? I've read a bit about them but never used them, so if so, please explain threads a bit too.
Upvotes: 1
Views: 1959
Reputation: 42174
Think about it this way: only one sketch can be you "main" sketch. That's the sketch whose draw()
function is called. Processing calls this function automatically. If you're using something like eclipse, you specify your main sketch when you call the main()
method.
All of that being said, the easiest way to do this might be to simply call A.draw()
from B.draw()
.
If you do that, your A
class shouldn't extend PApplet
. Just pass in the B PApplet
and use that instead. Putting it all together:
class A{
float x;
float y;
PApplet myPApplet;
public A(float x, float y, PApplet myPApplet){
this.x = x;
this.y = y;
this.myPApplet = myPApplet;
}
public void draw(){
myPApplet.ellipse(x,y,10,10);
}
}
class B extends PApplet{
A a = new A(12,19, this);
public void draw(){
ellipse(25,25,25,25);
a.draw();
}
}
Think of each PApplet
as a separate window. If you want one window, then you should only have one PApplet
.
Upvotes: 2
Reputation: 300
If you want to use threads, you can use the Thread
class. Threads are like different process in the same program. But, they can share the same memory - meaning that they can both use the same variables (Unlike processes which don't share memory). Then you have thr whole issue of how to use threads correctly, you can see more on: here
Here is a simple example of how to use a thread:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
//your code
}
});
t.run();//don't forget to run!
Upvotes: 0