selectInput() and image import

edited April 2017 in Questions about Code

Hello everyone,

In my small program, the user selects an image file and then the file can be seen in full screen, I have made some mistakes in my code:

 import  java.io.File;
 import processing.opengl.*;
 PImage img;

 int mX, mY;



void setup() {
       size(640, 200);
       background(0);
       selectInput("Select a file to process:", "fileSelected");
}

void draw() {
      stroke(255);
      if(mousePressed) {
      line(mouseX, mouseY, pmouseX, pmouseY);
}
}

void fileSelected(File selection) {
      image(img, 0, 0);
      img = loadImage(selection.getAbsolutePath());
}

but it does not work, where I'm wrong? Another question: how can I directly open the browser in the folder where is the code?

thanks!

Tagged:

Answers

  • thanks for the link GoToLoop, but I do not find the solution to my problems. If you have time, or someone helps me to understand how to solve, Thank you in advance.

  • in simple terms, the program doesn't wait for the user to select a file.

    you should also catch the situation when user hit cancel in the dialogue :

     if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        println("User selected " + selection.getAbsolutePath());
        // load image here 
      }
    

    Obviously you can't also display an image before loading it. Without loading it, img is still empty!!!

    so this

      image(img, 0, 0);
      img = loadImage(selection.getAbsolutePath());
    

    must be

      img = loadImage(selection.getAbsolutePath());
      image(img, 0, 0);
    

    this might even work surprisingly, since you don't use background in draw(). If you would, you would use image(img, 0, 0); in draw

  • edited April 2017

    GoToLoop, I understand that the problem is "null pointer exception"

    your code GoToLoop:

    /**
    * SelectInput() for loadImage() in settings() (v1.0)
    * GoToLoop (2016-May-19)
    *
    * forum.Processing.org/two/discussion/16705/
    * null-pointer-exception-when-loading-image-in-settings-p3-1-1
    */
    
    PImage canvas;
    
    void settings() {
    selectInput("Please select canvas picture:", "selectImage");
    while (canvas == null)  delay(100);
    
    size(canvas.width, canvas.height);
    noSmooth();
    noLoop();
    }
    
    void draw() {
    background(canvas);
     }
    
    void selectImage(final File f) {
    if (f == null || f.isDirectory()) {
    println("Window was closed or you've hit cancel.\n");
    System.exit(0);
    }
    
    final String canvasPath = f.getPath();
    println(canvasPath);
    
    if ((canvas = loadImage(canvasPath)) == null) {
    println("is an invalid image file. Try again...\n");
    selectInput("Please select canvas picture:", "selectImage");
    }
    }
    

    does not work with Processing 3.3 you can fix it?

    Thanks Chrisir for having me explain an important aspect of my mistake. I have done the changes that you've recommended, but it does not work, I have error: "Null pointer exception", you can solve it?

    thank you all.

  • edited April 2017

    ... does not work with Processing 3.3 you can fix it?

    Have you even tried it out? Just copied & pasted it in my PDE 3.3 and it's simply worked here! 8->

    BtW, the code you've posted is mal-indented. L-)

  • edited April 2017

    GoToLoop, I'm sorry for how I've posted the code.

    You're right, your example works well. For a few minutes my PC would not open the execution of the file in the window, and I thought of a problem with the file, sorry again.

    I've added to your code the option to draw lines with the mouse, but I see nothing,

    /**
     * SelectInput() for loadImage() in settings() (v1.0)
     * GoToLoop (2016-May-19)
     *
     * forum.Processing.org/two/discussion/16705/
     * null-pointer-exception-when-loading-image-in-settings-p3-1-1
     */
     import  java.io.File;
     import processing.opengl.*;
    
     PImage canvas;
     int mX, mY;
    
    
    
     void settings() {
     selectInput("Please select canvas picture:", "selectImage");
     while (canvas == null)  delay(100);
    
    size(canvas.width, canvas.height);
    noSmooth();
    noLoop();
    }
    
    void draw() {
    background(canvas);
    stroke(255);
    if(mousePressed) {
    line(mouseX, mouseY, pmouseX, pmouseY);
    }
    }
    
    void selectImage(final File f) {
    if (f == null || f.isDirectory()) {
    println("Window was closed or you've hit cancel.\n");
    System.exit(0);
    }
    
    final String canvasPath = f.getPath();
    println(canvasPath);
    
    if ((canvas = loadImage(canvasPath)) == null) {
    println("is an invalid image file. Try again...\n");
    selectInput("Please select canvas picture:", "selectImage");
    }
    }
    

    I doing something wrong?

    What is the directory to be added to open the "Browser" in the folder where the file is currently running?

    thanks

  • edited April 2017

    hit ctrl-t in processing to auto-format the code (indents!!!!) prior to posting it here

    to tell the load dialogue the folder use selectInput with 3 parameters:

          File start1 = new File(sketchPath("")+"/myPrograms/*.*"); 
          selectInput("Select a file to write to:", "fileSelectedForLoad", start1);
    

    why do you use noLoop()? Don't.

    not sure we can use size after the whole thing but I think gotoloop is in the lead.

Sign In or Register to comment.