I am new to programming in Java and am making a project where there is a background image and a sun. The sun changes colour based on the time in real life.
My code is as listed below:
package demos;
import processing.core.*;
import java.time.LocalDateTime;
import java.util.TimerTask;
public class Trial extends PApplet{
String URL="http://cseweb.ucsd.edu/~minnes/palmTrees.jpg";
PImage backgroundImg;
public void setup(){
size(200,200);
backgroundImg = loadImage(URL,"jpg");
}
public void draw(){
int hour = LocalDateTime.now().getHour();
int minute = LocalDateTime.now().getMinute();
switch(hour){
case 13:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(238,238,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 14:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(205,205,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 15:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(255,215,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 16:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(238,201,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 17:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(255,193,37);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 18:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(205 ,133,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 19:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(255,0,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 20:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(211,211,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 21:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(169,169,169);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 22:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(105,105,105);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 23:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(79,79,79);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 00:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(0,0,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 1:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(79,79,79);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 2:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(105,105,105);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 3:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(169,169,169);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 4:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(211,211,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 5:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(255,0,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 6:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(205,133,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 7:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(255,193,37);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 8:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(121,29,121);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 9:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(111,209,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 10:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(230,209,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 11:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(255,200,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
case 12:
{
backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(255,209,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
default:
{ backgroundImg.resize(0 ,height);
image( backgroundImg , 0,0);
fill(255,209,0);
ellipse(width/4,height/5,width/5,height/5);
break;
}
}
}
}
For now it just executes the draw method once to change the colour of the sun. I need the draw method to execute every minute to change colour if the hour changes.