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 › timer/stopwatch mouse start/stop - calc. problem
Pages: 1 2 
timer/stopwatch mouse start/stop - calc. problem (Read 4449 times)
timer/stopwatch mouse start/stop - calc. problem
Aug 7th, 2009, 11:08am
 
hello, I've been browsing the site and found some helpful info.  However, its all in pieces.  I'm trying to make a timer in a window that counts minutes and seconds.  I would like to be able to start it and stop it by mouse click on either the clock or a separate start/stop "button".  Also each time it stops, when it clicked again it starts from 0.
Is this possible?  It seems like it is but I just can't put the code together for it.
Thanks,
D
Re: timer/stop watch mouse start and stop
Reply #1 - Aug 7th, 2009, 2:47pm
 
Creating a timer might be a starting point.
Re: timer/stop watch mouse start and stop
Reply #2 - Aug 7th, 2009, 8:39pm
 
yeah i've been fooling around with that but having a rough time.
Basically I need the timer to just read seconds and minutes.
so it looks like this "00:00".  I'll take that number and put it in a rectangle in my current window.
I'd also like to be able to start and stop with a mouseclick, but only if I click on an individual "button" if that is possible.
Thanks, I'll keep tooling around with the timer code.
Re: timer/stop watch mouse start and stop
Reply #3 - Aug 10th, 2009, 9:43am
 
It seems like I'll be using Java code for this.  I tried to run a java code but received an error message.  Do I need to download some additional java libraries?  Is there anywhere I could learn some more about using Java with Processing?
Re: timer/stop watch mouse start and stop
Reply #4 - Aug 10th, 2009, 11:18am
 
Quote:
Is there anywhere I could learn some more about using Java with Processing?

Processing is Java. You don't need to add any library to run Java code in Processing. Please report your code and error message if you want more help.

Quote:
It seems like I'll be using Java code for this.

PhiLho pointed out a useful link which does almost all what you want. Why don't you just reuse the code?

If you have syntax or code issues, please report the code so we can help.
Re: timer/stop watch mouse start and stop
Reply #5 - Aug 10th, 2009, 1:41pm
 
sorry, I've just recently started using processing and trying to teach myself as I go.  I just want a timer that displays mm:ss and is able to be controlled with a start stop button.  Probably something obvious to you, sorry for the many stupid questions.
the error message I get is "cannot parse error text: File /tmp/build16733.tmp/Stopwatch.java is missing

I've tried to run a few java codes but keep getting similar error messages.
Re: timer/stop watch mouse start and stop
Reply #6 - Aug 10th, 2009, 2:19pm
 
You haven't posted any source code...  So not sure how we can help.

However 'Stopwatch.java' looks to me like this.  And if you cut and paste that into Processing and run it, it doesn't work (no surprises there), with the error you quote...

