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 › User input to Processing
Page Index Toggle Pages: 1
User input to Processing? (Read 3472 times)
User input to Processing?
Nov 7th, 2009, 8:03am
 
Is it possible to code a sketch that when executed,  asks the user enter a number, between 1 and 9,  then once the user enters that number,  the sketch continues processing the remaining code,  and the user's entry is stored in a variable?

I know this sounds elementary to most of you,  but I am just starting to learn to code in Processing, and although I'm getting up there in years,  I still enjoy doing this.

Thanks for any help....

Jim
Re: User input to Processing?
Reply #1 - Nov 7th, 2009, 8:28am
 
Yes, it is possible.
Re: User input to Processing?
Reply #2 - Nov 7th, 2009, 8:29am
 
hehe, thats all he asked for...

i wasnt sure how i would solve that problem, and there are maybe better ways. but this is my solution.

You dont have to convert the char to an int, but i thought you want an integer to work with when you ask people to press a number. If it is just to choose a different mode, or programmpart. you can skip that.


PFont font;
int num = 0;

void setup(){
 size(500,500);
 font = createFont("Arial",22);
 textFont(font);
}

void draw(){
 background(255);
 println(num);
 if(num==0){
 fill(0);
 text("Please press a number between 1-9",80,100);
 }else{
   
 fill(255,0,0);
 for(int i = 0; i<num; i++)
 rect(100+i*12,100,10,10);
 fill(0);
 text("This is Number "+num,100,180);

 }
}

void keyPressed(){
int keyNum = Character.getNumericValue(key);
if(keyNum<=9 && keyNum>0)num = keyNum;
}
Re: User input to Processing?
Reply #3 - Nov 7th, 2009, 8:53am
 
Yeah,  you're right PhilHo !!  I did only ask if it was possible,  and you provided the answer.

I'm trying my best NOT to ask people to write the code for me,  as that is the only way I can learn,  but I will take Cedric's example and go from there.

Now let me ask this.....How would I display the form that asks for user input,  and after the user has entered the number,   THEN have that form close(or hide) and then display my main form that contains all the data?  Sorry if it's not clear,  but  I tried !!
Re: User input to Processing?
Reply #4 - Nov 7th, 2009, 8:53am
 
OK, seriously, here is an old sketch I did, meanwhile I added your constraint:
Code:
int queueSize = -1; // Was 50 before the change

ArrayDeque lines;

void setup()
{
 size(700, 700);
 frameRate(40);
 lines = new ArrayDeque();
 noFill();
 ellipseMode(CENTER);
 
 noLoop();
 PFont f = createFont("Verdana", 16);
 textFont(f);

}

void draw()
{
 background(128, 128, 255);
 if (queueSize < 0)
 {
   text("Type a number between 1 and 9", 10, 20);
   return;
 }

 if (mouseX != pmouseX || mouseY != pmouseY)
 {
   PVector p = new PVector(mouseX, mouseY);
   lines.addFirst(p);
 }
 if (lines.size() > queueSize)
 {
   lines.removeLast();
 }

 Iterator it = lines.iterator();
 PVector pp = null;
 while (it.hasNext())
 {
   PVector cp = (PVector) it.next();
   if (pp != null)
   {
stroke(14, 7, 42);
strokeWeight(1);
line(pp.x, pp.y, cp.x, cp.y);
stroke(214, 7, 142);
strokeWeight(5);
ellipse(cp.x, cp.y, 21, 21);
   }
   pp = cp;
 }
}

void keyReleased()
{
 int number = int(key) - int('0');
 if (number > 0)
 {
   println(number);
   queueSize = 10 * number;
   loop();
 }
}

It can be improved on GUI side, but basically it does what you need.

I highlighted the parts I added, as the code itself isn't really important.

[EDIT] Funny, Cedric was faster than me at coding, we came up independently at basically the same implementation, with little variants.
Re: User input to Processing?
Reply #5 - Nov 7th, 2009, 9:01am
 
yeah good to see i wasnt that wrong Smiley
Re: User input to Processing?
Reply #6 - Nov 7th, 2009, 9:01am
 
thank you sir,  I do appreciate you taking the time to help me.
Re: User input to Processing?
Reply #7 - Nov 8th, 2009, 5:04pm
 
Dear people,

while designing a simple interface element I encountered an annoying problem. This sketch will be used in an interactive menu. The idea is that when clicking on the square it gets bigger and when clicking on the small circle, it deflates the square to the original size. In other words, the square in its original size is a menu button and when clicking on it, it would reveal some content (text, for instance). Clicking on the small circle 'closes' the square. The only problem is that after clicking on the circle, the square does change the size to the original but then grows again... Somehow I should refine the conditions but don't know how.... I know the problem is that the cursor stays in the area defined to 'open' the circle after closing so it re-opens it...grrrrr. Any ideas?

float s = 40;

void setup(){
 size(200,200);
}
void draw(){
 background(175);
 fill(255);
 rectMode(CENTER);
 rect(100,100,s,s);
 color c=2;

 if(mouseX>=80&&mouseY>=80){
   s=s+1;
 }
 if(s>=85){
   s=85;
   fill(255);
   ellipse(127,127,7,7);
 }
 if(dist(127,127,mouseX,mouseY)<=7 && mousePressed){
   s=40;
 }

}






Re: User input to Processing?
Reply #8 - Nov 8th, 2009, 5:53pm
 
you get better answers if you just start a new threat, this is missleading. in most cases nobody will answer you.

Anyway, i checked your sketch...
your problem is that you check if mouseX and mouseY is bigger, but you also have to check that it is smaller then x,y of your rect minus s/2...and that was the other part of the problem. you have to check for s/2 not s...

EDIT: i just removed the code cause i believe you can figure that out yourself. if not ask again...
Re: User input to Processing?
Reply #9 - Nov 8th, 2009, 8:12pm
 
Sorry, couldn't find an appropriate topic. Tried different search words... Thanks for the help, will try it!
Re: User input to Processing?
Reply #10 - Nov 10th, 2009, 3:43pm
 
Here is my proven way to get an input from a user, an input dialog. It's not promoted here since processing has some incompatibility with java.swing but I've never seen it freeze my program. You can ask user for more numbers and ask user to comma separate them so you can later get all the numbers by splitting the input at commas.

SwingUtilities.invokeLater(new Runnable() {  
 public void run()
 {  
     String preset="1";
     String tempString=javax.swing.JOptionPane.showInputDialog(frame,"Type your input here",preset);
     int value=int(tempString);
 
}  
});
Page Index Toggle Pages: 1