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.
IndexProcessing DevelopmentLibraries,  Tool Development › napplet: Embedded & windowed sub-sketches (v0.3.0)
Pages: 1 2 3 
napplet: Embedded & windowed sub-sketches (v0.3.0) (Read 14973 times)
Re: napplet: Embedded and windowed sub-sketches.
Reply #15 - May 17th, 2010, 4:36am
 
Any global variables you define in the main sketch should be accessible to the napplets.  I should make an example sketch to show this, but the process is simple enough: just declare variables in your main sketch and then modify them however you like in the napplets.

Here's a modified version of the last example sketch to show how this works:
Code:

import napplet.*;

NAppletManager nappletManager;
PFont mainFont, codeFont;

// These will be changed by the napplets:
int nicePressTotal = 0;
int meanPressTotal = 0;

String titleText = "NApplets can be created in their own windows.";

String button1Text =
"Click Here\nto Create a\nNice Window";

String button2Text =
"Click Here\nto Create a\nMean Window";

void setup() {
size(400, 200);
mainFont = loadFont("ArialMT-18.vlw");
codeFont = loadFont("CourierNewPS-BoldMT-14.vlw");
textMode(SCREEN);
textAlign(CENTER, CENTER);

nappletManager = new NAppletManager(this);
}

void draw() {
background(0);
stroke(255);
fill(100);

rect(width/12, height/4, width/3, height/2);
rect(7*width/12, height/4, width/3, height/2);

textFont(mainFont);
fill(255);
text(titleText, width/2, height/8);
text(button1Text, width/4, height/2);
text(button2Text, 3*width/4, height/2);

String s;
s = "Total clicks on\nnice windows: " + nicePressTotal;
text(s, width/4, 7*height/8);
s = "Total clicks on\nmean windows: " + meanPressTotal;
text(s, 3*width/4, 7*height/8);
}

void mousePressed() {
if (mouseY >= height/4 && mouseY <= 3*height/4) {
if (mouseX >= width/12 && mouseX <= 5*width/12) {
nappletManager.createWindowedNApplet("NiceWindow",
(int) random(100, 400), (int) random(100, 400));
}
else if (mouseX >= 7*width/12 && mouseX <= 11*width/12) {
nappletManager.createWindowedNApplet("MeanWindow",
(int) random(100, 400), (int) random(100, 400));
}
}
}

public class NiceWindow extends NApplet {

String sketchText =
"This window can be\n" +
"closed with the controls\n" +
"on the title bar, because\n" +
"nappletCloseable is true.\n" +
"(Or the NApplet will close\n" +
"itself if you hit the X key.)";

public void setup() {
size(250, 250);
nappletCloseable = true;
textMode(SCREEN);
textAlign(CENTER, CENTER);
}

public void draw() {
background(0, 100, 0);
stroke(255);
fill(255);

textFont(mainFont);
text(sketchText, width/2, height/2);
}

public void keyPressed() {
if (key=='x' || key=='X') exit();
}

public void mousePressed() {
nicePressTotal += 1;
}
}


public class MeanWindow extends NApplet {

String sketchText =
"This window can't be\n" +
"closed with the controls\n" +
"on the title bar, because\n" +
"nappletCloseable is false.\n" +
"(But the NApplet will close\n" +
"itself if you hit the X key.)";

public void setup() {
size(250, 250);
nappletCloseable = false; // Not actually necessary, false by default.
textMode(SCREEN);
textAlign(CENTER, CENTER);
}

public void draw() {
background(100, 0, 0);
stroke(255);
fill(255);

textFont(mainFont);
text(sketchText, width/2, height/2);
}

public void keyPressed() {
if (key=='x' || key=='X') exit();
}

public void mousePressed() {
meanPressTotal += 1;
}
}
Re: napplet: Embedded and windowed sub-sketches
Reply #16 - May 24th, 2010, 2:32pm
 
Version 0.30 available for download.  New features include napplet nesting, built in mousewheel support, alpha for napplet backgrounds, and other goodies.
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #17 - May 25th, 2010, 4:39am
 
There's always the same error. No Such Field Error..

using a library that's incompatible with your version of processing..

but I have downloaded the actual version.. Don't know what to do now.. could it be that the problem is with the computer? or what else should I try/change..

thanks
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #18 - May 25th, 2010, 6:11am
 
beginner_processing wrote on May 25th, 2010, 4:39am:
There's always the same error. No Such Field Error..

using a library that's incompatible with your version of processing..

but I have downloaded the actual version.. Don't know what to do now.. could it be that the problem is with the computer or what else should I try/change..

thanks

What's the "actual version"   What version number

Have you gotten the library to work before  You posted a "thank you" post earlier--was it working then  If so, what version was that

What's the exact error message you get

I'd like to help you, but your posts are so vague and incomplete that I can't even tell what problem you're running into exactly.
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #19 - May 25th, 2010, 6:55am
 
Smitty.. thanks for your answer. I worked it out. I just had to open the new processing before and open then the file with: "file, open".. maybe it's so vague because it's not my mother tongue.. I'm sorry for this.. but now it works. i think that's the main thing. Smiley

