from_scratch

UCSD applet Sun - 1

Oct 15th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. package codopedia;
  2.  
  3. import processing.core.*;
  4. import java.util.Calendar;
  5. import java.text.SimpleDateFormat;//getTimeInstance()
  6.  
  7. public class MyPApplet extends PApplet{
  8.     private String url = "http://i.imgur.com/ZZoY9Ni.jpg";
  9.     private PImage backgroundImage;
  10.  
  11.     int hours, minutes, seconds = 0;
  12.    
  13.     public void setup(){
  14.         size(200, 200);
  15.         backgroundImage = loadImage(url, "jpg");
  16.     }
  17.    
  18.     public void draw(){
  19.         backgroundImage.resize(0, height);//height == height of the canvas and width == 0 means let java decide the width to keep the width and height ratio aspect
  20.         image(backgroundImage, 0, 0);
  21.         determineTime();
  22. /*      //We can test if the Sun changes color using the seconds variable
  23.         if(seconds >= 1 && seconds <= 20){
  24.             fill(255, 209, 0);
  25.         }else if(seconds > 20 && seconds <= 40){
  26.             fill(209, 0, 0);
  27.         }else{
  28.             fill(0, 0, 255);
  29.         }*/
  30.         if(hours >= 9 && hours <= 16){
  31.             fill(255, 209, 0);
  32.         }else if(hours > 16 && hours <= 18){
  33.             fill(209, 0, 0);
  34.         }else{
  35.             fill(0, 0, 255);
  36.         }
  37.         ellipse(width-width/6, height/5, width/5, height/5);
  38.     }
  39.    
  40.     private void determineTime() {
  41.         Calendar cal = Calendar.getInstance();
  42.         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  43.         String hourMinuteSecond = sdf.format(cal.getTime());
  44.         int colonBetweenMinutesAndSeconds = hourMinuteSecond.lastIndexOf(":");
  45.         if(colonBetweenMinutesAndSeconds > 0){
  46.             seconds = Integer.parseInt(hourMinuteSecond.substring(colonBetweenMinutesAndSeconds + 1));
  47.         }
  48.         int colonBetweenHoursAndMinutes = hourMinuteSecond.indexOf(":");
  49.         if(colonBetweenHoursAndMinutes > 0){
  50.             minutes = Integer.parseInt(hourMinuteSecond.substring(colonBetweenHoursAndMinutes + 1, colonBetweenMinutesAndSeconds));
  51.             hours = Integer.parseInt(hourMinuteSecond.substring(0, colonBetweenHoursAndMinutes));
  52.         }
  53.     }//method determineTime ends here.
  54. }//class MyPApplet ends here.
Advertisement
Add Comment
Please, Sign In to add comment