Dynamic Sketch Resizing?

edited January 2015 in Questions about Code
void setup(){
  size(400,400);
}

void draw(){
  background(0);
  if(keyPressed){
    frame.resize(600,400);
  }
}

This is a MVCS. When you press any button, the sketch window resizes properly, but the new area isn't colored in black like I would expect. What am I doing wrong here?

Answers

  • edited January 2015 Answer ✓

    Here's my attempt on it: :-bd

    // forum.processing.org/two/discussion/8864/dynamic-sketch-resizing
    
    void setup() {
      size(600, 400);
      frameRate(2);
      frame.setResizable(true);
    }
    
    void draw() {
      background((color) random(#000000));
    }
    
    void mousePressed() {
      frame.resize(width = 800, height = 600);
    }
    
    void mouseReleased() {
      frame.resize(width = 600, height = 400);
    }
    

    The width & height's forced reassignment isn't necessary. But it makes the canvas adapt faster to latest resize().

  • @ GoToLoop== yes your code works but a) i would be happy to understand & learn why TFGuy44 is wrong... b) with your code there is one frame during which the update is not achieved and you can see some "flash" c) as for me here the code i use - Of course adding java awt, sorry! but there is no flash...

    import java.awt.*;
        Color c;
        int r, g, b;
    
        void setup(){
          size(400,400);
    
           r = int(random(255));
           g = int(random(255));
          b = int(random(255));
          c = new Color(r,g,b);
           background(r, g, b);
    
    
        };
    
        void draw(){
    
          if(keyPressed){
    
            frame.resize(600,400);
    
            frame.setBackground(c);
    
          }
        };
    
  • Only thing @TfGuy44's code needed was frame.setResizable(true);.
    Since I've slowed down frameRate(), it's much more noticeable that "flash" I guess!

  • Right; excuse me i have not noticed this point...And thanks!

  • What about this one?

    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
      if(keyPressed){
        setSize(600,400);
        frame.resize(600,400);
      }
    }
    

    Setting the size of the canvas as well as the frame, seems to do the job..

    Here's some study I made on this once, considering insets...

    //no error handling for non image files!
    
    PImage img;
    
    //Minimum dimensions for the window holding an applet. This varies between platforms
    //They are PApplet's fields
    
    int newCanvasWidth  = MIN_WINDOW_WIDTH;  // made global to  use in draw
    int newCanvasHeight = MIN_WINDOW_HEIGHT;
    
    float initialWindowW ;
    float initialWindowH;
    
    
    java.awt.Insets insets;  //"An Insets object is a representation of the borders of a container"
                             //from http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Insets.html
    
    void setup()
    {
      initialWindowW = displayWidth * 0.85;
      initialWindowH = displayHeight * 0.85;
      size(int(initialWindowW), int(initialWindowH));   // always first line
      //frame.pack();     //frame.pack() no need for setResizable... plus insets
    
      insets = frame.getInsets();
    
      frame.setResizable(true);
    
          /// for debuging, system depende`nt, at least screen is...
      print("MIN_WINDOW_WIDTH = " + MIN_WINDOW_WIDTH);
      print("   MIN_WINDOW_HEIGHT = " + MIN_WINDOW_HEIGHT);
      print("   screenWidth = " + displayWidth);
      println("    screenHeight = " + displayHeight);
      background(20);
    }
    
    
    void draw(){
      if (img != null)
      {
    
        image(img, 0, 0, newCanvasWidth, newCanvasHeight);
      }
    }
    
    void getImageAndResize(File selected)
    { 
      String path = selected.getAbsolutePath();
    
      if (path == null)
      {
        println ("nono :-|");
      } 
      else
      {
    
        img = loadImage(path);
    
            // a temp variable for readability 
        int widthInsets =insets.left + insets.right;
        int heightInsets =insets.top + insets.bottom;
    
            // constrain values between screen size and minimum window size
        int newFrameWidth  = constrain(img.width + widthInsets, MIN_WINDOW_WIDTH, displayWidth);
        int newFrameHeight = constrain(img.height + heightInsets, MIN_WINDOW_HEIGHT, displayHeight -20);
    
            // Canvas should consider insets for constraining? I think so...
         newCanvasWidth  = constrain(img.width, MIN_WINDOW_WIDTH - widthInsets, displayWidth - widthInsets);
         newCanvasHeight = constrain(img.height, MIN_WINDOW_HEIGHT - heightInsets, displayHeight -20 - heightInsets);
    
    
            // set canvas size to img size WITHOUT INSETS
        setSize(newCanvasWidth, newCanvasHeight);
    
            // set frame size to image + Insets size
        frame.setSize(newFrameWidth, newFrameHeight);
    
    
            //// for debuging
        println(path);
        println(" ");
        print("imgW      = " + img.width);
        println("   imgH       = " + img.height);
        print("width+ins = " + widthInsets);
        println("      height+ins = " + heightInsets);
        print("nFrameW   = " + newFrameWidth);
        println("   nFrameH    = " + newFrameHeight);
        print("nCanvasw  = " + newCanvasWidth);
        println("   nCanvsH    = " + newCanvasHeight);
        println(" ------  ");
      }
    
    }
    
    
    void mouseClicked()
    {
      img = null;
    
      selectInput("select an image", "getImageAndResize" );
    }
    
Sign In or Register to comment.