but now I've another question.. (sorry for asking so many things... Embarrassed)
I've programmed a little version on my own. I'm able to create napplets in a new and in the same window now. but what I can't understand -  and I've studied the files I've downloaded.. - how is it possible to link to another file.. In this example it's linked to "scrollbar" or "pattern" or something like this.. It is linked as if it was a public class but it isn't, it's another file.. so, how can I import extern files?  I think that would make more sense than a public class, or not?

but at least it's running now.. thank you all for your great help..
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #20 - May 25th, 2010, 8:39am
 
Glad you've got it working, and happy I was able to help!

If you want to put classes in separate files, the easiest way to do that is to use a separate tab for each file, so you would have one tab for your main sketch, and then an extra tab for each NApplet class you want to use.

Or is this not what you're talking about?
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #21 - May 25th, 2010, 8:51am
 
yes.. it is exactly what I'm talking about... but I don't know how to make these tabs.. how to link to these other files..
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #22 - May 25th, 2010, 9:02am
 
Click on the arrow on the right side of the Processing window like this:
...
Select "New Tab" and type in a name for the new tab.  It gets saved in the same folder with the main sketch file, and you can create a separate class in that tab, or more methods for your main sketch, or whatever you like.
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #23 - May 25th, 2010, 9:37am
 
oh.. I didn't know that this is possible Smiley never noticed this button.. thank you.. Smiley now I see a little bit better how it works..

I changed my program so that they are in tabs. but now it's the problem with the void setup()..

Duplicate method setup() in type "THE NAME OF MY FOLDER"..

I'm not really sure what the public in front of the void means.. So I've written it and I also let it be.. but anyway there's the same error.. I've looked at the napplet-example but there are also more than one void setup() in the files...

what's my mistake?
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #24 - May 25th, 2010, 9:43am
 
if it helps you, here the three tabs:

main tab:
Code:
import napplet.*;
NAppletManager nappletManager;

void setup() {  
 size(600, 600);  
 nappletManager = new NAppletManager(this);
 nappletManager.createNApplet("Menu", 0,0);
 nappletManager.createNApplet("Zeichnen", 0,100);
 
}


void draw()
{  background(0);
}



menu:
Code:
   
void setup() {    size(600, 100);  
}  
void draw() {  
 background(0, 100, 0);  
 stroke(255);    fill(255);  
}  


zeichnen: (a bit long, not really important, didn't know whether "void draw()" or "public void draw()")

Code:


 
float rotx=-.02, roty=-.04;
int achl=600;
int a=100, offx=0, offy=0,offz=0;
int transx, transy, transz=-300;
int lastWidth, lastHeight;
int pfeil=6;

void setup() {    size(600, 500,P3D);  
stroke(0);  
strokeWeight(1);  
strokeJoin(MITER);  
transx=width/2;
transy=height/2;
}  


void draw() {  
 background(255);
 pushMatrix();
translate(transx,transy,transz);  
rotateX(rotx);  
rotateY(roty);  
koordinatensystem();  
popMatrix();
}  


 void koordinatensystem(){  
strokeWeight(1.5);  
beginShape(LINES);
 stroke(255,0,0); //z-achse rot  
 vertex(0,0,-achl);  
 vertex(0,0,achl);
 
vertex(0,0,-achl);  // Pfeil 1
vertex(pfeil,0,-achl+pfeil);
vertex(0,0,-achl);
vertex(-pfeil,0,-achl+pfeil);

vertex(0,0,achl); //Pfeil 2
vertex(pfeil,0,achl-pfeil);
vertex(0,0,achl);
vertex(-pfeil,0,achl-pfeil);

 stroke(0,255,0);  //y-achse grün
 vertex(0,-achl,0);  
 vertex(0,achl,0);  
    vertex(0,achl,0); // Pfeil 1
    vertex(0,achl-pfeil,pfeil);
    vertex(0,achl,0);
    vertex(0,achl-pfeil,-pfeil);
 
    vertex(0,-achl,0); // Pfeil 2
    vertex(pfeil,-achl+pfeil,0);
    vertex(0,-achl,0);
    vertex(-pfeil,-achl+pfeil,0);
   
   
   
 stroke(0,0,255);  //x-achse blau
 vertex(-achl,0,0);  
 vertex(achl,0,0);  
 
 vertex(-achl,0,0); // Pfeil 1
 vertex(-achl+pfeil,0,pfeil);  
  vertex(-achl,0,0);
 vertex(-achl+pfeil,0,-pfeil);  
 
 vertex(achl,0,0); // Pfeil 2
  vertex(achl-pfeil,0,pfeil);
   vertex(achl,0,0);
  vertex(achl-pfeil,0,-pfeil);
endShape();
}

void mouseDragged() {  float factor = 0.01;  
if (key==CODED){ // Alt + Maus --> Rotation  
if(keyCode==ALT){    
 rotx += (pmouseY-mouseY) * factor;
roty += (mouseX-pmouseX) * factor;  

}    

if(keyCode==CONTROL){ // Ctrl + Maus --> Translation  
transx+=(mouseX-pmouseX); transy+=(mouseY-pmouseY);    }  }}

void keyPressed(){  
 if(key!=CODED){    
if (key=='1'){
 rotx=0;
 roty=0;    
 transx=width/2;
 transy=height/2;
 transz=-300;    
}    

if (key=='3'){
 rotx=0;
 roty=radians(-90);
 transx=width/2;
 transy=height/2;
 transz=-300;    
}    

if (key=='7'){
 rotx=radians(-90);
 roty=0;
 transx=width/2;
 transy=height/2;    
 transz=-300;    
}    
}  
   
 if (key == CODED) {    
   if(keyCode==CONTROL){
if(key=='1'){  
 rotx=0;  
 roty=PI;
}
   
if(key=='3'){  
 rotx=0;  
 roty=radians(90);
}
   
if(key=='7'){  
 rotx=radians(90);  
 roty=0;
}    
   }  
 }




}
}



