Multiple Windows (Multiple Applet) Resizing

edited July 2016 in How To...

Hi, i´m trying to use this surface.setSize(img.width, img.height); but from antother surface, how i can resize the main surface from a class created surface. this is my code

ControlWindow CWindow;
Button OpenImage;
Button SaveFile;
PImage img;

void settings() {
  size(320, 240);
  smooth();
  noLoop();
}

void setup() { //This is the one y wanna to resize from the Control Window
  surface.setTitle("Output");
  surface.setResizable(true);
  CWindow = new ControlWindow();
}

void draw() {
  background(250);
  if(img!=null){
    image(img, 0, 0);  
  }
}

class ControlWindow extends PApplet {

  public ControlWindow() {
    super();
    PApplet.runSketch(new String[]{this.getClass().getName()}, this);
  }

  public void settings() {
    size(400, 400);
    OpenImage = new Button(this,150, 70,50,30,"Nuevo", GREEN, RED );
    SaveFile = new Button(this,150, 140,65,30,"Guardar", YELLOW, RED );
    //smooth();
    noLoop();
  }
  public void setup() { 
    surface.setTitle("Control Window");
    surface.setResizable(true);

  }

  public void draw() {
    background(250);
    OpenImage.SetButton();
    SaveFile.SetButton();
  }
  int bana=0;
  public void mousePressed() {
    if(OpenImage.ButtonPressed()){
      String Path = FileChooser("Imagenes", "Open File", new String[] {
    "jpg", "bmp", "png"}); 
    println(Path);
    img= loadImage(Path);
    //here is where i need to set the new size SOMETHING.setSize(img.width, img.height);        
    }
    if(SaveFile.ButtonPressed()){

    }
  }

  public void mouseDragged() {

  }
}

Answers

  •   protected PApplet getEnclosingPApplet() {
        try {
          return (PApplet) getClass().getDeclaredField("this$0").get(this);
        }
    
        catch (ReflectiveOperationException cause) {
          throw new RuntimeException(cause);
        }
      }
    
  • Do you want to resize the main sketch window or CWindow?

  • edited September 2015 Answer ✓

    To get the enclosing applet and its surface create 2 global variables at line 5

    PApplet app;
    PSurface appSurface;
    

    then the first 2 lines in setup (after line 12)

    app = this;
    appSurface = surface;
    

    Then in the inner class you can use this variable and

    SOMETHING.setSize(img.width, img.height);

    becomes

    appSurface.setSize(img.width, img.height);

    An alternative approach to using reflection :)

  • Hi quark, your answer was so useful, this is just i need, thanks!, GoToLoop, thanks for the answer but, i can´t understand this, i´m a begginer jeje, did you know about a book that i can follow to learn?

  • edited September 2015 Answer ✓

    @FertheBana, getEnclosingPApplet() is a utility method which returns the PApplet's reference of the top class enclosing your inner class ControlWindow. That is, the main sketch itself! :-B
    Therefore, there's no need to request it from the sketch. The inner class finds that out alone! \m/

    Here's a variant of your sketch that stores getEnclosingPApplet() in the field p.
    Then relies on that to invoke getSurface() and setSize() + setTitle() within mousePressed():

    // forum.processing.org/two/discussion/12383/
    // multiple-windows-multiple-applet-resizing
    
    // 2015-Sep-03
    
    void settings() {
      size(320, 240, JAVA2D);
      smooth(4);
      noLoop();
    
      new ControlWindow(400, 400);
    }
    
    void draw() {
      background((color) random(#000000));
    }
    
    class ControlWindow extends PApplet {
      static final int GROWTH = 10;
      final PApplet p = getEnclosingPApplet();
    
      ControlWindow(int w, int h) {
        runSketch(new String[] {
          "--location=" + w/2 + ',' + h/2, "", str(w), str(h)
          }, this
        );
      }
    
      void settings() {
        smooth(4);
        noLoop();
      }
    
      void setup() {
        surface.setSize(int(args[0]), int(args[1]));
        surface.setResizable(true);
      }
    
      void mousePressed() {
        int grow = mouseButton == LEFT? GROWTH : -GROWTH;
        int w = constrain(p.width  + grow, 100, 1900);
        int h = constrain(p.height + grow, 100, 1000);
    
        p.getSurface().setSize(w, h);
        p.getSurface().setTitle(w + ", " + h);
        p.redraw();
      }
    
      protected PApplet getEnclosingPApplet() {
        try {
          return (PApplet) getClass().getDeclaredField("this$0").get(this);
        }
    
        catch (ReflectiveOperationException cause) {
          throw new RuntimeException(cause);
        }
      }
    }
    
  • @GoToLoop, it´s very very interesting way, i'll try to undestand it and use it in my code :)

  • Any further doubts about it, just ask it! ;)

  • edited September 2015

    @GoToLoop can you explain me this?
    int grow = mouseButton == LEFT? GROWTH : -GROWTH; how can i read this textually? (if mouse button is left then...bla bla bla...) i can´t understan this kind of code easily, i´m so noob :)

  • oookkaayyy jajaja that was easiest than appear XD soorry if i´m being anoying with my english sikills :(

  • @GoToLoop, can you teachme how to close individual applets? i´m opening a new nested applet when i press a button, but, every time i did, another applet apears :(, i need to close the nested applet if there is already open.

  • edited September 2015 Answer ✓

    It's much simpler to merely "hide" them.
    Use p.getSurface().setVisible(false); + p.noLoop(); in order to "hibernate" a PApplet.
    And p.getSurface().setVisible(true); + p.loop(); to "wake" it up back. :ar!

  • @GoToLoop, where can i find getSurface() methods, or more information to play with ppaplets and surfaces? :)

  • edited September 2015 Answer ✓

    Function getSurface(), and many others btW, is undocumented! @-)
    We need to look straight to Processing's source code in order to even know it exists: [-(
    https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L814

  • :'( by the hardway

Sign In or Register to comment.