We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › embedding a PApplet in another PApplet
Page Index Toggle Pages: 1
embedding a PApplet in another PApplet (Read 741 times)
embedding a PApplet in another PApplet
Mar 23rd, 2009, 4:52pm
 
I wanted to ask if embeding a PApplet in another PApplet is possible. I know that PApplet can by included in a Swing application but I wanted to write a whole application from smaller processing apps.
The most basic code to demonstrate my problem:

//The main class which loads a child applet
TestP5 t = null;

void setup(){
 size( 800 , 600 );
 t = new TestP5();
 t.init();
}

void draw(){
 background( 0 );
 t.draw();   //i also tried without this lint => same result
}

//the child applet
class TestP5 extends PApplet{
 void setup(){
   size( 300 , 300 );
 }

 void draw(){
   background( 255 );
   fill( 0 );
   rect( 0 , 0 , 100 , 200 );
 }
}

The expected result would be a 800x800 window with a 300x300 black rectangle which contains another black rectangle... Both work separately but i can't 'glue' them together (thou i get no errors)
I'm obviously missing something but I can't figure out what it is. Please help me with that
Re: embedding a PApplet in another PApplet
Reply #1 - Mar 23rd, 2009, 4:57pm
 
Using "add(t);" will insert t into your primary applet.
For more details, see this link:

http://dev.processing.org/reference/core/javadoc/processing/core/PApplet.html

If you only need a second area that would be drawn to the same form, I would recommend using a PGraphics object instead.

http://processing.org/reference/PGraphics.html
Re: embedding a PApplet in another PApplet
Reply #2 - Mar 23rd, 2009, 5:15pm
 
Thank You for Your reply. Unfortunately it doesn't seem to work. frame is set no != null in draw method, so i modified my code to

//The main class which loads a child applet
TestP5 t = null;
boolean frameAdded = false;

void setup(){
 size( 800 , 600 );
 t = new TestP5();
 t.init();
}

void draw(){
 background( 0 );
 if( frame != null && frameAdded != true ){
   frame.add( t );
   frameAdded = true;
 }

 t.draw();  
}
//... rest of the code

...and the result is the same. If this doesn't work out I'll write an Swing app that contains a few PApplets that communicate with each other. I just wanted to avoid Swing.
Using PGraphics is not my case since I'm processing real time video in the other applet.
Re: embedding a PApplet in another PApplet
Reply #3 - Mar 23rd, 2009, 5:21pm
 
I'm sorry, I realized after that I was using "add(t);" and not "frame.add(t);".

Try this:

Code:


TestP5 t;

void setup() {
size(640,480);

setLayout(null);//allows placement anywhere

t = new TestP5();
t.init();
t.setBounds(40,40,300,300);
add(t);

frameRate(30);
}

void draw() {
background(0,64,0);
t.redraw();
}


class TestP5 extends PApplet {
TestP5() {
super();
}

void setup() {
size(300,300);
noLoop();
}

void draw() {
background(0);
rect(20,20,50,50);
}

}
Re: embedding a PApplet in another PApplet
Reply #4 - Mar 23rd, 2009, 5:23pm
 
Thank You very much! Works like a charm.
I wouldn't figure it out by myself.
Page Index Toggle Pages: 1