Would "noLoop" be the only way to stop this flashing?

edited November 2013 in Questions about Code

Hi, I'm a VERY recent Processing rookie starting to try out some basic code. I am learning a little about functions at the moment and so have stuck some data in to make a very basic bar graph of the number of days in each month. I have set the rgb values of each bar to be random integers between 0 and 255, with the idea being that it would give a random colour to each one. When I hit run the colurs are flashing endlessly, where I would rather them just appear once and stay. Is this a correct example of where I should be using noLoop() to stop this happening? Is there a better way? Code below.

void setup(){

  size(1200,600); 
  smooth(); 
   background(255);

}

void MonthsGraph(int BarNo, int NoDays , String MonthName){ 


int TotalBarNo = 12;

int BarWidth = width/24;

String label = "Month Name";

int BarSpace = width/24 * (BarNo-1);

int x = BarNo * (width/24 -10)+ BarSpace;
int y = 10;

float r = random(0, 255);
float g = random(0, 255);
float b = random(0, 255);

noStroke();
fill(r,g,b);
rect(x, y, BarWidth, NoDays);

fill(0);
textSize(18);
textAlign(CENTER);
text(MonthName, x +20, 100);


}

void draw(){



  MonthsGraph(1,31, "Jan"); 
  MonthsGraph(2,28, "Feb"); 
  MonthsGraph(3,31, "Mar"); 
  MonthsGraph(4,30, "April"); 
  MonthsGraph(5,31, "May"); 
  MonthsGraph(6,30, "June"); 
  MonthsGraph(7,31, "July"); 
  MonthsGraph(8,31, "Aug"); 
  MonthsGraph(9,30, "Sept"); 
  MonthsGraph(10,31, "Oct"); 
  MonthsGraph(11,30, "Nov"); 
  MonthsGraph(12,31, "Dec"); 


}

Answers

  • edited November 2013 Answer ✓

    Is this a correct example of where I should be using noLoop() to stop this happening? Is there a better way? Code below.

    Yes, w/ noLoop(), draw() is invoked once only!
    A better option is creating a Month class. There you can have many field variables to represent coordinates, label and color! (*)

    A nice trick -> use (color) random(#00000); to pick a random color! :-bd

  • Thanks! I didn't know about the random for hex codes, it is certainly tidier!

    Do you mean create "Month" as the class and then create each bar as an "object"? :)

Sign In or Register to comment.