call function in multiple window (G4P)

RazRaz
edited March 2016 in Library Questions

ok, I know I talk a lot and I make no sense in my questions, but I need last question and I will try to be more clear. so, I've created a code which has 2 windows (multiple window) that I made with G4P library. In addition, I've tried to convert string to a code (it can be a method or whatever). And now here is my question: how Can I make the code which is converted from strings appear in the second window? For example: I write the string: "rect(100,100,100,100);" and I convert it to method (like usual), and I want this rect will be drawn on the multiple window. Here is my code (part of it):

Answers

  • RazRaz
    edited March 2016
     import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
     
    import g4p_controls.*;
    
    final ScriptEngine js = new ScriptEngineManager().getEngineByName("js");
     
    
    GTextArea txa1;
    GButton start;
    GWindow window;
    
    void setup() {
      size(800, 600);
      smooth(4);
     
      js.put("p", this); // puts sketch's PApplet as p into JS.
     
      js.put("w", width);  // puts width's  current value as w into JS.
      js.put("h", height); // puts height's current value as h into JS.
     
       start=new GButton(this,20,20,50,60,"Show");
      
      
      txa1 = new GTextArea(this, width/2-100, height/2-80, 200, 160);
      txa1.tag = "Raz";
      txa1.setPromptText("Text area 1");
       
    }
     
    void draw() {
    }
     
    static final String evalP5(final ScriptEngine js, final String... phrases) {
      for (int i = 0; i < phrases.length; phrases[i] = "p." + phrases[i++]);
      return evalJS(js, phrases);
    }
     
    static final String evalJS(final ScriptEngine js, final String... phrases) {
      final String expression = join(phrases, ENTER);
     
      try {
        js.eval(expression);
      }
      catch (final ScriptException cause) {
        System.err.println(expression);
        throw new RuntimeException(cause);
      }
     
      return expression;
    }
    public void handleButtonEvents(GButton button,GEvent event){
     if(event == GEvent.CLICKED){
       button.setEnabled(false);
       createwindow();
       ShowText();
     }
    }
    void createwindow(){
     window=GWindow.getWindow(this,"window",500,500,500,500,JAVA2D);
    }
    void ShowText(){
     String[] lines = txa1.getTextAsArray();
     println(evalP5( js, "background(0)", lines[0]),ENTER);
    }
    void windowDraw(PApplet app){
    //here is the code that I need
    }
    
  • edited March 2016

    I guess you're gonna need to put() another global variable into JS in order to represent your GWindow:

    void createwindow() {
      window = GWindow.getWindow(this, "window", 500, 500, 500, 500, JAVA2D);
      js.put("q", window); // puts GWindow as q into JS.
    }
    

    And create another customized eval() for it:

    static final String evalGW(final ScriptEngine js, final String... statements) {
      for (int i = 0; i < statements.length; statements[i] = "q." + statements[i++]);
      return evalJS(js, statements);
    }
    

    WARNING: Not tested yet! (:|

  • Please do not post the same question twice. I have deleted the duplicate.

  • After creating the window you must add a draw handler so after line 61 add

    window.addDrawHandler(this, "windowDraw");

    then modify your last function to

    void windowDraw(PApplet app, GWinData data){
      app.background (0);
      //here is the code that I need
    }
    

    This should get you started :)

  • You might also look at this discussion for background info on multiple websites. Aloso my website gives more info.

  • RazRaz
    edited March 2016

    Thank you both! I have 3 more questions: how do I eval strings to a function? Like:

    void setup(){
    size(200,200);//this will control the size of the second window
    background(0);//this will control the background of it
    }
    void draw(){
    fill(255);
    rect(100,100,100,100);
    }
    

    How do I eval the setup and draw functions? That my question

    And the second question-how can I eval the string "size(200,200)" for example and it will be control the size of the window?

    And the last-when I wrote on the text area rect(100,100,100,100). And its works. But when I use mouseX,mouseY it doesn't work :( so how can I do it will work?

  • edited March 2016

    Processing doesn't allow us to re-issue size() unfortunately.
    Most we can do is use the undocumented PSurface::setSize() method.
    And we can call the undocumented function getSurface() in order to grab sketch's PSurface reference:
    getSurface().setSize(300, 200);

  • Thank you! And how I can do with the setup and draw functions? (What I asked in the last comment)

  • edited March 2016

    It's not easy to fully reply those questions b/c it demands that you understand how the PDE (Processing's IDE) and its pre-processor hides/abstracts many things from us.
    A sketch is basically the class PApplet methods modifying the canvas, which is a PGraphics object.

    When we issue something like background() or rect(), we're actually modifying a PGraphics object, which at the end of draw() has its content finally rendered on sketch's PSurface.

    The ScriptEngine is a clean slate, knowing nothing about PApplet or its PGraphics "canvas".
    We need to transfer to ScriptEngine everything we intend to be accessed from it.

    Most important thing is our sketch's PApplet reference: js.put("p", this);
    The statement above creates a global variable called p in JS and initialize it w/ sketch's this, which is a PApplet reference.

    That's exactly what we do w/ many other Processing's library. We pass this to them too.
    So they can also access sketch's canvas and other Processing's API.

    Since variable p represents our sketch, we can finally use its members from JS.
    For example, we can issue p.background(0) in order to fill sketch's canvas w/ black color from JS' ScriptEngine.

  • RazRaz
    edited March 2016

    I worte void setup(){ on line (in the text area after I compiled) and it showed me that try1=0. why? :((

    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
    
    import g4p_controls.*;
    
    final ScriptEngine js = new ScriptEngineManager().getEngineByName("js");
    
    int try1; 
    
    GTextArea txa1;
    GButton start;
    GWindow window;
    
    void setup() {
      size(800, 600);
      smooth(4);
    
      js.put("p", this); // puts sketch's PApplet as p into JS.
    
      js.put("w", width);  // puts width's  current value as w into JS.
      js.put("h", height); // puts height's current value as h into JS.
    
       start=new GButton(this,20,20,50,60,"Show");
    
    
      txa1 = new GTextArea(this, width/2-100, height/2-80, 200, 160);
      txa1.tag = "Raz";
      txa1.setPromptText("Text area 1");
    
    }
    
    void draw() {
    }
    static final String evalGW(final ScriptEngine js, final String... statements) {
      for (int i = 0; i < statements.length; statements[i] = "q." + statements[i++]);
      return evalJS(js, statements);
    }
    
    static final String evalP5(final ScriptEngine js, final String... phrases) {
      for (int i = 0; i < phrases.length; phrases[i] = "p." + phrases[i++]);
      return evalJS(js, phrases);
    }
    
    static final String evalJS(final ScriptEngine js, final String... phrases) {
      final String expression = join(phrases, ENTER);
    
      try {
        js.eval(expression);
      }
      catch (final ScriptException cause) {
        System.err.println(expression);
        throw new RuntimeException(cause);
      }
    
      return expression;
    }
    public void handleButtonEvents(GButton button,GEvent event){
     if(event == GEvent.CLICKED){
       button.setEnabled(false);
       createwindow();
       ShowText();
     }
    }
    void createwindow(){
     window=GWindow.getWindow(this,"window",500,500,500,500,JAVA2D);
     js.put("q", window); // puts GWindow as q into JS.
     window.addDrawHandler(this, "windowDraw");
    }
    
    void windowDraw(PApplet app,GWinData data){
     String lines[]= txa1.getTextAsArray();
     for(int i=0; i<lines.length; i++){
       if(lines[i].equals("void setup(){")==true)
         try1=i;
       println("setup"+try1);
     }
    }
    
  • Please format your code.

  • I tried but look it didn't work

  • I fixed your code. Make sure you have a blank line before and after the code, then select the code and hit Ctrl+O

    try1 = 0 because it matches the first element in the array and tha is index value 0 i.e. i == 0 the first

  • so how can I do that try1 will be the value of the line number?

  • Change line 75 to

    try1 = i + 1;

  • but quark, it doesn't work if it is in line 2 or 3. it shows that try1=0; :(

  • I used

    void windowDraw(PApplet app,GWinData data){
     String lines[]= txa1.getTextAsArray();
     for(int i=0; i<lines.length; i++){
       if(lines[i].equals("void setup(){")==true)
         try1=i + 1;
       println("setup"+try1);
     }
    }
    

    I put void setup(){ in the fourth line and I got the following output

    setup4
    setup4
    setup4
    setup4
    setup4
    setup4
    setup4
    setup4
    setup4
    

    for ever. So it worked for me

  • RazRaz
    edited March 2016

    ok thanks! now I have last question (I think :-\" ) I added this to the function

    <

    pre> void windowDraw(PApplet app,GWinData data){ String lines[]= txa1.getTextAsArray(); for(int i=0; i<lines.length; i++){ if(lines[i].equals("void setup(){")==true) try1=i + 1; if(lines[i].equals("}")==true) try2=i + 1; println("setup"+try1); println("closeSet"+try2); } } and I wrote it on the text box and it showed that now try2 is 0! why? :( (please add a global int "try2" if you want to add this code).

    someone: please edit and format my code because my computer decided to do problems about formating the code. X( thank you :)

Sign In or Register to comment.