00zhang
YaBB Newbies
Offline
Posts: 4
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(); } }