Make a "timer" in Processing

edited June 2016 in How To...

So basically I have a shape, and I want to make it so, every 2 seconds, the shape randomly changes color. I already know how to have the shape randomly change color, but I don't know how to make it happen every 2 seconds. Can someone answer ASAP? Thanks.



  • Answer ✓
    // GoToLoop (2016-Jun-09)
    static final int FPS = 60, DELAY = 2 * 1000;
    color c;
    void setup() {
      size(400, 300);
    void draw() {
      frame.setTitle(frameCount/FPS + "    #" + hex(c, 6));
    void randomTimedColor() {
      for (;; delay(DELAY))  c = (color) random(#000000);
  • Thanks! However, in my code, I want to make a shape change color, and I am using RGB instead of hexadecimal. Can you make a shape and then show me how to randomly change the color of that shape randomly every 2 seconds with RGB? Thank you so much!

  • edited June 2016 Answer ✓

    You said you already knew how to randomize the shape's color. GoToLoop has shown you how to start a thread that changes a color variable's value every 2 seconds. If you are unhappy with how he is picking a random color, you can simple use your own way of picking a new random color.

    To be clear: You asked a question. You got an answer. You thanked us for the answer, then asked the same question again. It's like you don't see that what you have is an answer to your question. This is confusing.

  • Sorry about that. I think I wasn't clear in my second question. I'll be clearer - How do you switch from RGB color mode to hexadecimal (like the one GoToLoop used)? I searched the Processing reference, but there was only HSB and RGB. Thanks.

  • edited June 2016 Answer ✓

    That's just an example, you can put anything you like in the for loop in the called method, anything.

    Hash colours are just another way of writing rgb colours. In this case, using it as an argument to random is just getting you a random color. If you're using hsb then just pick a random h, s and B separately and combine them.

Sign In or Register to comment.