暂停和重置计时器

时间:2017-04-14 12:07:21

标签: java button timer processing

我想添加一个暂停计时器运行的按钮和另一个将重置计时器的按钮。

代码如下:

import processing.sound.*;
  void playSoundFile(String filename) {
  SoundFile player;
  player = new SoundFile(this, filename);
  player.play();
}
//load image 
PImage bg;
//store millis function 
int m = millis(); 
//also stores millis for later use
int startingTime;

void setup() {
size(410,308);
//load the background image and store it in the url variable 
String url = "http://yodas.ws/2007/11/keele1.JPG";
bg = loadImage(url,"jpg");
//stores the millis function
startingTime = millis();


}

void draw() {
//sets the background to the image that was loaded
background (bg);
//stores second, minute, hour
int s = second();
int m = minute();
int h = hour();
//stores and converts the second, minute and hour to string
String sec = str(s);
String minu = str(m);
String hr = str(h);
String time = hr + ":" + minu;
//displays the time variable
text (time, 300, 50);
//sets the colour of the text
fill(255,172,108);
//sets the text size to 32 pixels
textSize(32);

int seconds = (millis() - startingTime) / 1000;
   //divides seconds by 60 for a minute 
   int minutes = seconds / 60;
   //divides minutes by 60 for a hour 
   int hours = minutes/60;
   //multiplies minutes by 60 for seconds 
   seconds-=minutes * 60;
  //multiplies hours by 60 for minutes 
   minutes-=hours * 60;



   //causes a 10 second delay when user presses a key 
   if (keyPressed == true){
      playSoundFile("down.mp3");
      delay(10000);



   }


   //sets the colour of the text, displays text and text size 
   fill(255, 0, 0);
   fill(255,172,108);
   text((hours) + ":" + (minutes) + ":" + (seconds),20,50);
   textSize(32);


}


//void keyPressed(){
  //if (m < 0){
   // delay(1000);
 // }
//}
//reset timer 

  //resets the timer when the user clicks the mouse
  void mouseClicked(){
  if (m > 0) {
  startingTime = millis();
  //keyPressed();
  playSoundFile("down.mp3");

  };



};

0 个答案:

没有答案