And I've also tried (when I had only one tab with other classes) to place a button (controlp5) in one napplet and to do something in the second napplet if the button is pressed.. but it also doesn't work.. don't know what I forgot.. thanks for your help..
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #25 - May 25th, 2010, 9:50am
 
You need one setup() method for the main sketch, and one setup() method for each NApplet subclass.  The setup() methods for the NApplets must be inside the NApplet declaration.

To make it easier to remember, each tab acts like it's part of the main tab.  What this means is that if you want to create a NApplet in a tab, you still need to wrap the NApplet with a declaration, just like if you were typing it in the main tab.  For example, you might have your main tab like this:
Code:
import napplet.*;

NAppletManager n;

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

n = new NAppletManager(this);
n.createEmbeddedNApplet("MyNApplet", 50, 50);
}

void draw() {
background(255);
}

And then have a separate tab called "MyNApplet" like this:
Code:
public class MyNApplet extends NApplet {

public void setup() {
// NApplet setup code in here.
}

public void draw() {
// NApplet draw code in here.
}
}


This should work just the same as it would if you put it all in one big tab.

As for the public keyword, it's not what's important here.  I think you need to have it on the NApplet class and on the NApplet's draw() and setup() methods, but maybe you don't.  (I don't have time to test this right now, but you can.)

On the methods for the main applet (like the main setup() and draw() methods) you can make them public or not, and it will work either way.


Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #26 - May 25th, 2010, 9:52am
 
Ah yes, so for example, your menu tab should read:
Code:

public class Menu extends NApplet {

 void setup() {    size(600, 100);  
 }  

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

}

If that doesn't work, try "public void setup()" etc.

EDIT:  And I'm not sure about how to get controlP5 working, but one thing at a time. Wink
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #27 - May 25th, 2010, 12:51pm
 
now it works...


but... (it couldn't be different Wink ) ..

Code:
controlP5 = new ControlP5(this);
controlP5.addToggle("toggle",false,5,10,50,20);
}

void toggle(boolean but1) {
if(but1==true) {

parentPApplet.background(255);
} else {
button1=false;
}
}


here I have a part of a sub-tab. There I have a controlP5 button and if I press it, I set the main background on 255 (parentPApplet.background...) this works.. but in the else-case I work with "button1" (boolean).. that is set false and in the main tab:

Code:
if(button1==false) {
background(25);
rect(10,10,20,20);
}


this code.. It works too, so I can't be my fault.. but it doesn't work immediately but when I have resized the window... why?? I think that's not a problem of controlP5 but of napplet..

Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #28 - May 25th, 2010, 4:40pm
 
Well, that does sound odd.  Let me make a suggest to narrow down the possibilities:  Instead of using the button1 boolean only in the false case, try using it in both cases, so try something like:

In your control tab, change the toggle method to:
Code:
void toggle(boolean but1) {
button1 = but1;
}


And then in the main tab, something like this:
Code:

void draw() {
if (button1==true) {
background(255);
} else {
background(25);
}

// .....
}

This is more consistent, because it means you'll always be setting the background at the beginning of each draw().

Let me know if that works, and if you're still seeing the bug I'll look deeper into it.
Re: napplet: Embedded & windowed sub-sketches (v0.3.0)
Reply #29 - May 26th, 2010, 2:28am
 
I tried it like this yesterday.. but that doesn't seem to be the problem. the problem is, that only with parentPapplet it works.. otherwise you have to resize the window first.. so is there a parentPapplet-function for everything.. I know that you can use parentPapplet.background or .height, .width but could I also use it to set a function true or  something like this...
So I could take a parentPapplet function to make the things work (but how to use it to set a function true or false?)or there has to be another option how the (boolean)-variable of one napplet can influence the other napplet IMMEDIATELY and not after resizing the window..
(by the way: I know that it's "more beautiful" with the same variables and so on, this example that I've posted I've written like this to show in one code the both ways I've tried it .. )
Pages: 1 2 3