Whilst "Processing is Java", this doesn't mean you can simply drop any old Java code in and expect it to run, as you have discovered.  There's some stuff going on behind the scenes that makes things a little more complicated than that: for example Processing expects to find a setup function.  If you transfer the code from the main method in the linked URL to setup you can get it to run without any errors (though you'd still need to adapt it to your purpose)...  However I don't think that's going to help you learn processing, so I'm not going to explain it further.

You'd do much better to look at PhilHo's link and figure out how that works before moving on to class definitions, unless you've already used these in other languages...  i.e. learn to walk before you start trying to run Wink
Re: timer/stop watch mouse start and stop
Reply #7 - Aug 10th, 2009, 3:47pm
 
Yeah, you're right.  Some times i get ahead of myself.  Here is some code that I'm working with.  Basic stuff.  I broke down the code from the suggested thread.  I'm now trying to figure out how to get the seconds to rollover after sixty and keep a 2 digit minute and seconds. mm:ss format.  After I get that far, I will work on the code for the start/stop button.  Thank you for your patience.

Code:
int startingTime;
float TextFont;

void setup() {
size(400, 400);
PFont font= loadFont("Verdana-12.vlw");
textFont(font);


startingTime = millis();
}

void draw() {
background(255,255,255);

int seconds = (millis() - startingTime) / 1000;
int minutes = seconds / 60;
fill(0,0,0);

text((minutes) + ":" + (seconds),50,50);




}


Re: timer/stop watch mouse start and stop
Reply #8 - Aug 10th, 2009, 3:59pm
 
never mind.. i figured it out.  Thanks.
now to get it to show in mm:ss .

Code:
int startingTime;
float TextFont;

void setup() {
size(400, 400);
PFont font= loadFont("Verdana-12.vlw");
textFont(font);


startingTime = millis();
}

void draw() {
background(255,255,255);

int seconds = (millis() - startingTime) / 1000;
int minutes = seconds / 60;
int hours = minutes/60;
seconds-=minutes * 60;
minutes-=hours * 60;
fill(0,0,0);

text((minutes) + ":" + (seconds),100,100);




}


Re: timer/stop watch mouse start and stop
Reply #9 - Aug 10th, 2009, 9:13pm
 
okay a sort of back door entrance to this.
I still need to..

a. get 2 digit minutes and seconds.
b. needs to start at 00:00
c. 1 button to start and stop, another to reset.

so far so good.
Code:
int startingTime;
float TextFont;
boolean bover = false;
boolean locked = false;


void setup() {
size(400, 400);
PFont font= loadFont("Verdana-12.vlw");
textFont(font);

startingTime = millis();
}

void draw() {
background(255,255,255);

int seconds = (millis() - startingTime) / 1000;
int minutes = seconds / 60;
int hours = minutes/60;
seconds-=minutes * 60;
minutes-=hours * 60;

fill(255);
stroke(0);
rect(150,95,100,50);
fill(0,0,0);
text(( minutes) + ":" + (seconds),190,125);


// Test if the cursor is over the box
if (mouseX > 150 && mouseX < 250 &&
mouseY > 150 && mouseY < 200) {
bover = true;
if(!locked) {
fill(255);
stroke(153);

}
} else {

bover = false;
fill(255);
stroke(0);
}

// Draw the box
rect(150, 150, 100, 50);
fill(0);
text("Reset",180,180);
}

void mousePressed() {
if(bover) {
locked = true;
startingTime = millis();

} else {
locked = false;

}


}



void mouseReleased() {
locked = false;
}


Re: timer/stop watch mouse start and stop
Reply #10 - Aug 11th, 2009, 1:25am
 
You might find nf() helpful for formatting the numbers Wink
Re: timer/stop watch mouse start and stop
Reply #11 - Aug 11th, 2009, 4:42am
 
Quote:
c. 1 button to start and stop, another to reset


solution #1 > build your own buttons
http://processing.org/learning/topics/button.html

solution #2 > use a library
http://processing.org/reference/libraries/#interface
Re: timer/stop watch mouse start and stop
Reply #12 - Aug 12th, 2009, 6:49pm
 
Wow. That's exactly what I need. Thank you for your pacience and responses.
Re: timer/stop watch mouse start and stop
Reply #13 - Aug 14th, 2009, 8:15pm
 
Ok, the nf() worked perfectly.  I'm having trouble with the pause, continue option.  Any advice on where to find out info on this?
Re: timer/stop watch mouse start and stop
Reply #14 - Aug 14th, 2009, 10:48pm
 
ok I'm slowly moving along here.
Like I've said, I figured out the nf().  Now I have the pause button working, but when it's "unpaused" it starts as if it never stopped.
Also when the "reset" button is hit, it does the same thing as if I hit the "pause" button.

What I really want, is the clock to start a 00:00 until  a "start" button his hit.
Then, I would like to hit the "stop" button, but without the option to continue, rather just freeze the time.
And finally a "reset" button to bring it all back to 00:00.

Code:
 int startingTime;
float TextFont;
boolean bOver = false;
boolean locked = false;
boolean paused = false;
boolean cOver = false;


void setup() {
size(400, 400);
PFont font= loadFont("Verdana-12.vlw");
textFont(font);

startingTime = millis();
}

void draw() {
background(255,255,255);

int seconds = (millis() - startingTime) / 1000;
int minutes = seconds / 60;
int hours = minutes/60;
seconds-=minutes * 60;
minutes-=hours * 60;
String sm = nf(minutes, 2); //minutes
String s2 = nf(seconds, 2); //seconds
fill(255);
stroke(0);
rect(150,95,100,50);
fill(0,0,0);
text(( sm) + ":" + (s2),185,125);
fill(255);

// Test if the cursor is over the box
if (mouseX > 150 && mouseX < 250 &&
mouseY > 150 && mouseY < 200) {
bOver = true;
if(!locked) {



}
} else {

bOver = false;

}
if (mouseX > 150 && mouseX < 250 &&
mouseY > 205 && mouseY < 265){
cOver = true;
if(!locked){
}
} else {

cOver = false;

}

// Draw the box
rect(150, 150, 100, 50);
fill(0);
text("Reset",184,180);

fill(255);
rect(150, 205, 100, 50);
fill(0);
text("Pause",184, 235);

}

void mousePressed() {
if(bOver) {
locked = true;
startingTime = millis();

} else {
locked = false;


}
if(cOver) {
paused = !paused;
if(paused)
noLoop();
else
loop();


}
}


Pages: 1 2