Problem with 3.x - Pimage and size() - Errors

edited August 2015 in Questions about Code

I have just updated my processing to Processing 3.x, but loads of my sketches have stopped working. e.g:

CODE:

void setup() { background(33); source = loadImage("5.jpg"); size(source.width, source.height); }

ERROR: "The size of this sketch could not be determined from your code....."

any idea how I can set the size of the sketch to the PImage size?

Answers

  • edited August 2015

    If size() doesn't accept variables or other expressions as its argument, it's merely an illusion which can be replaced by some user configuration inside the PDE! :(|)
    That is, it's not programmatically anymore! :-\"
    Every Processing big series comes w/ loss of features & flexibility! X(

  • edited August 2015

    thats a bit annoying :-( , i have hundreds of sketches that resize dependent on variables? GRRRR

  • I found this example useful, even though it requires some tinkering to work on version 3.0. Thanks @_vk.

    http://forum.processing.org/two/discussion/comment/48246/#Comment_48246

  • Cool! :-c

    Perhaps you could post the tinkered version compatible with 3.0, for reference.

  • There are just a couple of minor tweaks. For one, MIN_WINDOW_WIDTH and MIN_WINDOW_WIDTH no longer exist as system constants, so I had to define them:

    int MIN_WINDOW_WIDTH = 128;
    int MIN_WINDOW_WIDTH = 128;
    

    And the system prompted me to change a couple of instances of frame.whatever() to surface.whatever(). That's all it took, but there's still an issue with an extra height inset sized band of pixels at the bottom of the image. I haven't figured that one out yet. Here's the whole thing:

    //no error handling for non image files!
    
    int MIN_WINDOW_WIDTH = 128;
    int MIN_WINDOW_HEIGHT = 128;
    
    
    PImage img;
    int newCanvasWidth  = MIN_WINDOW_WIDTH;  // made global to  use in draw
    int newCanvasHeight = MIN_WINDOW_HEIGHT;
    String path ="";
    
    java.awt.Insets insets;  //"An Insets object is a representation of the borders of a container"
    //from: docs.oracle.com/javase/1.4.2/docs/api/java/awt/Insets.html
    
    void setup() {
      size(400, 200);
      frame.pack();     //frame.pack() ... plus insets
      insets = frame.getInsets();
      surface.setResizable(true);
    
      /// for debuging, system dependent, at least screen is...
      print("MIN_WINDOW_WIDTH = " + MIN_WINDOW_WIDTH);
      print("   MIN_WINDOW_HEIGHT = " + MIN_WINDOW_HEIGHT);
      print("   screenWidth = " + displayWidth);
      println("    screenHeight = " + displayHeight);
    
      text("click window to load an image", 10, 100);
    }
    
    void draw() {
      if (img != null) {
        image(img, 0, 0, newCanvasWidth, newCanvasHeight);
      }
    }
    
    void getImageAndResize() { 
      selectInput("select an image", "handleImage");
    }
    
    void handleImage(File selection) {
      if (selection== null) {
        println ("nono");
      } 
      else {
        path = selection.getAbsolutePath();
        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);
    
        // 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 - heightInsets);
    
        // set canvas size to img size WITHOUT INSETS
        // set frame size to image + Insets size
        setSizes(newCanvasWidth, newCanvasHeight, 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(" ------  ");
      }
      println(width + "  "+ height);
    }
    
    void setSizes (int canvasX, int canvasY, int frameX, int frameY) {
      // set canvas size to img size WITHOUT INSETS
      setSize(canvasX, canvasY);
    
      // set frame size to image + Insets size
      surface.setSize(frameX, frameY);
      println(width + "  "+ height);
    }
    
    void mouseClicked() {
      img = null;
      getImageAndResize();
    }
    
  • surely an easier way? ;-)

  • Sorry - but I think the removal of variables from the SIZE() function is just plain dumb! WTF???

  • Is Processing supposed to be artist, amateur programmer friendly? =))

  • fryfry
    Answer ✓

    Just use surface.setSize() and be done with it. I've added more background here: https://github.com/processing/processing/wiki/Changes-in-3.0#things-that-may-break-your-2x-sketches

    @GoToLoop The negativity is unnecessary. If you're not here to help people find solutions, then keep quiet. The extra noise gets in the way of what are much simpler solutions.

  • edited August 2015
    • I'm in the dark & worried as any1 else here! ~:>
    • Finally the chief dev made a rare visit here in order to clear some things out about the most breaking version of Processing ever!
    • But after reading that wiki link, I've come to realize that size() still works as before Processing 3; as long as it's inside a new callback called settings():
      https://processing.org/reference/settings_.html
    • But that deepens even more the chasm between Java & JS Modes.
    • PJS didn't even catch Processing 2's easier features like IntList & Table.
    • But who cares about flexibility & deployment. PJS was removed as an official project after all! :-&
  • edited August 2015 Answer ✓

    Using @fry 's advice and example I was able to cut the above code down to this, much easier to deal with:

    PImage img;
    String path ="";
    
    void setup() {
      size(400, 200);
      surface.setResizable(true);
    
      text("click window to load an image", 10, 100);
    }
    
    void draw() {
      if (img != null) {
        image(img, 0, 0);
      }
    }
    
    void handleImage(File selection) {
      if (selection== null) {
        println ("nono");
      } 
      else {
        path = selection.getAbsolutePath();
        img = loadImage(path);
        surface.setSize(img.width, img.height);
      }
    }
    
    void mouseClicked() {
      img = null;
      selectInput("select an image", "handleImage");
    }
    
  • I'm curious whether setResizable(true); is actually necessary for setSize()?

  • It works without the setResizable(), at least in the couple of examples I have here.

  • Seems like surface is the new frame.
    I wonder whether it got setTitle() & setVisible() too?

  • setTitle() yes, I dunno about setVisible().

  • edited August 2015

    the new version is too painful...none of my old sketches work at all (or run painfully slow).

    gone back to 2.x. :-(

  • edited August 2015

    It's clear that presenting the beta as the main download is a big mistake! :-t
    And that ends up falling at us, the answerers... [-(

  • edited September 2015 Answer ✓

    Here is the easiest solution in only two simple steps. Anyway, Mr. Fry had already answered that question.

    In your setup(), do as this :
    - Import the image.
    - Use the method setSize() to the surface object, with the dimensions of the imported image as parameters.

    PImage img;
    void setup() {
      img = loadImage("my_image.jpg");
      surface.setSize(img.width, img.height);
    }
    
  • Here is my solution to this problem, just comment the line size and putting the information as below:

    Sem título

Sign In or Register to comment.