1

I am looking for a way to make a type of import button where users can import their music from their folder. I've got no idea on how to do this so any tips would be grateful.

Nico
  • 3,471
  • 2
  • 29
  • 40

1 Answers1

1

Here is something to get you going:

float buttonX;
float buttonY;
float buttonW;
float buttonH;

void setup() {

  textSize(24);

  frame.setResizable(false);

  background(255);

  size(600, 200);

  fill(0);
  stroke(0);
  noFill();

  buttonW = 200;
  buttonH = 50;
  buttonX = width - width/2 - buttonW/2;
  buttonY = height/2 - buttonH/2;
}

void draw() {

  background(255);
  fill(0);

  rectMode(CORNER);

  rect(buttonX, buttonY, buttonW, buttonH);

  fill(255);

  textAlign(LEFT);
  text("Import File", buttonX+35, buttonY+30);
}

void mouseClicked() {
  if (mouseX>buttonX && mouseX < buttonX+buttonW && mouseY > buttonY && mouseY < buttonY+buttonH) {
    selectInput("Import music file", "fileSelected");
  }
}

/* Taken from Processing.org */
void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or user hit cancel");
  } else {
    println("User selected " + selection.getAbsolutePath());
  }
}

Notice some things here. The button is simply a rect that is drawn on the screen and then the click is bound within the limit of the rect, which in effect creates a button. I am showing this here so you can understand that you can create a GUI with the simplest and the most basic objects available to you in Processing. This is a very simple sketch that doesn't do anything other than give you a file browser (check Processing docs for that) and prints the path of the file you choose or writes out a message if you hit cancel. What you do with this is up to you. Have fun with it.

Once you're done playing around and want to see what others have done and what some of the GUIs made by others look like, go to this page and check out the GUI section: http://www.processing.org/reference/libraries/ (this is only to see how they've implemented their buttons, not for the file browser).

Nico
  • 3,471
  • 2
  • 29
  • 40
  • Thanks for that, I used something similar to that but instead of drawing a rect, I used an image I already had. Not too sure on how to upload the song when I click on a song on the file browser – user3423784 Mar 24 '14 at 13:35
  • @user3423784 What do you mean upload? Upload it to a website? I suggest you ask a new question with relative tags since that isn't directly related to your question here. – Nico Mar 24 '14 at 20:57
  • I meant as in upload it to my music player, such as I click a song from my music folder by clicking on my import button, and the player will open the file and play it – user3423784 Mar 25 '14 at 13:08
  • @user3423784 Ask a new question with the code for your music player. Leave a comment here when you've done so and I'll see what I can do. – Nico Mar 25 '14 at 15:36
  • Hey, iv'e made a new question with most of my code here:http://stackoverflow.com/questions/22643968/adding-import-button-to-music-player – user3423784 Mar 25 '14 at 19:03