Advertisement
AvidMalignus

Java Clock

Sep 6th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. // clock code, written by C.J. Winkler
  2.  
  3. void setup() {
  4.   size(500, 500);
  5. }
  6.  
  7. void draw() {
  8.   background(255);
  9.  
  10.   // get the current time
  11.   int s = second();  // values from 0 - 59
  12.   int m = minute();  // values from 0 - 59
  13.   int h = hour();    // values from 0 - 23
  14.  
  15.   // move origin to center of the screen
  16.   translate(250, 250);
  17.  
  18.   // draw the seconds hand
  19.   pushMatrix();
  20.   rotate(radians(map(s, 0, 59, 0, 360)));
  21.   stroke(255, 0, 0);
  22.   strokeWeight(1);
  23.   line(0, 0, 0, -225);
  24.   popMatrix();
  25.  
  26.   // draw the minutes hand
  27.   pushMatrix();
  28.   rotate(radians(map(m + s / 60.0, 0.0, 60.0, 0.0, 360.0)));
  29.   stroke(0, 0, 255);
  30.   strokeWeight(5);
  31.   line(0, 0, 0, -200);
  32.   popMatrix();
  33.  
  34.   // draw the hours hand
  35.   pushMatrix();
  36.   if (h > 12) h -= 12;
  37.   rotate(radians(map(h + m / 60.0, 0.0, 24, 0.0, 360.0)));
  38.   strokeWeight(5);
  39.   stroke(0, 255, 0);
  40.   line(0, 0, 0, -100);
  41.   popMatrix();
  42.  
  43.   // "button" at the center
  44.   fill(0);
  45.   noStroke();
  46.   ellipse(0, 0, 15, 15);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement