Screen is Blinking

I am using an Image I drew with a dynamic background colour to simulate day/ night… Depending on the time of day the background colour changes dynamically. What happens is that not only my background changes, but the whole screen blinks every second. Is there a way to avoid this from happening?

My code is the following…

int m = millis();
int sketch_seconds = 0; 
String sketch_period = "A";
float b_value = 0;
PImage desert; 

void setup(){

//set up the background full screen starting time 6AM
//variable Int b_value; 
background(0,27,0);
desert = loadImage("desert_illustrator.png");

//load the mountains and desert image 
//set up the two arrows on the watch starting time 6AM

}

void draw(){

//draw the sky colour, B value changes fill(0,0,value), through the day 
//6AM to 12AM

image(desert, 0, 0, width, height);

if (millis() > m + 1000){

  m = millis();
  
  if (sketch_seconds < 24) {
  sketch_period = "A"; 
  println("case A");  }

  else if (sketch_seconds < 48) {
  sketch_period = "B"; 
  println("case B");}

  else if (sketch_seconds < 72) {
  sketch_period = "C"; 
  println("case C");}

  else if (sketch_seconds < 96) {
  sketch_period = "D"; 
  println("case D");}

  else if (sketch_seconds < 97) {
  sketch_period = "E"; 
  println("case E");}

  println("check switch statement");

  switch (sketch_period) {
 
  //sun is rising, sky brightens 
  case "A" : 
    println("case A");
    sketch_seconds = sketch_seconds + 1;
    b_value = b_value + (255/24);
    background (0,27,b_value);
    break;
  
  //sky at its brightest, sun is present and moving 
  case "B" :
    println("case B");
    sketch_seconds = sketch_seconds + 1;
    b_value = 255;
    background (0,27,255);
    break;
  
  //sun goes down, moon comes up 
  case "C" : 
    println("case C");
    sketch_seconds = sketch_seconds + 1;
    b_value = b_value - (255/24);
    background (0,27,b_value);
    break;
  
  //sky at its darkest, moon is present and moving 
  case "D" : 
    println("case D");
    sketch_seconds = sketch_seconds + 1;
    b_value = 0; 
    background (0,27,0);
    break;
  
  //96th second, time to reset some values 
  case "E" :
    println("case E");
    sketch_seconds = 0;
    break;
  }
  }


//small arrow on the watch indicates hour, full rotation in 48 seconds 
//large arrow on the watch indicates minutes, full rotation in 4 seconds

}

Hi,

Try moving that line at the end of your draw() function:
image(desert, 0, 0, width, height);

2 Likes

This is a necessary line that draws an Image on the canvas. Not an option to remove it…

I am not telling you to remove it, just to move it to the end of your code.

Right now you are telling you program to draw an image and then to draw a background on top of that image so the image does not show up.

By putting that line in the end you first draw the background and then, on top of that background, draw your image.

1 Like

ok, that works… misunderstood…

Thank you!

Happy to help :wink: