Advertisement
GilesCartmel

Creative Coding w5_02_a

Aug 29th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. /*
  2.  * Creative Coding
  3.  * Week 5, 02 - Digital Clock
  4.  * by Indae Hwang and Jon McCormack
  5.  * Copyright (c) 2014 Monash University
  6.  *
  7.  * This sketch shows how to use text in Processing
  8.  * The sketch creates a digital clock that shows the current time in hours, minutes and seconds
  9.  * Use the 'h', 'm' and 's' keys to enlarge the hours, minutes or seconds in the display.
  10.  *
  11.  * Adapted by Giles Cartmel
  12.  * Gives 3D effect by drawing a scale of grey up to the ratio position of the number
  13.  * based on it's min/max values within the scale of 20 to 200 size, drawing the actual number
  14.  * at it's relative position in white, and all further sizes up to 200 are drawn in very transparent grey
  15.  * Numbers are all shown in 2 digit numbers with leading 0's
  16.  * FrameRate set to 2 so that the "pips" can invert every half a second
  17.  *
  18.  */
  19. PFont myFont;    // font data
  20. int   gap;       // gap between digits
  21.  
  22. void setup() {
  23.   size(1024, 600);
  24.  
  25.   myFont = createFont("Arial", 200);
  26.   textFont(myFont);  // set the current font to myFont
  27.   gap = 300;
  28.   ellipseMode(RADIUS);
  29.   frameRate(2);
  30. }
  31.  
  32. void draw() {
  33.   background(0);
  34.   fill(255);
  35.  
  36.   // draw h, m, s
  37.   drawNumber(hour(), -gap, 0, 0, 23);
  38.   drawNumber(minute(), 0, 0, 0, 59);
  39.   drawNumber(second(), gap, 0, 0, 59);
  40.  
  41.   if (frameCount%2 == 0) {
  42.     stroke(100);
  43.     fill(0);
  44.   } else {
  45.     stroke(0);
  46.     fill(255);
  47.   }
  48.   ellipse(width/2-gap/2,height/2-20,10,10);
  49.   ellipse(width/2-gap/2,height/2+20,10,10);
  50.   ellipse(width/2+gap/2,height/2-20,10,10);
  51.   ellipse(width/2+gap/2,height/2+20,10,10);
  52. }
  53.  
  54. /*
  55.  * drawNumber
  56.  * takes an integer and draws it offset from the centre of the screen by
  57.  * offsetX and offsetY. If big is true then use a big size for the type.
  58.  *
  59.  */
  60. void drawNumber(int number, float offsetX, float offsetY, int minVal, int maxVal) {
  61.   String theText = nf(number,2); // convert number to 2 digit string with leading 0 if necessary
  62.   int pos = (int) map(number, minVal, maxVal, 20, 200);
  63.   for (int i = 20; i <= 200; i++) {
  64.     if (i < pos) {
  65.       stroke((int) map(i,20,200,20,pos));
  66.       fill((int) map(i,20,200,20,pos),50);
  67.     } else if (i == pos) {
  68.       stroke(255);
  69.       fill(255);
  70.     } else {
  71.       stroke(128,25);
  72.       fill(30,5);
  73.     }
  74.     textSize(i);
  75.  
  76.     float tWidth = textWidth(theText) * 0.5;
  77.     float tAscent = textAscent() * 0.375;
  78.  
  79.     // draw text offset from the centre of the screen
  80.     text(theText, width/2 - tWidth + offsetX, height/2 + tAscent + offsetY);
  81.   }
  82. }
  83.  
  84. void keyPressed() {
  85.   if (key=='s') {
  86.     saveFrame("Giles_5_02_a_"+String.valueOf(year())+"-"+String.valueOf(month())+"-"+String.valueOf(day())+"-"+String.valueOf(hour())+"-"+String.valueOf(minute())+"-"+String.valueOf(second())+".jpg");
  87.   }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement