SelectFolder(), SelectInput(), SelectOutput() Questions

Hell @Thundercat,

This will open to a specified directory:

// Code from these links was modified:
// https://forum.processing.org/beta/num_1140487204.html
// https://stackoverflow.com/questions/13516829/jfilechooser-change-default-directory-in-windows

import javax.swing.*;
PImage img;
boolean loaded = false;

void setup()
  {
  size(500, 500);
  }

void draw()
  {
  if(loaded) 
    {
    image(img, 0, 0);  
    println(img.width);
    }
  }

void mousePressed()
  {
  launchChooser();
  }

void launchChooser()
  {
  SwingUtilities.invokeLater(new Runnable()
    {
    public void run()
      {
      loaded = false;
      try 
        {
        JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File("D:/Users/GLV/Pictures/"));
        int returnVal = fc.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) 
          {
          File file = fc.getSelectedFile();
          //String name = file.getName().toLowerCase();
          //if (name.endsWith(".png") || name.endsWith(".gif")) 
          //  {
            img = loadImage(file.getAbsolutePath());
            println(file.getAbsolutePath());
            loaded = true;
            println(loaded, img.width, img.height);
            //}
          }
        }
      catch (Exception e) 
        {
        e.printStackTrace();
        }
      }
    }
    );
  }

//void imageChosen() 
//  {
//  image(img, 0, 0, width, height);
//  }

I gleaned some insight from the source code here that helped in my search for a solution:
processing4/core/src/processing/core/PApplet.java at main · benfry/processing4 · GitHub

:)

1 Like