We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Filechooser
Page Index Toggle Pages: 1
Filechooser (Read 407 times)
Filechooser
Nov 17th, 2008, 5:25pm
 
Hello,everyone,
can anybody help me with this problem?

I find code from:

http://processing.org/hacks/doku.php?id=hacks:filechooser

I modified the code, and integrated it into my sketch:

 import javax.swing.*;
 
   Button rbOpen;    
  File file;  
  PImage Buf ;
 
  void setup(){

      Buf = loadImage("wh.jpg");
      size(950,700);     // setup the size of the window
     
      background(125, 125, 125);   // setup the background color
   
      PFont metaBold;
      metaBold = loadFont("Meta-Bold.vlw.gz");  
      textFont(metaBold, 12);
     
      rbOpen = new Button(500+50,225+35,50,20,"Open");
   
  }
 
  void draw() {
    rbOpen.update();
    image(Buf,0,0,500,500);
  }
  void mouseReleased() {
      if (rbOpen.pressed()) {      
       
      try {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());      
      }
      catch (Exception e) {
       e.printStackTrace();
      }
     final JFileChooser fc = new JFileChooser();
     //In response to a button click:
     int returnVal = fc.showOpenDialog(this);
   
     if (returnVal == JFileChooser.APPROVE_OPTION) {
       file = fc.getSelectedFile();
      // see if it's an image
      // (better to write a function and check for all supported extensions)
        if (file.getName().endsWith("jpg")) {
               PImage img = loadImage(file.getPath());
             if (img != null) {              
               println(file.getPath());
               //println("x");
                //size(950,700);    
               ///println(img.width*img.height);
                //image(img,0,0,500,500);      
    else {
   // just print the contents to the console
   String lines[] = loadStrings(file); // loadStrings can take a Java File Object too
   for (int i = 0; i < lines.length; i++) {
     println(lines[i]);
   }
 }
     }
      else {
        println("Open command cancelled by user.");
       }
      return;
 }
  }  
  class Button {
   
 int x, y;
 int w, h;
 color basecolor, highlightcolor;
 color currentcolor;
 boolean over = false;
 boolean pressed = false;  
 boolean locked = false;
 String btext;

 Button(int _x, int _y, int _w, int _h, String _btext) {
   x = _x;
   y = _y;
   w = _w;
   h = _h;
   basecolor =color(153, 153, 153);;
   highlightcolor = color(128, 0, 0);
   currentcolor = basecolor;
   btext = _btext;
 }
 
boolean pressed()
 {
   if(over) {
     locked = true;
     return true;
   } else {
     locked = false;
     return false;
   }    
 }
 
 void update() {
 
   if (locked==false) {
     over();
     if (over() && mousePressed) {
       currentcolor = highlightcolor;
       }
     else
       currentcolor = basecolor;
   }
   else {
     locked = false;
   }
   
   draw();
 }
 
 boolean overRect(int x, int y, int width, int height) {
   if (mouseX >= x && mouseX <= x+width &&
     mouseY >= y && mouseY <= y+height) {
     return true;
   } else {
     return false;
   }
 }

 boolean over()
 {
   if( overRect(x, y, w, h) ) {
     over = true;
     return true;
   } else {
     over = false;
     return false;
   }
 }
 
 void draw()
 {
   stroke(255);
   fill(currentcolor);
   rect(x, y, w, h);
   fill(255, 255, 255);
   text(btext, x+2, y+h*3/4);
   fill(255);
   noStroke();
 }
}

Re: Filechooser
Reply #1 - Nov 17th, 2008, 5:25pm
 
thx in advance~~~
Re: Filechooser
Reply #2 - Nov 17th, 2008, 6:36pm
 
Fixed the curly brace issue and made the filedialog display.
I've always had problems using the UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
so I disabled it.. that's how I got it working now.

Code:

import javax.swing.*;

Button rbOpen;
File file;
PImage Buf ;

void setup(){

Buf = loadImage("wh.jpg");
size(950,700); // setup the size of the window

background(125, 125, 125); // setup the background color

PFont metaBold;
metaBold = loadFont("Meta-Bold.vlw.gz");
textFont(metaBold, 12);

rbOpen = new Button(500+50,225+35,50,20,"Open");

}

void draw() {
rbOpen.update();
image(Buf,0,0,500,500);
}

void mouseReleased() {
if (rbOpen.pressed()) {

/*try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}*/
final JFileChooser fc = new JFileChooser();
//In response to a button click:
int returnVal = fc.showOpenDialog(this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
// see if it's an image
// (better to write a function and check for all supported extensions)
if (file.getName().endsWith("jpg")) {
PImage img = loadImage(file.getPath());
if (img != null) {
println(file.getPath());
//println("x");
//size(950,700);
///println(img.width*img.height);
//image(img,0,0,500,500);
}
}
else {
// just print the contents to the console
String lines[] = loadStrings(file); // loadStrings can take a Java File Object too
for (int i = 0; i < lines.length; i++) {
println(lines[i]);
}
}
}
else {
println("Open command cancelled by user.");
}
return;
}
}

class Button {

int x, y;
int w, h;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
boolean locked = false;
String btext;

Button(int _x, int _y, int _w, int _h, String _btext) {
x = _x;
y = _y;
w = _w;
h = _h;
basecolor =color(153, 153, 153);
;
highlightcolor = color(128, 0, 0);
currentcolor = basecolor;
btext = _btext;
}

boolean pressed()
{
if(over) {
locked = true;
return true;
}
else {
locked = false;
return false;
}
}

void update() {

if (locked==false) {
over();
if (over() && mousePressed) {
currentcolor = highlightcolor;
}
else
currentcolor = basecolor;
}
else {
locked = false;
}

draw();
}

boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
}
else {
return false;
}
}

boolean over()
{
if( overRect(x, y, w, h) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}

void draw()
{
stroke(255);
fill(currentcolor);
rect(x, y, w, h);
fill(255, 255, 255);
text(btext, x+2, y+h*3/4);
fill(255);
noStroke();
}
}
Re: Filechooser
Reply #3 - Nov 17th, 2008, 7:27pm
 
that hack is now out of date--use the built-in functions selectInput(), selectOutput(), or selectFolder().
Page Index Toggle Pages: 1