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 & HelpSyntax Questions › getting user input before draw runs
Page Index Toggle Pages: 1
getting user input before draw runs (Read 1665 times)
getting user input before draw runs
Jan 8th, 2010, 8:56am
 
I'm new to Processing, so this may be a stupid question. I'm writing a program that creates an animated line graph from a CSV file. I want the user to be able to define certain things, such as the length of the axes, before the data is plotted. Ideally, I want to create a GUI that allows the user to control these parameters.

Any thoughts on how I should structure the program? I've already written code that plots the data, but I can't see how I can insert the GUI code inside the same draw function. I want the GUI to be active before the plotting begins, and then to disappear. It's as if I need two draw functions. Is there a way of achieving something like that?

Any help would be much appreciated. Thanks!

jim
Re: getting user input before draw runs
Reply #1 - Jan 8th, 2010, 10:08am
 
Code:
boolean settingsComplete;  // defaults to false

void setup() {
 size(400,300);
}

void draw() {
 // (!settingsComplete) is equivalent to (settingsComplete == false)
 if(!settingsComplete) {
   // Draw the GUI
   // Presumably you'll have a control to determine when all settings have been set:
   if(okButtonPressed){
     settingsComplete = true;
   }
 }
 
 // if settingsComplete then draw...
 else {
   // plot data...
   // You could obviously add a control to switch 'settingsComplete' back to false and return to the GUI...
 }
}
Re: getting user input before draw runs
Reply #2 - Jan 8th, 2010, 12:01pm
 
Jim,

You can put the GUI inside setup(). You're welcome to use the following code, which I took from a program I'm working on. The GUI comes up, gets your input, vanishes, all before draw().

Are there a lot of people named Giles? I believe there's another id Giles on this forum. Cheers.

import javax.swing.*;
Object[] possibilities = {"60 FPS", "50 FPS", "40 FPS", "30 FPS", "15 FPS"};

void setup()
{
 size(400, 300,P2D);
 String s = (String)JOptionPane.showInputDialog(
                   frame,
                   "Please choose frame rate for image capture:",
                   "Choose frame rate",
                   JOptionPane.WARNING_MESSAGE,
                   null,
                   possibilities,
                   "60 FPS");
}

void draw()
{}
Re: getting user input before draw runs
Reply #3 - Jan 9th, 2010, 2:28am
 
blindfish -- many thanks! I think that should do it. Obvious now I see it Wink

liudr -- I don't quite understand your solution. I thought that the screen was only updated when draw is running? And, yes, there's are a lot of Giles folk out there. Common as muck Wink

Re: getting user input before draw runs
Reply #4 - Jan 9th, 2010, 3:03am
 
JOptionPane is Swing, which is OK to use here because the screen update thread isn't started yet (I think). It will pop out a dialog box, outside of the sketch's surface. It is suited to small, simple dialogs/options, and you can chain these pop ups if you have several things to ask. Not the best interface, but OK for simple stuff.
Re: getting user input before draw runs
Reply #5 - Jan 9th, 2010, 12:03pm
 
Yes, it is a popup dialog box. It is also independent from draw. Unless you want your user to see something drawn on the canvas before they make their choices, you can just use this. It gives a user a list to choose from, which is stored in possibilities, and the default value is 60 FPS.

If you want a user to give you a value, say 3.5, use this instead
 String user_input=javax.swing.JOptionPane.showInputDialog(frame,"Auto tracking parameters",preset);

where preset is the default value that you should assign, to 5 for instance. Just give it a try.
Re: getting user input before draw runs
Reply #6 - Jan 13th, 2010, 12:16pm
 
Hey,

If you do not want your gui to always be visible
you can activate it at certain mouse position and hide when mouse leaves the area

If parameters are changed through your GUI then update the plot area

Page Index Toggle Pages: 1